Skip to content

Commit

Permalink
front: center map on pathSteps if no path is found
Browse files Browse the repository at this point in the history
Signed-off-by: Clara Ni <clara.ni@outlook.fr>
  • Loading branch information
clarani committed Dec 11, 2024
1 parent e4f331e commit 5a176ec
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ const ManageTrainSchedule = ({ trainIdToEdit }: ManageTrainScheduleProps) => {
<div className="floating-itinerary">
<Itinerary pathProperties={pathProperties} setPathProperties={setPathProperties} />
</div>
<Map pathProperties={pathProperties}>
<Map pathProperties={pathProperties} simulationPathSteps={compact(pathSteps)}>
<IncompatibleConstraints pathProperties={pathProperties} />
</Map>
</div>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,9 +67,7 @@ const StcdmResults = ({
);
}, [outputs]);

const simulationPathSteps = hasSimulationResults
? outputs.results.simulationPathSteps
: undefined;
const simulationPathSteps = hasSimulationResults ? outputs.results.simulationPathSteps : [];

return (
<>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,13 @@ 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';
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';

Expand All @@ -38,7 +36,7 @@ type MarkerInformation = {

type ItineraryMarkersProps = {
map: Map;
simulationPathSteps?: PathStep[];
simulationPathSteps: PathStep[];
showStdcmAssets: boolean;
};

Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -173,7 +168,7 @@ const ItineraryMarkers = ({ map, simulationPathSteps, showStdcmAssets }: Itinera
</Marker>
);
}),
[simulationPathSteps, pathSteps, showStdcmAssets]
[markersInformation, showStdcmAssets]
);
return Markers;
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -63,7 +64,7 @@ type MapProps = {
hideItinerary?: boolean;
preventPointSelection?: boolean;
mapId?: string;
simulationPathSteps?: PathStep[];
simulationPathSteps: PathStep[];
showStdcmAssets?: boolean;
isFeasible?: boolean;
};
Expand Down Expand Up @@ -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 (
<>
Expand Down

0 comments on commit 5a176ec

Please sign in to comment.