-
Notifications
You must be signed in to change notification settings - Fork 70
Expand file tree
/
Copy pathTimesStopsOutput.tsx
More file actions
304 lines (271 loc) · 11.6 KB
/
TimesStopsOutput.tsx
File metadata and controls
304 lines (271 loc) · 11.6 KB
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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
import { useEffect, useMemo, useRef, useState } from 'react';
import cx from 'classnames';
import { useSelector } from 'react-redux';
import type { PathPropertiesFormatted } from 'applications/operationalStudies/types';
import {
getPowerRestrictionsWarnings,
countWarnings,
} from 'applications/operationalStudies/views/Scenario/components/ManageTimetableItem/PowerRestrictionsSelector/helpers/powerRestrictionWarnings';
import type {
CorePathfindingResultSuccess,
ReceptionSignal,
RollingStock,
SimulationResponseSuccess,
} from 'common/api/osrdEditoastApi';
import { matchPathStepAndOp } from 'modules/pathfinding/utils';
import { NO_POWER_RESTRICTION } from 'modules/powerRestriction/consts';
import type { SimulationSummary, TimetableItemWithDetails } from 'modules/timetableItem/types';
import type { TimetableItem, Train } from 'reducers/osrdconf/types';
import { getUseNewTimesStopsTable } from 'reducers/user/userSelectors';
import { formatLocalTime } from 'utils/date';
import { Duration } from 'utils/duration';
import { buildPowerRestrictionsFromRows, computeOptimisticRow } from './helpers/cellUpdate';
import { buildOpMatchParams } from './helpers/utils';
import useOutputTableData from './hooks/useOutputTableData';
import useTimesStopsTableData from './hooks/useTimesStopsTableData';
import useUpdateTimesStopsTable from './hooks/useUpdateTimesStopsTable';
import TimesStops from './TimesStops';
import TimesStopsTable from './TimesStopsTable';
import { TableType, type PendingEdit, type TimesStopsRow, type TimesStopsRowNew } from './types';
type TimesStopsOutputProps = {
infraId: number;
isValid?: boolean;
selectedTrain: Train;
timetableItemsWithDetails: TimetableItemWithDetails[];
upsertTimetableItems: (timetableItems: TimetableItem[]) => void;
simulatedTrain?: SimulationResponseSuccess['final_output'];
simulatedPath?: CorePathfindingResultSuccess;
simulatedPathItemTimes?: Extract<SimulationSummary, { isValid: true }>['pathItemTimes'];
simulatedPathItemRespect?: Extract<SimulationSummary, { isValid: true }>['pathItemRespect'];
operationalPointsOnPath?: PathPropertiesFormatted['operationalPoints'];
voltages?: PathPropertiesFormatted['voltages'];
isSimulationDataLoading?: boolean;
rollingStock?: RollingStock;
};
const TimesStopsOutput = ({
infraId,
isValid = false,
selectedTrain,
timetableItemsWithDetails,
upsertTimetableItems,
simulatedTrain,
simulatedPathItemTimes,
simulatedPathItemRespect,
operationalPointsOnPath,
voltages,
isSimulationDataLoading = false,
rollingStock,
}: TimesStopsOutputProps) => {
const useNewTimesStopsTable = useSelector(getUseNewTimesStopsTable);
// Refs used to track simulation refresh after a user edit (see isAwaitingSimulation):
// - preEditPathItemTimesRef: batch summary (simulatedPathItemTimes reference)
// - isTrainSimulationPendingRef: all simulation queries (isSimulationDataLoading)
const preEditPathItemTimesRef = useRef<typeof simulatedPathItemTimes>(undefined);
const isTrainSimulationPendingRef = useRef(false);
// Only call the hook that corresponds to the active table to avoid unnecessary computation
const legacyRows = useOutputTableData(
infraId,
isValid,
useNewTimesStopsTable ? undefined : selectedTrain,
useNewTimesStopsTable ? undefined : simulatedTrain,
useNewTimesStopsTable ? undefined : simulatedPathItemTimes,
useNewTimesStopsTable ? undefined : operationalPointsOnPath
);
const { rows: newRows, stableIsValid } = useTimesStopsTableData(
infraId,
isValid,
isSimulationDataLoading,
selectedTrain,
useNewTimesStopsTable ? simulatedTrain : undefined,
useNewTimesStopsTable ? simulatedPathItemTimes : undefined,
useNewTimesStopsTable ? simulatedPathItemRespect : undefined,
useNewTimesStopsTable ? operationalPointsOnPath : undefined
);
// Keeps the last edit visible until selectedTrain.schedule gets a new reference.
// Bridges the gap between the save request completing and the Redux update propagating through the tree.
// Note: useOptimistic was considered but doesn't work here. It reverts to the source state as soon
// as the async action resolves, but at that point selectedTrain.schedule hasn't been updated yet by
// Redux — causing the same flash. pinnedState stays active until the data itself changes.
const [pinnedState, setPinnedState] = useState<{
edit: PendingEdit;
forSchedule: Train['schedule'];
} | null>(null);
const optimisticEdit =
pinnedState !== null && pinnedState.forSchedule === selectedTrain.schedule
? pinnedState.edit
: null;
// The single source of truth for what the table displays. Any derived data fed to
// TimesStopsTable (warnings, styling, etc.) should be computed from optimisticRows,
// not from selectedTrain, to stay in sync with the displayed values during edits.
const optimisticRows = useMemo(
() =>
optimisticEdit
? newRows.map((row) =>
row.id === optimisticEdit.rowId
? { ...row, ...computeOptimisticRow(row, optimisticEdit) }
: row
)
: newRows,
[newRows, optimisticEdit]
);
const startTime = useMemo(() => new Date(selectedTrain.start_time), [selectedTrain.start_time]);
const availablePowerRestrictions = useMemo(
() => Object.keys(rollingStock?.power_restrictions ?? {}),
[rollingStock]
);
const { powerRestrictionWarningCount, incompatiblePowerRestrictionIds } = useMemo(() => {
const empty = {
powerRestrictionWarningCount: 0,
incompatiblePowerRestrictionIds: new Set<string>(),
};
// Built from optimisticRows (not selectedTrain.power_restrictions) so warnings
// update immediately when the user edits a cell, before the API round-trip completes.
const powerRestrictions = buildPowerRestrictionsFromRows(optimisticRows);
if (!voltages?.length || !rollingStock || !powerRestrictions.length) return empty;
const pathStepPositions = new Map<string, number>();
selectedTrain.path.forEach((pathStep) => {
const matchingOp = operationalPointsOnPath?.find((op) =>
matchPathStepAndOp(pathStep.location, buildOpMatchParams(op))
);
if (matchingOp) pathStepPositions.set(pathStep.id, matchingOp.position);
});
const rangesWithId = powerRestrictions.flatMap((pr) => {
if (pr.value === NO_POWER_RESTRICTION) return [];
const begin = pathStepPositions.get(pr.from);
const end = pathStepPositions.get(pr.to);
if (begin === undefined || end === undefined) return [];
return [{ begin, end, value: pr.value, fromId: pr.from }];
});
if (!rangesWithId.length) return empty;
const warnings = getPowerRestrictionsWarnings(
rangesWithId,
voltages,
rollingStock.effort_curves.modes
);
const warningRanges = [
...warnings.invalidCombinationWarnings,
...warnings.modeNotSupportedWarnings,
...warnings.missingPowerRestrictionWarnings,
];
const incompatibleIds = new Set<string>(
rangesWithId
.filter((pr) => warningRanges.some((w) => w.end > pr.begin && w.begin < pr.end))
.map((pr) => pr.fromId)
);
return {
powerRestrictionWarningCount: countWarnings(warnings),
incompatiblePowerRestrictionIds: incompatibleIds,
};
}, [optimisticRows, voltages, selectedTrain.path, operationalPointsOnPath, rollingStock]);
const {
updateArrival,
updateStopDuration,
updateDeparture,
updateReceptionSignal,
updatePowerRestrictions,
} = useUpdateTimesStopsTable(
selectedTrain,
newRows,
timetableItemsWithDetails,
upsertTimetableItems
);
// True if we are still waiting for fresh simulation data after a user edit.
// Both conditions must be false before we clear the loading state:
// - Condition 1 (batch summary): simulatedPathItemTimes must get a new reference.
// - Condition 2 (all simulation queries): isSimulationDataLoading must be false.
const isAwaitingSimulation =
(preEditPathItemTimesRef.current !== undefined &&
simulatedPathItemTimes === preEditPathItemTimesRef.current) ||
(isTrainSimulationPendingRef.current && isSimulationDataLoading);
// Reset refs once both simulation pipelines are done
useEffect(() => {
if (
!isAwaitingSimulation &&
(preEditPathItemTimesRef.current !== undefined || isTrainSimulationPendingRef.current)
) {
preEditPathItemTimesRef.current = undefined;
isTrainSimulationPendingRef.current = false;
}
}, [isAwaitingSimulation]);
const commitEdit = (edit: PendingEdit, updateFn: () => Promise<void>) => {
if (isAwaitingSimulation) return;
setPinnedState({ edit, forSchedule: selectedTrain.schedule });
preEditPathItemTimesRef.current = simulatedPathItemTimes;
isTrainSimulationPendingRef.current = true;
updateFn().catch(() => {
setPinnedState(null);
preEditPathItemTimesRef.current = undefined;
isTrainSimulationPendingRef.current = false;
});
};
const handleArrivalChange = (row: TimesStopsRowNew, arrival: Date | null) =>
commitEdit({ rowId: row.id, field: 'requestedArrival', value: arrival }, () =>
updateArrival(row, arrival)
);
const handleDepartureChange = (row: TimesStopsRowNew, departure: Date | null) =>
commitEdit({ rowId: row.id, field: 'requestedDeparture', value: departure }, () =>
updateDeparture(row, departure)
);
const handleStopDurationChange = (row: TimesStopsRowNew, durationSeconds: number | null) =>
commitEdit(
{
rowId: row.id,
field: 'stopDuration',
value: durationSeconds !== null ? new Duration({ seconds: durationSeconds }) : null,
},
() => updateStopDuration(row, durationSeconds)
);
const handleReceptionSignalChange = (
row: TimesStopsRowNew,
signal: ReceptionSignal | undefined
) => {
commitEdit({ rowId: row.id, field: 'receptionSignal', value: signal }, () =>
updateReceptionSignal(row, signal)
);
};
const handlePowerRestrictionChange = (row: TimesStopsRowNew, value: string | null) =>
commitEdit({ rowId: row.id, field: 'powerRestriction', value }, () =>
updatePowerRestrictions(row, value)
);
if (useNewTimesStopsTable) {
return (
<TimesStopsTable
rows={optimisticRows}
startTime={startTime}
isValid={stableIsValid}
isComputedDataPending={isAwaitingSimulation}
availablePowerRestrictions={availablePowerRestrictions}
powerRestrictionWarningCount={powerRestrictionWarningCount}
incompatiblePowerRestrictionIds={incompatiblePowerRestrictionIds}
onArrivalChange={handleArrivalChange}
onStopDurationChange={handleStopDurationChange}
onDepartureChange={handleDepartureChange}
onReceptionSignalChange={handleReceptionSignalChange}
onPowerRestrictionChange={handlePowerRestrictionChange}
/>
);
}
return (
<TimesStops
rows={legacyRows}
tableType={TableType.Output}
cellClassName={({ rowData: rowData_, columnId }) => {
const rowData = rowData_ as TimesStopsRow;
// TODO: compare Date objects rather than strings
const arrivalScheduleNotRespected =
rowData.arrival?.time && rowData.calculatedArrival
? formatLocalTime(rowData.calculatedArrival) !== rowData.arrival.time
: false;
const negativeDiffMargins = rowData.diffMargins && parseInt(rowData.diffMargins) < 0;
return cx({
'warning-schedule': arrivalScheduleNotRespected,
'warning-margin': negativeDiffMargins,
'secondary-code-column': columnId === 'ch',
});
}}
headerRowHeight={40}
dataIsLoading={newRows.length === 0}
/>
);
};
export default TimesStopsOutput;