mirror of
https://github.com/mauriceboe/TREK.git
synced 2026-11-27 23:27:31 +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.
100 lines
4.2 KiB
TypeScript
100 lines
4.2 KiB
TypeScript
import type { CollectionTourItem } from '@trek/shared';
|
|
import { create } from 'zustand';
|
|
import { addListener, removeListener } from '../api/websocket';
|
|
import { collectionToursRepo } from '../repo/collectionToursRepo';
|
|
import { OnlineOnlyError } from '../repo/onlineOnly';
|
|
import type { WebSocketEvent } from '../types';
|
|
import { getApiErrorMessage } from '../utils/apiError';
|
|
|
|
/**
|
|
* How far the open collection's tours are: asked for, there, or not to be had
|
|
* right now; `off` while Tours is off for the user in every trip.
|
|
*/
|
|
type CollectionTourStatus = 'idle' | 'loading' | 'ready' | 'error' | 'offline' | 'off';
|
|
|
|
/** The answer the server gives while Tours is on neither for the instance nor in any of the user's trips. */
|
|
const TOURS_OFF = 'Tours addon is not enabled';
|
|
|
|
function failureOf(err: unknown): CollectionTourStatus {
|
|
if (err instanceof OnlineOnlyError) return 'offline';
|
|
return getApiErrorMessage(err, '') === TOURS_OFF ? 'off' : 'error';
|
|
}
|
|
|
|
interface CollectionTourState {
|
|
/** The collection the tours are read for. */
|
|
collectionId: number | null;
|
|
tours: CollectionTourItem[];
|
|
status: CollectionTourStatus;
|
|
/** Raised by every read: only the answer to the latest one is taken in. */
|
|
loadSeq: number;
|
|
load: (collectionId: number) => Promise<void>;
|
|
/** Removes one kept tour; a refusal puts it back and throws. */
|
|
remove: (collectionId: number, tourId: number) => Promise<void>;
|
|
clear: () => void;
|
|
}
|
|
|
|
/**
|
|
* LIB-03: the whole tours kept in the collection that is open. Read when it
|
|
* opens, again when another member keeps or removes one (watchCollectionTours),
|
|
* and never shown under another collection, not even while that one loads.
|
|
* Reads can overlap (two members' changes in a row, the first read and a
|
|
* change); only the latest one's answer counts, so an older list never
|
|
* replaces a newer one.
|
|
*/
|
|
|
|
/** The list with a tour put back where it stood, unless it is there already. */
|
|
function withPutBack(tours: CollectionTourItem[], tour: CollectionTourItem, at: number): CollectionTourItem[] {
|
|
if (tours.some((item) => item.id === tour.id)) return tours;
|
|
const index = Math.min(Math.max(at, 0), tours.length);
|
|
return [...tours.slice(0, index), tour, ...tours.slice(index)];
|
|
}
|
|
|
|
export const useCollectionTourStore = create<CollectionTourState>((set, get) => ({
|
|
collectionId: null,
|
|
tours: [],
|
|
status: 'idle',
|
|
loadSeq: 0,
|
|
load: async (collectionId) => {
|
|
const seq = get().loadSeq + 1;
|
|
set({ loadSeq: seq });
|
|
// Another collection's tours never show under this one.
|
|
if (get().collectionId !== collectionId) set({ collectionId, tours: [], status: 'loading' });
|
|
const current = () => get().loadSeq === seq && get().collectionId === collectionId;
|
|
try {
|
|
const { tours } = await collectionToursRepo.list(collectionId);
|
|
if (current()) set({ tours, status: 'ready' });
|
|
} catch (err: unknown) {
|
|
if (current()) set({ status: failureOf(err) });
|
|
}
|
|
},
|
|
remove: async (collectionId, tourId) => {
|
|
const before = get().tours;
|
|
const at = before.findIndex((tour) => tour.id === tourId);
|
|
const removed = before[at];
|
|
if (get().collectionId === collectionId) set({ tours: before.filter((tour) => tour.id !== tourId) });
|
|
try {
|
|
await collectionToursRepo.remove(collectionId, tourId);
|
|
} catch (err) {
|
|
// Only the removed tour comes back: a list read meanwhile keeps the other members' changes.
|
|
if (get().collectionId === collectionId && removed) set({ tours: withPutBack(get().tours, removed, at) });
|
|
throw err;
|
|
}
|
|
},
|
|
clear: () => set((state) => ({ collectionId: null, tours: [], status: 'idle', loadSeq: state.loadSeq + 1 })),
|
|
}));
|
|
|
|
/**
|
|
* Keeps the open collection's tours current while its members work on it: a
|
|
* `collections:updated` for that collection reads them again. Returns the
|
|
* function that stops listening.
|
|
*/
|
|
export function watchCollectionTours(): () => void {
|
|
const onEvent = (event: WebSocketEvent) => {
|
|
const { collectionId } = useCollectionTourStore.getState();
|
|
if (event?.type !== 'collections:updated' || collectionId === null || event.collectionId !== collectionId) return;
|
|
void useCollectionTourStore.getState().load(collectionId);
|
|
};
|
|
addListener(onEvent);
|
|
return () => removeListener(onEvent);
|
|
}
|