From 5a176ecf007f6088f87fe1a6caf9f8ef51a6d28b Mon Sep 17 00:00:00 2001 From: Clara Ni Date: Thu, 5 Dec 2024 14:15:31 +0100 Subject: [PATCH] front: center map on pathSteps if no path is found Signed-off-by: Clara Ni --- .../views/ManageTrainSchedule.tsx | 2 +- .../stdcm/components/StdcmResults/StdcmResults.tsx | 4 +--- .../ManageTrainScheduleMap/ItineraryMarkers.tsx | 13 ++++--------- .../components/ManageTrainSchedule/Map.tsx | 13 +++++++++---- 4 files changed, 15 insertions(+), 17 deletions(-) diff --git a/front/src/applications/operationalStudies/views/ManageTrainSchedule.tsx b/front/src/applications/operationalStudies/views/ManageTrainSchedule.tsx index e1558c60c74..1c6e725bbc1 100644 --- a/front/src/applications/operationalStudies/views/ManageTrainSchedule.tsx +++ b/front/src/applications/operationalStudies/views/ManageTrainSchedule.tsx @@ -122,7 +122,7 @@ const ManageTrainSchedule = ({ trainIdToEdit }: ManageTrainScheduleProps) => {
- + diff --git a/front/src/applications/stdcm/components/StdcmResults/StdcmResults.tsx b/front/src/applications/stdcm/components/StdcmResults/StdcmResults.tsx index 2550e7b1f5e..aebc149d6e5 100644 --- a/front/src/applications/stdcm/components/StdcmResults/StdcmResults.tsx +++ b/front/src/applications/stdcm/components/StdcmResults/StdcmResults.tsx @@ -67,9 +67,7 @@ const StcdmResults = ({ ); }, [outputs]); - const simulationPathSteps = hasSimulationResults - ? outputs.results.simulationPathSteps - : undefined; + const simulationPathSteps = hasSimulationResults ? outputs.results.simulationPathSteps : []; return ( <> diff --git a/front/src/modules/trainschedule/components/ManageTrainSchedule/ManageTrainScheduleMap/ItineraryMarkers.tsx b/front/src/modules/trainschedule/components/ManageTrainSchedule/ManageTrainScheduleMap/ItineraryMarkers.tsx index 82b21e0f8d8..cbba2530b6e 100644 --- a/front/src/modules/trainschedule/components/ManageTrainSchedule/ManageTrainScheduleMap/ItineraryMarkers.tsx +++ b/front/src/modules/trainschedule/components/ManageTrainSchedule/ManageTrainScheduleMap/ItineraryMarkers.tsx @@ -4,7 +4,6 @@ import type { Position } from '@turf/helpers'; import cx from 'classnames'; import type { Map } from 'maplibre-gl'; import { Marker } from 'react-map-gl/maplibre'; -import { useSelector } from 'react-redux'; import destinationSVG from 'assets/pictures/destination.svg'; import stdcmDestination from 'assets/pictures/mapMarkers/destination.svg'; @@ -12,7 +11,6 @@ import stdcmVia from 'assets/pictures/mapMarkers/intermediate-point.svg'; import stdcmOrigin from 'assets/pictures/mapMarkers/start.svg'; import originSVG from 'assets/pictures/origin.svg'; import viaSVG from 'assets/pictures/via.svg'; -import { useOsrdConfSelectors } from 'common/osrdContext'; import type { PathStep } from 'reducers/osrdconf/types'; import { getNearestTrack } from 'utils/mapHelper'; @@ -38,7 +36,7 @@ type MarkerInformation = { type ItineraryMarkersProps = { map: Map; - simulationPathSteps?: PathStep[]; + simulationPathSteps: PathStep[]; showStdcmAssets: boolean; }; @@ -88,12 +86,9 @@ const extractMarkerInformation = (pathSteps: (PathStep | null)[], showStdcmAsset }, [] as MarkerInformation[]); const ItineraryMarkers = ({ map, simulationPathSteps, showStdcmAssets }: ItineraryMarkersProps) => { - const { getPathSteps } = useOsrdConfSelectors(); - const pathSteps = useSelector(getPathSteps); - const markersInformation = useMemo( - () => extractMarkerInformation(simulationPathSteps || pathSteps, showStdcmAssets), - [simulationPathSteps, pathSteps, showStdcmAssets] + () => extractMarkerInformation(simulationPathSteps, showStdcmAssets), + [simulationPathSteps, showStdcmAssets] ); const getMarkerDisplayInformation = useCallback( @@ -173,7 +168,7 @@ const ItineraryMarkers = ({ map, simulationPathSteps, showStdcmAssets }: Itinera ); }), - [simulationPathSteps, pathSteps, showStdcmAssets] + [markersInformation, showStdcmAssets] ); return Markers; }; diff --git a/front/src/modules/trainschedule/components/ManageTrainSchedule/Map.tsx b/front/src/modules/trainschedule/components/ManageTrainSchedule/Map.tsx index 3afb9d6efce..d0cb144d925 100644 --- a/front/src/modules/trainschedule/components/ManageTrainSchedule/Map.tsx +++ b/front/src/modules/trainschedule/components/ManageTrainSchedule/Map.tsx @@ -2,6 +2,7 @@ import { type PropsWithChildren, useCallback, useEffect, useMemo, useRef, useSta import bbox from '@turf/bbox'; import type { Feature, Point } from 'geojson'; +import { compact } from 'lodash'; import type { MapLayerMouseEvent } from 'maplibre-gl'; import ReactMapGL, { AttributionControl, ScaleControl } from 'react-map-gl/maplibre'; import type { MapRef } from 'react-map-gl/maplibre'; @@ -63,7 +64,7 @@ type MapProps = { hideItinerary?: boolean; preventPointSelection?: boolean; mapId?: string; - simulationPathSteps?: PathStep[]; + simulationPathSteps: PathStep[]; showStdcmAssets?: boolean; isFeasible?: boolean; }; @@ -218,11 +219,15 @@ const Map = ({ }, []); useEffect(() => { - if (pathGeometry) { - const newViewport = computeBBoxViewport(bbox(pathGeometry), mapViewport); + const points = pathGeometry ?? { + coordinates: compact(simulationPathSteps.map((step) => step.coordinates)), + type: 'LineString', + }; + if (points.coordinates.length > 2) { + const newViewport = computeBBoxViewport(bbox(points), mapViewport); dispatch(updateViewport(newViewport)); } - }, [pathGeometry]); + }, [pathGeometry, simulationPathSteps]); return ( <>