mirror of
https://github.com/mauriceboe/TREK.git
synced 2026-11-14 00:01:03 +00:00
d46b6fb646
The tour map stage and the trip planner page hook had grown past the complexity and length the lint ratchet holds them to. Marking a spot on a recorded paddle track moves into useTrackLinePick, and what the planner map frames into usePlannerFocusPoints, each with its own tests. The planner's viewport snapshot no longer reads a ref while rendering, so the count of that warning goes down by one.
32 lines
1.5 KiB
TypeScript
32 lines
1.5 KiB
TypeScript
import type { TourPlannerController } from '../../components/Tours/planner/useTourPlanner';
|
|
import { pickOnTrack, refuseTrackPick, useTrackMarkToolStore } from '../../store/trackMarkToolStore';
|
|
import { projectOntoTrack, TRACK_PICK_TOLERANCE_M } from '../../utils/paddleTrackMarks';
|
|
|
|
type MapClick = (lat: number, lng: number) => void;
|
|
|
|
/**
|
|
* PAD-03 on a recorded paddle track: with a marking tool on, a click on the
|
|
* line marks the spot on it, one too far from the line is refused. Otherwise
|
|
* the planner's own click (adding a waypoint) stays.
|
|
*/
|
|
export function useTrackLinePick(
|
|
tourPlanner: Pick<TourPlannerController, 'mode' | 'readOnlyGpxAnalysis'>,
|
|
canPlaceEdit: boolean,
|
|
guard: (handler: MapClick | undefined) => MapClick | undefined,
|
|
onAdd: MapClick | undefined
|
|
): { pickOnLine: boolean; onAddOrMark: MapClick | undefined } {
|
|
const trackPick = useTrackMarkToolStore((state) => state.pick);
|
|
const trackLine = tourPlanner.mode.type === 'view-gpx' ? tourPlanner.readOnlyGpxAnalysis?.routeCoordinates : null;
|
|
const pickOnLine = canPlaceEdit && trackPick !== null && !!trackLine?.length;
|
|
const onTrackClick = guard(
|
|
pickOnLine && trackLine
|
|
? (lat, lng) => {
|
|
const projected = projectOntoTrack(trackLine, { lat, lng });
|
|
if (!projected || projected.offM > TRACK_PICK_TOLERANCE_M) refuseTrackPick();
|
|
else pickOnTrack(projected.atM);
|
|
}
|
|
: undefined
|
|
);
|
|
return { pickOnLine, onAddOrMark: onAdd ?? onTrackClick };
|
|
}
|