Files
TREK/client/src/pages/tripPlanner/usePlannerFocusPoints.ts
T
Maurice eb9f6a3438 feat(tours): the Tours map shows the trip's tours while none is open
The map stayed empty until a tour was opened. It now draws every tour shown in the list, line and marker as on the plan, frames them, and opens a tour from its marker.
2026-10-25 10:51:57 +01:00

68 lines
2.9 KiB
TypeScript

import { useEffect, useMemo, useRef, useState } from 'react';
import type { Bbox } from '../../components/Map/usePoiExplore';
import type { TourPlannerController } from '../../components/Tours/planner/useTourPlanner';
import type { WandererBrowser } from '../../components/Tours/wanderer/useWandererBrowser';
import type { Place } from '../../types';
/**
* What the TOUR-PLANNER map frames: a browsed wanderer trail, the recorded
* track or the control points of the open tour, and with nothing open the
* trip's tours on show (`overviewPoints`), else the area the plan map last
* showed (or the trip's places). The plan map reports its viewport into the
* returned ref.
*/
export function usePlannerFocusPoints(
activeTab: string,
places: readonly Place[],
tourPlanner: TourPlannerController,
wandererBrowser: Pick<WandererBrowser, 'open' | 'line'>,
overviewPoints: readonly [number, number][] = []
) {
const lastPlanMapViewportRef = useRef<Bbox | null>(null);
// Nothing reported yet when the planner first opens; the effect takes the area the plan map last showed.
const [plannerViewportSnapshot, setPlannerViewportSnapshot] = useState<Bbox | null>(null);
useEffect(() => {
if (activeTab === 'tour-planner') setPlannerViewportSnapshot(lastPlanMapViewportRef.current);
}, [activeTab, tourPlanner.newDraftGeneration]);
const plannerFallbackFocusPoints = useMemo<[number, number][]>(() => {
const bounds = plannerViewportSnapshot;
if (bounds) {
const latitudeSpan = bounds.north - bounds.south;
const longitudeSpan = bounds.east - bounds.west;
if (latitudeSpan > 0 && latitudeSpan < 160 && longitudeSpan > 0 && longitudeSpan < 340) {
return [
[bounds.south, bounds.west],
[bounds.north, bounds.east],
];
}
}
return places
.filter((place) => Number.isFinite(place.lat) && Number.isFinite(place.lng))
.map((place) => [place.lat, place.lng] as [number, number]);
}, [places, plannerViewportSnapshot]);
const focusPoints = useMemo<[number, number][]>(() => {
if (wandererBrowser.open) return wandererBrowser.line ?? plannerFallbackFocusPoints;
if (tourPlanner.mode.type === 'view-gpx') return tourPlanner.readOnlyGpxAnalysis?.routeCoordinates ?? [];
if (tourPlanner.mode.type === 'new-draft') {
return tourPlanner.waypoints.map((point) => [point.lat, point.lng]);
}
if (tourPlanner.mode.type === 'edit-saved' && tourPlanner.waypoints.length) {
return tourPlanner.waypoints.map((point) => [point.lat, point.lng]);
}
if (tourPlanner.mode.type === 'neutral' && overviewPoints.length) return [...overviewPoints];
return plannerFallbackFocusPoints;
}, [
wandererBrowser.open,
wandererBrowser.line,
tourPlanner.mode.type,
tourPlanner.readOnlyGpxAnalysis?.routeCoordinates,
tourPlanner.waypoints,
overviewPoints,
plannerFallbackFocusPoints,
]);
return { focusPoints, lastPlanMapViewportRef };
}