tweak(calls): Abstract initialize call action & own number handling

This commit is contained in:
Taso committed 2021-07-28 14:41:19 -04:00
1 parent af6e291368
commit be4323f337
6 files changed
+40 -53

No files matched your search

@@ -7,18 +7,13 @@ import MoreVertIcon from '@material-ui/icons/MoreVert';
import { SearchContacts } from './SearchContacts'; import { SearchContacts } from './SearchContacts';
import { useHistory } from 'react-router-dom'; import { useHistory } from 'react-router-dom';
import LogDebugEvent from '../../../../os/debug/LogDebugEvents'; import LogDebugEvent from '../../../../os/debug/LogDebugEvents';
import { CallEvents } from '../../../../../../typings/call';
import { useFilteredContacts } from '../../hooks/state'; import { useFilteredContacts } from '../../hooks/state';
import { fetchNui } from '../../../../utils/fetchNui'; import { useCall } from '../../../../os/call/hooks/useCall';
import { ServerPromiseResp } from '../../../../../../typings/common';
import { useSnackbar } from '../../../../ui/hooks/useSnackbar';
import { useTranslation } from 'react-i18next';
export const ContactList = () => { export const ContactList = () => {
const filteredContacts = useFilteredContacts(); const filteredContacts = useFilteredContacts();
const history = useHistory(); const history = useHistory();
const { addAlert } = useSnackbar(); const { initializeCall } = useCall();
const { t } = useTranslation();
const openContactInfo = (contactId: number) => { const openContactInfo = (contactId: number) => {
history.push(`/contacts/${contactId}`); history.push(`/contacts/${contactId}`);
@@ -30,14 +25,7 @@ export const ContactList = () => {
level: 2, level: 2,
data: true, data: true,
}); });
fetchNui<ServerPromiseResp>(CallEvents.INITIALIZE_CALL, { initializeCall(number);
receiverNumber: number,
}).then((resp) => {
if (resp.status === 'error') {
addAlert({ message: t('CALLS.FEEDBACK.ERROR'), type: 'error' });
console.error(resp.errorMsg);
}
});
}; };
const handleMessage = (phoneNumber: string) => { const handleMessage = (phoneNumber: string) => {
@@ -5,12 +5,9 @@ import PhoneIcon from '@material-ui/icons/Phone';
import PersonAddIcon from '@material-ui/icons/PersonAdd'; import PersonAddIcon from '@material-ui/icons/PersonAdd';
import { DialInputCtx, IDialInputCtx } from '../context/InputContext'; import { DialInputCtx, IDialInputCtx } from '../context/InputContext';
import { useHistory } from 'react-router-dom'; import { useHistory } from 'react-router-dom';
import { CallEvents } from '../../../../../typings/call';
import { useTranslation } from 'react-i18next'; import { useTranslation } from 'react-i18next';
import { InputBase } from '../../../ui/components/Input'; import { InputBase } from '../../../ui/components/Input';
import { fetchNui } from '../../../utils/fetchNui'; import { useCall } from '../../../os/call/hooks/useCall';
import { ServerPromiseResp } from '../../../../../typings/common';
import { useSnackbar } from '../../../ui/hooks/useSnackbar';
const useStyles = makeStyles((theme: Theme) => ({ const useStyles = makeStyles((theme: Theme) => ({
root: { root: {
@@ -35,19 +32,12 @@ export const DialerInput = () => {
const classes = useStyles(); const classes = useStyles();
const history = useHistory(); const history = useHistory();
const { t } = useTranslation(); const { t } = useTranslation();
const { addAlert } = useSnackbar(); const { initializeCall } = useCall();
const { inputVal, set } = useContext<IDialInputCtx>(DialInputCtx); const { inputVal, set } = useContext<IDialInputCtx>(DialInputCtx);
const handleCall = (number: string) => { const handleCall = (number: string) => {
fetchNui<ServerPromiseResp>(CallEvents.INITIALIZE_CALL, { initializeCall(number);
receiverNumber: number,
}).then((resp) => {
if (resp.status === 'error') {
addAlert({ message: t('CALLS.FEEDBACK.ERROR'), type: 'error' });
console.error(resp.errorMsg);
}
});
}; };
const handleNewContact = (number: string) => { const handleNewContact = (number: string) => {
@@ -12,11 +12,9 @@ import { Box, IconButton, ListItemIcon, ListItemText } from '@material-ui/core';
import { useHistory } from 'react-router-dom'; import { useHistory } from 'react-router-dom';
import { makeStyles, Theme } from '@material-ui/core/styles'; import { makeStyles, Theme } from '@material-ui/core/styles';
import dayjs from 'dayjs'; 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 { useMyPhoneNumber } from '../../../../os/simcard/hooks/useMyPhoneNumber';
import { useDialHistory } from '../../hooks/useDialHistory'; import { useDialHistory } from '../../hooks/useDialHistory';
import { useCall } from '../../../../os/call/hooks/useCall';
const useStyles = makeStyles((theme: Theme) => ({ const useStyles = makeStyles((theme: Theme) => ({
callForward: { callForward: {
@@ -28,25 +26,16 @@ const useStyles = makeStyles((theme: Theme) => ({
})); }));
export const DialerHistory: React.FC = () => { export const DialerHistory: React.FC = () => {
const { addAlert } = useSnackbar();
const myNumber = useMyPhoneNumber(); const myNumber = useMyPhoneNumber();
const { getDisplayByNumber } = useContactActions(); const { getDisplayByNumber } = useContactActions();
const { initializeCall } = useCall();
const calls = useDialHistory(); const calls = useDialHistory();
const classes = useStyles(); const classes = useStyles();
const history = useHistory(); const history = useHistory();
const { t } = useTranslation(); const { t } = useTranslation();
const handleCall = (phoneNumber) => { const handleCall = (phoneNumber) => {
fetchNui<ServerPromiseResp>(CallEvents.INITIALIZE_CALL, { initializeCall(phoneNumber);
receiverNumber: phoneNumber,
}).then((resp) => {
if (resp.status === 'error') {
addAlert({ message: t('CALLS.FEEDBACK.ERROR'), type: 'error' });
console.error(resp.errorMsg);
}
});
}; };
if (!calls?.length) { if (!calls?.length) {
@@ -8,11 +8,11 @@ import ChatIcon from '@material-ui/icons/Chat';
import PhoneIcon from '@material-ui/icons/Phone'; import PhoneIcon from '@material-ui/icons/Phone';
import { useTranslation } from 'react-i18next'; import { useTranslation } from 'react-i18next';
import { useHistory } from 'react-router'; import { useHistory } from 'react-router';
import { CallEvents } from '../../../../../../typings/call';
import { fetchNui } from '../../../../utils/fetchNui'; import { fetchNui } from '../../../../utils/fetchNui';
import { useSnackbar } from '../../../../ui/hooks/useSnackbar'; import { useSnackbar } from '../../../../ui/hooks/useSnackbar';
import { ServerPromiseResp } from '../../../../../../typings/common'; import { ServerPromiseResp } from '../../../../../../typings/common';
import { useMyPhoneNumber } from '../../../../os/simcard/hooks/useMyPhoneNumber'; import { useMyPhoneNumber } from '../../../../os/simcard/hooks/useMyPhoneNumber';
import { useCall } from '../../../../os/call/hooks/useCall';
const useStyles = makeStyles((theme: Theme) => ({ const useStyles = makeStyles((theme: Theme) => ({
icon: { icon: {
@@ -25,6 +25,7 @@ export const ListingActions: React.FC<MarketplaceListing> = ({ children, ...list
const myNumber = useMyPhoneNumber(); const myNumber = useMyPhoneNumber();
const { t } = useTranslation(); const { t } = useTranslation();
const history = useHistory(); const history = useHistory();
const { initializeCall } = useCall();
const { addAlert } = useSnackbar(); const { addAlert } = useSnackbar();
const handleDeleteListing = () => { const handleDeleteListing = () => {
@@ -64,14 +65,7 @@ export const ListingActions: React.FC<MarketplaceListing> = ({ children, ...list
}; };
const handleCall = () => { const handleCall = () => {
fetchNui<ServerPromiseResp>(CallEvents.INITIALIZE_CALL, { initializeCall(listing.number);
receiverNumber: listing.number,
}).then((resp) => {
if (resp.status === 'error') {
addAlert({ message: t('CALLS.FEEDBACK.ERROR'), type: 'error' });
console.error(resp.errorMsg);
}
});
}; };
const handleMessage = () => { const handleMessage = () => {
+1
View File
@@ -14,6 +14,7 @@
"INCOMING": "Incoming call" "INCOMING": "Incoming call"
}, },
"FEEDBACK": { "FEEDBACK": {
"ERROR_MYSELF": "You cannot call your own phone number!",
"ERROR": "Error occurred while completing this call" "ERROR": "Error occurred while completing this call"
} }
}, },
+26 -1
View File
@@ -6,6 +6,9 @@ import { fetchNui } from '../../../utils/fetchNui';
import { useCallback } from 'react'; import { useCallback } from 'react';
import { useMyPhoneNumber } from '../../simcard/hooks/useMyPhoneNumber'; import { useMyPhoneNumber } from '../../simcard/hooks/useMyPhoneNumber';
import { useDialingSound } from './useDialingSound'; import { useDialingSound } from './useDialingSound';
import { useSnackbar } from '../../../ui/hooks/useSnackbar';
import { useTranslation } from 'react-i18next';
import { ServerPromiseResp } from '../../../../../typings/common';
interface CallHook { interface CallHook {
call: ActiveCall; call: ActiveCall;
@@ -13,13 +16,35 @@ interface CallHook {
acceptCall(): void; acceptCall(): void;
rejectCall(): void; rejectCall(): void;
endCall(): void; endCall(): void;
initializeCall(number: string): void;
} }
export const useCall = (): CallHook => { export const useCall = (): CallHook => {
const [call, setCall] = useRecoilState(callerState.currentCall); const [call, setCall] = useRecoilState(callerState.currentCall);
const myPhoneNumber = useMyPhoneNumber(); const myPhoneNumber = useMyPhoneNumber();
const { t } = useTranslation();
const { addAlert } = useSnackbar();
const { endDialTone, startDialTone } = useDialingSound(); 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<ServerPromiseResp>(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(() => { const acceptCall = useCallback(() => {
fetchNui(CallEvents.ACCEPT_CALL, { fetchNui(CallEvents.ACCEPT_CALL, {
transmitterNumber: call.transmitter, transmitterNumber: call.transmitter,
@@ -39,5 +64,5 @@ export const useCall = (): CallHook => {
}); });
}, [call, myPhoneNumber]); }, [call, myPhoneNumber]);
return { call, setCall, acceptCall, rejectCall, endCall }; return { call, setCall, acceptCall, rejectCall, endCall, initializeCall };
}; };