-
Notifications
You must be signed in to change notification settings - Fork 46
/
Copy pathuseManageTrainScheduleContext.tsx
89 lines (75 loc) · 3.09 KB
/
useManageTrainScheduleContext.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
import { createContext, useContext, useMemo, type ReactNode, useState } from 'react';
import { compact } from 'lodash';
import { useSelector } from 'react-redux';
import type { InfraWithState } from 'common/api/osrdEditoastApi';
import { useOsrdConfSelectors } from 'common/osrdContext';
import type { RangedValue } from 'common/types';
import getPathVoltages from 'modules/pathfinding/helpers/getPathVoltages';
import usePathfinding from 'modules/pathfinding/hooks/usePathfinding';
import type { PathfindingState } from 'modules/pathfinding/types';
import { upsertPathStepsInOPs } from 'modules/pathfinding/utils';
import type { SuggestedOP } from 'modules/trainschedule/components/ManageTrainSchedule/types';
import type { PathStep } from 'reducers/osrdconf/types';
import type { ManageTrainSchedulePathProperties } from '../types';
type ManageTrainScheduleContextType = {
pathProperties?: ManageTrainSchedulePathProperties;
setPathProperties: (pathProperties?: ManageTrainSchedulePathProperties) => void;
voltageRanges: RangedValue[];
launchPathfinding: (pathSteps: (PathStep | null)[]) => void;
pathfindingState: PathfindingState;
infraInfo: { infra?: InfraWithState; reloadCount: number };
/** Operational points along the path (including origin and destination) and vias added by clicking on map */
pathStepsAndSuggestedOPs?: SuggestedOP[];
} | null;
const ManageTrainScheduleContext = createContext<ManageTrainScheduleContextType>(null);
type ManageTrainScheduleContextProviderProps = { children: ReactNode };
export const ManageTrainScheduleContextProvider = ({
children,
}: ManageTrainScheduleContextProviderProps) => {
const { getPathSteps } = useOsrdConfSelectors();
const pathSteps = useSelector(getPathSteps);
const [pathProperties, setPathProperties] = useState<ManageTrainSchedulePathProperties>();
const { launchPathfinding, pathfindingState, infraInfo } = usePathfinding(setPathProperties);
const voltageRanges = useMemo(
() => getPathVoltages(pathProperties?.electrifications, pathProperties?.length),
[pathProperties]
);
const pathStepsAndSuggestedOPs = useMemo(() => {
if (!pathProperties) return undefined;
return upsertPathStepsInOPs(pathProperties.suggestedOperationalPoints, compact(pathSteps));
}, [pathProperties?.suggestedOperationalPoints, pathSteps]);
const providedContext = useMemo(
() => ({
pathProperties,
setPathProperties,
voltageRanges,
launchPathfinding,
pathfindingState,
infraInfo,
pathStepsAndSuggestedOPs,
}),
[
pathProperties,
setPathProperties,
voltageRanges,
launchPathfinding,
pathfindingState,
infraInfo,
pathStepsAndSuggestedOPs,
]
);
return (
<ManageTrainScheduleContext.Provider value={providedContext}>
{children}
</ManageTrainScheduleContext.Provider>
);
};
export const useManageTrainScheduleContext = () => {
const context = useContext(ManageTrainScheduleContext);
if (!context) {
throw new Error(
'useManageTrainScheduleContext must be used within a ManageTrainScheduleContext'
);
}
return context;
};