tweak(config): Config var & send as event method

config -> ResourceConfig for readability and the resource config in NUI is now being referenced consistently from state so it can be referenced at runtime and not compile time.

Also removes the need to copy the config before using `cp`
This commit is contained in:
Taso committed 2021-04-13 22:07:22 -04:00
1 parent 8ce6ec7559
commit dd3eac4a0e
18 files changed
+66 -89

No files matched your search

+8 -1
View File
@@ -32,10 +32,12 @@ import { usePhone } from './os/phone/hooks/usePhone';
import { useTranslation } from 'react-i18next';
import { useSnackbar } from './ui/hooks/useSnackbar';
import { PhoneEvents } from '../../typings/phone';
import dayjs from 'dayjs';
import { isDefaultWallpaper } from './apps/settings/utils/isDefaultWallpaper';
import { useKeyboardService } from './os/keyboard/hooks/useKeyboardService';
import dayjs from 'dayjs';
import DefaultConfig from '../../config.json';
function Phone() {
const { t, i18n } = useTranslation();
@@ -168,4 +170,9 @@ InjectDebugData<any>([
method: PhoneEvents.SET_TIME,
data: dayjs().format('hh:mm'),
},
{
app: 'PHONE',
method: PhoneEvents.SET_CONFIG,
data: DefaultConfig,
},
]);
@@ -42,8 +42,8 @@ export function ProfileForm({ profile, showPreview }: IProps) {
const Nui = useNuiRequest();
const classes = useStyles();
const { t } = useTranslation();
const { config } = usePhone();
const { ResourceConfig } = usePhone();
// note that this assumes we are defensively checking
// that profile is not null in a parent above this component.
// Annoyingling adding conditionals above this line to not render
@@ -77,7 +77,7 @@ export function ProfileForm({ profile, showPreview }: IProps) {
Nui.send(event, updatedProfile);
};
if (!profile && !config.match.allowEdtiableProfileName) {
if (!profile && !ResourceConfig.match.allowEditableProfileName) {
return <PageText text={t('APPS_MATCH_PROFILE_CONFIGURATION')} />;
}
@@ -101,7 +101,7 @@ export function ProfileForm({ profile, showPreview }: IProps) {
label={t('APPS_MATCH_EDIT_PROFILE_NAME')}
value={name}
handleChange={setName}
allowChange={config.match.allowEdtiableProfileName}
allowChange={ResourceConfig.match.allowEditableProfileName}
/>
<ProfileField
label={t('APPS_MATCH_EDIT_PROFILE_BIO')}
@@ -53,14 +53,14 @@ export const AddTweetModal = () => {
const Nui = useNuiRequest();
const classes = useStyles();
const { message, setMessage, modalVisible, setModalVisible } = useModal();
const { config } = usePhone();
const { ResourceConfig } = usePhone();
const [showEmoji, setShowEmoji] = useState(false);
const [showImagePrompt, setShowImagePrompt] = useState(false);
const [link, setLink] = useState('');
const [images, setImages] = useState<Image[]>([]);
const { characterLimit, newLineLimit } = config.twitter;
const { characterLimit, newLineLimit } = ResourceConfig.twitter;
const reset = () => {
setShowImagePrompt(false);
@@ -93,7 +93,7 @@ export const AddTweetModal = () => {
window.addEventListener('keydown', _handleEscape, true);
return () => window.removeEventListener('keydown', _handleEscape);
});
if (!config) return null;
if (!ResourceConfig) return null;
const isValidMessage = (message) => {
if (message.length > characterLimit) return false;
@@ -154,7 +154,7 @@ export const AddTweetModal = () => {
setMessage(message.concat(emojiObject.native));
};
if (!config) return null;
if (!ResourceConfig) return null;
return (
<Modal visible={modalVisible} handleClose={_handleClose}>
@@ -172,7 +172,9 @@ export const AddTweetModal = () => {
/>
<div className={classes.buttonsContainer}>
<IconButtons
onImageClick={images.length < config.twitter.maxImages ? toggleShowImagePrompt : null}
onImageClick={
images.length < ResourceConfig.twitter.maxImages ? toggleShowImagePrompt : null
}
onEmojiClick={toggleShowEmoji}
/>
<ControlButtons
+2 -2
View File
@@ -17,12 +17,12 @@ const useStyles = makeStyles({
function Avatar({ avatarUrl, showInvalidImage, height, width }) {
const classes = useStyles();
const [showImageError, setShowImageError] = useState(false);
const { config } = usePhone();
const { ResourceConfig } = usePhone();
const handleImageError = () => setShowImageError(true);
const handleImageLoad = () => setShowImageError(false);
if (!config || !config.twitter.enableAvatars) return null;
if (!ResourceConfig || !ResourceConfig.twitter.enableAvatars) return null;
return (
<div className={classes.root}>
@@ -20,10 +20,10 @@ const useStyles = makeStyles({
export const IconButtons = ({ onImageClick, onEmojiClick }) => {
const classes = useStyles();
const { config } = usePhone();
const { ResourceConfig } = usePhone();
if (!config) return null;
const { enableImages, enableEmojis } = config.twitter;
if (!ResourceConfig) return null;
const { enableImages, enableEmojis } = ResourceConfig.twitter;
return (
<div className={classes.buttons}>
@@ -26,7 +26,7 @@ export function Profile() {
const classes = useStyles();
const { t } = useTranslation();
const { profile } = useProfile();
const { config } = usePhone();
const { ResourceConfig } = usePhone();
// note that this assumes we are defensively checking
// that profile is not null in a parent above this component.
@@ -51,9 +51,9 @@ export function Profile() {
};
// fetching the config is an asynchronous call so defend against it
if (!config) return null;
if (!ResourceConfig) return null;
const { enableAvatars, allowEdtiableProfileName } = config.twitter;
const { enableAvatars, allowEditableProfileName } = ResourceConfig.twitter;
return (
<div className={classes.root}>
@@ -69,7 +69,7 @@ export function Profile() {
label={t('APPS_TWITTER_EDIT_PROFILE_NAME')}
value={name}
handleChange={handleNameChange}
allowChange={allowEdtiableProfileName}
allowChange={allowEditableProfileName}
/>
<ProfileField
label={t('APPS_TWITTER_EDIT_PROFILE_BIO')}
@@ -30,9 +30,9 @@ export function ProfilePrompt() {
const { profile } = useProfile();
const defaultProfileNames = useRecoilValue(twitterState.defaultProfileNames);
const [profileName, setProfileName] = useState(profile?.profile_name || '');
const { config } = usePhone();
const { ResourceConfig } = usePhone();
const showDefaultProfileNames = !config.twitter.allowEdtiableProfileName && !profile;
const showDefaultProfileNames = !ResourceConfig.twitter.allowEditableProfileName && !profile;
const eventName = showDefaultProfileNames
? TwitterEvents.CREATE_PROFILE
: TwitterEvents.UPDATE_PROFILE;
@@ -11,9 +11,9 @@ export const ShowMore = ({ id, isReported, isMine }) => {
const Nui = useNuiRequest();
const { t } = useTranslation();
const [anchorEl, setAnchorEl] = useState<Element | null>(null);
const { config } = usePhone();
const { ResourceConfig } = usePhone();
if (!config) return null;
if (!ResourceConfig) return null;
const handleClick = (event: React.MouseEvent) => {
const element = event.currentTarget;
@@ -29,8 +29,8 @@ export const ShowMore = ({ id, isReported, isMine }) => {
handleClose();
};
const allowedToDelete = config.twitter.allowDeleteTweets && isMine;
const allowedToReport = config.twitter.allowReportTweets && !isMine;
const allowedToDelete = ResourceConfig.twitter.allowDeleteTweets && isMine;
const allowedToReport = ResourceConfig.twitter.allowReportTweets && !isMine;
// if the user cannot perform any actions in show more then don't
// allow them to interact with it
@@ -81,10 +81,10 @@ export const Tweet = (tweet: ITweet) => {
} = tweet;
const classes = useStyles();
const { t } = useTranslation();
const { config } = usePhone();
const { ResourceConfig } = usePhone();
if (!config) return null;
const { enableAvatars, enableImages } = config.twitter;
if (!ResourceConfig) return null;
const { enableAvatars, enableImages } = ResourceConfig.twitter;
// this is a workaround to transfer string new lines and returns that
// are stored into the database into html for the UI to render
@@ -23,10 +23,10 @@ export const TweetMessage = ({ modalVisible, message, handleChange }) => {
const textFieldRef = useRef(null);
const textFieldInputRef = useRef(null);
const classes = useStyles();
const { config } = usePhone();
const { ResourceConfig } = usePhone();
const { t } = useTranslation();
const { characterLimit, newLineLimit } = config.twitter;
const { characterLimit, newLineLimit } = ResourceConfig.twitter;
useEffect(() => {
textFieldInputRef.current && textFieldInputRef.current.focus();
@@ -46,7 +46,7 @@ export const TweetMessage = ({ modalVisible, message, handleChange }) => {
[handleChange],
);
if (!config) return null;
if (!ResourceConfig) return null;
let errorMessage = null;
-41
View File
@@ -1,41 +0,0 @@
{
"Locale": "en",
"KeyTogglePhone": 288,
"KeyTakeCall": 38,
"PhoneAsItem": false,
"SwimDestroy": false,
"RunRate": 10,
"DestoryChance": 100,
"DestroyPhoneReCheck": 3,
"notificationPosition": {
"horizontal": "right",
"vertical": "top"
},
"general": {
"useDashNumber": true
},
"twitter": {
"showNotifications": true,
"generateProfileNameFromUsers": true,
"allowEdtiableProfileName": true,
"allowDeleteTweets": true,
"allowReportTweets": true,
"characterLimit": 160,
"newLineLimit": 10,
"enableAvatars": true,
"enableEmojis": true,
"enableImages": true,
"maxImages": 3
},
"match": {
"generateProfileNameFromUsers": true,
"allowEdtiableProfileName": true
},
"bank": {
"showNotifications": true
},
"debug": {
"level": "debug",
"enabled": true
}
}
+4 -1
View File
@@ -6,7 +6,10 @@ const configState = atom({
default: Default,
});
export const useConfig = (state: any = configState): any => {
// This is NOT the resource config but instead the NUI specific config,
// this means that if there are changs to the config in `../default.json`,
// it will require a rebuild for those to take effect.
export const usePhoneConfig = (state: any = configState): any => {
const [config, _setConfig] = useRecoilState(state);
const setConfig = (key, value) => {
+3 -3
View File
@@ -1,13 +1,13 @@
import { atom } from 'recoil';
import { IServerConfig } from '../../../../../typings/config';
import { ResourceConfig } from '../../../../../typings/config';
export const phoneState = {
visibility: atom<boolean>({
key: 'phoneVisibility',
default: false,
}),
phoneConfig: atom<IServerConfig>({
key: 'phoneConfig',
resourceConfig: atom<ResourceConfig>({
key: 'resourceConfig',
default: null,
}),
phoneReady: atom<boolean>({
+10 -4
View File
@@ -1,13 +1,12 @@
import { useRecoilValue } from 'recoil';
import { IServerConfig } from '../../../../../typings/config';
import ServerConfig from '../../../config.autogen.json';
import { ResourceConfig } from '../../../../../typings/config';
import { useNotifications } from '../../notifications/hooks/useNotifications';
import { useNuiRequest } from 'fivem-nui-react-lib';
import { phoneState } from './state';
import { PhoneEvents } from '../../../../../typings/phone';
interface IUsePhone {
config?: IServerConfig;
ResourceConfig?: ResourceConfig;
openPhone(): void;
closePhone(): void;
isPhoneOpen: boolean;
@@ -18,6 +17,7 @@ export const usePhone = (): IUsePhone => {
const Nui = useNuiRequest();
const isPhoneOpen = useRecoilValue(phoneState.visibility);
const isPhoneReady = useRecoilValue(phoneState.phoneReady);
const config = useRecoilValue(phoneState.resourceConfig);
const { removeAlerts } = useNotifications();
@@ -29,5 +29,11 @@ export const usePhone = (): IUsePhone => {
Nui.send(PhoneEvents.OPEN_PHONE);
};
return { config: ServerConfig as any, closePhone, openPhone, isPhoneOpen, isPhoneReady };
return {
ResourceConfig: config as ResourceConfig,
closePhone,
openPhone,
isPhoneOpen,
isPhoneReady,
};
};
+2 -2
View File
@@ -13,11 +13,11 @@ export const usePhoneService = () => {
}, [send]);
const setVisibility = useSetRecoilState(phoneState.visibility);
const setPhoneConfig = useSetRecoilState(phoneState.phoneConfig);
const setResourceConfig = useSetRecoilState(phoneState.resourceConfig);
const setPhoneReady = useSetRecoilState(phoneState.phoneReady);
const setPhoneTime = useSetRecoilState(phoneState.phoneTime);
useNuiEvent('PHONE', PhoneEvents.SET_VISIBILITY, setVisibility);
useNuiEvent('PHONE', PhoneEvents.SET_PHONE_READY, setPhoneReady);
useNuiEvent('PHONE', PhoneEvents.SET_CONFIG, setPhoneConfig);
useNuiEvent('PHONE', PhoneEvents.SET_CONFIG, setResourceConfig);
useNuiEvent('PHONE', PhoneEvents.SET_TIME, setPhoneTime);
};
+3 -3
View File
@@ -21,13 +21,13 @@ const useStyles = makeStyles({
function Notification({ children, handleClose, open }) {
const classes = useStyles();
const { config } = usePhone();
const { ResourceConfig } = usePhone();
const currentTheme = usePhoneTheme();
if (!config) return null;
if (!ResourceConfig) return null;
const { horizontal, vertical } = config.notificationPosition;
const { horizontal, vertical } = ResourceConfig.notificationPosition;
return (
<ThemeProvider theme={currentTheme}>
+2 -2
View File
@@ -1,8 +1,8 @@
import ClientUtils from './cl_utils';
import { IServerConfig } from '../../typings/config';
import { ResourceConfig } from '../../typings/config';
// Setup and export the config for the resource
export const config: IServerConfig = JSON.parse(
export const config: ResourceConfig = JSON.parse(
LoadResourceFile(GetCurrentResourceName(), 'config.json'),
);
+2 -2
View File
@@ -1,8 +1,8 @@
import { Server } from 'esx.js';
import { IServerConfig } from '../../typings/config';
import { ResourceConfig } from '../../typings/config';
// Setup and export config loaded at runtime
export const config: IServerConfig = JSON.parse(
export const config: ResourceConfig = JSON.parse(
LoadResourceFile(GetCurrentResourceName(), 'config.json'),
);