forked from Yadro/time-tracker
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTaskHooks.ts
More file actions
106 lines (86 loc) · 2.67 KB
/
Copy pathTaskHooks.ts
File metadata and controls
106 lines (86 loc) · 2.67 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
import { useCallback, useEffect, useRef, useState } from 'react';
import { calcDuration, calcDurationGaps, msToTime } from '../helpers/DateTime';
import TaskModel, { ITimeRangeModel } from '../modules/tasks/models/TaskModel';
import TaskTimeItemModel from '../modules/tasks/models/TaskTimeItemModel';
export function useTaskDuration(model?: TaskModel, showZero?: boolean) {
const intervalRef = useRef<NodeJS.Timeout>();
const [duration, setDuration] = useState<string>('');
useEffect(() => {
if (!model) {
return;
}
const duration = model.duration;
const durationMs =
duration !== 0 || showZero ? msToTime(duration, model.active) : '';
setDuration(durationMs);
if (model.active) {
intervalRef.current = setInterval(() => {
setDuration(msToTime(model?.duration || 0));
}, 1000);
}
return () => {
if (intervalRef.current) {
clearInterval(intervalRef.current);
}
};
}, [model, model?.active]);
return duration;
}
export function useTimeItemsDuration(taskTime: TaskTimeItemModel[]) {
const [durationMs, setDurationMs] = useState<number>(0);
const [restMs, setRestMs] = useState<number>(0);
const intervalRef = useRef<NodeJS.Timeout>();
const calcTaskDuration = useCallback(
() => calcDuration(taskTime.map((t) => t.time)),
[taskTime]
);
const calcTaskGapsDuration = useCallback(
() => calcDurationGaps(taskTime.map((t) => t.time)),
[taskTime]
);
const setTimes = useCallback(() => {
setDurationMs(calcTaskDuration());
setRestMs(calcTaskGapsDuration());
}, [calcTaskDuration, calcTaskGapsDuration]);
useEffect(() => {
setTimes();
intervalRef.current = setInterval(() => {
setTimes();
}, 1000);
return () => {
if (intervalRef.current) {
clearInterval(intervalRef.current);
}
};
}, [setTimes, taskTime]);
return {
durationMs,
restMs,
};
}
export function useTimeRangeDuration(timeRange: ITimeRangeModel | undefined) {
const [duration, setDuration] = useState<string>('');
const intervalRef = useRef<NodeJS.Timeout>();
const calcTimeRangeDuration = useCallback(
() => msToTime(timeRange ? calcDuration([timeRange]) : 0),
[timeRange]
);
useEffect(() => {
if (!timeRange) {
return;
}
setDuration(calcTimeRangeDuration());
const haveActiveTime = !timeRange.end;
if (haveActiveTime) {
intervalRef.current = setInterval(() => {
setDuration(calcTimeRangeDuration());
}, 1000);
}
return () => {
if (intervalRef.current) {
clearInterval(intervalRef.current);
}
};
}, [calcTimeRangeDuration, timeRange]);
return duration;
}