mirror of
https://github.com/darksaid98/new-phone-who-dis.git
synced 2026-12-04 14:38:45 +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',
|
app: 'CALL',
|
||||||
method: 'setCaller',
|
method: 'setCaller',
|
||||||
data: {
|
data: {
|
||||||
accepted: false,
|
accepted: true,
|
||||||
isTransmitter: false,
|
isTransmitter: true,
|
||||||
transmitter: 'Chip',
|
transmitter: 'Chip',
|
||||||
receiver: 'Taso',
|
receiver: 'Taso',
|
||||||
phone_number: '860-4504',
|
phone_number: '860-4504',
|
||||||
|
|||||||
@@ -13,3 +13,10 @@ export interface ICall {
|
|||||||
receiverSource?: number;
|
receiverSource?: number;
|
||||||
timestamp: string;
|
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 React, { useState, useCallback, useEffect } from 'react'
|
||||||
|
import { useDuration } from '../hooks/useDuration';
|
||||||
|
|
||||||
function CallTimer({ isAccepted }) {
|
function CallTimer({ isAccepted }) {
|
||||||
const [time, setTime] = useState({ ms: 0, s: 0, m: 0, h: 0 })
|
const { duration, setDuration } = useDuration()
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
let timer = null
|
let timer;
|
||||||
if (isAccepted) {
|
if (isAccepted) {
|
||||||
run()
|
run()
|
||||||
timer = setInterval(run, 10)
|
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 = () => {
|
const run = () => {
|
||||||
if (updatedM === 60) {
|
if (updatedM === 60) {
|
||||||
@@ -28,10 +34,9 @@ function CallTimer({ isAccepted }) {
|
|||||||
updatedMs = 0
|
updatedMs = 0
|
||||||
}
|
}
|
||||||
updatedMs++;
|
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 (
|
return (
|
||||||
<div style={{
|
<div style={{
|
||||||
@@ -39,9 +44,9 @@ function CallTimer({ isAccepted }) {
|
|||||||
marginTop: '2em',
|
marginTop: '2em',
|
||||||
fontSize: 30
|
fontSize: 30
|
||||||
}}>
|
}}>
|
||||||
<span>{(time.h >= 10) ? time.h : '0'+time.h}</span> :
|
<span>{(duration.h >= 10) ? duration.h : '0'+duration.h}</span> :
|
||||||
<span>{(time.m >= 10) ? time.m : '0'+time.m}</span> :
|
<span>{(duration.m >= 10) ? duration.m : '0'+duration.m}</span> :
|
||||||
<span>{(time.s >= 10) ? time.s : '0'+time.s}</span>
|
<span>{(duration.s >= 10) ? duration.s : '0'+duration.s}</span>
|
||||||
</div>
|
</div>
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -7,6 +7,10 @@ export const callerState = {
|
|||||||
}),
|
}),
|
||||||
callModal: atom({
|
callModal: atom({
|
||||||
key: 'callingModal',
|
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 { useRecoilState } from 'recoil';
|
||||||
import { CallProps } from '../../common/typings/call';
|
import { CallProps, ICallDuration } from '../../common/typings/call';
|
||||||
import { callerState } from './state';
|
import { callerState } from './state';
|
||||||
|
|
||||||
interface CallHook {
|
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