diff --git a/client/src/pages/tripPlanner/usePlannerAddons.ts b/client/src/pages/tripPlanner/usePlannerAddons.ts index 267036ee7..fb1dc3bea 100644 --- a/client/src/pages/tripPlanner/usePlannerAddons.ts +++ b/client/src/pages/tripPlanner/usePlannerAddons.ts @@ -1,8 +1,10 @@ -import { useEffect, useMemo } from 'react'; +import type { TripAddon } from '@trek/shared'; +import { useEffect, useMemo, useRef } from 'react'; import { useShallow } from 'zustand/react/shallow'; import { useTripAddons } from '../../components/Trips/useTripAddons'; import { useAddonStore } from '../../store/addonStore'; +import { useTripStore } from '../../store/tripStore'; import { tripAddonEnabled } from '../../utils/tripAddons'; /** @@ -20,6 +22,13 @@ const WHEN_UNKNOWN: Readonly> = { dawarich: false, }; +/** The trip addons whose sections the trip reads as empty while they are off. */ +const SECTION_ADDONS = ['packing', 'budget', 'documents'] as const; + +function switchedOn(before: readonly TripAddon[], after: readonly TripAddon[], id: string): boolean { + return before.find((a) => a.id === id)?.enabled === false && after.find((a) => a.id === id)?.enabled === true; +} + /** * The addons the trip planner shows for one trip, in both shells: the trip * addons as the trip has them (the owner's choice or the instance switch), @@ -47,6 +56,17 @@ export function usePlannerAddons(tripId: number) { ) as Record; }, [tripAddons.addons, instance]); + // A section switched back on for the open trip was read as empty while it was off. + const lastList = useRef<{ tripId: number; addons: TripAddon[] } | null>(null); + useEffect(() => { + if (tripAddons.addons.length === 0) return; + const before = lastList.current?.tripId === tripId ? lastList.current.addons : null; + lastList.current = { tripId, addons: tripAddons.addons }; + if (before && SECTION_ADDONS.some((id) => switchedOn(before, tripAddons.addons, id))) { + void useTripStore.getState().hydrateActiveTrip(tripId); + } + }, [tripId, tripAddons.addons]); + // Until then enabledAddons is a guess, and the tab guard must not evict a tab on it. const addonsLoaded = tripAddons.loaded && (tripAddons.addons.length > 0 || instance.loaded); diff --git a/client/src/pages/tripPlanner/useTripPlanner.tripAddons.test.tsx b/client/src/pages/tripPlanner/useTripPlanner.tripAddons.test.tsx index 9a578ccf7..516be7d2f 100644 --- a/client/src/pages/tripPlanner/useTripPlanner.tripAddons.test.tsx +++ b/client/src/pages/tripPlanner/useTripPlanner.tripAddons.test.tsx @@ -1,4 +1,4 @@ -// FE-TP-TRIPADDONS-001 to FE-TP-TRIPADDONS-005 +// FE-TP-TRIPADDONS-001 to FE-TP-TRIPADDONS-006 import { act, renderHook, waitFor } from '@testing-library/react'; import type { TripAddon } from '@trek/shared'; import { http, HttpResponse } from 'msw'; @@ -184,4 +184,19 @@ describe('useTripPlanner — the trip’s own addons', () => { whatsnext: true, }); }); + it('FE-TP-TRIPADDONS-006: the owner switching Lists back on reads the trip’s sections again, a first read does not', async () => { + seedInstanceAddons(['packing', 'budget']); + const hydrateActiveTrip = vi.fn(async () => {}); + useTripStore.setState({ hydrateActiveTrip } as Partial); + tripHas([addon('packing', false, true), addon('budget', true, true)]); + await renderPlanner(); + await waitFor(() => expect(useTripAddonsStore.getState().trips['1:42']?.status).toBe('ready')); + expect(hydrateActiveTrip).not.toHaveBeenCalled(); + + act(() => useTripAddonsStore.getState().publish(42, [addon('packing', false, true), addon('budget', false, true)])); + expect(hydrateActiveTrip).not.toHaveBeenCalled(); + + act(() => useTripAddonsStore.getState().publish(42, [addon('packing', true, true), addon('budget', false, true)])); + expect(hydrateActiveTrip).toHaveBeenCalledWith(42); + }); });