Files
TREK/client/src/pages/tripPlanner/useTourMapPoints.ts
T
Maurice 2449616ee9 feat(tours): plan a multi-day tour as one route split into stages
A hut-to-hut hike or a paddle trip over several days is now planned in the
Tours planner as one tour: the whole route is planned as before, and stops
along it are marked as stage ends (MDT-01). The stages are read off that one
route in its order, never kept as copies, so a change inside a stage changes
the whole tour and every figure on it.

- A Day tour / Multi-day tour switch beside the tour type; a stop's menu
  ends a stage there or lets it go on. Back to a day tour removes every stage
  end after a confirmation, as one step undo brings back.
- The list of points is headed per stage with its distance and moving time;
  a stop that ends a stage carries a moon at its number, on the map too.
- The overview lists the stages from stop to stop with distance, climb,
  descent and time (hiking rule per stage, paddle estimate per stage, the
  router's time shared by distance otherwise), marks where each ends across
  the elevation profile, and a stage of more than a day on the move warns.
- A stage end a change cannot keep (its stop removed, moved to an end or
  made a shaping point) is dropped and said so, with undo, never moved
  to another point.
- A multi-day tour may take a day a stage, so its time limit grows with its
  stages; the tour keeps its one type, portages included.
- The list and the MCP tools show the stage count and where stages end.

Stage ends are a flag on the waypoint (migration 046000); a tour without one
is a day tour, as every tour saved before.
2026-10-20 15:26:18 +02:00

136 lines
5.5 KiB
TypeScript

import { isClosedLoop } from '@trek/shared';
import { useMemo } from 'react';
import type { useOutsidePressGuard } from '../../components/Ferratas/useOutsidePressGuard';
import { paddleRolesById } from '../../components/Tours/planner/paddlePlanOf';
import type { TourPlannerController } from '../../components/Tours/planner/useTourPlanner';
import { useWaypointPlaceNames } from '../../components/Tours/planner/useWaypointPlaceNames';
import { useTranslation } from '../../i18n';
import { usePlacedPointHint } from './usePlacedPointHint';
import { useTourMapPlacing, type TourMapMode } from './useTourMapPlacing';
const NO_POINTS: never[] = [];
const ROLE_KEY = {
start: 'tours.planner.start',
via: 'tours.planner.via',
end: 'tours.planner.end',
} as const;
type PointsPlanner = Pick<
TourPlannerController,
| 'mode'
| 'isSaving'
| 'waypoints'
| 'selectedWaypointId'
| 'setSelectedWaypointId'
| 'setWaypointPosition'
| 'addWaypoint'
| 'undo'
| 'maxHikingDifficulty'
| 'isPaddle'
>;
interface Options {
tourPlanner: PointsPlanner;
canPlaceEdit: boolean;
mode: TourMapMode;
/** A card is open over the map (a via ferrata's, a paddle obstacle's): the "point set" hint gives way to it. */
cardOpen: boolean;
/** Drops the click that closed such a card. */
guard: ReturnType<typeof useOutsidePressGuard>['guard'];
/** Control points of an editable version previewed over an import: shown, never moved. */
previewPoints?: readonly { lat: number; lng: number }[] | null;
}
/**
* The tour's control points on the planner map, for either mode: what the map
* shows and may change, the "set points" tool, and the hint with its undo
* after a point went in. A click places a point only while the tool is on,
* only when it was a click and not a drag, and never when it closed a card.
*/
export function useTourMapPoints({ tourPlanner, canPlaceEdit, mode, cardOpen, guard, previewPoints }: Options) {
const { t } = useTranslation();
const editing = tourPlanner.mode.type === 'new-draft' || tourPlanner.mode.type === 'edit-saved';
// The same lookup as the waypoint list, so a pin is named on hover as its row is.
const placeNames = useWaypointPlaceNames(editing ? tourPlanner.waypoints : NO_POINTS);
const canEditRoute = canPlaceEdit && !tourPlanner.isSaving && editing;
const { active, toggle, gate, attach } = useTourMapPlacing(mode);
const placeOn = canEditRoute && active;
const placedHint = usePlacedPointHint({
waypoints: tourPlanner.waypoints,
difficulty: tourPlanner.maxHikingDifficulty,
undo: tourPlanner.undo,
enabled: editing && !cardOpen,
});
const { notePlacement } = placedHint;
const { addWaypoint } = tourPlanner;
const addPoint = (lat: number, lng: number) => {
notePlacement();
addWaypoint(lat, lng);
};
const onAdd = guard(gate(placeOn ? addPoint : undefined));
// Kept by identity while nothing about the points changed, so the map does not redraw its pins on every render.
const { waypoints, isPaddle } = tourPlanner;
const points = useMemo(() => {
if (editing) {
// The way back of a loop lies on the start; one pin stands for both, and moving it moves both.
const shown = isClosedLoop(waypoints) ? waypoints.slice(0, -1) : waypoints;
// A paddle tour names a pin for the paddler (put-in, take-out, re-entry) and says where a lock is.
const roles = isPaddle ? paddleRolesById(waypoints) : null;
// Stops are numbered on their own: a shaping point (P2A-03) only steers and takes no number.
let stop = 0;
// MDT-01: the stages of a multi-day tour, counted as their ends come along the route.
let stage = 0;
return shown.map((point) => {
if (point.shaping) {
return {
...point,
name: placeNames[point.id] ?? t('tours.planner.shapingPoint'),
detail: t('tours.planner.shapingPoint'),
};
}
stop += 1;
const role = roles?.get(point.id);
const detail = role ? t(`tours.paddle.role.${role}`) : roles ? '' : t(ROLE_KEY[point.role]);
const lock = point.lock ? t('tours.paddle.lock') : '';
if (point.stageEnd) stage += 1;
const stageEnd = point.stageEnd ? t('tours.stages.endOf', { number: stage }) : '';
return {
...point,
label: String(stop),
name: placeNames[point.id] ?? t('tours.planner.waypointLabel', { n: stop }),
detail: [detail, lock, stageEnd].filter(Boolean).join(', ') || t(ROLE_KEY[point.role]),
};
});
}
if (!previewPoints?.length) return NO_POINTS;
const last = previewPoints.length - 1;
return previewPoints.map((point, index) => {
const role = index === 0 ? 'start' : index === last ? 'end' : 'via';
return {
id: `preview-${index}`,
lat: point.lat,
lng: point.lng,
role,
name: t('tours.planner.waypointLabel', { n: index + 1 }),
detail: t(ROLE_KEY[role]),
} as const;
});
}, [editing, isPaddle, waypoints, placeNames, previewPoints, t]);
return {
waypoints: {
points,
selectedId: editing ? tourPlanner.selectedWaypointId : null,
onSelect: tourPlanner.setSelectedWaypointId,
onMove: canEditRoute ? tourPlanner.setWaypointPosition : undefined,
onAdd,
},
/** The switch for the layer pill, there only while the tour can be edited. */
placeTool: canEditRoute ? { active, onToggle: toggle } : undefined,
placeOn,
/** Goes on the element around the map, where the presses are followed. */
attachSurface: attach,
placedHint,
};
}