mirror of
https://github.com/darksaid98/new-phone-who-dis.git
synced 2026-12-03 06:28:51 +00:00
tweak(call/nui): duration as global state
This commit is contained in:
1 parent
dd008775f9
commit
d3b04f4cd9
6 files changed
+36
-13
No files matched your search
+2
-2
@@ -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',
|
||||
|
||||
@@ -12,4 +12,11 @@ export interface ICall {
|
||||
receiver: string;
|
||||
receiverSource?: number;
|
||||
timestamp: string;
|
||||
}
|
||||
|
||||
export interface ICallDuration {
|
||||
ms: number;
|
||||
s: number;
|
||||
m: number;
|
||||
h: number;
|
||||
}
|
||||
@@ -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 (
|
||||
<div style={{
|
||||
@@ -39,9 +44,9 @@ function CallTimer({ isAccepted }) {
|
||||
marginTop: '2em',
|
||||
fontSize: 30
|
||||
}}>
|
||||
<span>{(time.h >= 10) ? time.h : '0'+time.h}</span> :
|
||||
<span>{(time.m >= 10) ? time.m : '0'+time.m}</span> :
|
||||
<span>{(time.s >= 10) ? time.s : '0'+time.s}</span>
|
||||
<span>{(duration.h >= 10) ? duration.h : '0'+duration.h}</span> :
|
||||
<span>{(duration.m >= 10) ? duration.m : '0'+duration.m}</span> :
|
||||
<span>{(duration.s >= 10) ? duration.s : '0'+duration.s}</span>
|
||||
</div>
|
||||
)
|
||||
}
|
||||
|
||||
@@ -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}
|
||||
})
|
||||
};
|
||||
@@ -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 {
|
||||
|
||||
@@ -0,0 +1,7 @@
|
||||
import { useRecoilState } from 'recoil';
|
||||
import { callerState } from './state';
|
||||
|
||||
export const useDuration = () => {
|
||||
const [duration, setDuration] = useRecoilState(callerState.callDuration);
|
||||
return { duration, setDuration}
|
||||
}
|
||||
Reference in new issue
Block a user