-
-
Notifications
You must be signed in to change notification settings - Fork 128
Expand file tree
/
Copy pathMapContent.tsx
More file actions
68 lines (62 loc) · 2.28 KB
/
Copy pathMapContent.tsx
File metadata and controls
68 lines (62 loc) · 2.28 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
import { Layer } from 'leaflet'
import { useCallback, useEffect, useState } from 'react'
import { useTranslation } from 'react-i18next'
import { TileLayer, useMap } from 'react-leaflet'
import { MapProps } from './map-types'
import { MapPlannedRouteLayer } from './MapLayers/MapPlannedRouteLayer'
import { MapRouteLayer } from './MapLayers/MapRouteLayer'
import { useRecenterOnDataChange } from './useRecenterOnDataChange'
export function MapContent({
positionGroups,
plannedRouteStops,
showNavigationButtons,
focusTarget,
}: MapProps) {
const [tileUrl, setTileUrl] = useState('https://tile-a.openstreetmap.fr/hot/{z}/{x}/{y}.png')
const map = useMap()
const { i18n } = useTranslation()
useRecenterOnDataChange({ positionGroups, plannedRouteStops })
// Fly to (and scroll into view) an externally requested location — e.g. clicking a
// coverage-gap's geomarker focuses the last-seen ping before the bus went dark.
useEffect(() => {
if (!map || !focusTarget) return
map.flyTo(focusTarget.loc, Math.max(map.getZoom(), 16))
map.getContainer().scrollIntoView({ behavior: 'smooth', block: 'center' })
}, [map, focusTarget])
useEffect(() => {
const handleLanguageChange = (lng: string) => {
const newUrl =
lng === 'he'
? 'https://tile-a.openstreetmap.fr/hot/{z}/{x}/{y}.png'
: 'https://tile-a.openstreetmap.fr/osmfr/{z}/{x}/{y}.png?lang=en'
setTileUrl(newUrl)
}
i18n.on('languageChanged', handleLanguageChange)
return () => {
i18n.off('languageChanged', handleLanguageChange)
}
}, [i18n])
const navigateMarkers = useCallback(
(groupIndex: number, positionId: number, marker: Layer) => {
const pos = positionGroups[groupIndex]?.positions[positionId]
if (!map || !pos?.loc) return
map.flyTo(pos.loc, map.getZoom())
marker.openPopup()
},
[map, positionGroups],
)
return (
<>
<TileLayer
attribution='© <a href="https://www.openstreetmap.org/copyright">OpenStreetMap</a> contributors'
url={tileUrl}
/>
<MapRouteLayer
positionGroups={positionGroups}
showNavigationButtons={showNavigationButtons}
navigateMarkers={navigateMarkers}
/>
<MapPlannedRouteLayer plannedRouteStops={plannedRouteStops} />
</>
)
}