-
Notifications
You must be signed in to change notification settings - Fork 46
/
Copy pathStdcmView.tsx
233 lines (204 loc) · 8.09 KB
/
StdcmView.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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
import { useEffect, useState } from 'react';
import { isEqual, isNil } from 'lodash';
import useStdcm from 'applications/stdcm/hooks/useStdcm';
import { LoaderFill } from 'common/Loaders';
import { useOsrdConfActions } from 'common/osrdContext';
import type { StdcmConfSliceActions } from 'reducers/osrdconf/stdcmConf';
import { useAppDispatch } from 'store';
import { replaceElementAtIndex } from 'utils/array';
import StdcmEmptyConfigError from '../components/StdcmEmptyConfigError';
import StdcmConfig from '../components/StdcmForm/StdcmConfig';
import StdcmHeader from '../components/StdcmHeader';
import StdcmHelpModule from '../components/StdcmHelpModule/StdcmHelpModule';
import StdcmResults from '../components/StdcmResults';
import StdcmStatusBanner from '../components/StdcmStatusBanner';
import useStdcmEnvironment, { NO_CONFIG_FOUND_MSG } from '../hooks/useStdcmEnv';
import useStdcmForm from '../hooks/useStdcmForm';
import type { StdcmSimulation } from '../types';
const StdcmView = () => {
// TODO : refacto. state useStdcm. Maybe we can merge some state together in order to reduce the number of refresh
const currentSimulationInputs = useStdcmForm();
const [simulationsList, setSimulationsList] = useState<StdcmSimulation[]>([]);
const [selectedSimulationIndex, setSelectedSimulationIndex] = useState(-1);
const [showStatusBanner, setShowStatusBanner] = useState(false);
const [retainedSimulationIndex, setRetainedSimulationIndex] = useState(-1);
const [showBtnToLaunchSimulation, setShowBtnToLaunchSimulation] = useState(false);
const [isDebugMode, setIsDebugMode] = useState(false);
const [showHelpModule, setShowHelpModule] = useState(false);
const {
launchStdcmRequest,
cancelStdcmRequest,
isPending,
isRejected,
isCanceled,
stdcmResults,
pathProperties,
stdcmTrainConflicts,
hasConflicts,
isCalculationFailed,
} = useStdcm({ showFailureNotification: false });
const { loading, error, loadStdcmEnvironment } = useStdcmEnvironment();
const dispatch = useAppDispatch();
const { resetStdcmConfig, updateStdcmConfigWithData } =
useOsrdConfActions() as StdcmConfSliceActions;
const selectedSimulation = simulationsList[selectedSimulationIndex];
const showResults = showStatusBanner || simulationsList.length > 0 || hasConflicts;
const handleRetainSimulation = () => setRetainedSimulationIndex(selectedSimulationIndex);
const handleSelectSimulation = (index: number) => {
if (retainedSimulationIndex === -1) {
setSelectedSimulationIndex(index);
setShowBtnToLaunchSimulation(false);
}
};
const handleStartNewQuery = () => {
setSimulationsList([]);
setSelectedSimulationIndex(-1);
setRetainedSimulationIndex(-1);
dispatch(resetStdcmConfig());
};
const toggleHelpModule = () => setShowHelpModule((show) => !show);
// reset config data with the selected simulation data
useEffect(() => {
if (selectedSimulation) {
const { pathSteps, consist } = selectedSimulation.inputs;
dispatch(
updateStdcmConfigWithData({
rollingStockID: consist?.tractionEngine?.id,
towedRollingStockID: consist?.towedRollingStock?.id,
totalLength: consist?.totalLength,
totalMass: consist?.totalMass,
maxSpeed: consist?.maxSpeed,
speedLimitByTag: consist?.speedLimitByTag,
stdcmPathSteps: pathSteps,
})
);
}
}, [selectedSimulation]);
useEffect(() => {
if (!isDebugMode) {
setShowBtnToLaunchSimulation(!isEqual(currentSimulationInputs, selectedSimulation?.inputs));
}
}, [currentSimulationInputs]);
useEffect(() => {
if (isPending && !isDebugMode) {
setShowBtnToLaunchSimulation(false);
}
}, [isPending]);
useEffect(() => {
if (!isDebugMode) {
loadStdcmEnvironment();
} else {
setShowBtnToLaunchSimulation(true);
}
}, [isDebugMode]);
useEffect(() => {
if (isCanceled) {
setShowBtnToLaunchSimulation(true);
}
}, [isCanceled]);
useEffect(() => {
/*
* Due to frequent re-renders and the fact that "speedSpaceChartData" is initially null before
* "formattedPathProperties" is computed, we need to check if the current simulation is already
* listed in the simulations list. This helps us determine whether to add a new simulation or update
* the existing one.
*/
const lastSimulation = simulationsList[simulationsList.length - 1];
const isSimulationAlreadyListed = isEqual(lastSimulation?.inputs, currentSimulationInputs);
const isSimulationOutputsComplete = stdcmResults?.stdcmResponse || hasConflicts;
if (isSimulationOutputsComplete) {
const newSimulation = {
...(isSimulationAlreadyListed
? { ...lastSimulation }
: {
id: simulationsList.length + 1,
creationDate: new Date(),
inputs: currentSimulationInputs,
}),
...(pathProperties && {
outputs: {
pathProperties,
...(stdcmResults?.stdcmResponse &&
stdcmResults?.speedSpaceChartData && {
results: stdcmResults.stdcmResponse,
speedSpaceChartData: stdcmResults.speedSpaceChartData,
}),
...(stdcmTrainConflicts && {
conflicts: stdcmTrainConflicts,
}),
},
}),
};
const updateSimulationsList = isSimulationAlreadyListed
? replaceElementAtIndex(simulationsList, simulationsList.length - 1, newSimulation)
: [...simulationsList, newSimulation];
setSimulationsList(updateSimulationsList as StdcmSimulation[]);
setShowStatusBanner(true);
}
}, [
pathProperties,
stdcmResults?.speedSpaceChartData?.formattedPathProperties,
stdcmTrainConflicts,
]);
// We have a simulation with an error.
useEffect(() => {
if (isRejected) {
setShowStatusBanner(true);
}
}, [isRejected]);
// select the last simulation in the list
useEffect(() => {
if (simulationsList.length > 0) {
setSelectedSimulationIndex(simulationsList.length - 1);
}
}, [simulationsList]);
// If we've got an error during the loading of the stdcm env which is not the "no config error" message,
// we let the error boundary manage it
if (error && error.message !== NO_CONFIG_FOUND_MSG) throw error;
return (
<div role="button" tabIndex={0} className="stdcm" onClick={() => setShowStatusBanner(false)}>
<StdcmHeader
isDebugMode={isDebugMode}
onDebugModeToggle={setIsDebugMode}
toggleHelpModule={toggleHelpModule}
showHelpModule={showHelpModule}
/>
{!isNil(error) ? (
<StdcmEmptyConfigError />
) : (
<div>
<StdcmConfig
isPending={isPending}
isDebugMode={isDebugMode}
showBtnToLaunchSimulation={showBtnToLaunchSimulation}
retainedSimulationIndex={retainedSimulationIndex}
launchStdcmRequest={launchStdcmRequest}
cancelStdcmRequest={cancelStdcmRequest}
/>
{showStatusBanner && <StdcmStatusBanner isFailed={isCalculationFailed} />}
{showResults && (
<div className="stdcm-results">
{(selectedSimulationIndex > -1 || hasConflicts) && (
<StdcmResults
isCalculationFailed={isCalculationFailed}
isDebugMode={isDebugMode}
onRetainSimulation={handleRetainSimulation}
onSelectSimulation={handleSelectSimulation}
onStartNewQuery={handleStartNewQuery}
retainedSimulationIndex={retainedSimulationIndex}
selectedSimulationIndex={selectedSimulationIndex}
showStatusBanner={showStatusBanner}
simulationsList={simulationsList}
pathTrackRanges={stdcmResults?.stdcmResponse.path.track_section_ranges}
/>
)}
</div>
)}
<StdcmHelpModule showHelpModule={showHelpModule} toggleHelpModule={toggleHelpModule} />
</div>
)}
{loading && <LoaderFill />}
</div>
);
};
export default StdcmView;