refactor(calls): refactor of call architecture

This commit is contained in:
Taso committed 2021-07-28 14:26:00 -04:00
1 parent 92b687b130
commit 36d1e993bd
21 files changed
+361 -185

No files matched your search

Binary file not shown.
@@ -10,11 +10,16 @@ import LogDebugEvent from '../../../../os/debug/LogDebugEvents';
import { CallEvents } from '../../../../../../typings/call';
import { useNuiRequest } from 'fivem-nui-react-lib';
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';
export const ContactList = () => {
const filteredContacts = useFilteredContacts();
const history = useHistory();
const Nui = useNuiRequest();
const { addAlert } = useSnackbar();
const { t } = useTranslation();
const openContactInfo = (contactId: number) => {
history.push(`/contacts/${contactId}`);
@@ -26,8 +31,13 @@ export const ContactList = () => {
level: 2,
data: true,
});
Nui.send(CallEvents.INITIALIZE_CALL, {
number,
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);
}
});
};
@@ -4,11 +4,13 @@ import { makeStyles, Theme } from '@material-ui/core/styles';
import PhoneIcon from '@material-ui/icons/Phone';
import PersonAddIcon from '@material-ui/icons/PersonAdd';
import { DialInputCtx, IDialInputCtx } from '../context/InputContext';
import { useNuiRequest } from 'fivem-nui-react-lib';
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';
const useStyles = makeStyles((theme: Theme) => ({
root: {
@@ -30,16 +32,21 @@ const useStyles = makeStyles((theme: Theme) => ({
}));
export const DialerInput = () => {
const Nui = useNuiRequest();
const classes = useStyles();
const history = useHistory();
const { t } = useTranslation();
const { addAlert } = useSnackbar();
const { inputVal, set } = useContext<IDialInputCtx>(DialInputCtx);
const handleCall = (number: string) => {
Nui.send(CallEvents.INITIALIZE_CALL, {
number,
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);
}
});
};
@@ -5,8 +5,6 @@ import PhoneIcon from '@material-ui/icons/Phone';
import PersonAddIcon from '@material-ui/icons/PersonAdd';
import { List } from '../../../../ui/components/List';
import { ListItem } from '../../../../ui/components/ListItem';
import { useNuiRequest } from 'fivem-nui-react-lib';
import { useSimcard } from '../../../../os/simcard/hooks/useSimcard';
import { useContactActions } from '../../../contacts/hooks/useContactActions';
import { CallEvents, CallHistoryItem } from '../../../../../../typings/call';
import { useTranslation } from 'react-i18next';
@@ -14,6 +12,10 @@ 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';
const useStyles = makeStyles((theme: Theme) => ({
callForward: {
@@ -25,8 +27,8 @@ const useStyles = makeStyles((theme: Theme) => ({
}));
export const DialerHistory = ({ calls }) => {
const Nui = useNuiRequest();
const { number: myNumber } = useSimcard();
const { addAlert } = useSnackbar();
const myNumber = useMyPhoneNumber();
const { getDisplayByNumber } = useContactActions();
const classes = useStyles();
@@ -34,10 +36,14 @@ export const DialerHistory = ({ calls }) => {
const history = useHistory();
const { t } = useTranslation();
const handleCall = (phoneNumber) => {
Nui.send(CallEvents.INITIALIZE_CALL, {
number: phoneNumber,
fetchNui<ServerPromiseResp>(CallEvents.INITIALIZE_CALL, {
receiverNumber: phoneNumber,
}).then((resp) => {
if (resp.status === 'error') {
addAlert({ message: t('CALLS.FEEDBACK.ERROR'), type: 'error' });
console.error(resp.errorMsg);
}
});
};
@@ -2,18 +2,17 @@ import { Box, Tooltip, Button } from '@material-ui/core';
import { makeStyles, Theme } from '@material-ui/core/styles';
import React from 'react';
import { MarketplaceEvents, MarketplaceListing } from '../../../../../../typings/marketplace';
import { useSimcard } from '../../../../os/simcard/hooks/useSimcard';
import DeleteIcon from '@material-ui/icons/Delete';
import ReportIcon from '@material-ui/icons/Report';
import ChatIcon from '@material-ui/icons/Chat';
import PhoneIcon from '@material-ui/icons/Phone';
import { useNuiRequest } from 'fivem-nui-react-lib';
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';
const useStyles = makeStyles((theme: Theme) => ({
icon: {
@@ -22,9 +21,8 @@ const useStyles = makeStyles((theme: Theme) => ({
}));
export const ListingActions: React.FC<MarketplaceListing> = ({ children, ...listing }) => {
const Nui = useNuiRequest();
const classes = useStyles();
const { number: myNumber } = useSimcard();
const myNumber = useMyPhoneNumber();
const { t } = useTranslation();
const history = useHistory();
const { addAlert } = useSnackbar();
@@ -66,8 +64,13 @@ export const ListingActions: React.FC<MarketplaceListing> = ({ children, ...list
};
const handleCall = () => {
Nui.send(CallEvents.INITIALIZE_CALL, {
number: listing.number,
fetchNui<ServerPromiseResp>(CallEvents.INITIALIZE_CALL, {
receiverNumber: listing.number,
}).then((resp) => {
if (resp.status === 'error') {
addAlert({ message: t('CALLS.FEEDBACK.ERROR'), type: 'error' });
console.error(resp.errorMsg);
}
});
};
@@ -22,7 +22,7 @@ import { useHistory, useLocation, useParams } from 'react-router-dom';
import { useTranslation } from 'react-i18next';
import Modal from '../../../../ui/components/Modal';
import { useContactActions } from '../../../contacts/hooks/useContactActions';
import { useSimcard } from '../../../../os/simcard/hooks/useSimcard';
import { useSimcard } from '../../../../os/simcard/hooks/useMyPhoneNumber';
import { MessageEvents } from '../../../../../../typings/messages';
const LARGE_HEADER_CHARS = 30;
@@ -5,7 +5,7 @@ import { AppContent } from '../../../ui/components/AppContent';
import { useContextMenu, MapSettingItem, SettingOption } from '../../../ui/hooks/useContextMenu';
import { usePhoneConfig } from '../../../config/hooks/usePhoneConfig';
import { List } from '../../../ui/components/List';
import { useSimcard } from '../../../os/simcard/hooks/useSimcard';
import { useSimcard } from '../../../os/simcard/hooks/useMyPhoneNumber';
import { IconSetObject, useApp } from '../../../os/apps/hooks/useApps';
import {
SettingItem,
+3
View File
@@ -12,6 +12,9 @@
"RINGING": "Ringing",
"OUTGOING": "Outgoing call",
"INCOMING": "Incoming call"
},
"FEEDBACK": {
"ERROR": "Error occurred while completing this call"
}
},
"SETTINGS": {
+19 -14
View File
@@ -2,7 +2,10 @@ import { useRecoilState } from 'recoil';
import { ActiveCall } from '../../../../../typings/call';
import { callerState } from './state';
import { CallEvents } from '../../../../../typings/call';
import { useNuiRequest } from 'fivem-nui-react-lib';
import { fetchNui } from '../../../utils/fetchNui';
import { useCallback } from 'react';
import { useMyPhoneNumber } from '../../simcard/hooks/useMyPhoneNumber';
import { useDialingSound } from './useDialingSound';
interface CallHook {
call: ActiveCall;
@@ -13,26 +16,28 @@ interface CallHook {
}
export const useCall = (): CallHook => {
const Nui = useNuiRequest();
const [call, setCall] = useRecoilState(callerState.currentCall);
const myPhoneNumber = useMyPhoneNumber();
const { endDialTone, startDialTone } = useDialingSound();
const acceptCall = () => {
Nui.send(CallEvents.ACCEPT_CALL, {
const acceptCall = useCallback(() => {
fetchNui(CallEvents.ACCEPT_CALL, {
transmitterNumber: call.transmitter,
});
};
}, [call]);
const rejectCall = () => {
Nui.send(CallEvents.REJECTED, {
phoneNumber: call.transmitter,
});
};
const endCall = () => {
Nui.send(CallEvents.END_CALL, {
const rejectCall = useCallback(() => {
fetchNui(CallEvents.REJECTED, {
transmitterNumber: call.transmitter,
});
};
}, [call]);
const endCall = useCallback(() => {
fetchNui(CallEvents.END_CALL, {
transmitterNumber: call.transmitter,
isTransmitter: call.transmitter === myPhoneNumber,
});
}, [call, myPhoneNumber]);
return { call, setCall, acceptCall, rejectCall, endCall };
};
@@ -0,0 +1,31 @@
import { useCallback, useEffect } from 'react';
import { useSoundProvider } from '../../sound/hooks/useSoundProvider';
interface useDialingSoundValue {
startDialTone: () => void;
endDialTone: () => void;
}
const DIAL_TONE_URL = 'media/misc/Outgoing-Dial-Effect.ogg';
export const useDialingSound = (): useDialingSoundValue => {
const sound = useSoundProvider();
useEffect(() => {
if (!sound.isMounted(DIAL_TONE_URL)) {
sound.mount(DIAL_TONE_URL, 0.3, true);
return;
}
sound.volume(DIAL_TONE_URL, 0.3);
}, [sound]);
const startDialTone = useCallback(() => {
sound.play(DIAL_TONE_URL, 0.3, true);
}, [sound]);
const endDialTone = useCallback(() => {
sound.stop(DIAL_TONE_URL);
}, [sound]);
return { startDialTone, endDialTone };
};
+1 -1
View File
@@ -1,7 +1,7 @@
import { atom } from 'recoil';
export const simcardState = {
number: atom({
number: atom<string | null>({
key: 'simcardNumber',
default: null,
}),
@@ -0,0 +1,4 @@
import { useRecoilValue } from 'recoil';
import { simcardState } from './state';
export const useMyPhoneNumber = () => useRecoilValue(simcardState.number);
-7
View File
@@ -1,7 +0,0 @@
import { useRecoilValue } from 'recoil';
import { simcardState } from './state';
export const useSimcard = () => {
const number = useRecoilValue(simcardState.number);
return { number };
};
@@ -1,6 +1,6 @@
import { useSetRecoilState } from 'recoil';
import { simcardState } from './state';
import { useSimcard } from './useSimcard';
import { useMyPhoneNumber } from './useMyPhoneNumber';
import InjectDebugData from '../../debug/InjectDebugData';
import { useNuiEvent } from 'fivem-nui-react-lib';
import { PhoneEvents } from '../../../../../typings/phone';
@@ -16,5 +16,5 @@ InjectDebugData([
export const useSimcardService = () => {
const setNumber = useSetRecoilState(simcardState.number);
useNuiEvent('SIMCARD', PhoneEvents.SET_NUMBER, setNumber);
return useSimcard();
return useMyPhoneNumber();
};