Files
TREK/client/src/components/Tours/planner/useTourRevisionWatch.ts
T
Maurice d72352911e feat(tours): tour revisions, conflicts and a live editor (P2C-03)
A saved tour now has a technical revision, raised inside the transaction of
every write that changes it: the route and its controls, the plan laid onto a
recorded paddle track, going back to that track. A write that names the
revision it was based on (expected_revision) is refused with a 409
{ code: 'tour_revision_conflict', revision } before anything of it is
written, so a stale draft never overwrites a newer tour unseen. A write that
names none writes as before, so older clients and queued offline writes keep
working; the queue never carries a base. A copy starts at 1, a source's
version stays provenance. Migration 044800 adds the column; list_tours and
get_tour_plan read it.

The editor follows the trip's tours live: a newer revision of the open tour
loads at once while the editor has no changes of its own, and shows a notice
while it has. A refused save keeps the draft and asks: save it as a new tour,
load the newer version, or overwrite on purpose.

useTourPlanner was at the size limit; its derived facts, undo history and
draft storage move into tourPlannerFacts, usePlannerHistory and
useTourDraftStorage, unchanged in behaviour.
2026-10-19 10:22:04 +02:00

30 lines
1.4 KiB
TypeScript

import type { TourListItem } from '@trek/shared';
import { useEffect } from 'react';
import { useTranslation } from '../../../i18n';
import { useToast } from '../../shared/Toast';
import type { TourPlannerController } from './useTourPlanner';
/**
* P2C-03, live: the trip's tours follow every save over the websocket, so a
* newer revision of the tour open in the editor means someone else saved it.
* An editor without changes of its own loads it again at once and says so; an
* editor with changes keeps them and shows the notice (TourRemoteChangeNotice),
* and its save asks what to do instead of overwriting.
*/
export function useTourRevisionWatch(planner: TourPlannerController, tours: readonly TourListItem[]) {
const { t } = useTranslation();
const toast = useToast();
const placeId = planner.mode.type === 'edit-saved' ? planner.mode.placeId : null;
const listed = placeId === null ? undefined : tours.find((tour) => tour.place_id === placeId)?.revision;
const { notice } = planner.revision;
const { hasUnsavedChanges, isSaving, openTour } = planner;
useEffect(() => {
if (placeId === null || isSaving || !notice(listed)) return;
if (hasUnsavedChanges) return;
void openTour({ place_id: placeId }).then((opened) => {
if (opened) toast.info(t('tours.revision.reloaded'));
});
}, [hasUnsavedChanges, isSaving, listed, notice, openTour, placeId, t, toast]);
}