From d3b04f4cd95f7743c5a4086bf4e574ccc0d33330 Mon Sep 17 00:00:00 2001 From: Christopher Date: Mon, 1 Feb 2021 20:14:27 +0100 Subject: [PATCH] tweak(call/nui): duration as global state --- phone/src/Phone.tsx | 4 ++-- phone/src/common/typings/call.ts | 7 +++++++ phone/src/modal/components/CallTimer.tsx | 23 ++++++++++++++--------- phone/src/modal/hooks/state.ts | 6 +++++- phone/src/modal/hooks/useCall.ts | 2 +- phone/src/modal/hooks/useDuration.ts | 7 +++++++ 6 files changed, 36 insertions(+), 13 deletions(-) create mode 100644 phone/src/modal/hooks/useDuration.ts diff --git a/phone/src/Phone.tsx b/phone/src/Phone.tsx index fbf4df8..2dace22 100644 --- a/phone/src/Phone.tsx +++ b/phone/src/Phone.tsx @@ -85,8 +85,8 @@ if (process.env.NODE_ENV === 'development') { app: 'CALL', method: 'setCaller', data: { - accepted: false, - isTransmitter: false, + accepted: true, + isTransmitter: true, transmitter: 'Chip', receiver: 'Taso', phone_number: '860-4504', diff --git a/phone/src/common/typings/call.ts b/phone/src/common/typings/call.ts index c8c5ef9..b183674 100644 --- a/phone/src/common/typings/call.ts +++ b/phone/src/common/typings/call.ts @@ -12,4 +12,11 @@ export interface ICall { receiver: string; receiverSource?: number; timestamp: string; +} + +export interface ICallDuration { + ms: number; + s: number; + m: number; + h: number; } \ No newline at end of file diff --git a/phone/src/modal/components/CallTimer.tsx b/phone/src/modal/components/CallTimer.tsx index 38c8cdf..6c283c9 100644 --- a/phone/src/modal/components/CallTimer.tsx +++ b/phone/src/modal/components/CallTimer.tsx @@ -1,18 +1,24 @@ import React, { useState, useCallback, useEffect } from 'react' +import { useDuration } from '../hooks/useDuration'; function CallTimer({ isAccepted }) { - const [time, setTime] = useState({ ms: 0, s: 0, m: 0, h: 0 }) + const { duration, setDuration } = useDuration() useEffect(() => { - let timer = null + let timer; if (isAccepted) { run() timer = setInterval(run, 10) + } else { + clearInterval(timer) } - return () => clearInterval(timer) }, []); - let updatedMs = time.ms, updatedS = time.s, updatedM = time.m, updatedH = time.h; + + + let updatedMs = duration.ms, updatedS = duration.s, updatedM = duration.m, updatedH = duration.h; + + if (!duration) return null; const run = () => { if (updatedM === 60) { @@ -28,10 +34,9 @@ function CallTimer({ isAccepted }) { updatedMs = 0 } updatedMs++; - return setTime({ ms: updatedMs, s: updatedS, m: updatedM, h: updatedH }) + return setDuration({ ms: updatedMs, s: updatedS, m: updatedM, h: updatedH }) } - if (!isAccepted) return null; return (
- {(time.h >= 10) ? time.h : '0'+time.h} :  - {(time.m >= 10) ? time.m : '0'+time.m} :  - {(time.s >= 10) ? time.s : '0'+time.s}  + {(duration.h >= 10) ? duration.h : '0'+duration.h} :  + {(duration.m >= 10) ? duration.m : '0'+duration.m} :  + {(duration.s >= 10) ? duration.s : '0'+duration.s} 
) } diff --git a/phone/src/modal/hooks/state.ts b/phone/src/modal/hooks/state.ts index 03a4814..674eacc 100644 --- a/phone/src/modal/hooks/state.ts +++ b/phone/src/modal/hooks/state.ts @@ -7,6 +7,10 @@ export const callerState = { }), callModal: atom({ key: 'callingModal', - default: false + default: true + }), + callDuration: atom({ + key: 'callDuration', + default: {ms: 0, s: 0, m: 0, h: 0} }) }; diff --git a/phone/src/modal/hooks/useCall.ts b/phone/src/modal/hooks/useCall.ts index 6adc8ea..daf4da6 100644 --- a/phone/src/modal/hooks/useCall.ts +++ b/phone/src/modal/hooks/useCall.ts @@ -1,5 +1,5 @@ import { useRecoilState } from 'recoil'; -import { CallProps } from '../../common/typings/call'; +import { CallProps, ICallDuration } from '../../common/typings/call'; import { callerState } from './state'; interface CallHook { diff --git a/phone/src/modal/hooks/useDuration.ts b/phone/src/modal/hooks/useDuration.ts new file mode 100644 index 0000000..67a143a --- /dev/null +++ b/phone/src/modal/hooks/useDuration.ts @@ -0,0 +1,7 @@ +import { useRecoilState } from 'recoil'; +import { callerState } from './state'; + +export const useDuration = () => { + const [duration, setDuration] = useRecoilState(callerState.callDuration); + return { duration, setDuration} +} \ No newline at end of file