Skip to content

Commit

Permalink
front: avoid unecessary path finding call in stdcm
Browse files Browse the repository at this point in the history
fix #10144

Adding a state for pathStepsLocations and only changes its value when
there is a deep diff

Signed-off-by: Benoit Simard <[email protected]>
  • Loading branch information
sim51 committed Jan 8, 2025
1 parent 9109fb7 commit 64bac43
Showing 1 changed file with 21 additions and 6 deletions.
27 changes: 21 additions & 6 deletions front/src/applications/stdcm/hooks/useStaticPathfinding.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { useEffect, useMemo, useState } from 'react';

import { compact } from 'lodash';
import { compact, isEqual } from 'lodash';
import { useSelector } from 'react-redux';

import {
Expand All @@ -13,22 +13,37 @@ import usePathProperties from 'modules/pathfinding/hooks/usePathProperties';
import { getPathfindingQuery } from 'modules/pathfinding/utils';
import { useStoreDataForRollingStockSelector } from 'modules/rollingStock/components/RollingStockSelector/useStoreDataForRollingStockSelector';
import type { StdcmConfSelectors } from 'reducers/osrdconf/stdcmConf/selectors';
import type { StdcmPathStep } from 'reducers/osrdconf/types';

/**
* Compute the path items locations from the path steps
*/
function pathStepsToLocations(
pathSteps: StdcmPathStep[]
): Array<NonNullable<StdcmPathStep['location']>> {
return compact(pathSteps.map((s) => s.location));
}

const useStaticPathfinding = (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] =
osrdEditoastApi.endpoints.postInfraByInfraIdPathfindingBlocks.useLazyQuery();

const pathStepsLocations = useMemo(
() => compact(pathSteps.map((step) => step.location)),
[pathSteps]
);
// When pathSteps changed
// => update the pathStepsLocations (if needed by doing a deep comparison).
useEffect(() => {
setPathStepsLocations((prev) => {
const newSteps = pathStepsToLocations(pathSteps);
if (isEqual(prev, newSteps)) return prev;
return newSteps;
});
}, [pathSteps]);

const pathProperties = usePathProperties(
infra?.id,
Expand Down

0 comments on commit 64bac43

Please sign in to comment.