diff --git a/phone/src/ui/components/Input.tsx b/phone/src/ui/components/Input.tsx index 86e56c1..178a678 100644 --- a/phone/src/ui/components/Input.tsx +++ b/phone/src/ui/components/Input.tsx @@ -1,24 +1,18 @@ -import React, { useCallback } from 'react'; +import React from 'react'; import MUITextField from '@material-ui/core/TextField'; import MUIInputBase, { InputBaseProps } from '@material-ui/core/InputBase'; -import { useNuiRequest } from 'fivem-nui-react-lib'; import { PhoneEvents } from '../../../../typings/phone'; import { TextFieldProps } from '@material-ui/core/TextField/TextField'; +import { fetchNui } from '../../utils/fetchNui'; +import { isEnvBrowser } from '../../utils/misc'; +import { noop } from '../../../../resources/utils/misc'; -const useToggleFocus = () => { - const Nui = useNuiRequest(); - return useCallback( - (keepGameFocus: boolean) => { - Nui.send(PhoneEvents.TOGGLE_KEYS, { - keepGameFocus, - }); - }, - [Nui], - ); -}; +const toggleKeys = (keepGameFocus: boolean) => + fetchNui(PhoneEvents.TOGGLE_KEYS, { + keepGameFocus, + }).catch((e) => (isEnvBrowser() ? noop : console.error(e))); export const TextField: React.FC = (props) => { - const toggleKeys = useToggleFocus(); return ( = (props) => { }; export const InputBase: React.FC = (props) => { - const toggleKeys = useToggleFocus(); return ( { sendMessage('SIMCARD', PhoneEvents.SET_NUMBER, number); }); -AddEventHandler('onResourceStop', function (resource: string) { +on('onResourceStop', (resource: string) => { if (resource === GetCurrentResourceName()) { sendMessage('PHONE', PhoneEvents.SET_VISIBILITY, false); SetNuiFocus(false, false); @@ -140,34 +137,22 @@ AddEventHandler('onResourceStop', function (resource: string) { * NUI Service Callback Registration * * * * * * * * * * * * * */ - -RegisterNuiCallbackType(PhoneEvents.OPEN_APP_BANK); -on(`__cfx_nui:${PhoneEvents.OPEN_APP_BANK}`, (data: any, cb: Function) => { - emitNet(BankEvents.FETCH_TRANSACTIONS); - emitNet(BankEvents.GET_CREDENTIALS); - cb(); -}); - -RegisterNuiCallbackType(PhoneEvents.OPEN_APP_DAILER); -on(`__cfx_nui:${PhoneEvents.OPEN_APP_DAILER}`, (data: any, cb: Function) => { - emitNet(CallEvents.FETCH_CALLS); - cb(); -}); - -RegisterNuiCallbackType(PhoneEvents.CLOSE_PHONE); -on(`__cfx_nui:${PhoneEvents.CLOSE_PHONE}`, async (data: any, cb: Function) => { +RegisterNuiCB(PhoneEvents.CLOSE_PHONE, async (_, cb) => { await hidePhone(); cb(); -}); // Called for when the phone is closed via the UI. - -RegisterNuiCallbackType(PhoneEvents.TOGGLE_KEYS); -on(`__cfx_nui:${PhoneEvents.TOGGLE_KEYS}`, async (data: any, cb: Function) => { - if (isPhoneOpen) { - SetNuiFocusKeepInput(data.keepGameFocus); - } - cb(); }); +// NOTE: This probably has an edge case when phone is closed for some reason +// and we need to toggle keep input off +RegisterNuiCB<{ keepGameFocus: boolean }>( + PhoneEvents.TOGGLE_KEYS, + async ({ keepGameFocus }, cb) => { + // We will only + if (isPhoneOpen) SetNuiFocusKeepInput(keepGameFocus); + cb({}); + }, +); + // setTick(async () => { // while (config.SwimDestroy) { // await Delay(config.RunRate * 1000); diff --git a/resources/utils/misc.ts b/resources/utils/misc.ts new file mode 100644 index 0000000..4b1407a --- /dev/null +++ b/resources/utils/misc.ts @@ -0,0 +1,2 @@ +// eslint-disable-next-line @typescript-eslint/no-empty-function +export const noop = () => {}; diff --git a/typings/phone.ts b/typings/phone.ts index 620e08f..6fb1f38 100644 --- a/typings/phone.ts +++ b/typings/phone.ts @@ -1,19 +1,9 @@ export const PHONE_NUMBER_REGEX = /^([0-9]{3}-[0-9]{4})|([0-9]{7})$/; export enum PhoneEvents { - UI_IS_READY = 'npwd:uiIsReady', - PLAYER_IS_READY = 'npwd:playerIsReady', - OPEN_APP_CONTACTS = 'npwd:app:CONTACTS', - OPEN_APP_BANK = 'npwd:app:BANK', - OPEN_APP_NOTES = 'npwd:app:NOTES', - OPEN_APP_LISTINGS = 'npwd:app:MARKETPLACE', - OPEN_APP_CAMERA = 'npwd:app:CAMERA', - OPEN_APP_DAILER = 'npwd:app:DIALER', OPEN_PHONE = 'npwd:open', CLOSE_PHONE = 'npwd:close', - SET_ALERT = 'npwd:setAlert', SET_VISIBILITY = 'npwd:setVisibility', - SET_NOTIFICATION = 'npwd:setNotification', SET_NUMBER = 'npwd:setNumber', SET_PHONE_READY = 'npwd:phoneReady', SET_CONFIG = 'npwd:setPhoneConfig',