|
| 1 | +import { type PropsWithChildren, useCallback, useEffect, useRef, useState } from 'react'; |
| 2 | + |
| 3 | +import bbox from '@turf/bbox'; |
| 4 | +import { compact } from 'lodash'; |
| 5 | +import type { MapRef } from 'react-map-gl/maplibre'; |
| 6 | + |
| 7 | +import type { GeoJsonLineString } from 'common/api/osrdEditoastApi'; |
| 8 | +import BaseMap from 'common/Map/BaseMap'; |
| 9 | +import MapButtons from 'common/Map/Buttons/MapButtons'; |
| 10 | +import { computeBBoxViewport } from 'common/Map/WarpedMap/core/helpers'; |
| 11 | +import { LAYER_GROUPS_ORDER, LAYERS } from 'config/layerOrder'; |
| 12 | +import ItineraryLayer from 'modules/trainschedule/components/ManageTrainSchedule/ManageTrainScheduleMap/ItineraryLayer'; |
| 13 | +import ItineraryMarkers, { |
| 14 | + type MarkerInformation, |
| 15 | +} from 'modules/trainschedule/components/ManageTrainSchedule/ManageTrainScheduleMap/ItineraryMarkers'; |
| 16 | +import { mapInitialState } from 'reducers/map'; |
| 17 | +import type { Viewport } from 'reducers/map'; |
| 18 | + |
| 19 | +type DefaultBaseMapProps = { |
| 20 | + mapId: string; |
| 21 | + infraId?: number; |
| 22 | + geometry?: GeoJsonLineString; |
| 23 | + pathStepMarkers?: MarkerInformation[]; |
| 24 | + isFeasible?: boolean; |
| 25 | +}; |
| 26 | + |
| 27 | +const ZOOM_DEFAULT = 5; |
| 28 | +const ZOOM_DELTA = 1.5; |
| 29 | + |
| 30 | +/** |
| 31 | + * Default base map used to display a path and some markers. |
| 32 | + * No interactions are available, except zoom in/out, pan and reset pitch/bearing. |
| 33 | + */ |
| 34 | +const DefaultBaseMap = ({ |
| 35 | + mapId, |
| 36 | + infraId, |
| 37 | + geometry, |
| 38 | + pathStepMarkers = [], |
| 39 | + isFeasible = true, |
| 40 | + children, |
| 41 | +}: PropsWithChildren<DefaultBaseMapProps>) => { |
| 42 | + const mapRef = useRef<MapRef | null>(null); |
| 43 | + const [viewPort, setViewPort] = useState(mapInitialState.viewport); |
| 44 | + |
| 45 | + const updateViewportChange = useCallback( |
| 46 | + (partialViewPort: Partial<Viewport>) => |
| 47 | + setViewPort((prev) => ({ |
| 48 | + ...prev, |
| 49 | + ...partialViewPort, |
| 50 | + })), |
| 51 | + [] |
| 52 | + ); |
| 53 | + |
| 54 | + const resetPitchBearing = () => { |
| 55 | + updateViewportChange({ |
| 56 | + ...viewPort, |
| 57 | + bearing: 0, |
| 58 | + pitch: 0, |
| 59 | + }); |
| 60 | + }; |
| 61 | + |
| 62 | + const zoomIn = () => { |
| 63 | + updateViewportChange({ |
| 64 | + ...viewPort, |
| 65 | + zoom: (viewPort.zoom || ZOOM_DEFAULT) + ZOOM_DELTA, |
| 66 | + }); |
| 67 | + }; |
| 68 | + const zoomOut = () => { |
| 69 | + updateViewportChange({ |
| 70 | + ...viewPort, |
| 71 | + zoom: (viewPort.zoom || ZOOM_DEFAULT) - ZOOM_DELTA, |
| 72 | + }); |
| 73 | + }; |
| 74 | + |
| 75 | + useEffect(() => { |
| 76 | + const points = geometry ?? { |
| 77 | + coordinates: compact(pathStepMarkers.map((step) => step.coordinates)), |
| 78 | + type: 'LineString', |
| 79 | + }; |
| 80 | + if (points.coordinates.length >= 2) { |
| 81 | + const newViewport = computeBBoxViewport(bbox(points), viewPort); |
| 82 | + updateViewportChange(newViewport); |
| 83 | + } |
| 84 | + }, [geometry, pathStepMarkers]); |
| 85 | + |
| 86 | + return ( |
| 87 | + <> |
| 88 | + <MapButtons |
| 89 | + zoomIn={zoomIn} |
| 90 | + zoomOut={zoomOut} |
| 91 | + map={mapRef.current ?? undefined} |
| 92 | + resetPitchBearing={resetPitchBearing} |
| 93 | + bearing={viewPort.bearing} |
| 94 | + withMapKeyButton={false} |
| 95 | + withSearchButton={false} |
| 96 | + withToggleLayersButton={false} |
| 97 | + viewPort={viewPort} |
| 98 | + isNewButtons |
| 99 | + /> |
| 100 | + <BaseMap |
| 101 | + mapId={mapId} |
| 102 | + mapRef={mapRef} |
| 103 | + infraId={infraId} |
| 104 | + interactiveLayerIds={[]} |
| 105 | + mapStyle={mapInitialState.mapStyle} |
| 106 | + viewPort={viewPort} |
| 107 | + updatePartialViewPort={updateViewportChange} |
| 108 | + hideAttribution |
| 109 | + showOSM |
| 110 | + > |
| 111 | + <ItineraryLayer |
| 112 | + layerOrder={LAYER_GROUPS_ORDER[LAYERS.ITINERARY.GROUP]} |
| 113 | + geometry={geometry} |
| 114 | + isFeasible={isFeasible} |
| 115 | + showStdcmAssets |
| 116 | + /> |
| 117 | + {infraId && ( |
| 118 | + <ItineraryMarkers |
| 119 | + infraId={infraId} |
| 120 | + simulationPathSteps={pathStepMarkers} |
| 121 | + showStdcmAssets |
| 122 | + /> |
| 123 | + )} |
| 124 | + |
| 125 | + {children} |
| 126 | + </BaseMap> |
| 127 | + </> |
| 128 | + ); |
| 129 | +}; |
| 130 | + |
| 131 | +export default DefaultBaseMap; |
0 commit comments