diff --git a/phone/src/modal/components/CallTimer.tsx b/phone/src/modal/components/CallTimer.tsx index 724dca8..2b6414b 100644 --- a/phone/src/modal/components/CallTimer.tsx +++ b/phone/src/modal/components/CallTimer.tsx @@ -1,36 +1,42 @@ -import React, { useCallback, useEffect, useRef } from 'react'; +import React, { useEffect, useRef } from 'react'; import { useDuration } from '../hooks/useDuration'; + +const runTimer = (updatedTime, setDuration) => { + let newTime = { ...updatedTime.current }; + if (newTime.m >= 60) { + newTime = { ...newTime, h: newTime.h + 1, m: 0 }; + } + if (newTime.s >= 60) { + newTime = { ...newTime, m: newTime.m + 1, s: 0 }; + } + if (newTime.ms >= 100) { + newTime = { ...newTime, s: newTime.s + 1, ms: 0 }; + } + + newTime = { ...newTime, ms: newTime.ms + 1 }; + updatedTime.current = newTime; + setDuration(newTime); +} + function CallTimer({ isAccepted }) { const { duration, setDuration } = useDuration(); const updatedTime = useRef({ ms: 0, s: 0, m: 0, h: 0 }); + const timer = useRef(null) - const run = useCallback(() => { - let newTime = { ...updatedTime.current }; - if (newTime.m >= 60) { - newTime = { ...newTime, h: newTime.h + 1, m: 0 }; - } - if (newTime.s >= 60) { - newTime = { ...newTime, m: newTime.m + 1, s: 0 }; - } - if (newTime.ms >= 100) { - newTime = { ...newTime, s: newTime.s + 1, ms: 0 }; - } - - newTime = { ...newTime, ms: newTime.ms + 1 }; - updatedTime.current = newTime; - setDuration(newTime); - }, [setDuration]); useEffect(() => { - let timer; if (isAccepted) { - run(); - timer = setInterval(run, 10); + runTimer(updatedTime, setDuration) + timer.current = setInterval(() => { + runTimer(updatedTime, setDuration) + }, 10); } else { - clearInterval(timer); + clearInterval(timer.current); + timer.current = null; } - }, [isAccepted, run]); + return () => clearInterval(timer.current) + }, [isAccepted, setDuration]); if (!duration) return null; diff --git a/phone/src/modal/hooks/state.ts b/phone/src/modal/hooks/state.ts index ea43d96..674eacc 100644 --- a/phone/src/modal/hooks/state.ts +++ b/phone/src/modal/hooks/state.ts @@ -7,7 +7,7 @@ export const callerState = { }), callModal: atom({ key: 'callingModal', - default: false + default: true }), callDuration: atom({ key: 'callDuration',