mirror of
https://github.com/mauriceboe/TREK.git
synced 2026-11-21 03:26:07 +00:00
03d6457bde
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.
137 lines
4.6 KiB
TypeScript
137 lines
4.6 KiB
TypeScript
import {
|
|
DEFAULT_PORTAGE_SPEED_KMH,
|
|
estimatePaddleTime,
|
|
paddleLayoutOfTrack,
|
|
recommendedPaddleSpeedKmh,
|
|
type TourListItem,
|
|
} from '@trek/shared';
|
|
import { useCallback, useEffect, useMemo, useState } from 'react';
|
|
import { useShallow } from 'zustand/react/shallow';
|
|
import { useTranslation } from '../../../i18n';
|
|
import { tourRepo } from '../../../repo/tourRepo';
|
|
import {
|
|
loadTrackMarks,
|
|
markTrackMarksSaved,
|
|
startTrackPick,
|
|
trackMarkEdits,
|
|
useTrackMarkToolStore,
|
|
type TrackPick,
|
|
} from '../../../store/trackMarkToolStore';
|
|
import { useToast } from '../../shared/Toast';
|
|
import type { TourPlannerController } from '../planner/useTourPlanner';
|
|
|
|
interface TrackPaddlePlanOptions {
|
|
tripId: number;
|
|
planner: TourPlannerController;
|
|
canEdit: boolean;
|
|
/** The tour as it stands after the save, for the lists and the map to take it in. */
|
|
onSaved: (tour: TourListItem) => Promise<void> | void;
|
|
}
|
|
|
|
/**
|
|
* PAD-03 and PAD-07 for a recorded paddle track: the portages and locks laid
|
|
* onto it and the speeds its estimate is reckoned with. The marks live in a
|
|
* store the map shares (it is clicked to place them); this loads them when a
|
|
* paddle import is opened, drops them when it is left, and saves them through
|
|
* the offline core. The track itself is never changed.
|
|
*/
|
|
export function useTrackPaddlePlan({ tripId, planner, canEdit, onSaved }: TrackPaddlePlanOptions) {
|
|
const { t } = useTranslation();
|
|
const toast = useToast();
|
|
const [saving, setSaving] = useState(false);
|
|
const tour = planner.mode.type === 'view-gpx' ? planner.mode.tour : null;
|
|
const paddleTour = tour?.tour_type === 'kayak' ? tour : null;
|
|
const totalM = (planner.readOnlyGpxAnalysis?.distanceKm ?? 0) * 1000;
|
|
const state = useTrackMarkToolStore(
|
|
useShallow((store) => ({
|
|
placeId: store.placeId,
|
|
marks: store.marks,
|
|
speeds: store.speeds,
|
|
dirty: store.dirty,
|
|
pick: store.pick,
|
|
pendingFromM: store.pendingFromM,
|
|
refused: store.refused,
|
|
}))
|
|
);
|
|
|
|
// The tour opened, saved elsewhere meanwhile, or left: the marks follow it, unsaved ones are dropped.
|
|
useEffect(() => {
|
|
if (!paddleTour) {
|
|
loadTrackMarks(null, null, { paddle: null, portage: null });
|
|
return;
|
|
}
|
|
loadTrackMarks(paddleTour.place_id, paddleTour.track_marks, {
|
|
paddle: paddleTour.paddle_speed_kmh ?? null,
|
|
portage: paddleTour.portage_speed_kmh ?? null,
|
|
});
|
|
}, [paddleTour]);
|
|
|
|
const active = !!paddleTour && state.placeId === paddleTour.place_id;
|
|
const layout = useMemo(() => paddleLayoutOfTrack(totalM, state.marks), [state.marks, totalM]);
|
|
const recommended = recommendedPaddleSpeedKmh(paddleTour?.boat_type ?? null);
|
|
const estimate = useMemo(
|
|
() =>
|
|
estimatePaddleTime(layout, {
|
|
paddleSpeedKmh: state.speeds.paddle ?? recommended,
|
|
portageSpeedKmh: state.speeds.portage,
|
|
breakMinutes: null,
|
|
}),
|
|
[layout, recommended, state.speeds.paddle, state.speeds.portage]
|
|
);
|
|
|
|
const save = useCallback(async () => {
|
|
if (!paddleTour || saving || !canEdit) return;
|
|
setSaving(true);
|
|
try {
|
|
const body = {
|
|
paddle_speed_kmh: state.speeds.paddle,
|
|
portage_speed_kmh: state.speeds.portage,
|
|
track_marks: state.marks,
|
|
};
|
|
const result = await tourRepo.updatePaddlePlan(tripId, paddleTour.place_id, body);
|
|
markTrackMarksSaved(result.tour.track_marks, {
|
|
paddle: result.tour.paddle_speed_kmh ?? null,
|
|
portage: result.tour.portage_speed_kmh ?? null,
|
|
});
|
|
await onSaved(result.tour);
|
|
toast.success(t('tours.paddle.marks.saved'));
|
|
} catch {
|
|
toast.error(t('tours.paddle.marks.failed'));
|
|
} finally {
|
|
setSaving(false);
|
|
}
|
|
}, [canEdit, onSaved, paddleTour, saving, state.marks, state.speeds, t, toast, tripId]);
|
|
|
|
const discard = useCallback(() => {
|
|
if (!paddleTour) return;
|
|
loadTrackMarks(paddleTour.place_id, paddleTour.track_marks, {
|
|
paddle: paddleTour.paddle_speed_kmh ?? null,
|
|
portage: paddleTour.portage_speed_kmh ?? null,
|
|
});
|
|
}, [paddleTour]);
|
|
|
|
return {
|
|
/** A recorded paddle track is open: the card shows. */
|
|
active,
|
|
canEdit,
|
|
saving,
|
|
marks: state.marks,
|
|
speeds: state.speeds,
|
|
dirty: state.dirty,
|
|
pick: state.pick,
|
|
pendingFromM: state.pendingFromM,
|
|
refused: state.refused,
|
|
totalM,
|
|
layout,
|
|
estimate,
|
|
recommendedPaddleSpeed: recommended,
|
|
defaultPortageSpeed: DEFAULT_PORTAGE_SPEED_KMH,
|
|
startPick: (pick: TrackPick) => startTrackPick(state.pick === pick ? null : pick),
|
|
edits: trackMarkEdits,
|
|
save,
|
|
discard,
|
|
};
|
|
}
|
|
|
|
export type TrackPaddlePlan = ReturnType<typeof useTrackPaddlePlan>;
|