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
118 lines (100 loc) · 2.99 KB
/
TaskHooks.ts
File metadata and controls
118 lines (100 loc) · 2.99 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
107
108
109
110
111
112
113
114
115
116
117
118
import { useCallback, useEffect, useMemo, useRef, useState } from 'react';
import { isBefore } from 'date-fns';
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 | undefined) {
const intervalRef = useRef<NodeJS.Timeout>();
const [duration, setDuration] = useState<string>('');
useEffect(() => {
if (!model) {
return;
}
const duration = model.duration;
if (duration !== 0) {
setDuration(msToTime(duration, model.active));
}
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;
}
export function useStartWorkingTime(
timeItems: TaskTimeItemModel[],
): Date | undefined {
return useMemo(() => {
let minTime: Date | undefined;
timeItems.forEach((time) => {
if (!minTime || isBefore(time.time.start, minTime)) {
minTime = time.time.start;
}
});
return minTime;
}, [timeItems]);
}