Skip to content

armgit / TREK

Public Imported from GitHub
0

The most complete open-source travel planner: real-time collaboration, bookings, budgets, packing lists, journals, offline PWA, SSO and an AI-ready MCP server, with planet-wide place search and routing built in.

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

Maurice769293a554d7
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<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);
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<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);
+ });
});