1
1
import { useEffect , useMemo , useState } from 'react' ;
2
2
3
- import { compact } from 'lodash' ;
3
+ import { compact , isEqual } from 'lodash' ;
4
4
import { useSelector } from 'react-redux' ;
5
5
6
6
import {
@@ -13,22 +13,37 @@ import usePathProperties from 'modules/pathfinding/hooks/usePathProperties';
13
13
import { getPathfindingQuery } from 'modules/pathfinding/utils' ;
14
14
import { useStoreDataForRollingStockSelector } from 'modules/rollingStock/components/RollingStockSelector/useStoreDataForRollingStockSelector' ;
15
15
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
+ }
16
26
17
27
const useStaticPathfinding = ( infra ?: InfraWithState ) => {
18
28
const { getStdcmPathSteps } = useOsrdConfSelectors ( ) as StdcmConfSelectors ;
19
-
20
29
const pathSteps = useSelector ( getStdcmPathSteps ) ;
30
+ const [ pathStepsLocations , setPathStepsLocations ] = useState ( pathStepsToLocations ( pathSteps ) ) ;
21
31
const { rollingStock } = useStoreDataForRollingStockSelector ( ) ;
22
32
23
33
const [ pathfinding , setPathfinding ] = useState < PathfindingResult > ( ) ;
24
34
25
35
const [ postPathfindingBlocks ] =
26
36
osrdEditoastApi . endpoints . postInfraByInfraIdPathfindingBlocks . useLazyQuery ( ) ;
27
37
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 ] ) ;
32
47
33
48
const pathProperties = usePathProperties (
34
49
infra ?. id ,
0 commit comments