forked from CodebuffAI/codebuff
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathuse-elapsed-time.ts
More file actions
116 lines (103 loc) · 3.04 KB
/
Copy pathuse-elapsed-time.ts
File metadata and controls
116 lines (103 loc) · 3.04 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
import { useCallback, useEffect, useMemo, useState } from 'react'
export interface ElapsedTimeTracker {
/**
* Start tracking elapsed time from now
*/
start: () => void
/**
* Stop tracking and reset to 0
*/
stop: () => void
/**
* Pause tracking, freezing the current elapsed time
*/
pause: () => void
/**
* Resume tracking from the frozen elapsed time
*/
resume: () => void
/**
* Get the current elapsed seconds
*/
elapsedSeconds: number
/**
* Get the start time timestamp (null if not started)
*/
startTime: number | null
/**
* Whether the timer is currently paused
*/
isPaused: boolean
}
/**
* Hook to track elapsed time with manual start/stop control
* Updates every second while active
*
* @returns ElapsedTimeTracker - Object with start/stop methods and current elapsed time
*
* @example
* const timer = useElapsedTime()
* timer.start() // Start timing
* timer.stop() // Stop and reset
*
* // Pass the timer object to components that need to display elapsed time
* <StatusIndicator timer={timer} />
* <MessageBlock timer={timer} />
*/
export const useElapsedTime = (): ElapsedTimeTracker => {
const [startTime, setStartTime] = useState<number | null>(null)
const [elapsedSeconds, setElapsedSeconds] = useState<number>(0)
const [isPaused, setIsPaused] = useState(false)
// Track accumulated time from previous pause/resume cycles
const [accumulatedSeconds, setAccumulatedSeconds] = useState(0)
const start = useCallback(() => {
setStartTime(Date.now())
setAccumulatedSeconds(0)
setIsPaused(false)
}, [])
const stop = useCallback(() => {
setStartTime(null)
setElapsedSeconds(0)
setAccumulatedSeconds(0)
setIsPaused(false)
}, [])
const pause = useCallback(() => {
if (startTime && !isPaused) {
// Capture current elapsed time before pausing
const currentElapsed = Math.floor((Date.now() - startTime) / 1000)
setAccumulatedSeconds(currentElapsed)
setElapsedSeconds(currentElapsed)
setIsPaused(true)
}
}, [startTime, isPaused])
const resume = useCallback(() => {
if (isPaused) {
// Set a new start time adjusted for accumulated time
setStartTime(Date.now() - accumulatedSeconds * 1000)
setIsPaused(false)
}
}, [isPaused, accumulatedSeconds])
useEffect(() => {
if (!startTime || isPaused) {
// When paused, keep showing the frozen elapsed time (don't reset)
if (!isPaused && !startTime) {
setElapsedSeconds(0)
}
return
}
const updateElapsed = () => {
const elapsed = Math.floor((Date.now() - startTime) / 1000)
setElapsedSeconds(elapsed)
}
// Update immediately
updateElapsed()
// Then update every second
const interval = setInterval(updateElapsed, 1000)
return () => clearInterval(interval)
}, [startTime, isPaused])
const timer = useMemo(
() => ({ start, stop, pause, resume, elapsedSeconds, startTime, isPaused }),
[start, stop, pause, resume, elapsedSeconds, startTime, isPaused],
)
return timer
}