Skip to content

Commit d13247f

Browse files
committed
front: avoid unecessary path finding call in stdcm
fix #10144 Adding a state for pathStepsLocations and only changes its value when there is a deep diff Signed-off-by: Benoit Simard <contact@bsimard.com>
1 parent dd492f0 commit d13247f

File tree

2 files changed

+22
-7
lines changed

2 files changed

+22
-7
lines changed

front/src/applications/stdcm/components/StdcmForm/StdcmConfig.tsx

+1-1
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@ const StdcmConfig = ({
147147
);
148148
setFormErrors(formErrorsStatus);
149149
}
150-
}, [pathfinding]);
150+
}, [pathfinding, pathSteps, t]);
151151

152152
useEffect(() => {
153153
if (!isDebugMode) {

front/src/applications/stdcm/hooks/useStaticPathfinding.ts

+21-6
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { useEffect, useMemo, useState } from 'react';
22

3-
import { compact } from 'lodash';
3+
import { compact, isEqual } from 'lodash';
44
import { useSelector } from 'react-redux';
55

66
import {
@@ -13,22 +13,37 @@ import usePathProperties from 'modules/pathfinding/hooks/usePathProperties';
1313
import { getPathfindingQuery } from 'modules/pathfinding/utils';
1414
import { useStoreDataForRollingStockSelector } from 'modules/rollingStock/components/RollingStockSelector/useStoreDataForRollingStockSelector';
1515
import type { StdcmConfSelectors } from 'reducers/osrdconf/stdcmConf/selectors';
16+
import type { StdcmPathStep } from 'reducers/osrdconf/types';
17+
18+
/**
19+
* Compute the path items locations from the path steps
20+
*/
21+
function pathStepsToLocations(
22+
pathSteps: StdcmPathStep[]
23+
): Array<NonNullable<StdcmPathStep['location']>> {
24+
return compact(pathSteps.map((s) => s.location));
25+
}
1626

1727
const useStaticPathfinding = (infra?: InfraWithState) => {
1828
const { getStdcmPathSteps } = useOsrdConfSelectors() as StdcmConfSelectors;
19-
2029
const pathSteps = useSelector(getStdcmPathSteps);
30+
const [pathStepsLocations, setPathStepsLocations] = useState(pathStepsToLocations(pathSteps));
2131
const { rollingStock } = useStoreDataForRollingStockSelector();
2232

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

2535
const [postPathfindingBlocks] =
2636
osrdEditoastApi.endpoints.postInfraByInfraIdPathfindingBlocks.useLazyQuery();
2737

28-
const pathStepsLocations = useMemo(
29-
() => compact(pathSteps.map((step) => step.location)),
30-
[pathSteps]
31-
);
38+
// When pathSteps changed
39+
// => update the pathStepsLocations (if needed by doing a deep comparison).
40+
useEffect(() => {
41+
setPathStepsLocations((prev) => {
42+
const newSteps = pathStepsToLocations(pathSteps);
43+
if (isEqual(prev, newSteps)) return prev;
44+
return newSteps;
45+
});
46+
}, [pathSteps]);
3247

3348
const pathProperties = usePathProperties(
3449
infra?.id,

0 commit comments

Comments
 (0)