Skip to content

Commit

Permalink
Merge pull request #2683 from ever-co/fix/task-time-tracking-issues
Browse files Browse the repository at this point in the history
Fix task time tracking issues
  • Loading branch information
evereq authored Jul 4, 2024
2 parents e93a487 + d3e252a commit 3d34eb0
Showing 1 changed file with 39 additions and 27 deletions.
66 changes: 39 additions & 27 deletions apps/web/app/hooks/features/useTimer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand All @@ -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
Expand Down Expand Up @@ -271,9 +271,9 @@ export function useTimer() {

// Loading states
useEffect(() => {
if (firstLoad) {
setTimerStatusFetching(loading);
}
// if (firstLoad) {
setTimerStatusFetching(loading);
// }
}, [loading, firstLoad, setTimerStatusFetching]);

useEffect(() => {
Expand Down Expand Up @@ -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 (
Expand Down

0 comments on commit 3d34eb0

Please sign in to comment.