Files
TREK/client/src/components/Tours/planner/usePaddleFields.ts
T
Maurice 03d6457bde feat(tours): paddle obstacle card, admin regions panel and obstacle marks on the route band
PAD-05B: a click on an obstacle on the map or on the route band opens its card in the look of the via ferrata card: the kind with its glyph, the name, how it stands to the route (on it at a distance along it, near it with the link marked unclear, or only in the area), the paddling rule its tags give with canoe read before boat, a condition as written, height, grade, hours and fees, the source with the date of the data and the OpenStreetMap link. A missing rule reads as unknown, never as allowed, and a portage way recorded as private says so. Objects only in the area show from zoom 13 on, on Leaflet and GL alike; on GL the lines only near the route are dashed as on Leaflet, one click handler picks the marker over the line, and a picked marker no longer flickers. The route band marks the obstacles on and near the route by their share of the measured line. The obstacle notice also names an answer cut at its limit.

PAD-05A: the admin area gets a Paddle water data card under the addons, on desktop and phone: a search in Geofabrik's list with each region's path, the regions with their state (waiting, reading with its progress, ready with objects and data date, failed with the reason), reading again and removing after a confirmation. The list follows a read without reloading. Region changes write audit rows.

Map colours of paddle objects and portages come from theme tokens read at draw time, as the via ferratas do; the ferrata layer now shares that reader. The rail takes the planner page's parts as one bag, the map hit tests of the tour layers are one helper, and the unused noWaterData string is gone from every locale.
2026-10-16 23:03:09 +00:00

78 lines
3.2 KiB
TypeScript

import {
recommendedPaddleSpeedKmh,
type PaddleBoatType,
type PaddleWaterContext,
type TourListItem,
} from '@trek/shared';
import { useCallback, useState } from 'react';
export interface PaddleFieldValues {
boatType: PaddleBoatType | null;
waterContext: PaddleWaterContext | null;
/** The speed the paddler chose, km/h; null while the boat's suggested speed applies. */
speedKmh: number | null;
/** PAD-07: the carrying speed the paddler chose, km/h; null while the starting value applies. Absent in an older draft. */
portageSpeedKmh?: number | null;
}
export const NO_PADDLE_FIELDS: PaddleFieldValues = {
boatType: null,
waterContext: null,
speedKmh: null,
portageSpeedKmh: null,
};
/** The paddle fields of a saved tour, or none for a hike or a list cached before they existed. */
export function paddleFieldsOf(
tour: Pick<TourListItem, 'boat_type' | 'water_context' | 'paddle_speed_kmh' | 'portage_speed_kmh'>
): PaddleFieldValues {
return {
boatType: tour.boat_type ?? null,
waterContext: tour.water_context ?? null,
speedKmh: tour.paddle_speed_kmh ?? null,
portageSpeedKmh: tour.portage_speed_kmh ?? null,
};
}
/**
* PAD-01: the boat, the water and the planning speed of a paddle tour. A
* speed the paddler typed stays when the boat changes; the boat's suggestion
* is offered beside it and only taken on purpose. `onEdit` tells the planner
* the draft changed, like any other field.
*/
export function usePaddleFields(initial: PaddleFieldValues, onEdit: () => boolean) {
const [values, setValues] = useState<PaddleFieldValues>(initial);
// Only a real change marks the draft edited: choosing the boat already chosen is no edit.
const edit = useCallback(
(next: Partial<PaddleFieldValues>) => {
const merged = { ...values, ...next };
const same =
merged.boatType === values.boatType &&
merged.waterContext === values.waterContext &&
Object.is(merged.speedKmh, values.speedKmh) &&
Object.is(merged.portageSpeedKmh ?? null, values.portageSpeedKmh ?? null);
if (!same && onEdit()) setValues(merged);
},
[onEdit, values]
);
const recommended = recommendedPaddleSpeedKmh(values.boatType);
return {
...values,
portageSpeedKmh: values.portageSpeedKmh ?? null,
recommendedSpeedKmh: recommended,
/** The speed the time is worked out with: the chosen one, else the boat's suggestion. */
effectiveSpeedKmh: values.speedKmh ?? recommended,
setBoatType: useCallback((boatType: PaddleBoatType) => edit({ boatType }), [edit]),
setWaterContext: useCallback((waterContext: PaddleWaterContext) => edit({ waterContext }), [edit]),
setSpeedKmh: useCallback((speedKmh: number | null) => edit({ speedKmh }), [edit]),
/** PAD-07: the carrying speed, or back to the starting value with null. */
setPortageSpeedKmh: useCallback((portageSpeedKmh: number | null) => edit({ portageSpeedKmh }), [edit]),
/** Back to the boat's suggestion, by choice. */
adoptRecommendedSpeed: useCallback(() => edit({ speedKmh: null }), [edit]),
/** Loaded from a tour or a draft, or cleared; not an edit. */
load: useCallback((next: PaddleFieldValues) => setValues(next), []),
};
}