Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

front: enhance pathfinding loading state management #10292

Open
wants to merge 1 commit into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ import StdcmConsist from './StdcmConsist';
import StdcmDestination from './StdcmDestination';
import StdcmLinkedTrainSearch from './StdcmLinkedTrainSearch';
import StdcmOrigin from './StdcmOrigin';
import useStaticPathfinding from '../../hooks/useStaticPathfinding';
import useStdcmPathfinding from '../../hooks/useStdcmPathfinding';
import type { StdcmConfigErrors } from '../../types';
import StdcmSimulationParams from '../StdcmSimulationParams';
import StdcmVias from './StdcmVias';
Expand Down Expand Up @@ -91,8 +91,7 @@ const StdcmConfig = ({
const maxSpeed = useSelector(getMaxSpeed);

const [showMessage, setShowMessage] = useState(false);

const { pathfinding, isPathFindingLoading } = useStaticPathfinding(infra);
const { isPathFindingLoading, pathfinding } = useStdcmPathfinding(infra);

const formRef = useRef<HTMLDivElement>(null);
const pathfindingBannerRef = useRef<HTMLDivElement>(null);
Expand Down Expand Up @@ -297,6 +296,7 @@ const StdcmConfig = ({
id="stdcm-map-config"
hideAttribution
hideItinerary
isPathfindingLoading={isPathFindingLoading}
preventPointSelection
pathGeometry={pathfinding?.geometry}
showStdcmAssets
Expand Down
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When a pathfinding is loading, it seems the loading banner is always green and not blue

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For a path like Brest > Narbonne > Perpignan (but should work for any path with a via), if I remove the origin, pathProperties endpoint is fired again (but it shouldn't, it's not the case right now) and bbox on map is updated with a path containing the via as if it was the origin and the destination.
When checking the network tab in dev tools, we see only pathProperties endpoint launching but in its payload, we can see that the track_section_ranges doesn't have the same length. So rtk query is probably relaunching pathfinding blocks as well in the dark.

Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,7 @@ import { useEffect, useMemo, useState } from 'react';
import { compact, isEqual } from 'lodash';
import { useSelector } from 'react-redux';

import {
osrdEditoastApi,
type InfraWithState,
type PathfindingResult,
} from 'common/api/osrdEditoastApi';
import { osrdEditoastApi, type InfraWithState } from 'common/api/osrdEditoastApi';
import { useOsrdConfSelectors } from 'common/osrdContext';
import usePathProperties from 'modules/pathfinding/hooks/usePathProperties';
import { getPathfindingQuery } from 'modules/pathfinding/utils';
Expand All @@ -24,17 +20,12 @@ function pathStepsToLocations(
return compact(pathSteps.map((s) => s.location));
}

const useStaticPathfinding = (infra?: InfraWithState) => {
const useStdcmPathfinding = (infra?: InfraWithState) => {
const { getStdcmPathSteps } = useOsrdConfSelectors() as StdcmConfSelectors;
const pathSteps = useSelector(getStdcmPathSteps);
const [pathStepsLocations, setPathStepsLocations] = useState(pathStepsToLocations(pathSteps));
const { rollingStock } = useStoreDataForRollingStockSelector();

const [pathfinding, setPathfinding] = useState<PathfindingResult>();

const [postPathfindingBlocks, { isFetching }] =
osrdEditoastApi.endpoints.postInfraByInfraIdPathfindingBlocks.useLazyQuery();

// When pathSteps changed
// => update the pathStepsLocations (if needed by doing a deep comparison).
useEffect(() => {
Expand All @@ -45,56 +36,40 @@ const useStaticPathfinding = (infra?: InfraWithState) => {
});
}, [pathSteps]);

const pathfindingPayload = useMemo(() => {
if (infra?.state !== 'CACHED' || pathStepsLocations.length < 2) {
return null;
}
return getPathfindingQuery({
infraId: infra.id,
rollingStock,
pathSteps: pathStepsLocations,
});
}, [pathStepsLocations, rollingStock, infra]);

const { data: pathfinding, isFetching } =
osrdEditoastApi.endpoints.postInfraByInfraIdPathfindingBlocks.useQuery(pathfindingPayload!, {
skip: !pathfindingPayload,
});

const pathProperties = usePathProperties(
infra?.id,
pathfinding?.status === 'success' ? pathfinding : undefined,
['geometry']
);

useEffect(() => {
const launchPathfinding = async () => {
setPathfinding(undefined);
if (infra?.state !== 'CACHED' || !rollingStock || pathStepsLocations.length < 2) {
return;
}

// Don't run the pathfinding if the origin and destination are the same:
const origin = pathSteps.at(0)!;
const destination = pathSteps.at(-1)!;
if (
origin.location!.uic === destination.location!.uic &&
origin.location!.secondary_code === destination.location!.secondary_code
) {
return;
}

const payload = getPathfindingQuery({
infraId: infra.id,
rollingStock,
pathSteps: pathStepsLocations,
});

if (payload === null) {
return;
}

const pathfindingResult = await postPathfindingBlocks(payload).unwrap();

setPathfinding(pathfindingResult);
};

launchPathfinding();
}, [pathStepsLocations, rollingStock, infra]);

const result = useMemo(
const pathfindingResult = useMemo(
() =>
pathfinding
? { status: pathfinding.status, geometry: pathProperties?.geometry ?? undefined }
? {
status: pathfinding.status,
geometry: pathProperties?.geometry ?? undefined,
}
: null,
[pathfinding, pathProperties]
);

return { pathfinding: result, isPathFindingLoading: isFetching };
return { isPathFindingLoading: isFetching, pathfinding: pathfindingResult };
};

export default useStaticPathfinding;
export default useStdcmPathfinding;
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,6 @@ const Map = ({
const terrain3DExaggeration = useSelector(getTerrain3DExaggeration);
const { viewport, mapSearchMarker, mapStyle, showOSM, layersSettings } = useSelector(getMap);
const mapRef = useRef<MapRef | null>(null);
const mapContainer = useMemo(() => mapRef.current?.getContainer(), [mapRef.current]);

const pathGeometry = useMemo(
() => geometry || pathProperties?.geometry,
Expand Down Expand Up @@ -210,14 +209,10 @@ const Map = ({
type: 'LineString',
};
if (points.coordinates.length > 2) {
const newViewport = computeBBoxViewport(bbox(points), viewport, {
width: mapContainer?.clientWidth,
height: mapContainer?.clientHeight,
padding: 60,
});
const newViewport = computeBBoxViewport(bbox(points), viewport);
dispatch(updateViewport(newViewport));
}
}, [pathGeometry, simulationPathSteps, mapContainer]);
}, [pathGeometry, simulationPathSteps]);

return (
<>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import { useParams } from 'react-router-dom';
import captureMap from 'applications/operationalStudies/helpers/captureMap';
import type { ManageTrainSchedulePathProperties } from 'applications/operationalStudies/types';
import type { PathProperties } from 'common/api/osrdEditoastApi';
import { LoaderFill } from 'common/Loaders';
import MapButtons from 'common/Map/Buttons/MapButtons';
import { CUSTOM_ATTRIBUTION } from 'common/Map/const';
import colors from 'common/Map/Consts/colors';
Expand Down Expand Up @@ -53,6 +54,7 @@ type MapProps = {
setMapCanvas?: (mapCanvas: string) => void;
hideAttribution?: boolean;
hideItinerary?: boolean;
isPathfindingLoading?: boolean;
preventPointSelection?: boolean;
id: string;
simulationPathSteps: MarkerInformation[];
Expand All @@ -73,6 +75,7 @@ const NewMap = ({
setMapCanvas,
hideAttribution = false,
hideItinerary = false,
isPathfindingLoading = false,
preventPointSelection = false,
id,
simulationPathSteps,
Expand All @@ -99,6 +102,14 @@ const NewMap = ({
[pathProperties, geometry]
);

const mapRef = useRef<MapRef | null>(null);
const mapContainer = useMemo(() => mapRef.current?.getContainer(), [mapRef.current]);

const mapViewport = useMemo(
() => (pathGeometry ? computeBBoxViewport(bbox(pathGeometry), viewport) : viewport),
[pathGeometry, viewport]
);

const [mapIsLoaded, setMapIsLoaded] = useState(false);

const [snappedPoint, setSnappedPoint] = useState<Feature<Point> | undefined>();
Expand All @@ -116,8 +127,6 @@ const NewMap = ({
[]
);

const mapRef = useRef<MapRef | null>(null);

const scaleControlStyle = {
left: 20,
bottom: 20,
Expand Down Expand Up @@ -228,14 +237,19 @@ const NewMap = ({
coordinates: compact(simulationPathSteps.map((step) => step.coordinates)),
type: 'LineString',
};
if (points.coordinates.length > 2) {
const newViewport = computeBBoxViewport(bbox(points), viewport);
if (points.coordinates.length > 2 && !isPathfindingLoading) {
const newViewport = computeBBoxViewport(bbox(points), mapViewport, {
width: mapContainer?.clientWidth,
height: mapContainer?.clientHeight,
padding: 60,
});
updateViewportChange(newViewport);
}
}, [pathGeometry, simulationPathSteps]);
}, [pathGeometry, simulationPathSteps, mapContainer, isPathfindingLoading]);

return (
<>
{isPathfindingLoading && <LoaderFill />}
<MapButtons
zoomIn={zoomIn}
zoomOut={zoomOut}
Expand Down
Loading