fix: improve step management in TrainingCoachPage
Some checks failed
Deploy Development / deploy (push) Successful in 33s
Test Suite / lint-backend (push) Successful in 0s
Test Suite / build-frontend (push) Successful in 5s
Test Suite / playwright-tests (push) Failing after 1m54s

- Refactored step clamping logic to ensure it correctly handles edge cases when the timeline is empty or when the unit changes.
- Updated useEffect to reset the step to 0 when the timeline is empty, enhancing user experience during training sessions.
This commit is contained in:
Lars 2026-04-29 06:54:41 +02:00
parent 3673053fe2
commit af3476b833

View File

@ -125,13 +125,23 @@ export default function TrainingCoachPage() {
const timeline = useMemo(() => flattenPlanTimeline(unit), [unit])
const clampStep = (s, len = timeline.length) =>
Math.max(0, Math.min(s, Math.max(len - 1, 0)))
useEffect(() => {
if (!unit) return
if (timeline.length === 0) {
setStep(0)
return
}
setStep((prev) => clampStep(prev, timeline.length))
}, [unit, timeline.length])
const elapsedMs =
pausedAccumMs + (runStartAt != null ? Date.now() - runStartAt : 0)
const tickDisplaySec = Math.max(0, Math.floor(elapsedMs / 1000))
const clampStep = (s) =>
Math.max(0, Math.min(s, Math.max(timeline.length - 1, 0)))
const currentEntry = timeline[step]
const nextEntry = timeline[step + 1] || null
const next2Entry = timeline[step + 2] || null