mirror of
https://github.com/mauriceboe/TREK.git
synced 2026-11-22 21:11:14 +00:00
9956c58dd5
- The trip's addon list is read through tripAddonsRepo into tripAddonsStore (per account and trip, kept in Dexie version 16 for offline), refreshed when the connection returns and live on tripAddons:changed. - The planner's tabs, dock, rail and deep links, the cost, tour and list readers, the PDF export, the share link options, the widgets and the watch follow the trip instead of the instance switch. - Trip settings gets an Addons area for the owner, on the desktop dialog and the phone sheet, on one useTripAddonRows hook: each trip addon with its switch, marked "This trip only" while it differs from the instance. Switching back to the instance's value drops the trip's choice. Switching needs a connection.
44 lines
1.9 KiB
TypeScript
44 lines
1.9 KiB
TypeScript
import type { StoreApi } from 'zustand';
|
|
import { useAddonStore, type CollabFeatures } from '../../src/store/addonStore';
|
|
import { RESET_STORES } from './storeRegistry';
|
|
|
|
// The state of each store in the registry when this module was first imported,
|
|
// before any test modified it.
|
|
const initialStates = Object.entries(RESET_STORES).map(([name, store]) => ({ name, store, state: store.getState() }));
|
|
|
|
/** Puts every store in RESET_STORES (tests/helpers/storeRegistry.ts) back to its initial state. */
|
|
export function resetAllStores(): void {
|
|
for (const { store, state } of initialStates) store.setState(state, true);
|
|
}
|
|
|
|
/**
|
|
* Tests routinely seed a store with a partially-populated slice of state,
|
|
* including partial nested objects (e.g. only `settings.time_format`). The
|
|
* store's own setState wants the exact field types, so seeding accepts a
|
|
* deep-partial view and casts at the boundary.
|
|
*
|
|
* Typed against zustand's own StoreApi rather than a hand-written
|
|
* `{ setState: ... }` shape: as of v5 setState is an overload pair (partial +
|
|
* replace?: false, full state + replace: true), and no single signature is
|
|
* assignable to it.
|
|
*/
|
|
type DeepPartial<T> = T extends object ? { [P in keyof T]?: DeepPartial<T[P]> } : T;
|
|
|
|
export function seedStore<T extends object>(store: Pick<StoreApi<T>, 'setState'>, state: DeepPartial<T>): void {
|
|
store.setState(state as Partial<T>);
|
|
}
|
|
|
|
/**
|
|
* The instance's addons as the app read them from GET /api/addons: these on,
|
|
* every other off. A trip that names no addon of its own (the default MSW
|
|
* answer of GET /api/trips/:id/addons) then shows exactly these.
|
|
*/
|
|
export function seedInstanceAddons(ids: string[], collabFeatures?: Partial<CollabFeatures>): void {
|
|
useAddonStore.setState((state) => ({
|
|
addons: ids.map((id) => ({ id, name: id, type: 'trip', icon: '', enabled: true })),
|
|
loaded: true,
|
|
failed: false,
|
|
collabFeatures: { ...state.collabFeatures, ...collabFeatures },
|
|
}));
|
|
}
|