From dd3eac4a0e016c8582cbae63deff8f3e7660c8d5 Mon Sep 17 00:00:00 2001 From: Taso Date: Tue, 13 Apr 2021 20:44:22 -0400 Subject: [PATCH] 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` --- phone/src/Phone.tsx | 9 +++- .../match/components/profile/ProfileForm.tsx | 8 ++-- .../apps/twitter/components/AddTweetModal.tsx | 12 +++--- phone/src/apps/twitter/components/Avatar.tsx | 4 +- .../components/buttons/IconButtons.tsx | 6 +-- .../twitter/components/profile/Profile.tsx | 8 ++-- .../components/profile/ProfilePrompt.tsx | 4 +- .../twitter/components/tweet/ShowMore.tsx | 8 ++-- .../apps/twitter/components/tweet/Tweet.tsx | 6 +-- .../twitter/components/tweet/TweetMessage.tsx | 6 +-- phone/src/config.autogen.json | 41 ------------------- phone/src/config/hooks/usePhoneConfig.ts | 5 ++- phone/src/os/phone/hooks/state.ts | 6 +-- phone/src/os/phone/hooks/usePhone.ts | 14 +++++-- phone/src/os/phone/hooks/usePhoneService.ts | 4 +- phone/src/ui/components/Notification.tsx | 6 +-- resources/client/client.ts | 4 +- resources/server/server.ts | 4 +- 18 files changed, 66 insertions(+), 89 deletions(-) delete mode 100644 phone/src/config.autogen.json diff --git a/phone/src/Phone.tsx b/phone/src/Phone.tsx index f7e292b..ad6a04b 100644 --- a/phone/src/Phone.tsx +++ b/phone/src/Phone.tsx @@ -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([ method: PhoneEvents.SET_TIME, data: dayjs().format('hh:mm'), }, + { + app: 'PHONE', + method: PhoneEvents.SET_CONFIG, + data: DefaultConfig, + }, ]); diff --git a/phone/src/apps/match/components/profile/ProfileForm.tsx b/phone/src/apps/match/components/profile/ProfileForm.tsx index 041d66d..c07b5f4 100644 --- a/phone/src/apps/match/components/profile/ProfileForm.tsx +++ b/phone/src/apps/match/components/profile/ProfileForm.tsx @@ -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 ; } @@ -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} /> { 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([]); - 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 ( @@ -172,7 +172,9 @@ export const AddTweetModal = () => { />
setShowImageError(true); const handleImageLoad = () => setShowImageError(false); - if (!config || !config.twitter.enableAvatars) return null; + if (!ResourceConfig || !ResourceConfig.twitter.enableAvatars) return null; return (
diff --git a/phone/src/apps/twitter/components/buttons/IconButtons.tsx b/phone/src/apps/twitter/components/buttons/IconButtons.tsx index 1964466..d2b6ec5 100644 --- a/phone/src/apps/twitter/components/buttons/IconButtons.tsx +++ b/phone/src/apps/twitter/components/buttons/IconButtons.tsx @@ -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 (
diff --git a/phone/src/apps/twitter/components/profile/Profile.tsx b/phone/src/apps/twitter/components/profile/Profile.tsx index 01f6510..c12bedf 100644 --- a/phone/src/apps/twitter/components/profile/Profile.tsx +++ b/phone/src/apps/twitter/components/profile/Profile.tsx @@ -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 (
@@ -69,7 +69,7 @@ export function Profile() { label={t('APPS_TWITTER_EDIT_PROFILE_NAME')} value={name} handleChange={handleNameChange} - allowChange={allowEdtiableProfileName} + allowChange={allowEditableProfileName} /> { const Nui = useNuiRequest(); const { t } = useTranslation(); const [anchorEl, setAnchorEl] = useState(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 diff --git a/phone/src/apps/twitter/components/tweet/Tweet.tsx b/phone/src/apps/twitter/components/tweet/Tweet.tsx index f49f49b..febfe98 100644 --- a/phone/src/apps/twitter/components/tweet/Tweet.tsx +++ b/phone/src/apps/twitter/components/tweet/Tweet.tsx @@ -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 diff --git a/phone/src/apps/twitter/components/tweet/TweetMessage.tsx b/phone/src/apps/twitter/components/tweet/TweetMessage.tsx index 43e68c4..9afe9ad 100644 --- a/phone/src/apps/twitter/components/tweet/TweetMessage.tsx +++ b/phone/src/apps/twitter/components/tweet/TweetMessage.tsx @@ -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; diff --git a/phone/src/config.autogen.json b/phone/src/config.autogen.json deleted file mode 100644 index 18697d0..0000000 --- a/phone/src/config.autogen.json +++ /dev/null @@ -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 - } -} diff --git a/phone/src/config/hooks/usePhoneConfig.ts b/phone/src/config/hooks/usePhoneConfig.ts index 7789a0e..0fb0abe 100644 --- a/phone/src/config/hooks/usePhoneConfig.ts +++ b/phone/src/config/hooks/usePhoneConfig.ts @@ -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) => { diff --git a/phone/src/os/phone/hooks/state.ts b/phone/src/os/phone/hooks/state.ts index e35284d..0b42183 100644 --- a/phone/src/os/phone/hooks/state.ts +++ b/phone/src/os/phone/hooks/state.ts @@ -1,13 +1,13 @@ import { atom } from 'recoil'; -import { IServerConfig } from '../../../../../typings/config'; +import { ResourceConfig } from '../../../../../typings/config'; export const phoneState = { visibility: atom({ key: 'phoneVisibility', default: false, }), - phoneConfig: atom({ - key: 'phoneConfig', + resourceConfig: atom({ + key: 'resourceConfig', default: null, }), phoneReady: atom({ diff --git a/phone/src/os/phone/hooks/usePhone.ts b/phone/src/os/phone/hooks/usePhone.ts index 172f439..43fed84 100644 --- a/phone/src/os/phone/hooks/usePhone.ts +++ b/phone/src/os/phone/hooks/usePhone.ts @@ -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, + }; }; diff --git a/phone/src/os/phone/hooks/usePhoneService.ts b/phone/src/os/phone/hooks/usePhoneService.ts index 71fbb88..faf94e3 100644 --- a/phone/src/os/phone/hooks/usePhoneService.ts +++ b/phone/src/os/phone/hooks/usePhoneService.ts @@ -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); }; diff --git a/phone/src/ui/components/Notification.tsx b/phone/src/ui/components/Notification.tsx index b3fa467..557377d 100644 --- a/phone/src/ui/components/Notification.tsx +++ b/phone/src/ui/components/Notification.tsx @@ -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 ( diff --git a/resources/client/client.ts b/resources/client/client.ts index 7b192e3..b71a071 100644 --- a/resources/client/client.ts +++ b/resources/client/client.ts @@ -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'), ); diff --git a/resources/server/server.ts b/resources/server/server.ts index f53a096..9029292 100644 --- a/resources/server/server.ts +++ b/resources/server/server.ts @@ -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'), );