From af3476b8334f1b61dcbe5e08b6af561c46d26cc1 Mon Sep 17 00:00:00 2001 From: Lars Date: Wed, 29 Apr 2026 06:54:41 +0200 Subject: [PATCH] fix: improve step management in TrainingCoachPage - 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. --- frontend/src/pages/TrainingCoachPage.jsx | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) 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