From 08be28a475b69278c865fc3080dccca124641251 Mon Sep 17 00:00:00 2001 From: Guido Doumen Date: Mon, 16 Nov 2026 11:22:38 +0100 Subject: [PATCH 1/2] Use scheduled workout prompt page service in useScheduledWorkoutPrompt --- .../useScheduledWorkoutPrompt.test.ts | 66 +++++-------- .../workouts/useScheduledWorkoutPrompt.ts | 98 +++++++------------ .../ActivitiesPage.rideAgain.test.tsx | 5 +- .../ActivitiesPage/ActivitiesPage.test.tsx | 9 +- src/pages/RoutesPage/RoutesPage.test.tsx | 5 +- src/pages/WorkoutsPage/WorkoutsPage.test.tsx | 9 +- 6 files changed, 73 insertions(+), 119 deletions(-) diff --git a/src/hooks/workouts/useScheduledWorkoutPrompt.test.ts b/src/hooks/workouts/useScheduledWorkoutPrompt.test.ts index bb86e4a9..18e63128 100644 --- a/src/hooks/workouts/useScheduledWorkoutPrompt.test.ts +++ b/src/hooks/workouts/useScheduledWorkoutPrompt.test.ts @@ -4,93 +4,81 @@ import { useScheduledWorkoutPrompt } from './useScheduledWorkoutPrompt'; const mockNavigate = jest.fn(); -let appStateStore: Record; -const mockGetState = jest.fn((key: string) => appStateStore[key]); -const mockSetState = jest.fn((key: string, value: any) => { - if (value === null) delete appStateStore[key]; - else appStateStore[key] = value; -}); - -class FakeWorkoutCalendar extends EventEmitter { - getScheduledToday = jest.fn(); +// Fake prompt page service: the once-per-session guard and the calendar lookup live in the +// service, so here `show` just returns what the service would decide. +class FakePromptService extends EventEmitter { + show = jest.fn(); + onYes = jest.fn((prompt: any) => ({ page: 'workouts', autoOpenDetailsId: prompt.workoutId })); + onNo = jest.fn(); + onCheckWorkouts = jest.fn(() => ({ page: 'workouts' })); } -let mockCalendar: FakeWorkoutCalendar; +let mockService: FakePromptService; jest.mock('incyclist-services', () => ({ - useAppState: () => ({ getState: mockGetState, setState: mockSetState }), - useWorkoutCalendar: () => mockCalendar, + getScheduledWorkoutPromptPageService: () => mockService, })); jest.mock('../../services', () => ({ navigate: (...args: any[]) => mockNavigate(...args), })); -jest.mock('../logging', () => ({ - useLogging: () => ({ logEvent: jest.fn(), logError: jest.fn() }), -})); - -const SCHEDULED = { - id: 'intervals:cal-entry-99', // calendar-entry composite id - must NOT be what's navigated with - name: 'VO2 Max Intervals', - type: 'scheduled', - workoutId: 'workout-42', - workout: { id: 'workout-42', name: 'VO2 Max Intervals' }, -}; +const PROMPT = { title: 'VO2 Max Intervals', workoutId: 'workout-42' }; describe('useScheduledWorkoutPrompt', () => { beforeEach(() => { jest.clearAllMocks(); - appStateStore = {}; - mockCalendar = new FakeWorkoutCalendar(); + mockService = new FakePromptService(); }); it('shows nothing when no workout is scheduled today', () => { - mockCalendar.getScheduledToday.mockReturnValue(undefined); + mockService.show.mockReturnValue(null); const { result } = renderHook(() => useScheduledWorkoutPrompt()); expect(result.current.prompt).toBeNull(); }); it('shows the prompt immediately when a workout is already scheduled at mount', () => { - mockCalendar.getScheduledToday.mockReturnValue(SCHEDULED); + mockService.show.mockReturnValue(PROMPT); const { result } = renderHook(() => useScheduledWorkoutPrompt()); expect(result.current.prompt).toEqual({ title: 'VO2 Max Intervals' }); }); it('sets the once-per-session guard as soon as the prompt is triggered', () => { - mockCalendar.getScheduledToday.mockReturnValue(SCHEDULED); + // the guard is owned by the service: the hook must ask the service exactly once at mount + mockService.show.mockReturnValue(PROMPT); renderHook(() => useScheduledWorkoutPrompt()); - expect(mockSetState).toHaveBeenCalledWith('scheduledPromptShown', true); + expect(mockService.show).toHaveBeenCalledTimes(1); }); it('does not show the prompt again once the guard is already set', () => { - appStateStore.scheduledPromptShown = true; - mockCalendar.getScheduledToday.mockReturnValue(SCHEDULED); + // service returns null once its guard is set + mockService.show.mockReturnValue(null); const { result } = renderHook(() => useScheduledWorkoutPrompt()); expect(result.current.prompt).toBeNull(); }); it('reacts to the calendar emitting "scheduled" after mount (async preload arrival)', () => { - mockCalendar.getScheduledToday.mockReturnValue(undefined); + mockService.show.mockReturnValue(null); const { result } = renderHook(() => useScheduledWorkoutPrompt()); expect(result.current.prompt).toBeNull(); - mockCalendar.getScheduledToday.mockReturnValue(SCHEDULED); + mockService.show.mockReturnValue(PROMPT); act(() => { - mockCalendar.emit('scheduled', SCHEDULED); + mockService.emit('scheduled', PROMPT); }); expect(result.current.prompt).toEqual({ title: 'VO2 Max Intervals' }); }); it('does nothing while disabled (enabled=false), even with a workout scheduled and no guard', () => { - mockCalendar.getScheduledToday.mockReturnValue(SCHEDULED); + mockService.show.mockReturnValue(PROMPT); const { result } = renderHook(() => useScheduledWorkoutPrompt(false)); expect(result.current.prompt).toBeNull(); - expect(mockSetState).not.toHaveBeenCalled(); + expect(mockService.show).not.toHaveBeenCalled(); + expect(mockService.listenerCount('scheduled')).toBe(0); }); it('onYes navigates to workouts with the workout content id (not the calendar entry id) and clears the prompt', () => { - mockCalendar.getScheduledToday.mockReturnValue(SCHEDULED); + mockService.show.mockReturnValue(PROMPT); const { result } = renderHook(() => useScheduledWorkoutPrompt()); act(() => { @@ -102,7 +90,7 @@ describe('useScheduledWorkoutPrompt', () => { }); it('onNo just dismisses without navigating', () => { - mockCalendar.getScheduledToday.mockReturnValue(SCHEDULED); + mockService.show.mockReturnValue(PROMPT); const { result } = renderHook(() => useScheduledWorkoutPrompt()); act(() => { @@ -114,7 +102,7 @@ describe('useScheduledWorkoutPrompt', () => { }); it('onCheckWorkouts navigates to workouts with no auto-open param and dismisses', () => { - mockCalendar.getScheduledToday.mockReturnValue(SCHEDULED); + mockService.show.mockReturnValue(PROMPT); const { result } = renderHook(() => useScheduledWorkoutPrompt()); act(() => { diff --git a/src/hooks/workouts/useScheduledWorkoutPrompt.ts b/src/hooks/workouts/useScheduledWorkoutPrompt.ts index e591eef9..8a43006b 100644 --- a/src/hooks/workouts/useScheduledWorkoutPrompt.ts +++ b/src/hooks/workouts/useScheduledWorkoutPrompt.ts @@ -1,12 +1,6 @@ -import { useCallback, useEffect, useRef, useState } from 'react'; -import { ScheduledWorkout, useAppState, useWorkoutCalendar } from 'incyclist-services'; +import { useCallback, useEffect, useMemo, useState } from 'react'; +import { getScheduledWorkoutPromptPageService, ScheduledWorkoutPromptDisplayProps } from 'incyclist-services'; import { navigate } from '../../services'; -import { useLogging } from '../logging'; - -// Once-per-app-session guard (workout-list-page-service-design.md §11.2). Deliberately kept in -// AppStateService's in-memory `state` (not `getPersistedState`/settings) - it must reset on every -// app launch, never survive to the next day, per the design's "no per-day persistence in Phase 1." -const SCHEDULED_PROMPT_GUARD_KEY = 'scheduledPromptShown'; export interface ScheduledWorkoutPromptState { /** Workout name, for "You have scheduled today - do you want to start it?" */ @@ -22,83 +16,59 @@ export interface UseScheduledWorkoutPromptResult { } /** - * Cross-cutting mobile-UI mechanism (workout-mobile-hld.md §3.1/§5, - * workout-list-page-service-design.md §11) - NOT owned by the Workouts feature. Meant to be - * called from every content page except `devices`/pairing (`routes`, `activities`, `workouts`, - * `search`, `main`). + * Cross-cutting mobile-UI mechanism - NOT owned by the Workouts feature. Meant to be called from + * every content page except `devices`/pairing (`routes`, `activities`, `workouts`, `search`, `main`). * - * On first check per app session, if `WorkoutCalendarService.getScheduledToday()` returns a - * workout and the once-per-session guard hasn't fired yet, this surfaces a prompt: Yes navigates - * to the Workouts page with an auto-open param (consumed by `WorkoutsPage`'s - * `autoOpenDetailsId` -> `WorkoutDetailsDialog`), No just dismisses, Check workouts navigates to - * the Workouts page. `scheduledToday` is populated asynchronously by preload, so this also - * subscribes to the calendar service's `'scheduled'` event to catch it arriving after mount, not - * just what's already known at the moment this hook first runs. + * Thin wrapper around the scheduled-workout prompt page service, which decides whether a prompt is + * due (including the once-per-session guard). This hook only holds the visible prompt, re-checks + * when the service relays `'scheduled'` (calendar data arrives asynchronously) and performs the + * navigation intents: Yes opens the Workouts page with an auto-open param, Check workouts opens + * the Workouts page, No just dismisses. * * @param enabled Defaults to `true`. Pass `false` from a host that renders more than one * navigation route (e.g. `NotImplementedPage` doubles as both `main`, which should trigger this, * and `user`, which shouldn't) - keeps the hook call itself unconditional (Rules of Hooks) while - * suppressing its side effects (calendar check, guard, navigation) for the excluded route. + * suppressing its side effects (service call, subscription, navigation) for the excluded route. */ export const useScheduledWorkoutPrompt = (enabled: boolean = true): UseScheduledWorkoutPromptResult => { - const appState = useAppState(); - const calendar = useWorkoutCalendar(); - const { logEvent } = useLogging('ScheduledWorkoutPrompt'); - - const [prompt, setPrompt] = useState<ScheduledWorkoutPromptState | null>(null); - - // Holds the id actually needed to re-open the details dialog. Deliberately NOT - // `scheduled.id` (WorkoutCalendarService.getScheduledWorkout()'s composite `source:workoutId` - // string) - `WorkoutListPageService.findWorkoutCard()`/`WorkoutCard.getId()` key cards by the - // plain `Workout.id` (same field `getUpcomingTrainingProps()`'s "Upcoming Training" rows - // already use for their own onOpenDetails calls). Passing the composite id through would - // silently fail the card lookup and leave `WorkoutDetailsDialog` unable to load anything. - const refWorkoutId = useRef<string | null>(null); - - const checkAndShow = useCallback(() => { - if (!enabled) return; - if (appState.getState(SCHEDULED_PROMPT_GUARD_KEY)) return; - - const scheduled: ScheduledWorkout | undefined = calendar.getScheduledToday(); - if (!scheduled) return; - - const workoutId = scheduled.workout?.id ?? scheduled.workoutId; - if (!workoutId) return; - - refWorkoutId.current = workoutId; - // Set the guard the moment the prompt is triggered, not on a later per-action basis - - // matches workout-list-page-service-design.md §11.2 ("it triggers the popup and sets the - // guard") and is simpler/safer than three separate call sites all needing to remember it. - appState.setState(SCHEDULED_PROMPT_GUARD_KEY, true); - setPrompt({ title: scheduled.name }); - logEvent({ message: 'scheduled workout prompt shown', workoutId }); - }, [enabled, appState, calendar, logEvent]); + const [promptProps, setPromptProps] = useState<ScheduledWorkoutPromptDisplayProps | null>(null); useEffect(() => { if (!enabled) return; + const service = getScheduledWorkoutPromptPageService(); + const checkAndShow = () => { + const props = service.show(); + if (props) setPromptProps(props); + }; + checkAndShow(); - calendar.on('scheduled', checkAndShow); + service.on('scheduled', checkAndShow); return () => { - calendar.off('scheduled', checkAndShow); + service.off('scheduled', checkAndShow); }; - }, [enabled, calendar, checkAndShow]); + }, [enabled]); const onYes = useCallback(() => { - setPrompt(null); - const workoutId = refWorkoutId.current; - navigate('workouts', workoutId ? { autoOpenDetailsId: workoutId } : undefined); - }, []); + if (!promptProps) return; + const nav = getScheduledWorkoutPromptPageService().onYes(promptProps); + setPromptProps(null); + navigate(nav.page, nav.autoOpenDetailsId ? { autoOpenDetailsId: nav.autoOpenDetailsId } : undefined); + }, [promptProps]); const onNo = useCallback(() => { - setPrompt(null); - }, []); + if (promptProps) getScheduledWorkoutPromptPageService().onNo(promptProps); + setPromptProps(null); + }, [promptProps]); const onCheckWorkouts = useCallback(() => { - setPrompt(null); - navigate('workouts'); - }, []); + const nav = promptProps ? getScheduledWorkoutPromptPageService().onCheckWorkouts(promptProps) : { page: 'workouts' as const }; + setPromptProps(null); + navigate(nav.page); + }, [promptProps]); + + const prompt = useMemo(() => (promptProps ? { title: promptProps.title } : null), [promptProps]); return { prompt, onYes, onNo, onCheckWorkouts }; }; diff --git a/src/pages/ActivitiesPage/ActivitiesPage.rideAgain.test.tsx b/src/pages/ActivitiesPage/ActivitiesPage.rideAgain.test.tsx index 42dc4974..8bcf3324 100644 --- a/src/pages/ActivitiesPage/ActivitiesPage.rideAgain.test.tsx +++ b/src/pages/ActivitiesPage/ActivitiesPage.rideAgain.test.tsx @@ -22,9 +22,8 @@ jest.mock('incyclist-services', () => ({ onCloseActivity: () => {}, onRideAgain: mockOnRideAgain, }), - useAppState: () => ({ getState: jest.fn(), setState: jest.fn() }), - useWorkoutCalendar: () => ({ - getScheduledToday: jest.fn(), + getScheduledWorkoutPromptPageService: () => ({ + show: jest.fn(), on: jest.fn(), off: jest.fn(), }), diff --git a/src/pages/ActivitiesPage/ActivitiesPage.test.tsx b/src/pages/ActivitiesPage/ActivitiesPage.test.tsx index aafa8d2c..0031cee2 100644 --- a/src/pages/ActivitiesPage/ActivitiesPage.test.tsx +++ b/src/pages/ActivitiesPage/ActivitiesPage.test.tsx @@ -27,11 +27,10 @@ jest.mock('incyclist-services', () => ({ onCloseActivity: () => {}, onDeleteActivity: (id: string) => mockOnDeleteActivity(id), }), - // Needed by useScheduledWorkoutPrompt (session 5.7), which every content page - including - // ActivitiesPage - now calls. - useAppState: () => ({ getState: jest.fn(), setState: jest.fn() }), - useWorkoutCalendar: () => ({ - getScheduledToday: jest.fn(), + // Needed by useScheduledWorkoutPrompt, which every content page - including + // ActivitiesPage - calls. + getScheduledWorkoutPromptPageService: () => ({ + show: jest.fn(), on: jest.fn(), off: jest.fn(), }), diff --git a/src/pages/RoutesPage/RoutesPage.test.tsx b/src/pages/RoutesPage/RoutesPage.test.tsx index c5645cff..92e756af 100644 --- a/src/pages/RoutesPage/RoutesPage.test.tsx +++ b/src/pages/RoutesPage/RoutesPage.test.tsx @@ -39,9 +39,8 @@ jest.mock('incyclist-services', () => ({ onDownloadDelete: mockOnDownloadDelete, onDownloadKeepInstead: mockOnDownloadKeepInstead, }), - useAppState: () => ({ getState: jest.fn(), setState: jest.fn() }), - useWorkoutCalendar: () => ({ - getScheduledToday: jest.fn(), + getScheduledWorkoutPromptPageService: () => ({ + show: jest.fn(), on: jest.fn(), off: jest.fn(), }), diff --git a/src/pages/WorkoutsPage/WorkoutsPage.test.tsx b/src/pages/WorkoutsPage/WorkoutsPage.test.tsx index 6142d742..6bfc16a1 100644 --- a/src/pages/WorkoutsPage/WorkoutsPage.test.tsx +++ b/src/pages/WorkoutsPage/WorkoutsPage.test.tsx @@ -36,11 +36,10 @@ jest.mock('incyclist-services', () => ({ onImportOpen: mockOnImportOpen, onSelectGroup: mockOnSelectGroup, }), - // Needed by useScheduledWorkoutPrompt (session 5.7), which every content page - including - // WorkoutsPage - now calls. - useAppState: () => ({ getState: jest.fn(), setState: jest.fn() }), - useWorkoutCalendar: () => ({ - getScheduledToday: jest.fn(), + // Needed by useScheduledWorkoutPrompt, which every content page - including + // WorkoutsPage - calls. + getScheduledWorkoutPromptPageService: () => ({ + show: jest.fn(), on: jest.fn(), off: jest.fn(), }), From 4b9f0277ce623b69eb7e4df6035148199d0db8a4 Mon Sep 17 00:00:00 2001 From: Guido Doumen <git@doumen-laumen.de> Date: Mon, 16 Nov 2026 14:23:41 +0100 Subject: [PATCH 2/2] chore: bump incyclist-services to 1.9.28 --- package-lock.json | 32 ++++++++++++++++---------------- package.json | 2 +- 2 files changed, 17 insertions(+), 17 deletions(-) diff --git a/package-lock.json b/package-lock.json index 823dd954..761a6ad2 100644 --- a/package-lock.json +++ b/package-lock.json @@ -24,7 +24,7 @@ "axios": "^1.17.0", "events": "^3.3.0", "gd-eventlog": "^0.1.27", - "incyclist-services": "^1.9.27", + "incyclist-services": "^1.9.28", "mqtt": "^5.15.2", "path-browserify": "^1.0.1", "process": "^0.11.10", @@ -8231,12 +8231,12 @@ "license": "MIT" }, "node_modules/axios": { - "version": "1.21.0", - "resolved": "https://registry.npmjs.org/axios/-/axios-1.21.0.tgz", - "integrity": "sha512-YCzXMB53Q9PZBsh7yN7wtapit4WepwcHS2fP8QhGsBxSz5YtgYobV7GGlWlAJi+RxfU77GQTQi2DSLUma42EYA==", + "version": "1.21.1", + "resolved": "https://registry.npmjs.org/axios/-/axios-1.21.1.tgz", + "integrity": "sha512-6eI5Q/Y4u/Um9dDlsRoq5pY2rxnUTv+L4PpLitnbk/i4jb3MxLZ5CsN5Ih3IysbeRYK4pR+P7qfMsOQl9fwwFQ==", "license": "MIT", "dependencies": { - "follow-redirects": "^1.16.0", + "follow-redirects": "^1.16.2", "form-data": "^4.0.6", "https-proxy-agent": "^5.0.1", "proxy-from-env": "^2.1.0" @@ -12006,9 +12006,9 @@ "license": "MIT" }, "node_modules/follow-redirects": { - "version": "1.16.0", - "resolved": "https://registry.npmjs.org/follow-redirects/-/follow-redirects-1.16.0.tgz", - "integrity": "sha512-y5rN/uOsadFT/JfYwhxRS5R7Qce+g3zG97+JrtFZlC9klX/W5hD7iiLzScI4nZqUS7DNUdhPgw4xI8W2LuXlUw==", + "version": "1.16.3", + "resolved": "https://registry.npmjs.org/follow-redirects/-/follow-redirects-1.16.3.tgz", + "integrity": "sha512-toOSoqN6d51Wjf1aOFJYcoVv1aND9rj/CwjLU0Vz40oDuSpWDayCeKe4JpL5NL0WdFvoOOCI3BRo69HahjbR1A==", "funding": [ { "type": "individual", @@ -12907,15 +12907,15 @@ } }, "node_modules/incyclist-services": { - "version": "1.9.27", - "resolved": "https://registry.npmjs.org/incyclist-services/-/incyclist-services-1.9.27.tgz", - "integrity": "sha512-MTQbfiX95uHyahK8jR1Jn7+TrckHaUQ8cxJrxx1VU4Ogit7zmqhKqQXtYNAtRV+VVvwvtJyyTICI6xP5sQm9JQ==", + "version": "1.9.28", + "resolved": "https://registry.npmjs.org/incyclist-services/-/incyclist-services-1.9.28.tgz", + "integrity": "sha512-qySSSQm/WHSinnvo6sJVU5hMTTWcOofLLW0ZBg2aIGClZhFShOZl6b05tqGY9jmHeYkQ4WWUim0tDIX6L1VfsQ==", "dependencies": { "@garmin/fitsdk": "^21.221.0", - "axios": "^1.21.0", + "axios": "^1.21.1", "incyclist-devices": "^3.0.46", "promise.any": "^2.0.6", - "semver": "^7.7.4", + "semver": "^7.8.6", "tcx-builder": "^1.1.1", "uuid": "^14.0.3", "xml2js": "^0.6.2" @@ -12925,9 +12925,9 @@ } }, "node_modules/incyclist-services/node_modules/semver": { - "version": "7.7.4", - "resolved": "https://registry.npmjs.org/semver/-/semver-7.7.4.tgz", - "integrity": "sha512-vFKC2IEtQnVhpT78h1Yp8wzwrf8CM+MzKMHGJZfBtzhZNycRFnXsHk6E5TxIkkMsgNS7mdX3AGB7x2QM2di4lA==", + "version": "7.8.6", + "resolved": "https://registry.npmjs.org/semver/-/semver-7.8.6.tgz", + "integrity": "sha512-9lpSSU4Mly3mrtruwLs1eLK6+gzbzqeBFwRNjIjE+9LuDpWdYctrQlQ3ej1Ilgi7gknpsTRnPoLP8DpCHqHIJw==", "license": "ISC", "bin": { "semver": "bin/semver.js" diff --git a/package.json b/package.json index 57a58d79..9292c7ee 100644 --- a/package.json +++ b/package.json @@ -39,7 +39,7 @@ "axios": "^1.17.0", "events": "^3.3.0", "gd-eventlog": "^0.1.27", - "incyclist-services": "^1.9.27", + "incyclist-services": "^1.9.28", "mqtt": "^5.15.2", "path-browserify": "^1.0.1", "process": "^0.11.10",