Files
TREK/client/src/hooks/usePlanMapViewport.ts
T
Maurice adb88142ab fix(places): the place search looks where the plan map is looking
Autocomplete and the full search took their location hint only from the
open day or the whole trip, and dropped a trip spread over several cities
as too wide. Zoomed in on one city of such a trip, the search went global.

The plan map now hands its viewport to the trip store on desktop and on
the phone, and useLocationBias tries it first. A viewport wider than the
same 60 km radius, one across the antimeridian or one from another trip
is ignored, and the day and the trip follow as before. The viewport is
cleared when the plan map leaves the screen and on a trip switch.

Refs #2620
2026-10-12 17:18:17 +02:00

27 lines
1.1 KiB
TypeScript

import { useCallback, useEffect } from 'react'
import { useTripStore } from '../store/tripStore'
interface Bounds { south: number; west: number; north: number; east: number }
/**
* Hands the plan map's viewport to the trip store, where the place search reads
* it as its first hint (#2620). Returns the callback for the map's
* `onViewportChange`. While `shown` is false, or once the map is gone, the
* viewport is cleared: a search opened away from the map has nothing visible
* to lean on, and falls back to the day and the trip.
*/
export function usePlanMapViewport(tripId: number, shown = true): (bounds: Bounds) => void {
const setPlanMapViewport = useTripStore((s) => s.setPlanMapViewport)
useEffect(() => {
if (!shown) setPlanMapViewport(null)
return () => setPlanMapViewport(null)
}, [shown, tripId, setPlanMapViewport])
return useCallback((bounds: Bounds) => {
if (!shown || !Number.isFinite(tripId)) return
const { south, west, north, east } = bounds
setPlanMapViewport({ tripId, south, west, north, east })
}, [shown, tripId, setPlanMapViewport])
}