diff --git a/frontend/src/pages/TrainingCoachPage.jsx b/frontend/src/pages/TrainingCoachPage.jsx index b84c3ba..09fdd4b 100644 --- a/frontend/src/pages/TrainingCoachPage.jsx +++ b/frontend/src/pages/TrainingCoachPage.jsx @@ -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