tweak(phone/dialer): Cleanup & type safety for state

Renamed some types to better represent their data
This commit is contained in:
Taso committed 2021-04-01 13:40:37 -04:00
1 parent a0c5f94514
commit e6deff23da
10 files changed
+46 -49

No files matched your search

@@ -8,7 +8,7 @@ import { ListItem } from '../../../../ui/components/ListItem';
import Nui from '../../../../os/nui-events/utils/Nui'; import Nui from '../../../../os/nui-events/utils/Nui';
import { useSimcard } from '../../../../os/simcard/hooks/useSimcard'; import { useSimcard } from '../../../../os/simcard/hooks/useSimcard';
import { useContacts } from '../../../contacts/hooks/useContacts'; import { useContacts } from '../../../contacts/hooks/useContacts';
import { CallEvents, ICall } from '../../../../../../typings/call'; import { CallEvents, CallHistoryItem } from '../../../../../../typings/call';
import { useTranslation } from 'react-i18next'; import { useTranslation } from 'react-i18next';
import { Box, IconButton, ListItemIcon, ListItemText } from '@material-ui/core'; import { Box, IconButton, ListItemIcon, ListItemText } from '@material-ui/core';
import { useHistory } from 'react-router-dom'; import { useHistory } from 'react-router-dom';
@@ -55,7 +55,7 @@ export const DialerHistory = ({ calls }) => {
return ( return (
<List disablePadding> <List disablePadding>
{calls.map((call: ICall) => {calls.map((call: CallHistoryItem) =>
call.transmitter === myNumber ? ( call.transmitter === myNumber ? (
<ListItem key={call.id} divider> <ListItem key={call.id} divider>
<ListItemIcon> <ListItemIcon>
+2 -1
View File
@@ -1,7 +1,8 @@
import { atom } from 'recoil'; import { atom } from 'recoil';
import { CallHistoryItem } from '../../../../../typings/call';
export const dialState = { export const dialState = {
history: atom({ history: atom<CallHistoryItem[]>({
key: 'dialHistory', key: 'dialHistory',
default: [], default: [],
}), }),
@@ -1,7 +1,6 @@
import { useRecoilValue } from 'recoil'; import { useRecoilValue } from 'recoil';
import { ICall } from '../../../../../typings/call';
import { dialState } from './state'; import { dialState } from './state';
export const useDialHistory = () => { export const useDialHistory = () => {
return useRecoilValue<ICall[]>(dialState.history); return useRecoilValue(dialState.history);
}; };
+3 -2
View File
@@ -1,11 +1,12 @@
import { atom } from 'recoil'; import { atom } from 'recoil';
import { ActiveCall } from '../../../../../typings/call';
export const callerState = { export const callerState = {
currentCall: atom({ currentCall: atom<ActiveCall>({
key: 'currentCall', key: 'currentCall',
default: null, default: null,
}), }),
callModal: atom({ callModal: atom<boolean>({
key: 'callingModal', key: 'callingModal',
default: false, default: false,
}), }),
+2 -2
View File
@@ -1,11 +1,11 @@
import { useRecoilState } from 'recoil'; import { useRecoilState } from 'recoil';
import { CallProps } from '../../../../../typings/call'; import { ActiveCall } from '../../../../../typings/call';
import Nui from '../../nui-events/utils/Nui'; import Nui from '../../nui-events/utils/Nui';
import { callerState } from './state'; import { callerState } from './state';
import { CallEvents } from '../../../../../typings/call'; import { CallEvents } from '../../../../../typings/call';
interface CallHook { interface CallHook {
call: CallProps; call: ActiveCall;
setCall: (details: any) => void; setCall: (details: any) => void;
acceptCall(): void; acceptCall(): void;
rejectCall(): void; rejectCall(): void;
@@ -1,7 +1,7 @@
import React from 'react'; import React from 'react';
import { useTranslation } from 'react-i18next'; import { useTranslation } from 'react-i18next';
import { useHistory } from 'react-router-dom'; import { useHistory } from 'react-router-dom';
import { CallProps } from '../../../../../typings/call'; import { ActiveCall } from '../../../../../typings/call';
import { useApp } from '../../apps/hooks/useApps'; import { useApp } from '../../apps/hooks/useApps';
import { useNotifications } from '../../notifications/hooks/useNotifications'; import { useNotifications } from '../../notifications/hooks/useNotifications';
import { useRingtoneSound } from '../../sound/hooks/useRingtoneSound'; import { useRingtoneSound } from '../../sound/hooks/useRingtoneSound';
@@ -27,7 +27,7 @@ export const useCallNotifications = () => {
notificationIcon, notificationIcon,
}; };
const setNotification = (call: CallProps) => { const setNotification = (call: ActiveCall) => {
stop(); stop();
if (!call || !call.active) { if (!call || !call.active) {
removeId(NOTIFICATION_ID); removeId(NOTIFICATION_ID);
+4 -4
View File
@@ -2,7 +2,7 @@ import { useEffect, useState } from 'react';
import { useNuiEvent } from '../../nui-events/hooks/useNuiEvent'; import { useNuiEvent } from '../../nui-events/hooks/useNuiEvent';
import { useSetRecoilState } from 'recoil'; import { useSetRecoilState } from 'recoil';
import { useCall } from './useCall'; import { useCall } from './useCall';
import { CallEvents, CallProps } from '../../../../../typings/call'; import { CallEvents, ActiveCall } from '../../../../../typings/call';
import InjectDebugData from '../../debug/InjectDebugData'; import InjectDebugData from '../../debug/InjectDebugData';
import { useCallModal } from './useCallModal'; import { useCallModal } from './useCallModal';
import { useHistory, useLocation } from 'react-router-dom'; import { useHistory, useLocation } from 'react-router-dom';
@@ -10,7 +10,7 @@ import { callerState } from './state';
import { useCallNotifications } from './useCallNotifications'; import { useCallNotifications } from './useCallNotifications';
import { useDuration } from './useDuration'; import { useDuration } from './useDuration';
InjectDebugData<CallProps | boolean>([ InjectDebugData<ActiveCall | boolean>([
/* { /* {
app: 'CALL', app: 'CALL',
method: 'setCaller', method: 'setCaller',
@@ -56,11 +56,11 @@ export const useCallService = () => {
} }
}, [history, modal, pathname, modalHasBeenOpenedThisCall]); }, [history, modal, pathname, modalHasBeenOpenedThisCall]);
const _setCall = (_call: CallProps) => { const _setCall = (_call: ActiveCall) => {
resetDuration(); resetDuration();
setCall(_call); setCall(_call);
setNotification(_call); setNotification(_call);
}; };
useNuiEvent<CallProps>('CALL', CallEvents.SET_CALLER, _setCall, call); useNuiEvent<ActiveCall>('CALL', CallEvents.SET_CALLER, _setCall, call);
useNuiEvent<boolean>('CALL', CallEvents.SET_CALL_MODAL, setModal); useNuiEvent<boolean>('CALL', CallEvents.SET_CALL_MODAL, setModal);
}; };
+22 -19
View File
@@ -1,5 +1,5 @@
import { CallEvents } from '../../typings/call'; import { CallEvents } from '../../typings/call';
import { ICall } from '../../typings/call'; import { CallHistoryItem } from '../../typings/call';
import { phoneCallStartAnim, phoneCallEndAnim } from './functions'; import { phoneCallStartAnim, phoneCallEndAnim } from './functions';
const exp = (global as any).exports; const exp = (global as any).exports;
@@ -34,23 +34,26 @@ on(`__cfx_nui:${CallEvents.ACCEPT_CALL}`, (data: any, cb: Function) => {
cb(); cb();
}); });
onNet(CallEvents.WAS_ACCEPTED, (channelId: number, currentCall: ICall, isTransmitter: boolean) => { onNet(
exp['mumble-voip'].SetCallChannel(channelId); CallEvents.WAS_ACCEPTED,
phoneCallStartAnim(); // Trigger call animation only if the call was accepted. (channelId: number, currentCall: CallHistoryItem, isTransmitter: boolean) => {
SendNuiMessage( exp['mumble-voip'].SetCallChannel(channelId);
JSON.stringify({ phoneCallStartAnim(); // Trigger call animation only if the call was accepted.
app: 'CALL', SendNuiMessage(
method: CallEvents.SET_CALLER, JSON.stringify({
data: { app: 'CALL',
active: true, method: CallEvents.SET_CALLER,
transmitter: currentCall.transmitter, data: {
receiver: currentCall.receiver, active: true,
isTransmitter: isTransmitter, transmitter: currentCall.transmitter,
accepted: true, receiver: currentCall.receiver,
}, isTransmitter: isTransmitter,
}), accepted: true,
); },
}); }),
);
},
);
RegisterNuiCallbackType(CallEvents.REJECTED); // Fires when cancelling and rejecting a call. RegisterNuiCallbackType(CallEvents.REJECTED); // Fires when cancelling and rejecting a call.
on(`__cfx_nui:${CallEvents.REJECTED}`, (data: any, cb: Function) => { on(`__cfx_nui:${CallEvents.REJECTED}`, (data: any, cb: Function) => {
@@ -115,7 +118,7 @@ function openCallModal(show: boolean) {
); );
} }
onNet(CallEvents.SEND_HISTORY, (calls: ICall) => { onNet(CallEvents.SEND_HISTORY, (calls: CallHistoryItem[]) => {
SendNuiMessage( SendNuiMessage(
JSON.stringify({ JSON.stringify({
app: 'DIALER', app: 'DIALER',
+6 -6
View File
@@ -1,7 +1,7 @@
import { CallEvents } from '../../typings/call'; import { CallEvents } from '../../typings/call';
import { getIdentifierByPhoneNumber, getPlayer, getSource } from './functions'; import { getIdentifierByPhoneNumber, getPlayer, getSource } from './functions';
import { getPlayerFromIdentifier } from './functions'; import { getPlayerFromIdentifier } from './functions';
import { ICall } from '../../typings/call'; import { CallHistoryItem } from '../../typings/call';
import { pool } from './db'; import { pool } from './db';
import { v4 as uuidv4 } from 'uuid'; import { v4 as uuidv4 } from 'uuid';
@@ -9,25 +9,25 @@ import { mainLogger } from './sv_logger';
const callLogger = mainLogger.child({ module: 'calls' }); const callLogger = mainLogger.child({ module: 'calls' });
async function saveCall(call: ICall) { async function saveCall(call: CallHistoryItem) {
const query = const query =
'INSERT INTO npwd_calls (identifier, transmitter, receiver, start) VALUES (?, ?, ?, ?)'; 'INSERT INTO npwd_calls (identifier, transmitter, receiver, start) VALUES (?, ?, ?, ?)';
await pool.query(query, [call.identifier, call.transmitter, call.receiver, call.start]); await pool.query(query, [call.identifier, call.transmitter, call.receiver, call.start]);
} }
async function updateCall(call: ICall, isAccepted: boolean, end: number) { async function updateCall(call: CallHistoryItem, isAccepted: boolean, end: number) {
const query = 'UPDATE npwd_calls SET is_accepted=?, end=? WHERE identifier = ?'; const query = 'UPDATE npwd_calls SET is_accepted=?, end=? WHERE identifier = ?';
await pool.query(query, [isAccepted, end, call.identifier]); await pool.query(query, [isAccepted, end, call.identifier]);
} }
async function fetchCalls(phoneNumber: string): Promise<ICall[]> { async function fetchCalls(phoneNumber: string): Promise<CallHistoryItem[]> {
const query = 'SELECT * FROM npwd_calls WHERE receiver = ? OR transmitter = ? ORDER BY id DESC'; const query = 'SELECT * FROM npwd_calls WHERE receiver = ? OR transmitter = ? ORDER BY id DESC';
const [result] = await pool.query(query, [phoneNumber, phoneNumber]); const [result] = await pool.query(query, [phoneNumber, phoneNumber]);
const calls = <ICall[]>result; const calls = <CallHistoryItem[]>result;
return calls; return calls;
} }
let calls: Map<string, ICall> = new Map(); let calls: Map<string, CallHistoryItem> = new Map();
onNet(CallEvents.INITIALIZE_CALL, async (phoneNumber: string) => { onNet(CallEvents.INITIALIZE_CALL, async (phoneNumber: string) => {
const _source = getSource(); const _source = getSource();
+2 -9
View File
@@ -1,4 +1,4 @@
export interface CallProps { export interface ActiveCall {
active: boolean; active: boolean;
accepted: boolean; accepted: boolean;
isTransmitter: boolean; isTransmitter: boolean;
@@ -6,7 +6,7 @@ export interface CallProps {
receiver: string; receiver: string;
} }
export interface ICall { export interface CallHistoryItem {
id?: number; id?: number;
identifier: string; identifier: string;
transmitter: string; transmitter: string;
@@ -18,13 +18,6 @@ export interface ICall {
accepted: boolean; accepted: boolean;
} }
export interface ICallDuration {
ms: number;
s: number;
m: number;
h: number;
}
export enum CallEvents { export enum CallEvents {
INITIALIZE_CALL = 'npwd:beginCall', INITIALIZE_CALL = 'npwd:beginCall',
START_CALL = 'npwd:startCall', START_CALL = 'npwd:startCall',