-
Notifications
You must be signed in to change notification settings - Fork 46
/
Copy pathManchetteWithSpaceTimeChart.tsx
441 lines (396 loc) · 15.3 KB
/
ManchetteWithSpaceTimeChart.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
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
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
import { useMemo, useRef, useState, useCallback, useEffect } from 'react';
import { Slider } from '@osrd-project/ui-core';
import { KebabHorizontal, Iterations } from '@osrd-project/ui-icons';
import Manchette, { type WaypointMenuData } from '@osrd-project/ui-manchette';
import {
useManchettesWithSpaceTimeChart,
timeScaleToZoomValue,
DEFAULT_ZOOM_MS_PER_PX,
} from '@osrd-project/ui-manchette-with-spacetimechart';
import {
ConflictLayer,
PathLayer,
SpaceTimeChart,
WorkScheduleLayer,
OccupancyBlockLayer,
} from '@osrd-project/ui-spacetimechart';
import type { Conflict } from '@osrd-project/ui-spacetimechart';
import type {
SpaceTimeChartProps,
HoveredItem,
} from '@osrd-project/ui-spacetimechart/dist/lib/types';
import cx from 'classnames';
import { compact } from 'lodash';
import { createPortal } from 'react-dom';
import type { OperationalPoint, TrainSpaceTimeData } from 'applications/operationalStudies/types';
import upward from 'assets/pictures/workSchedules/ScheduledMaintenanceUp.svg';
import type { PostWorkSchedulesProjectPathApiResponse } from 'common/api/osrdEditoastApi';
import OSRDMenu from 'common/OSRDMenu';
import cutSpaceTimeRect from 'modules/simulationResult/components/SpaceTimeChart/helpers/utils';
import { ASPECT_LABELS_COLORS } from 'modules/simulationResult/consts';
import type {
AspectLabel,
LayerRangeData,
WaypointsPanelData,
} from 'modules/simulationResult/types';
import { updateSelectedTrainId } from 'reducers/simulationResults';
import { useAppDispatch } from 'store';
import SettingsPanel from './SettingsPanel';
import { getIdFromTrainPath, getPathStyle } from './utils';
import ManchetteMenuButton from '../SpaceTimeChart/ManchetteMenuButton';
import ProjectionLoadingMessage from '../SpaceTimeChart/ProjectionLoadingMessage';
import useWaypointMenu from '../SpaceTimeChart/useWaypointMenu';
import WaypointsPanel from '../SpaceTimeChart/WaypointsPanel';
type ManchetteWithSpaceTimeChartProps = {
operationalPoints: OperationalPoint[];
projectPathTrainResult: TrainSpaceTimeData[];
selectedTrainScheduleId?: number;
waypointsPanelData?: WaypointsPanelData;
conflicts?: Conflict[];
workSchedules?: PostWorkSchedulesProjectPathApiResponse;
projectionLoaderData: {
totalTrains: number;
allTrainsProjected: boolean;
};
handleTrainDrag?: (
draggedTrainId: number,
newDepartureTime: Date,
{ stopPanning }: { stopPanning: boolean }
) => Promise<void>;
height?: number;
onTrainClick?: (trainId: number) => void;
};
export const MANCHETTE_WITH_SPACE_TIME_CHART_DEFAULT_HEIGHT = 561;
const BOTTOM_TOOLBAR_HEIGHT = 40;
const SPACE_TIME_CHART_DIFF_HEIGHT = 8;
const ManchetteWithSpaceTimeChartWrapper = ({
operationalPoints,
projectPathTrainResult,
selectedTrainScheduleId,
waypointsPanelData,
conflicts = [],
workSchedules,
projectionLoaderData: { totalTrains, allTrainsProjected },
height = MANCHETTE_WITH_SPACE_TIME_CHART_DEFAULT_HEIGHT,
handleTrainDrag,
onTrainClick,
}: ManchetteWithSpaceTimeChartProps) => {
const dispatch = useAppDispatch();
const [chartKey, setChartKey] = useState(0);
useEffect(() => {
setChartKey((prev) => prev + 1);
}, [waypointsPanelData]);
const manchetteWithSpaceTimeCharWrappertRef = useRef<HTMLDivElement>(null);
const manchetteWithSpaceTimeChartRef = useRef<HTMLDivElement>(null);
const [hoveredItem, setHoveredItem] = useState<null | HoveredItem>(null);
const [draggingState, setDraggingState] = useState<{
draggedTrain: TrainSpaceTimeData;
initialDepartureTime: Date;
}>();
const spaceTimeChartRef = useRef<HTMLDivElement>(null);
const [waypointsPanelIsOpen, setWaypointsPanelIsOpen] = useState(false);
const [tmpSelectedTrain, setTmpSelectedTrain] = useState(selectedTrainScheduleId);
useEffect(() => {
setTmpSelectedTrain(selectedTrainScheduleId);
}, [selectedTrainScheduleId]);
// Cut the space time chart curves if the first or last waypoints are hidden
const { filteredProjectPathTrainResult: cutProjectedTrains, filteredConflicts: cutConflicts } =
useMemo(() => {
let filteredProjectPathTrainResult = projectPathTrainResult;
let filteredConflicts = conflicts;
if (!waypointsPanelData || waypointsPanelData.filteredWaypoints.length < 2)
return { filteredProjectPathTrainResult, filteredConflicts };
const { filteredWaypoints } = waypointsPanelData;
const firstPosition = filteredWaypoints.at(0)!.position;
const lastPosition = filteredWaypoints.at(-1)!.position;
if (firstPosition !== 0 || lastPosition !== operationalPoints.at(-1)!.position) {
filteredProjectPathTrainResult = projectPathTrainResult.map((train) => ({
...train,
spaceTimeCurves: train.spaceTimeCurves.map(({ positions, times }) => {
const cutPositions: number[] = [];
const cutTimes: number[] = [];
for (let i = 1; i < positions.length; i += 1) {
const currentRange: LayerRangeData = {
spaceStart: positions[i - 1],
spaceEnd: positions[i],
timeStart: times[i - 1],
timeEnd: times[i],
};
const interpolatedRange = cutSpaceTimeRect(currentRange, firstPosition, lastPosition);
// TODO : remove reformatting the datas when https://github.com/OpenRailAssociation/osrd-ui/issues/694 is merged
if (!interpolatedRange) continue;
if (i === 1 || cutPositions.length === 0) {
cutPositions.push(interpolatedRange.spaceStart);
cutTimes.push(interpolatedRange.timeStart);
}
cutPositions.push(interpolatedRange.spaceEnd);
cutTimes.push(interpolatedRange.timeEnd);
}
return {
positions: cutPositions,
times: cutTimes,
};
}),
signalUpdates: compact(
train.signalUpdates.map((signal) => {
const updatedSignalRange = cutSpaceTimeRect(
{
spaceStart: signal.position_start,
spaceEnd: signal.position_end,
timeStart: signal.time_start,
timeEnd: signal.time_end,
},
firstPosition,
lastPosition
);
if (!updatedSignalRange) return null;
// TODO : remove reformatting the datas when https://github.com/OpenRailAssociation/osrd-ui/issues/694 is merged
return {
...signal,
position_start: updatedSignalRange.spaceStart,
position_end: updatedSignalRange.spaceEnd,
time_start: updatedSignalRange.timeStart,
time_end: updatedSignalRange.timeEnd,
};
})
),
}));
filteredConflicts = compact(
conflicts.map((conflict) => cutSpaceTimeRect(conflict, firstPosition, lastPosition))
);
return { filteredProjectPathTrainResult, filteredConflicts };
}
return { filteredProjectPathTrainResult, filteredConflicts };
}, [waypointsPanelData?.filteredWaypoints, projectPathTrainResult, conflicts]);
const manchetteWaypoints = useMemo(() => {
const rawWaypoints = waypointsPanelData?.filteredWaypoints ?? operationalPoints;
return rawWaypoints.map((waypoint) => ({
id: waypoint.id,
position: waypoint.position,
name: waypoint.extensions?.identifier?.name,
secondaryCode: waypoint.extensions?.sncf?.ch,
weight: waypoint.weight ?? 0,
}));
}, [waypointsPanelData, operationalPoints]);
const { manchetteProps, spaceTimeChartProps, handleScroll, handleXZoom, xZoom } =
useManchettesWithSpaceTimeChart(
manchetteWaypoints,
cutProjectedTrains,
manchetteWithSpaceTimeChartRef,
tmpSelectedTrain,
height,
spaceTimeChartRef
);
const [showSettingsPanel, setShowSettingsPanel] = useState(false);
const [settings, setSettings] = useState({
showConflicts: false,
showSignalsStates: false,
});
const occupancyBlocks = cutProjectedTrains.flatMap((train) => {
const departureTime = train.departureTime.getTime();
return train.signalUpdates.map((block) => ({
timeStart: departureTime + block.time_start,
timeEnd: departureTime + block.time_end,
spaceStart: block.position_start,
spaceEnd: block.position_end,
color: ASPECT_LABELS_COLORS[block.aspect_label as AspectLabel],
}));
});
const onPanOverloaded: SpaceTimeChartProps['onPan'] = async (payload) => {
const { isPanning } = payload;
if (!handleTrainDrag) {
// if no handleTrainDrag, we pan normally
spaceTimeChartProps.onPan(payload);
return;
}
// if dragging
if (draggingState) {
const { draggedTrain, initialDepartureTime } = draggingState;
dispatch(updateSelectedTrainId(draggedTrain.id));
const timeDiff = payload.data.time - payload.initialData.time;
const newDeparture = new Date(initialDepartureTime.getTime() + timeDiff);
await handleTrainDrag(draggedTrain.id, newDeparture, { stopPanning: !isPanning });
// stop dragging if necessary
if (!isPanning) {
setDraggingState(undefined);
}
return;
}
// if not dragging, we check if we should start dragging
if (hoveredItem && 'pathId' in hoveredItem.element) {
const hoveredTrainId = getIdFromTrainPath(hoveredItem.element.pathId);
const train = projectPathTrainResult.find((res) => res.id === hoveredTrainId);
if (train) {
setTmpSelectedTrain(train.id);
setDraggingState({
draggedTrain: train,
initialDepartureTime: train.departureTime,
});
} else {
console.error(`No train found with id ${hoveredTrainId}`);
}
}
// if no hovered train, we pan normally
spaceTimeChartProps.onPan(payload);
};
const waypointMenuData = useWaypointMenu(waypointsPanelData);
const manchettePropsWithWaypointMenu = useMemo(
() => ({
...manchetteProps,
waypoints: manchetteProps.waypoints.map((waypoint) => ({
...waypoint,
onClick: waypointMenuData.handleWaypointClick,
})),
waypointMenuData: {
menu: <OSRDMenu menuRef={waypointMenuData.menuRef} items={waypointMenuData.menuItems} />,
activeWaypointId: waypointMenuData.activeWaypointId,
manchetteWrapperRef: manchetteWithSpaceTimeCharWrappertRef,
} as WaypointMenuData,
}),
[manchetteProps, waypointMenuData]
);
const handleHoveredChildUpdate: SpaceTimeChartProps['onHoveredChildUpdate'] = useCallback(
({ item }: { item: HoveredItem | null }) => {
setHoveredItem(item);
},
[setHoveredItem]
);
const handleClick: SpaceTimeChartProps['onClick'] = () => {
if (!draggingState && hoveredItem && 'pathId' in hoveredItem.element) {
if (selectedTrainScheduleId !== Number(hoveredItem.element.pathId)) {
const trainId = getIdFromTrainPath(hoveredItem.element.pathId);
onTrainClick?.(trainId);
}
}
};
return (
<div ref={manchetteWithSpaceTimeCharWrappertRef} className="manchette-space-time-chart-wrapper">
{waypointMenuData.activeWaypointId &&
manchetteWithSpaceTimeCharWrappertRef.current &&
createPortal(
<div
style={{
width: '100%',
position: 'absolute',
top: 0,
left: 0,
bottom: 0,
}}
/>,
manchetteWithSpaceTimeCharWrappertRef.current
)}
<div className="header">
{waypointsPanelData && (
<>
<ManchetteMenuButton setWaypointsPanelIsOpen={setWaypointsPanelIsOpen} />
{waypointsPanelIsOpen && (
<WaypointsPanel
waypointsPanelIsOpen={waypointsPanelIsOpen}
setWaypointsPanelIsOpen={setWaypointsPanelIsOpen}
waypoints={operationalPoints}
waypointsPanelData={waypointsPanelData}
/>
)}
</>
)}
{!allTrainsProjected && (
<ProjectionLoadingMessage
projectedTrainsNb={projectPathTrainResult.length}
totalTrains={totalTrains}
/>
)}
</div>
<div className="header-separator" />
<div
ref={manchetteWithSpaceTimeChartRef}
className={cx('manchette flex', {
'no-scroll': !!waypointMenuData.activeWaypointId,
})}
style={{ height }}
onScroll={handleScroll}
>
<Manchette {...manchettePropsWithWaypointMenu} height={height - BOTTOM_TOOLBAR_HEIGHT} />
<div
ref={spaceTimeChartRef}
className="space-time-chart-container"
style={{
bottom: 0,
left: 0,
top: 2,
height: height - SPACE_TIME_CHART_DIFF_HEIGHT,
}}
>
<div className="toolbar">
<button
type="button"
className="reset-button"
onClick={() => handleXZoom(timeScaleToZoomValue(DEFAULT_ZOOM_MS_PER_PX))}
>
<Iterations />
</button>
<button
type="button"
className="menu-button"
onClick={() => setShowSettingsPanel(true)}
>
<KebabHorizontal />
</button>
</div>
{showSettingsPanel && (
<SettingsPanel
settings={settings}
onChange={setSettings}
onClose={() => setShowSettingsPanel(false)}
/>
)}
<SpaceTimeChart
className="inset-0 absolute h-full"
key={chartKey}
height={height}
spaceOrigin={
(waypointsPanelData?.filteredWaypoints ?? operationalPoints).at(0)?.position || 0
}
timeOrigin={Math.min(...projectPathTrainResult.map((p) => +p.departureTime))}
{...spaceTimeChartProps}
onPan={onPanOverloaded}
onClick={handleClick}
onHoveredChildUpdate={handleHoveredChildUpdate}
>
{spaceTimeChartProps.paths.map((path) => (
<PathLayer
key={path.id}
path={path}
{...getPathStyle(hoveredItem, path, !!draggingState)}
/>
))}
{workSchedules && (
<WorkScheduleLayer
workSchedules={workSchedules.map((ws) => ({
type: ws.type,
timeStart: new Date(ws.start_date_time),
timeEnd: new Date(ws.end_date_time),
spaceRanges: ws.path_position_ranges.map(({ start, end }) => [start, end]),
}))}
imageUrl={upward}
/>
)}
{settings.showConflicts && <ConflictLayer conflicts={cutConflicts} />}
{settings.showSignalsStates && (
<OccupancyBlockLayer occupancyBlocks={occupancyBlocks} />
)}
</SpaceTimeChart>
</div>
</div>
<Slider
containerClassName="space-time-h-slider-container"
className="space-time-h-slider"
value={xZoom}
onChange={(e) => {
handleXZoom(Number(e.target.value));
}}
/>
</div>
/* TODO use margin or absolute to align with handle */
);
};
export default ManchetteWithSpaceTimeChartWrapper;