import type L from 'leaflet'; import { useEffect, useRef } from 'react'; import { useMap } from 'react-leaflet'; import type { Place } from '../../types'; import { frameLng } from '../../utils/lngFrame'; import type { ViewportPadding } from '../../utils/mapViewport'; import { useLngFrame } from './lngFrameContext'; import { isUnderChrome } from './mapChromePadding'; import { PLACE_FOCUS_ZOOM, selectedPlaceTarget } from './selectedPlaceTarget'; interface SelectionControllerProps { /** False while the traveller locked the map (#2010): a pick changes nothing on it. */ follow: boolean; places: Place[]; selectedPlaceId: number | null; dayPlaces: Place[]; selectedPlace: Place | null; paddingBox: ViewportPadding; revealSelectedPlace: () => void; } /** * Where the Leaflet map goes when a place is selected: there, and out from under the * panels. The GL twin is `useSelectionCameraGL`. */ export function SelectionController({ follow, places, selectedPlaceId, dayPlaces, selectedPlace, paddingBox, revealSelectedPlace, }: SelectionControllerProps) { const map = useMap(); const frame = useLngFrame(); const prev = useRef(null); /** The panel geometry the last pan was measured against. */ const pannedWith = useRef(paddingBox); const target = selectedPlaceTarget(selectedPlaceId, places, dayPlaces, selectedPlace); const lat = target?.lat ?? null; const lng = target?.lng == null ? null : frameLng(frame, target.lng); useEffect(() => { if (!follow || selectedPlaceId == null) return; let frame: number | null = null; const revealAfterMove = () => { if (frame !== null) cancelAnimationFrame(frame); frame = requestAnimationFrame(() => revealSelectedPlace()); }; map.on('moveend', revealAfterMove); return () => { map.off('moveend', revealAfterMove); if (frame !== null) cancelAnimationFrame(frame); }; }, [map, follow, selectedPlaceId, revealSelectedPlace]); useEffect(() => { const isNew = selectedPlaceId !== prev.current; prev.current = selectedPlaceId; if (!isNew || !follow || !selectedPlaceId || lat == null || lng == null) return; pannedWith.current = paddingBox; const zoom = map.getZoom(); const focusZoom = Math.max(zoom, PLACE_FOCUS_ZOOM); const center = paddedCenter(map, [lat, lng], paddingBox, focusZoom); if (zoom < focusZoom) { map.setView(center, focusZoom, { animate: false }); } else { map.panTo(center, { animate: true }); } }, [selectedPlaceId, follow, lat, lng, map, paddingBox]); // The place card only learns its height after it laid out, and grows again when its // details arrive. If it grew over the pin the selection placed, bring the pin back out; // a pin still in the open part of the map stays where the traveller left it (#2991). useEffect(() => { if (pannedWith.current === paddingBox) return; pannedWith.current = paddingBox; if (!follow || !selectedPlaceId || lat == null || lng == null) return; if (typeof map.latLngToContainerPoint !== 'function') return; if (isUnderChrome(map.latLngToContainerPoint([lat, lng]), map.getSize(), paddingBox)) { map.panTo(paddedCenter(map, [lat, lng], paddingBox, map.getZoom()), { animate: true }); } }, [paddingBox, follow, selectedPlaceId, lat, lng, map]); return null; } /** * Offset the centre at the destination zoom so the pin lands in the middle of the * visible map area, above the bottom card and between the side panels. */ function paddedCenter( map: L.Map, latlng: [number, number], padding: ViewportPadding, zoom: number ): L.LatLngExpression { if (typeof map.project !== 'function' || typeof map.unproject !== 'function') { return latlng; } const { top, right, bottom, left } = padding; const point = map.project(latlng, zoom).add([(right - left) / 2, (bottom - top) / 2]); return map.unproject(point, zoom); }