From be4323f337f79bef010dbce80704be26ab8d7d66 Mon Sep 17 00:00:00 2001 From: Taso Date: Wed, 28 Jul 2021 14:41:19 -0400 Subject: [PATCH] tweak(calls): Abstract initialize call action & own number handling --- .../contacts/components/List/ContactList.tsx | 18 +++---------- .../apps/dialer/components/DialerInput.tsx | 16 +++-------- .../dialer/components/views/DialerHistory.tsx | 19 +++---------- .../MarketplaceList/ListingActions.tsx | 12 +++------ phone/src/locale/en.json | 1 + phone/src/os/call/hooks/useCall.ts | 27 ++++++++++++++++++- 6 files changed, 40 insertions(+), 53 deletions(-) diff --git a/phone/src/apps/contacts/components/List/ContactList.tsx b/phone/src/apps/contacts/components/List/ContactList.tsx index 0cd43af..3e44f6d 100644 --- a/phone/src/apps/contacts/components/List/ContactList.tsx +++ b/phone/src/apps/contacts/components/List/ContactList.tsx @@ -7,18 +7,13 @@ import MoreVertIcon from '@material-ui/icons/MoreVert'; import { SearchContacts } from './SearchContacts'; import { useHistory } from 'react-router-dom'; import LogDebugEvent from '../../../../os/debug/LogDebugEvents'; -import { CallEvents } from '../../../../../../typings/call'; import { useFilteredContacts } from '../../hooks/state'; -import { fetchNui } from '../../../../utils/fetchNui'; -import { ServerPromiseResp } from '../../../../../../typings/common'; -import { useSnackbar } from '../../../../ui/hooks/useSnackbar'; -import { useTranslation } from 'react-i18next'; +import { useCall } from '../../../../os/call/hooks/useCall'; export const ContactList = () => { const filteredContacts = useFilteredContacts(); const history = useHistory(); - const { addAlert } = useSnackbar(); - const { t } = useTranslation(); + const { initializeCall } = useCall(); const openContactInfo = (contactId: number) => { history.push(`/contacts/${contactId}`); @@ -30,14 +25,7 @@ export const ContactList = () => { level: 2, data: true, }); - fetchNui(CallEvents.INITIALIZE_CALL, { - receiverNumber: number, - }).then((resp) => { - if (resp.status === 'error') { - addAlert({ message: t('CALLS.FEEDBACK.ERROR'), type: 'error' }); - console.error(resp.errorMsg); - } - }); + initializeCall(number); }; const handleMessage = (phoneNumber: string) => { diff --git a/phone/src/apps/dialer/components/DialerInput.tsx b/phone/src/apps/dialer/components/DialerInput.tsx index 4970640..bba4228 100644 --- a/phone/src/apps/dialer/components/DialerInput.tsx +++ b/phone/src/apps/dialer/components/DialerInput.tsx @@ -5,12 +5,9 @@ import PhoneIcon from '@material-ui/icons/Phone'; import PersonAddIcon from '@material-ui/icons/PersonAdd'; import { DialInputCtx, IDialInputCtx } from '../context/InputContext'; import { useHistory } from 'react-router-dom'; -import { CallEvents } from '../../../../../typings/call'; import { useTranslation } from 'react-i18next'; import { InputBase } from '../../../ui/components/Input'; -import { fetchNui } from '../../../utils/fetchNui'; -import { ServerPromiseResp } from '../../../../../typings/common'; -import { useSnackbar } from '../../../ui/hooks/useSnackbar'; +import { useCall } from '../../../os/call/hooks/useCall'; const useStyles = makeStyles((theme: Theme) => ({ root: { @@ -35,19 +32,12 @@ export const DialerInput = () => { const classes = useStyles(); const history = useHistory(); const { t } = useTranslation(); - const { addAlert } = useSnackbar(); + const { initializeCall } = useCall(); const { inputVal, set } = useContext(DialInputCtx); const handleCall = (number: string) => { - fetchNui(CallEvents.INITIALIZE_CALL, { - receiverNumber: number, - }).then((resp) => { - if (resp.status === 'error') { - addAlert({ message: t('CALLS.FEEDBACK.ERROR'), type: 'error' }); - console.error(resp.errorMsg); - } - }); + initializeCall(number); }; const handleNewContact = (number: string) => { diff --git a/phone/src/apps/dialer/components/views/DialerHistory.tsx b/phone/src/apps/dialer/components/views/DialerHistory.tsx index 9493665..4f9dcd4 100644 --- a/phone/src/apps/dialer/components/views/DialerHistory.tsx +++ b/phone/src/apps/dialer/components/views/DialerHistory.tsx @@ -12,11 +12,9 @@ import { Box, IconButton, ListItemIcon, ListItemText } from '@material-ui/core'; import { useHistory } from 'react-router-dom'; import { makeStyles, Theme } from '@material-ui/core/styles'; import dayjs from 'dayjs'; -import { fetchNui } from '../../../../utils/fetchNui'; -import { ServerPromiseResp } from '../../../../../../typings/common'; -import { useSnackbar } from '../../../../ui/hooks/useSnackbar'; import { useMyPhoneNumber } from '../../../../os/simcard/hooks/useMyPhoneNumber'; import { useDialHistory } from '../../hooks/useDialHistory'; +import { useCall } from '../../../../os/call/hooks/useCall'; const useStyles = makeStyles((theme: Theme) => ({ callForward: { @@ -28,25 +26,16 @@ const useStyles = makeStyles((theme: Theme) => ({ })); export const DialerHistory: React.FC = () => { - const { addAlert } = useSnackbar(); const myNumber = useMyPhoneNumber(); const { getDisplayByNumber } = useContactActions(); + const { initializeCall } = useCall(); const calls = useDialHistory(); - const classes = useStyles(); - const history = useHistory(); - const { t } = useTranslation(); + const handleCall = (phoneNumber) => { - fetchNui(CallEvents.INITIALIZE_CALL, { - receiverNumber: phoneNumber, - }).then((resp) => { - if (resp.status === 'error') { - addAlert({ message: t('CALLS.FEEDBACK.ERROR'), type: 'error' }); - console.error(resp.errorMsg); - } - }); + initializeCall(phoneNumber); }; if (!calls?.length) { diff --git a/phone/src/apps/marketplace/components/MarketplaceList/ListingActions.tsx b/phone/src/apps/marketplace/components/MarketplaceList/ListingActions.tsx index 3a035b8..b214279 100644 --- a/phone/src/apps/marketplace/components/MarketplaceList/ListingActions.tsx +++ b/phone/src/apps/marketplace/components/MarketplaceList/ListingActions.tsx @@ -8,11 +8,11 @@ import ChatIcon from '@material-ui/icons/Chat'; import PhoneIcon from '@material-ui/icons/Phone'; import { useTranslation } from 'react-i18next'; import { useHistory } from 'react-router'; -import { CallEvents } from '../../../../../../typings/call'; import { fetchNui } from '../../../../utils/fetchNui'; import { useSnackbar } from '../../../../ui/hooks/useSnackbar'; import { ServerPromiseResp } from '../../../../../../typings/common'; import { useMyPhoneNumber } from '../../../../os/simcard/hooks/useMyPhoneNumber'; +import { useCall } from '../../../../os/call/hooks/useCall'; const useStyles = makeStyles((theme: Theme) => ({ icon: { @@ -25,6 +25,7 @@ export const ListingActions: React.FC = ({ children, ...list const myNumber = useMyPhoneNumber(); const { t } = useTranslation(); const history = useHistory(); + const { initializeCall } = useCall(); const { addAlert } = useSnackbar(); const handleDeleteListing = () => { @@ -64,14 +65,7 @@ export const ListingActions: React.FC = ({ children, ...list }; const handleCall = () => { - fetchNui(CallEvents.INITIALIZE_CALL, { - receiverNumber: listing.number, - }).then((resp) => { - if (resp.status === 'error') { - addAlert({ message: t('CALLS.FEEDBACK.ERROR'), type: 'error' }); - console.error(resp.errorMsg); - } - }); + initializeCall(listing.number); }; const handleMessage = () => { diff --git a/phone/src/locale/en.json b/phone/src/locale/en.json index 24749a8..7508872 100644 --- a/phone/src/locale/en.json +++ b/phone/src/locale/en.json @@ -14,6 +14,7 @@ "INCOMING": "Incoming call" }, "FEEDBACK": { + "ERROR_MYSELF": "You cannot call your own phone number!", "ERROR": "Error occurred while completing this call" } }, diff --git a/phone/src/os/call/hooks/useCall.ts b/phone/src/os/call/hooks/useCall.ts index 261d10f..85b307a 100644 --- a/phone/src/os/call/hooks/useCall.ts +++ b/phone/src/os/call/hooks/useCall.ts @@ -6,6 +6,9 @@ import { fetchNui } from '../../../utils/fetchNui'; import { useCallback } from 'react'; import { useMyPhoneNumber } from '../../simcard/hooks/useMyPhoneNumber'; import { useDialingSound } from './useDialingSound'; +import { useSnackbar } from '../../../ui/hooks/useSnackbar'; +import { useTranslation } from 'react-i18next'; +import { ServerPromiseResp } from '../../../../../typings/common'; interface CallHook { call: ActiveCall; @@ -13,13 +16,35 @@ interface CallHook { acceptCall(): void; rejectCall(): void; endCall(): void; + initializeCall(number: string): void; } export const useCall = (): CallHook => { const [call, setCall] = useRecoilState(callerState.currentCall); const myPhoneNumber = useMyPhoneNumber(); + const { t } = useTranslation(); + const { addAlert } = useSnackbar(); const { endDialTone, startDialTone } = useDialingSound(); + const initializeCall = useCallback( + (number) => { + // We allow calling of ourselves in development + if (process.env.NODE_ENV !== 'development' && myPhoneNumber === number) { + return addAlert({ message: t('CALLS.FEEDBACK.ERROR_MYSELF'), type: 'error' }); + } + + fetchNui(CallEvents.INITIALIZE_CALL, { + receiverNumber: number, + }).then((resp) => { + if (resp.status === 'error') { + addAlert({ message: t('CALLS.FEEDBACK.ERROR'), type: 'error' }); + console.error(resp.errorMsg); + } + }); + }, + [addAlert, myPhoneNumber, t], + ); + const acceptCall = useCallback(() => { fetchNui(CallEvents.ACCEPT_CALL, { transmitterNumber: call.transmitter, @@ -39,5 +64,5 @@ export const useCall = (): CallHook => { }); }, [call, myPhoneNumber]); - return { call, setCall, acceptCall, rejectCall, endCall }; + return { call, setCall, acceptCall, rejectCall, endCall, initializeCall }; };