import { Fragment, createElement, useMemo, useState } from 'react' import { renderIconMarkup } from '../../utils/iconMarkup' import { Marker, Polyline, Tooltip, useMap, useMapEvents } from 'react-leaflet' import L from 'leaflet' import { escapeHtml } from '@trek/shared' import { getTransitMapSegments, type TransitMapSegment } from './transitGeometry' import { cleanEndpointName } from './reservationName' import { useSettingsStore } from '../../store/settingsStore' import { hopIsVisible, labelFloorPx } from '../../utils/reservationRoutes' import { TRANSPORT_COLOR, TRANSPORT_META, transportHop, type TransportHop, type TransportType } from './transportHops' import type { Reservation } from '../../types' const ENDPOINT_PANE = 'reservation-endpoints' function useEndpointPane() { const map = useMap() useMemo(() => { if (typeof map?.getPane !== 'function' || typeof map?.createPane !== 'function') return if (!map.getPane(ENDPOINT_PANE)) { const pane = map.createPane(ENDPOINT_PANE) pane.style.zIndex = '650' pane.style.pointerEvents = 'auto' } }, [map]) } function endpointIcon(type: TransportType, label: string | null): L.DivIcon { const { icon: IconCmp } = TRANSPORT_META[type] const svg = renderIconMarkup(createElement(IconCmp, { size: 13, color: 'white', strokeWidth: 2.5 })) const labelHtml = label ? `${escapeHtml(label)}` : '' const estWidth = label ? Math.max(40, label.length * 6 + 28) : 26 return L.divIcon({ className: 'trek-endpoint-marker', html: `
${svg}${labelHtml}
`, iconSize: [estWidth, 22], iconAnchor: [estWidth / 2, 11], popupAnchor: [0, -11], }) } /** What a hop will draw: the road it was routed along when there is one, else its arcs. */ function linesFor(item: TransportItem, roadRoutes: Map | undefined): [number, number][][] { const road = roadRoutes?.get(item.res.id) return road && road.length >= 2 ? [road] : item.arcs } interface TransportItem extends TransportHop { transitSegs: TransitMapSegment[] } interface Props { reservations: Reservation[] showConnections: boolean // Accepted for call-site compatibility only: the floating route/duration badge // on the arc is no longer drawn, so nothing is gated on this. showStats: boolean onEndpointClick?: (reservationId: number) => void // Real road-network geometry for car/bus/taxi/bicycle bookings, keyed by // reservation id. When present it is drawn instead of the straight arc. roadRoutes?: Map } export default function ReservationOverlay({ reservations, showConnections, onEndpointClick, roadRoutes }: Props) { useEndpointPane() const map = useMap() const [zoom, setZoom] = useState(() => map.getZoom()) useMapEvents({ zoomend: () => setZoom(map.getZoom()), }) const showEndpointLabels = useSettingsStore(s => s.settings.map_booking_labels) === true const items = useMemo(() => { const out: TransportItem[] = [] for (const r of reservations) { const hop = transportHop(r, true) if (!hop) continue out.push({ ...hop, transitSegs: hop.type === 'transit' ? getTransitMapSegments(r) : [] }) } return out }, [reservations]) const visibleItems = useMemo(() => { const project = (p: readonly [number, number]) => map.latLngToContainerPoint([p[0], p[1]]) return items.filter(item => { // A transit journey draws its real rail/bus alignment, not a straight from->to // line, so the endpoint-proximity declutter (which exists to hide tiny no-op // straight connectors) must not suppress it. Otherwise a zoomed-out day — e.g. one // with no other places to tighten the map onto — hides the whole route (#1570). if (item.transitSegs.length > 0) return true // Measured along what is drawn, not between the ends: a routed drive can // cover half the screen while its endpoints sit close together (#2275). return hopIsVisible(item.type, linesFor(item, roadRoutes), project) }) }, [items, zoom, map, roadRoutes]) const labelVisibleIds = useMemo(() => { const set = new Set() for (const item of visibleItems) { const fromPx = map.latLngToContainerPoint([item.from.lat, item.from.lng]) const toPx = map.latLngToContainerPoint([item.to.lat, item.to.lng]) if (fromPx.distanceTo(toPx) >= labelFloorPx(item.type)) set.add(item.res.id) } return set }, [visibleItems, zoom, map]) if (!showConnections) return null return ( <> {visibleItems.map(item => { if (item.transitSegs.length > 0) { return item.transitSegs.map((seg, segIdx) => ( {!seg.walk && ( )} )) } // Prefer the real road route (car/bus/taxi/bicycle) over the straight arc. return linesFor(item, roadRoutes).map((seg, segIdx) => ( )) })} {visibleItems.flatMap(item => item.waypoints.map((wp, wi) => ( onEndpointClick?.(item.res.id) }} >
{wp.name}
{item.res.title &&
{item.res.title}
}
)))} ) }