11'use client'
22
33import { useEffect , useMemo , useState , useSyncExternalStore } from 'react'
4- import type { Microcycle , Plan , Workout } from '@/modules/training/plans'
4+ import type { MicrocycleTree , PlanTree , WorkoutTree } from '@/modules/training/plans'
55
66const STORAGE_KEY = 'training-app:active-workout-selection'
77const SSR_SNAPSHOT = '__SSR_SELECTION__'
@@ -17,7 +17,7 @@ const subscribeToSelection = (onStoreChange: () => void) => {
1717 return ( ) => window . removeEventListener ( 'storage' , onStoreChange )
1818}
1919
20- const firstAvailableSelection = ( plans : Plan [ ] ) : WorkoutSelection => {
20+ const firstAvailableSelection = ( plans : PlanTree [ ] ) : WorkoutSelection => {
2121 const plan = plans [ 0 ]
2222 const microcycle = plan ?. microcycles [ 0 ]
2323 const workout = microcycle ?. workouts [ 0 ]
@@ -29,7 +29,7 @@ const firstAvailableSelection = (plans: Plan[]): WorkoutSelection => {
2929 }
3030}
3131
32- const isValidSelection = ( plans : Plan [ ] , selection : WorkoutSelection ) => {
32+ const isValidSelection = ( plans : PlanTree [ ] , selection : WorkoutSelection ) => {
3333 const plan = plans . find ( ( item ) => item . id === selection . planId )
3434 if ( ! plan ) return false
3535
@@ -40,15 +40,15 @@ const isValidSelection = (plans: Plan[], selection: WorkoutSelection) => {
4040}
4141
4242export function useWorkoutSelection (
43- plans : Plan [ ] ,
43+ plans : PlanTree [ ] ,
4444 options : { readOnly ?: boolean } ,
4545) : {
4646 resolvedSelection : WorkoutSelection
47- activePlan : Plan | null
48- activeMicrocycle : Microcycle | null
49- activeWorkout : Workout | null
50- selectPlan : ( plan : Plan ) => void
51- selectMicrocycle : ( plan : Plan , microcycleId : number | string ) => void
47+ activePlan : PlanTree | null
48+ activeMicrocycle : MicrocycleTree | null
49+ activeWorkout : WorkoutTree | null
50+ selectPlan : ( plan : PlanTree ) => void
51+ selectMicrocycle : ( plan : PlanTree , microcycleId : number | string ) => void
5252 selectWorkout : ( workoutId : number | string ) => void
5353} {
5454 const { readOnly } = options
@@ -82,7 +82,7 @@ export function useWorkoutSelection(
8282 window . localStorage . setItem ( STORAGE_KEY , JSON . stringify ( resolvedSelection ) )
8383 } , [ plans , readOnly , resolvedSelection , storedSelectionRaw ] )
8484
85- const selectPlan = ( plan : Plan ) => {
85+ const selectPlan = ( plan : PlanTree ) => {
8686 const nextMicrocycle = plan . microcycles [ 0 ] ?? null
8787 const nextWorkout = nextMicrocycle ?. workouts [ 0 ] ?? null
8888 setSelection ( {
@@ -92,7 +92,7 @@ export function useWorkoutSelection(
9292 } )
9393 }
9494
95- const selectMicrocycle = ( plan : Plan , microcycleId : number | string ) => {
95+ const selectMicrocycle = ( plan : PlanTree , microcycleId : number | string ) => {
9696 const microcycle = plan . microcycles . find ( ( item ) => item . id === microcycleId ) ?? null
9797 const nextWorkout = microcycle ?. workouts [ 0 ] ?? null
9898 setSelection ( {
0 commit comments