Implement session outcomes, step timing, and live timer fixes
This commit is contained in:
@@ -5,12 +5,23 @@ export function useSessionTimer(startedAt: string | undefined | null): string |
|
||||
const intervalRef = useRef<ReturnType<typeof setInterval> | null>(null)
|
||||
|
||||
useEffect(() => {
|
||||
// Always clear any previous interval before (re)initializing.
|
||||
if (intervalRef.current) {
|
||||
clearInterval(intervalRef.current)
|
||||
intervalRef.current = null
|
||||
}
|
||||
|
||||
if (!startedAt) {
|
||||
setElapsed(null)
|
||||
return
|
||||
}
|
||||
|
||||
const startTime = new Date(startedAt).getTime()
|
||||
const parsedStartTime = new Date(startedAt).getTime()
|
||||
// If the server timestamp is invalid or ahead of the local clock, fall back to "now"
|
||||
// so the timer still starts ticking immediately for the user.
|
||||
const startTime = Number.isNaN(parsedStartTime) || parsedStartTime > Date.now()
|
||||
? Date.now()
|
||||
: parsedStartTime
|
||||
|
||||
const tick = () => {
|
||||
const diff = Math.max(0, Math.floor((Date.now() - startTime) / 1000))
|
||||
|
||||
Reference in New Issue
Block a user