diff --git a/apps/web/app/hooks/features/useTimer.ts b/apps/web/app/hooks/features/useTimer.ts index c87d48021..fd64d7950 100644 --- a/apps/web/app/hooks/features/useTimer.ts +++ b/apps/web/app/hooks/features/useTimer.ts @@ -84,27 +84,27 @@ function useLocalTimeCounter(timerStatus: ITimerStatus | null, activeTeamTask: I // Update local time status (storage and store) only when global timerStatus changes useEffect(() => { - if (firstLoad) { - const localStatus = getLocalCounterStatus(); - localStatus && setLocalTimerStatus(localStatus); - - const timerStatusDate = timerStatus?.lastLog?.createdAt - ? moment(timerStatus?.lastLog?.createdAt).unix() * 1000 - timerStatus?.lastLog?.duration - : 0; - - timerStatus && - updateLocalTimerStatus({ - runnedDateTime: - (timerStatus.running ? timerStatusDate || Date.now() : 0) || localStatus?.runnedDateTime || 0, - running: timerStatus.running, - lastTaskId: timerStatus.lastLog?.taskId || null - }); - } + // if (firstLoad) { + const localStatus = getLocalCounterStatus(); + localStatus && setLocalTimerStatus(localStatus); + + const timerStatusDate = timerStatus?.lastLog?.createdAt + ? moment(timerStatus?.lastLog?.createdAt).unix() * 1000 - timerStatus?.lastLog?.duration + : 0; + + timerStatus && + updateLocalTimerStatus({ + runnedDateTime: + (timerStatus.running ? timerStatusDate || Date.now() : 0) || localStatus?.runnedDateTime || 0, + running: timerStatus.running, + lastTaskId: timerStatus.lastLog?.taskId || null + }); + // } }, [firstLoad, timerStatus, getLocalCounterStatus, setLocalTimerStatus, updateLocalTimerStatus]); // THis is form constant update of the progress line timerSecondsRef.current = useMemo(() => { - if (!firstLoad) return 0; + // if (!firstLoad) return 0; if (seconds > timerSecondsRef.current) { return seconds; } @@ -115,16 +115,16 @@ function useLocalTimeCounter(timerStatus: ITimerStatus | null, activeTeamTask: I }, [seconds, firstLoad, timerStatusRef]); useEffect(() => { - if (firstLoad) { - timerSecondsRef.current = 0; - setTimerSeconds(0); - } + // if (firstLoad) { + timerSecondsRef.current = 0; + setTimerSeconds(0); + // } }, [activeTeamTask?.id, setTimerSeconds, firstLoad, timerSecondsRef]); useEffect(() => { - if (firstLoad) { - setTimerSeconds(timerSecondsRef.current); - } + // if (firstLoad) { + setTimerSeconds(timerSecondsRef.current); + // } }, [setTimerSeconds, firstLoad]); // Time Counter @@ -271,9 +271,9 @@ export function useTimer() { // Loading states useEffect(() => { - if (firstLoad) { - setTimerStatusFetching(loading); - } + // if (firstLoad) { + setTimerStatusFetching(loading); + // } }, [loading, firstLoad, setTimerStatusFetching]); useEffect(() => { @@ -355,11 +355,23 @@ export function useTimer() { running: false }); + syncTimer(); + return stopTimerQueryCall(timerStatus?.lastLog?.source || TimerSource.TEAMS).then((res) => { res.data && !isEqual(timerStatus, res.data) && setTimerStatus(res.data); }); + // eslint-disable-next-line react-hooks/exhaustive-deps }, [timerStatus, setTimerStatus, stopTimerQueryCall, taskId, updateLocalTimerStatus]); + useEffect(() => { + if (timerStatus?.running) { + const syncTimerInterval = setInterval(() => { + syncTimer(); + }, 60000); + return () => clearInterval(syncTimerInterval); + } + }, [syncTimer, timerStatus]); + // If active team changes then stop the timer useEffect(() => { if (