Files
TREK/client/src/components/Tours/terrain/useTerrainPins.ts
T
Maurice 1bc82f97fd feat(tours): route paddle tours on the water, shaping points and auto-loaded regions
Paddle tours are planned on the water by TREK's own router: a water network
per paddle region, built from the same Geofabrik extract as the obstacles and
searched in a worker thread of the server (no sidecar). Rivers, canals and
lakes (direct or along the shore), portages round weirs, locks by policy,
whitewater by boat, gaps marked. Found portages and locks become points of
their own; an unplannable leg says why and offers a way out.

- Shaping points: a click on the line sets a point that steers the route
  without being a stop (hikes and paddle tours); recompute a single leg.
- Lake style direct/shore, suggested from the wind forecast.
- Regions load by themselves where a paddler plans (admin setting: automatic,
  on request, off; size cap and a daily count), with requests for the admins,
  a notification, and the load followed on the leg.
- Portage from the last point, portages follow footways via the walking
  router (or stay straight), "put in here" on slipways and put-ins.
- MCP plan_paddle_route with the REST route's rules.
2026-10-18 21:21:32 +00:00

79 lines
2.9 KiB
TypeScript

import type { Marker as MapLibreMarker } from 'maplibre-gl';
import { useEffect, useRef, type RefObject } from 'react';
import { bindPinHoverLabel, mapHoverLabel } from '../../Map/mapHoverLabel';
import { waypointPinElement } from './terrainLayers';
import type { TerrainGl } from './useTerrainMap';
export interface TerrainWaypoints {
/** `name` and `detail` (its role) show on hover. */
points: Array<{
id: string;
lat: number;
lng: number;
name?: string;
detail?: string;
label?: string;
shaping?: boolean;
}>;
selectedId: string | null;
onSelect: (id: string) => void;
/** Absent while the tour cannot be edited; the pins then stay put. */
onMove?: (id: string, lat: number, lng: number) => boolean;
/** Absent while the tour cannot be edited; a click on the map then adds nothing. */
onAdd?: (lat: number, lng: number) => void;
}
/**
* The planner's numbered control points, pinned to the ground and draggable while
* editing. They stay up while a new style loads and are redrawn once it is in.
*/
export function useTerrainPins(
gl: TerrainGl | null,
ready: boolean,
waypoints: TerrainWaypoints,
handlers: RefObject<TerrainWaypoints>
) {
const { points, selectedId, onMove } = waypoints;
const editable = !!onMove;
const pinsRef = useRef<MapLibreMarker[]>([]);
useEffect(() => {
if (!gl || !ready) return;
pinsRef.current.forEach((pin) => pin.remove());
const hoverLabel = mapHoverLabel(gl.map, 'planner-waypoint-label');
// A label left behind by a turn or a tilt would float over the wrong spot.
const hideOnMove = () => hoverLabel.hide();
gl.map.on('movestart', hideOnMove);
const unbind: (() => void)[] = [() => gl.map.off('movestart', hideOnMove)];
pinsRef.current = points.map((point, index) => {
const el = waypointPinElement(point, index, point.id === selectedId, editable);
const name = point.name ?? String(index + 1);
el.setAttribute('aria-label', name);
unbind.push(bindPinHoverLabel(el, hoverLabel, gl.map.getContainer(), { name, detail: point.detail ?? null }));
el.addEventListener('click', (event) => {
event.stopPropagation();
handlers.current.onSelect(point.id);
});
const pin = new gl.engine.Marker({ element: el, draggable: editable })
.setLngLat([point.lng, point.lat])
.addTo(gl.map);
pin.on('dragend', () => {
const at = pin.getLngLat();
if (!handlers.current.onMove?.(point.id, at.lat, at.lng)) pin.setLngLat([point.lng, point.lat]);
});
pin.on('dragstart', () => hoverLabel.hide());
return pin;
});
return () => {
unbind.forEach((off) => off());
hoverLabel.remove();
};
}, [gl, ready, points, selectedId, editable, handlers]);
// The pins go with the map, not with each redraw.
useEffect(() => {
const pins = pinsRef;
return () => pins.current.forEach((pin) => pin.remove());
}, []);
}