feat(tours): keep whole tours in collections and take them into trips

A saved tour can now be kept in a collection, apart from any trip, and
later made into a tour of another trip. The bookmark button in a tour's
footer and in the planner's head (waiting for the save while there are
unsaved changes) opens a dialog with every list of the user; a list they
may only read stays in it, dimmed with a lock and a tooltip. The list
keeps an independent copy: the line, the type, every measure and setting,
the waypoints in order with their roles, the origin, and the trip and tour
it was saved from as they were called then. Its days, its links to places
of the trip and its obstacle decisions stay in the trip, and later changes
to the tour do not reach the copy.

A collection shows its kept tours as cards above its places: the grade
tile with the source badge, measures and the waypoint count as badges,
the trip it came from, and who kept it. The owner or an admin of the list
can remove one after a question; tours made from it stay. "Add to a trip"
shows the kept tour on a map with its facts and numbered waypoints, lists
the user's trips (the ones they may not add tours to dimmed), and asks
outright whether the waypoints come along. "Line only" keeps the line as
saved and adds no waypoints; a tour kept without a line can only bring
them. The new tour has an id of its own, no days and no new places, and
names the trip and tour it was saved from; the dialog then offers to open
that trip. Neither the collection nor the original tour changes.

On the server the kept tours are a table of their own in the tours domain
(collection_tours, migration 046100), each row written whole once. Every
copy, the in-trip "save as new tour", the take-over from another trip and
both directions here, now goes through one copy model (tour-copy.helpers)
instead of a second one. Reading and removing follow the collection's
roles, keeping one also needs access to the source trip, and taking one
over needs place_edit on the target trip; the kept tour is read again
inside the write, so one removed meanwhile leaves no half-made tour. The
MCP tools list_collection_tours, get_collection_tour,
save_tour_to_collection, take_over_collection_tour and
delete_collection_tour run over the same service and checks. All 27
locales carry the new strings, and the wiki describes the flow.
This commit is contained in:
Maurice
2026-10-20 13:15:42 +02:00
parent 3659297af1
commit 3fe34061fd
85 changed files with 6144 additions and 181 deletions
@@ -0,0 +1,86 @@
// LIB-03: the tours the open collection keeps, read when it opens, kept current
// by its members' changes and never shown under another collection.
import type { CollectionTourItem } from '@trek/shared';
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
import { addListener, removeListener } from '../api/websocket';
import { collectionToursRepo } from '../repo/collectionToursRepo';
import { OnlineOnlyError } from '../repo/onlineOnly';
import type { WebSocketEvent } from '../types';
import { useCollectionTourStore, watchCollectionTours } from './collectionTourStore';
// FE-STORE-COLLECTION-TOURS-001 to FE-STORE-COLLECTION-TOURS-005
const tour = (id: number, name: string) => ({ id, collection_id: 3, name }) as CollectionTourItem;
function later<T>() {
let settle: (value: T) => void = () => undefined;
const promise = new Promise<T>((resolve) => {
settle = resolve;
});
return { promise, settle };
}
beforeEach(() => {
useCollectionTourStore.getState().clear();
vi.mocked(addListener).mockClear();
vi.mocked(removeListener).mockClear();
});
afterEach(() => vi.restoreAllMocks());
describe('collectionTourStore', () => {
it('FE-STORE-COLLECTION-TOURS-001: reads a collection’s tours and says how far it got', async () => {
vi.spyOn(collectionToursRepo, 'list').mockResolvedValue({ tours: [tour(1, 'Ridge')] });
const loading = useCollectionTourStore.getState().load(3);
expect(useCollectionTourStore.getState()).toMatchObject({ collectionId: 3, status: 'loading', tours: [] });
await loading;
expect(useCollectionTourStore.getState()).toMatchObject({ status: 'ready', tours: [tour(1, 'Ridge')] });
});
it('FE-STORE-COLLECTION-TOURS-002: an answer for a collection left meanwhile counts for nothing', async () => {
const slow = later<{ tours: CollectionTourItem[] }>();
vi.spyOn(collectionToursRepo, 'list')
.mockReturnValueOnce(slow.promise)
.mockResolvedValueOnce({ tours: [tour(2, 'Lake')] });
const first = useCollectionTourStore.getState().load(3);
await useCollectionTourStore.getState().load(4);
slow.settle({ tours: [tour(1, 'Ridge')] });
await first;
expect(useCollectionTourStore.getState()).toMatchObject({ collectionId: 4, tours: [tour(2, 'Lake')] });
});
it('FE-STORE-COLLECTION-TOURS-003: offline and failed reads say so', async () => {
vi.spyOn(collectionToursRepo, 'list')
.mockRejectedValueOnce(new OnlineOnlyError())
.mockRejectedValueOnce(new Error('500'));
await useCollectionTourStore.getState().load(3);
expect(useCollectionTourStore.getState().status).toBe('offline');
await useCollectionTourStore.getState().load(3);
expect(useCollectionTourStore.getState().status).toBe('error');
});
it('FE-STORE-COLLECTION-TOURS-004: removing takes the tour off at once and puts it back on a refusal', async () => {
vi.spyOn(collectionToursRepo, 'list').mockResolvedValue({ tours: [tour(1, 'Ridge'), tour(2, 'Lake')] });
await useCollectionTourStore.getState().load(3);
const remove = vi.spyOn(collectionToursRepo, 'remove').mockResolvedValueOnce(undefined);
await useCollectionTourStore.getState().remove(3, 1);
expect(useCollectionTourStore.getState().tours.map((kept) => kept.id)).toEqual([2]);
remove.mockRejectedValueOnce(new Error('403'));
await expect(useCollectionTourStore.getState().remove(3, 2)).rejects.toThrow('403');
expect(useCollectionTourStore.getState().tours.map((kept) => kept.id)).toEqual([2]);
});
it('FE-STORE-COLLECTION-TOURS-005: reads again when a member changes the open collection, and stops listening', async () => {
const list = vi.spyOn(collectionToursRepo, 'list').mockResolvedValue({ tours: [] });
await useCollectionTourStore.getState().load(3);
list.mockClear();
const stop = watchCollectionTours();
const onEvent = vi.mocked(addListener).mock.calls[0]?.[0] as (event: WebSocketEvent) => void;
onEvent({ type: 'collections:updated', collectionId: 4 });
onEvent({ type: 'collections:left', collectionId: 3 });
expect(list).not.toHaveBeenCalled();
onEvent({ type: 'collections:updated', collectionId: 3 });
expect(list).toHaveBeenCalledWith(3);
stop();
expect(removeListener).toHaveBeenCalledWith(onEvent);
});
});
+67
View File
@@ -0,0 +1,67 @@
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';
/** How far the open collection's tours are: asked for, there, or not to be had right now. */
type CollectionTourStatus = 'idle' | 'loading' | 'ready' | 'error' | 'offline';
interface CollectionTourState {
/** The collection the tours are read for. */
collectionId: number | null;
tours: CollectionTourItem[];
status: CollectionTourStatus;
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.
*/
export const useCollectionTourStore = create<CollectionTourState>((set, get) => ({
collectionId: null,
tours: [],
status: 'idle',
load: async (collectionId) => {
// Another collection's tours never show under this one.
if (get().collectionId !== collectionId) set({ collectionId, tours: [], status: 'loading' });
try {
const { tours } = await collectionToursRepo.list(collectionId);
if (get().collectionId === collectionId) set({ tours, status: 'ready' });
} catch (err: unknown) {
if (get().collectionId === collectionId) set({ status: err instanceof OnlineOnlyError ? 'offline' : 'error' });
}
},
remove: async (collectionId, tourId) => {
const before = get().tours;
if (get().collectionId === collectionId) set({ tours: before.filter((tour) => tour.id !== tourId) });
try {
await collectionToursRepo.remove(collectionId, tourId);
} catch (err) {
if (get().collectionId === collectionId) set({ tours: before });
throw err;
}
},
clear: () => set({ collectionId: null, tours: [], status: 'idle' }),
}));
/**
* 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);
}