-
Notifications
You must be signed in to change notification settings - Fork 46
/
Copy pathDefaultBaseMap.tsx
131 lines (119 loc) · 3.53 KB
/
DefaultBaseMap.tsx
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
import { type PropsWithChildren, useCallback, useEffect, useRef, useState } from 'react';
import bbox from '@turf/bbox';
import { compact } from 'lodash';
import type { MapRef } from 'react-map-gl/maplibre';
import type { GeoJsonLineString } from 'common/api/osrdEditoastApi';
import BaseMap from 'common/Map/BaseMap';
import MapButtons from 'common/Map/Buttons/MapButtons';
import { computeBBoxViewport } from 'common/Map/WarpedMap/core/helpers';
import { LAYER_GROUPS_ORDER, LAYERS } from 'config/layerOrder';
import ItineraryLayer from 'modules/trainschedule/components/ManageTrainSchedule/ManageTrainScheduleMap/ItineraryLayer';
import ItineraryMarkers, {
type MarkerInformation,
} from 'modules/trainschedule/components/ManageTrainSchedule/ManageTrainScheduleMap/ItineraryMarkers';
import { mapInitialState } from 'reducers/map';
import type { Viewport } from 'reducers/map';
type DefaultBaseMapProps = {
mapId: string;
infraId?: number;
geometry?: GeoJsonLineString;
pathStepMarkers?: MarkerInformation[];
isFeasible?: boolean;
};
const ZOOM_DEFAULT = 5;
const ZOOM_DELTA = 1.5;
/**
* Default base map used to display a path and some markers.
* No interactions are available, except zoom in/out, pan and reset pitch/bearing.
*/
const DefaultBaseMap = ({
mapId,
infraId,
geometry,
pathStepMarkers = [],
isFeasible = true,
children,
}: PropsWithChildren<DefaultBaseMapProps>) => {
const mapRef = useRef<MapRef | null>(null);
const [viewPort, setViewPort] = useState(mapInitialState.viewport);
const updateViewportChange = useCallback(
(partialViewPort: Partial<Viewport>) =>
setViewPort((prev) => ({
...prev,
...partialViewPort,
})),
[]
);
const resetPitchBearing = () => {
updateViewportChange({
...viewPort,
bearing: 0,
pitch: 0,
});
};
const zoomIn = () => {
updateViewportChange({
...viewPort,
zoom: (viewPort.zoom || ZOOM_DEFAULT) + ZOOM_DELTA,
});
};
const zoomOut = () => {
updateViewportChange({
...viewPort,
zoom: (viewPort.zoom || ZOOM_DEFAULT) - ZOOM_DELTA,
});
};
useEffect(() => {
const points = geometry ?? {
coordinates: compact(pathStepMarkers.map((step) => step.coordinates)),
type: 'LineString',
};
if (points.coordinates.length >= 2) {
const newViewport = computeBBoxViewport(bbox(points), viewPort);
updateViewportChange(newViewport);
}
}, [geometry, pathStepMarkers]);
return (
<>
<MapButtons
zoomIn={zoomIn}
zoomOut={zoomOut}
map={mapRef.current ?? undefined}
resetPitchBearing={resetPitchBearing}
bearing={viewPort.bearing}
withMapKeyButton={false}
withSearchButton={false}
withToggleLayersButton={false}
viewPort={viewPort}
isNewButtons
/>
<BaseMap
mapId={mapId}
mapRef={mapRef}
infraId={infraId}
interactiveLayerIds={[]}
mapStyle={mapInitialState.mapStyle}
viewPort={viewPort}
updatePartialViewPort={updateViewportChange}
hideAttribution
showOSM
>
<ItineraryLayer
layerOrder={LAYER_GROUPS_ORDER[LAYERS.PATH.GROUP]}
geometry={geometry}
isFeasible={isFeasible}
showStdcmAssets
/>
{infraId && (
<ItineraryMarkers
infraId={infraId}
simulationPathSteps={pathStepMarkers}
showStdcmAssets
/>
)}
{children}
</BaseMap>
</>
);
};
export default DefaultBaseMap;