mirror of
https://github.com/mauriceboe/TREK.git
synced 2026-12-05 18:34:09 +00:00
f833a05461
A tour can now point at the packing list of its trip, as a whole or at one of its categories (say "Hiking gear" for a hike), and shows it in its detail. The new Packing list section reads the list live as the user may see it: the backpack tile with the item count on its corner and the category's own colour, how many are packed with a thin progress bar, the first items ticked off or not with their quantity, how many more there are, and a button that opens the trip's Lists tab. Who may change the tour links one, changes it or lets go through a dialog that lists the whole list and every category with its colour and item count, and writes only on its confirm. It is a reference, never a copy: the items stay in Packing, are ticked off and edited there, and each traveller sees in the tour only the items Packing shows them, so an item kept to oneself never shows through a tour. A category that is renamed, cleared or only holds items of others reads as missing, an empty one as empty, a link that could not be read says so with a retry, and while Lists are switched off the link stays, dimmed, and can still be let go. Removing the link, or the tour, leaves the list and every item as they are. Nothing suggests or changes an item by itself. On the server the link is a row of its own in the tours domain (tour_packing_links, migration 046110), one per tour and gone with it. GET, PUT and DELETE /api/trips/:tripId/tours/:placeId/packing-link read it with the trip and change it with place_edit; linking needs the Lists addon and, for a category, an item of it the user can see. The writes answer the tour with its link and tell the trip through tours:changed, so the offline client queues them as writes of the tour and replays them like one. A copied trip keeps its tours' links, a tour saved as new or taken over does not. The MCP tools get_tour_packing, link_tour_packing and unlink_tour_packing run over the same service and checks; the read also hands the tour's facts as context for suggesting what to pack, which only becomes an item through the packing tools. The empty category placeholder now lives in @trek/shared so both sides leave it out alike. All 27 locales carry the new strings, and the wiki describes the link and the tools.
65 lines
2.6 KiB
TypeScript
65 lines
2.6 KiB
TypeScript
import { PACKING_PLACEHOLDER_NAME } from '../packing/packing-placeholder';
|
|
import type { TourPackingLink } from './tour-packing-link.schema';
|
|
|
|
/** What a packing item has to say for a tour's link to show it. */
|
|
export interface LinkablePackingItem {
|
|
name: string;
|
|
category?: string | null;
|
|
checked?: number | boolean | null;
|
|
}
|
|
|
|
/**
|
|
* How a tour's packing link reads against the items its viewer can see:
|
|
* `ready` shows at least one item; `empty` points at something that is there
|
|
* but holds no item yet (an empty packing list, a category just made);
|
|
* `missing` names a category without one item the viewer can see, because it
|
|
* was renamed or cleared, or its items are kept to other travellers.
|
|
*/
|
|
export type TourPackingLinkState = 'ready' | 'empty' | 'missing';
|
|
|
|
export interface LinkedPackingItems<T> {
|
|
state: TourPackingLinkState;
|
|
/** The items of the list or category, in the order Packing lists them. */
|
|
items: T[];
|
|
/** How many of them are ticked off. */
|
|
packed: number;
|
|
}
|
|
|
|
/**
|
|
* The items a link shows, read from the packing items the viewer can see, so
|
|
* what one traveller keeps to themselves never shows through a tour. Nothing
|
|
* here writes: the list, its categories and its items stay Packing's.
|
|
*/
|
|
export function linkedPackingItems<T extends LinkablePackingItem>(
|
|
link: Pick<TourPackingLink, 'category'>,
|
|
visible: readonly T[],
|
|
): LinkedPackingItems<T> {
|
|
const inScope = link.category === null ? visible : visible.filter((item) => item.category === link.category);
|
|
const items = inScope.filter((item) => item.name !== PACKING_PLACEHOLDER_NAME);
|
|
let state: TourPackingLinkState = 'ready';
|
|
if (items.length === 0) state = link.category === null || inScope.length > 0 ? 'empty' : 'missing';
|
|
return { state, items, packed: items.filter((item) => Boolean(item.checked)).length };
|
|
}
|
|
|
|
/** A category a tour can point at: its name and how many items it holds. */
|
|
export interface PackingCategoryChoice {
|
|
name: string;
|
|
count: number;
|
|
}
|
|
|
|
/**
|
|
* The named categories of a packing list in the order the list first shows
|
|
* them, each with its items counted (an empty category's placeholder is no
|
|
* item). Items without a category sit in no category a tour can point at;
|
|
* the whole list takes them in.
|
|
*/
|
|
export function packingCategoriesOf(visible: readonly LinkablePackingItem[]): PackingCategoryChoice[] {
|
|
const counts = new Map<string, number>();
|
|
for (const item of visible) {
|
|
if (!item.category) continue;
|
|
const counted = item.name === PACKING_PLACEHOLDER_NAME ? 0 : 1;
|
|
counts.set(item.category, (counts.get(item.category) ?? 0) + counted);
|
|
}
|
|
return [...counts].map(([name, count]) => ({ name, count }));
|
|
}
|