Fix rest timer freezing at 0:01 (#14)

The tick callback shadowed the imported i18n helper `t` with the timer
object, so the final tick threw a TypeError on t('Rest over — next set!')
before reaching stopRest(). The interval kept running and re-throwing, the
display stayed on the last value it wrote, and the pending server-side
'rest over' push was never cancelled.

Closes #14
This commit is contained in:
Duarte Santos committed 2026-07-22 14:44:15 +02:00
1 parent f3ba05aece
commit 33317dac91
1 file changed
+9 -9
+9 -9
View File
@@ -40,26 +40,26 @@ export const useUI = create((set, get) => ({
set({ timer: { left: sec, total: sec, endsAt } })
pushRestTimer(sec)
timerTick = () => {
const t = get().timer
if (!t) return
const left = Math.max(0, Math.round((t.endsAt - Date.now()) / 1000))
if (left === t.left) return
const tm = get().timer
if (!tm) return
const left = Math.max(0, Math.round((tm.endsAt - Date.now()) / 1000))
if (left === tm.left) return
const snd = useStore.getState().S.sound
if (left <= 0) {
beep(snd, 880, 0.15); beep(snd, 880, 0.15, 0.25); beep(snd, 1320, 0.4, 0.5)
vibrate([200, 100, 200]); get().toast(t('Rest over — next set! 💪')); get().stopRest(); return
}
if (left <= 3) beep(snd, 660, 0.1)
set({ timer: { ...t, left } })
set({ timer: { ...tm, left } })
}
timerInt = setInterval(timerTick, 1000)
document.addEventListener('visibilitychange', timerTick)
},
addRest(sec) {
const t = get().timer
if (!t) return
const left = t.left + sec
set({ timer: { ...t, left, total: t.total + sec, endsAt: t.endsAt + sec * 1000 } })
const tm = get().timer
if (!tm) return
const left = tm.left + sec
set({ timer: { ...tm, left, total: tm.total + sec, endsAt: tm.endsAt + sec * 1000 } })
pushRestTimer(left)
},
stopRest() {