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.
This commit is contained in:
parent
3673053fe2
commit
af3476b833
|
|
@ -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
|
||||
|
|
|
|||
Loading…
Reference in New Issue
Block a user