diff --git a/client/src/pages/tripPlanner/TourPlannerMapStage.tsx b/client/src/pages/tripPlanner/TourPlannerMapStage.tsx index 6479fe097..f040319b9 100644 --- a/client/src/pages/tripPlanner/TourPlannerMapStage.tsx +++ b/client/src/pages/tripPlanner/TourPlannerMapStage.tsx @@ -281,7 +281,8 @@ export function TourPlannerMapStage({ ) : ( { expect(result.current.focusPoints).toEqual([[48, 11]]); }); + it('FE-TP-FOCUSPOINTS-005: with nothing open the trip tours on show are framed, an open tour frames its own points', () => { + const tours: [number, number][] = [ + [46.5, 12.0], + [46.7, 12.2], + ]; + const neutral = renderHook(() => usePlannerFocusPoints('tour-planner', PLACES, planner(), closed, tours)); + expect(neutral.result.current.focusPoints).toEqual(tours); + const editing = renderHook(() => + usePlannerFocusPoints( + 'tour-planner', + PLACES, + planner({ mode: { type: 'new-draft' }, waypoints: [{ lat: 1, lng: 2 }] }), + closed, + tours + ) + ); + expect(editing.result.current.focusPoints).toEqual([[1, 2]]); + }); + it('FE-TP-FOCUSPOINTS-002: the area the plan map last showed wins once the planner opens again', () => { const { result, rerender } = renderHook(({ tab }) => usePlannerFocusPoints(tab, PLACES, planner(), closed), { initialProps: { tab: 'plan' }, diff --git a/client/src/pages/tripPlanner/usePlannerFocusPoints.ts b/client/src/pages/tripPlanner/usePlannerFocusPoints.ts index b882772af..91a1cbe66 100644 --- a/client/src/pages/tripPlanner/usePlannerFocusPoints.ts +++ b/client/src/pages/tripPlanner/usePlannerFocusPoints.ts @@ -7,14 +7,16 @@ 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 - * area the plan map last showed (or the trip's places). The plan map reports - * its viewport into the returned ref. + * 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: Pick, + overviewPoints: readonly [number, number][] = [] ) { const lastPlanMapViewportRef = useRef(null); // Nothing reported yet when the planner first opens; the effect takes the area the plan map last showed. @@ -49,6 +51,7 @@ export function usePlannerFocusPoints( 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, @@ -56,6 +59,7 @@ export function usePlannerFocusPoints( tourPlanner.mode.type, tourPlanner.readOnlyGpxAnalysis?.routeCoordinates, tourPlanner.waypoints, + overviewPoints, plannerFallbackFocusPoints, ]); diff --git a/client/src/pages/tripPlanner/useTourOverview.test.ts b/client/src/pages/tripPlanner/useTourOverview.test.ts new file mode 100644 index 000000000..f9eaa9896 --- /dev/null +++ b/client/src/pages/tripPlanner/useTourOverview.test.ts @@ -0,0 +1,40 @@ +// FE-TP-TOUROVERVIEW-001 to 003: the trip's tours on the Tours map while none is open. +import { renderHook } from '@testing-library/react'; +import { beforeEach, describe, expect, it } from 'vitest'; + +import { resetAllStores } from '../../../tests/helpers/store'; +import type { Place } from '../../types'; +import { useTourOverview } from './useTourOverview'; + +const PLACES = [ + { id: 1, lat: 46.6, lng: 12.1, route_geometry: '[[46.5,12.0],[46.7,12.2],[46.6,12.3]]' }, + { id: 2, lat: 45.4, lng: 12.3, route_geometry: '[[45.43,12.32],[45.44,12.34]]' }, + { id: 3, lat: 45.4, lng: 12.3, route_geometry: null }, + { id: 4, lat: 48, lng: 11, route_geometry: '[[48,11],[48.1,11.1]]' }, +] as unknown as Place[]; +const TOURS: ReadonlySet = new Set([1, 2, 3]); + +describe('useTourOverview', () => { + beforeEach(() => resetAllStores()); + + it('FE-TP-TOUROVERVIEW-001: shows the tours with a line, not the other places', () => { + const { result } = renderHook(() => useTourOverview(5, PLACES, TOURS, true)); + expect(result.current.tourPlaces.map((place) => place.id)).toEqual([1, 2]); + }); + + it('FE-TP-TOUROVERVIEW-002: frames each line by its corners', () => { + const { result } = renderHook(() => useTourOverview(5, PLACES, TOURS, true)); + expect(result.current.framePoints).toEqual([ + [46.5, 12.0], + [46.7, 12.3], + [45.43, 12.32], + [45.44, 12.34], + ]); + expect(result.current.focusSuffix).toBe(':tours:1,2'); + }); + + it('FE-TP-TOUROVERVIEW-003: shows nothing while a tour is open', () => { + const { result } = renderHook(() => useTourOverview(5, PLACES, TOURS, false)); + expect(result.current).toEqual({ tourPlaces: [], framePoints: [], focusSuffix: '' }); + }); +}); diff --git a/client/src/pages/tripPlanner/useTourOverview.ts b/client/src/pages/tripPlanner/useTourOverview.ts new file mode 100644 index 000000000..95048355f --- /dev/null +++ b/client/src/pages/tripPlanner/useTourOverview.ts @@ -0,0 +1,46 @@ +import { useMemo } from 'react'; +import { useTourMapVisibility } from '../../components/Tours/useTourMapVisibility'; +import type { Place } from '../../types'; +import { parseRenderableRouteGeometry } from '../../utils/routeGeometry'; + +/** + * The trip's tours on the Tours map while none is open, as the plan map shows them: + * each tour's place, whose line and marker the map draws in the tour's colour, and the + * corners of those lines for the camera to frame. A tour hidden from the map in the + * list stays off it here too. Empty while `showing` is off (a tour open, an import + * viewed, the wanderer browser up), so the map then shows that alone. + */ +export function useTourOverview( + tripId: number, + places: readonly Place[], + tourPlaceIds: ReadonlySet, + showing: boolean +) { + const { hiddenIds } = useTourMapVisibility(tripId, tourPlaceIds); + const tourPlaces = useMemo( + () => + showing + ? places.filter((place) => tourPlaceIds.has(place.id) && !hiddenIds.has(place.id) && !!place.route_geometry) + : [], + [showing, places, tourPlaceIds, hiddenIds] + ); + const framePoints = useMemo(() => tourPlaces.flatMap((place) => cornersOf(place.route_geometry)), [tourPlaces]); + return { + tourPlaces, + framePoints, + // Part of the camera's key, so the map frames the tours again once they are loaded or one is shown or hidden. + focusSuffix: showing ? `:tours:${tourPlaces.map((place) => place.id).join(',')}` : '', + }; +} + +/** The south-west and north-east corners of a stored line, or nothing for a line that does not parse. */ +function cornersOf(routeGeometry: string | null | undefined): [number, number][] { + const points = parseRenderableRouteGeometry(routeGeometry); + if (!points?.length) return []; + const lats = points.map(([lat]) => lat); + const lngs = points.map(([, lng]) => lng); + return [ + [Math.min(...lats), Math.min(...lngs)], + [Math.max(...lats), Math.max(...lngs)], + ]; +} diff --git a/client/src/pages/tripPlanner/useTripPlannerPage.ts b/client/src/pages/tripPlanner/useTripPlannerPage.ts index ccffbbc3e..099ac2910 100644 --- a/client/src/pages/tripPlanner/useTripPlannerPage.ts +++ b/client/src/pages/tripPlanner/useTripPlannerPage.ts @@ -16,6 +16,7 @@ import { useMapLocationSearch } from './useMapLocationSearch' import { usePlannerFocusPoints } from './usePlannerFocusPoints' import { useTripPlanner } from './useTripPlanner' import { useTourWorkspace } from './useTourWorkspace' +import { useTourOverview } from './useTourOverview' import { mapFocusKeyOf, offeredWaysOf, paddleLineOf } from './tourMapFocus' import { placeFormContext } from './placeFormContext' @@ -41,7 +42,12 @@ export function useTripPlannerPage() { useAssistantEntityLinks({ planner, workspace }) const tourDetailOpenerRef = useRef<{ placeId: number; element: HTMLElement } | null>(null) - const { focusPoints, lastPlanMapViewportRef } = usePlannerFocusPoints(activeTab, places, tourPlanner, wandererBrowser) + // With no tour open, the Tours map shows the trip's tours and frames them. + const tourOverview = useTourOverview( + tripId, places, planner.tourPlaceIds, + activeTab === 'tour-planner' && tourPlanner.mode.type === 'neutral' && !wandererBrowser.open, + ) + const { focusPoints, lastPlanMapViewportRef } = usePlannerFocusPoints(activeTab, places, tourPlanner, wandererBrowser, tourOverview.framePoints) // PAD-06: an obstacle or area the paddle checklist asks for moves the camera as a picked search result does. const paddleFocus = usePaddleObstacleStore(state => state.focus) @@ -152,7 +158,9 @@ export function useTripPlannerPage() { variantLines: drawn.overlays, previewPoints: tourConversion.status === 'idle' ? null : tourConversion.points, focusPoints: offered.frame?.points ?? mapFocusPoints, - focusKey: mapFocusKeyOf(wandererBrowser, offered.frame, mapFocusKey), + focusKey: mapFocusKeyOf(wandererBrowser, offered.frame, mapFocusKey) + tourOverview.focusSuffix, + overviewPlaces: tourOverview.tourPlaces, + openOverviewTour: workspace.openTourInPlanner, captureViewport, }, locationSearch,