|
| 1 | +// @ts-check |
| 2 | +import * as React from 'react' |
| 3 | +import { Marker, useMapEvents } from 'react-leaflet' |
| 4 | +import { divIcon } from 'leaflet' |
| 5 | + |
| 6 | +import { useStorage } from '@store/useStorage' |
| 7 | + |
| 8 | +import { RouteTile } from './RouteTile' |
| 9 | +import { routeMarker } from './routeMarker' |
| 10 | +import { |
| 11 | + useRouteStore, |
| 12 | + ROUTE_COORD_EPSILON, |
| 13 | + getRouteCoordKey, |
| 14 | +} from './useRouteStore' |
| 15 | + |
| 16 | +const ACTIVE_Z_INDEX = 1800 |
| 17 | +const INACTIVE_Z_INDEX = 900 |
| 18 | + |
| 19 | +const RouteAnchor = React.memo( |
| 20 | + ({ |
| 21 | + entry, |
| 22 | + selected, |
| 23 | + onSelect, |
| 24 | + routeCount, |
| 25 | + variant = 'start', |
| 26 | + icon = null, |
| 27 | + }) => { |
| 28 | + const baseIcon = React.useMemo( |
| 29 | + () => icon || routeMarker(variant === 'destination' ? 'end' : 'start'), |
| 30 | + [icon, variant], |
| 31 | + ) |
| 32 | + const badgeIcon = React.useMemo(() => { |
| 33 | + if (routeCount <= 1) return null |
| 34 | + return divIcon({ |
| 35 | + className: 'route-count-wrapper', |
| 36 | + html: `<span class="route-count-badge route-count-badge--${variant}">${routeCount}</span>`, |
| 37 | + iconSize: [0, 0], |
| 38 | + iconAnchor: [0, 0], |
| 39 | + }) |
| 40 | + }, [routeCount, variant]) |
| 41 | + |
| 42 | + return ( |
| 43 | + <> |
| 44 | + {variant !== 'destination' && !selected && ( |
| 45 | + <Marker |
| 46 | + position={[entry.lat, entry.lon]} |
| 47 | + icon={baseIcon} |
| 48 | + zIndexOffset={INACTIVE_Z_INDEX} |
| 49 | + riseOnHover |
| 50 | + eventHandlers={{ |
| 51 | + click: () => onSelect(entry.key), |
| 52 | + }} |
| 53 | + title={routeCount > 1 ? `${routeCount} routes` : ''} |
| 54 | + /> |
| 55 | + )} |
| 56 | + {badgeIcon && ( |
| 57 | + <Marker |
| 58 | + position={[entry.lat, entry.lon]} |
| 59 | + icon={badgeIcon} |
| 60 | + interactive={false} |
| 61 | + keyboard={false} |
| 62 | + pane="tooltipPane" |
| 63 | + zIndexOffset={ |
| 64 | + selected ? ACTIVE_Z_INDEX + 200 : INACTIVE_Z_INDEX + 200 |
| 65 | + } |
| 66 | + /> |
| 67 | + )} |
| 68 | + </> |
| 69 | + ) |
| 70 | + }, |
| 71 | +) |
| 72 | + |
| 73 | +const ActiveRoute = React.memo(({ selection }) => { |
| 74 | + const route = useRouteStore( |
| 75 | + React.useCallback( |
| 76 | + (state) => state.routeCache[selection.routeId], |
| 77 | + [selection.routeId], |
| 78 | + ), |
| 79 | + ) |
| 80 | + |
| 81 | + if (!route) return null |
| 82 | + return <RouteTile route={route} orientation={selection.orientation} /> |
| 83 | +}) |
| 84 | + |
| 85 | +export function RouteLayer({ routes }) { |
| 86 | + const enabled = useStorage((s) => !!s.filters?.routes?.enabled) |
| 87 | + const compactView = useStorage( |
| 88 | + (s) => s.userSettings.routes?.compactView ?? true, |
| 89 | + ) |
| 90 | + const syncRoutes = useRouteStore((s) => s.syncRoutes) |
| 91 | + const poiIndex = useRouteStore((s) => s.poiIndex) |
| 92 | + const routeCache = useRouteStore((s) => s.routeCache) |
| 93 | + const activeRoutes = useRouteStore((s) => s.activeRoutes) |
| 94 | + const activePoiId = useRouteStore((s) => s.activePoiId) |
| 95 | + const selectPoi = useRouteStore((s) => s.selectPoi) |
| 96 | + const clearSelection = useRouteStore((s) => s.clearSelection) |
| 97 | + |
| 98 | + React.useEffect(() => { |
| 99 | + syncRoutes(routes || []) |
| 100 | + }, [routes, syncRoutes]) |
| 101 | + |
| 102 | + React.useEffect(() => { |
| 103 | + if (!enabled || !compactView) { |
| 104 | + clearSelection() |
| 105 | + } |
| 106 | + }, [enabled, compactView, clearSelection]) |
| 107 | + |
| 108 | + useMapEvents({ |
| 109 | + click: ({ originalEvent }) => { |
| 110 | + if (!originalEvent.defaultPrevented) { |
| 111 | + clearSelection() |
| 112 | + } |
| 113 | + }, |
| 114 | + }) |
| 115 | + |
| 116 | + const destinationSummary = React.useMemo(() => { |
| 117 | + if (!compactView) |
| 118 | + return { keys: new Set(), icons: new Map(), counts: new Map() } |
| 119 | + const keys = new Set() |
| 120 | + const icons = new Map() |
| 121 | + const counts = new Map() |
| 122 | + activeRoutes.forEach((selection) => { |
| 123 | + const route = routeCache[selection.routeId] |
| 124 | + if (!route) return |
| 125 | + const isForward = selection.orientation === 'forward' |
| 126 | + const lat = isForward ? route.end_lat : route.start_lat |
| 127 | + const lon = isForward ? route.end_lon : route.start_lon |
| 128 | + const coordKey = getRouteCoordKey(lat, lon) |
| 129 | + keys.add(coordKey) |
| 130 | + counts.set(coordKey, (counts.get(coordKey) || 0) + 1) |
| 131 | + if (!icons.has(coordKey)) { |
| 132 | + icons.set(coordKey, routeMarker('end')) |
| 133 | + } |
| 134 | + }) |
| 135 | + return { keys, icons, counts } |
| 136 | + }, [activeRoutes, routeCache, compactView]) |
| 137 | + |
| 138 | + const anchors = React.useMemo(() => { |
| 139 | + if (!compactView) return [] |
| 140 | + const values = Object.values(poiIndex) |
| 141 | + return values.map((entry) => { |
| 142 | + const uniqueRoutes = new Set() |
| 143 | + values.forEach((candidate) => { |
| 144 | + if ( |
| 145 | + Math.abs(candidate.lat - entry.lat) <= ROUTE_COORD_EPSILON && |
| 146 | + Math.abs(candidate.lon - entry.lon) <= ROUTE_COORD_EPSILON |
| 147 | + ) { |
| 148 | + candidate.routes.forEach((ref) => { |
| 149 | + if (routeCache[ref.routeId]) { |
| 150 | + uniqueRoutes.add(ref.routeId) |
| 151 | + } |
| 152 | + }) |
| 153 | + } |
| 154 | + }) |
| 155 | + return { |
| 156 | + entry, |
| 157 | + routeCount: |
| 158 | + uniqueRoutes.size || new Set(entry.routes.map((r) => r.routeId)).size, |
| 159 | + } |
| 160 | + }) |
| 161 | + }, [compactView, poiIndex, routeCache]) |
| 162 | + |
| 163 | + if (!enabled) { |
| 164 | + return null |
| 165 | + } |
| 166 | + |
| 167 | + if (!compactView) { |
| 168 | + return ( |
| 169 | + <> |
| 170 | + {routes.map((route) => ( |
| 171 | + <RouteTile key={route.id} route={route} /> |
| 172 | + ))} |
| 173 | + </> |
| 174 | + ) |
| 175 | + } |
| 176 | + |
| 177 | + return ( |
| 178 | + <> |
| 179 | + {anchors.map(({ entry, routeCount }) => { |
| 180 | + const entryCoordKey = getRouteCoordKey(entry.lat, entry.lon) |
| 181 | + const iconOverride = destinationSummary.icons.get(entryCoordKey) |
| 182 | + const destinationCount = destinationSummary.counts.get(entryCoordKey) |
| 183 | + if (destinationCount && destinationCount > 1) { |
| 184 | + return ( |
| 185 | + <RouteAnchor |
| 186 | + key={`${entry.key}-destination`} |
| 187 | + entry={entry} |
| 188 | + selected={entry.key === activePoiId} |
| 189 | + onSelect={selectPoi} |
| 190 | + routeCount={destinationCount} |
| 191 | + variant="destination" |
| 192 | + icon={iconOverride || undefined} |
| 193 | + /> |
| 194 | + ) |
| 195 | + } |
| 196 | + if ( |
| 197 | + destinationSummary.keys.has(entryCoordKey) && |
| 198 | + entry.key !== activePoiId |
| 199 | + ) { |
| 200 | + return null |
| 201 | + } |
| 202 | + return ( |
| 203 | + <RouteAnchor |
| 204 | + key={entry.key} |
| 205 | + entry={entry} |
| 206 | + selected={entry.key === activePoiId} |
| 207 | + onSelect={selectPoi} |
| 208 | + routeCount={destinationCount || routeCount} |
| 209 | + variant="start" |
| 210 | + icon={iconOverride || undefined} |
| 211 | + /> |
| 212 | + ) |
| 213 | + })} |
| 214 | + {activeRoutes.map((selection) => ( |
| 215 | + <ActiveRoute |
| 216 | + key={`${selection.routeId}-${selection.orientation}`} |
| 217 | + selection={selection} |
| 218 | + /> |
| 219 | + ))} |
| 220 | + </> |
| 221 | + ) |
| 222 | +} |
0 commit comments