fix(trip-addons): read the trip's lists again when their addon is switched back on

The sections of an addon that was off were read as empty, so switching Lists,
Costs or Documents back on while the trip is open now reads the trip again.
This commit is contained in:
Maurice
2026-11-23 14:55:21 +01:00
parent 56ce66f666
commit 769293a554
2 changed files with 37 additions and 2 deletions
@@ -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<Record<string, boolean>> = {
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<string, boolean>;
}, [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);
@@ -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<TripStoreState>);
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);
});
});