mirror of
https://github.com/darksaid98/new-phone-who-dis.git
synced 2026-12-04 22:16:40 +00:00
refactor(calls): refactor of call architecture
This commit is contained in:
1 parent
92b687b130
commit
36d1e993bd
21 files changed
+361
-185
No files matched your search
@@ -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,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);
|
||||
@@ -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();
|
||||
};
|
||||
Reference in new issue
Block a user