-
Notifications
You must be signed in to change notification settings - Fork 46
/
Copy pathuseScenarioData.ts
180 lines (155 loc) · 5.46 KB
/
useScenarioData.ts
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
import { useCallback, useEffect, useMemo, useState } from 'react';
import { keyBy, sortBy } from 'lodash';
import { useSelector } from 'react-redux';
import {
osrdEditoastApi,
type InfraWithState,
type ScenarioResponse,
type TimetableDetailedResult,
type TrainScheduleResult,
} from 'common/api/osrdEditoastApi';
import { setFailure } from 'reducers/main';
import {
getSelectedTrainId,
getTrainIdUsedForProjection,
} from 'reducers/simulationResults/selectors';
import { useAppDispatch } from 'store';
import { castErrorToFailure } from 'utils/error';
import { mapBy } from 'utils/types';
import useAutoUpdateProjection from './useAutoUpdateProjection';
import useLazyLoadTrains from './useLazyLoadTrains';
import usePathProjection from './usePathProjection';
import useSimulationResults from './useSimulationResults';
const useScenarioData = (
scenario: ScenarioResponse,
timetable: TimetableDetailedResult,
infra: InfraWithState
) => {
const dispatch = useAppDispatch();
const trainIdUsedForProjection = useSelector(getTrainIdUsedForProjection);
const selectedTrainId = useSelector(getSelectedTrainId);
const [trainSchedules, setTrainSchedules] = useState<TrainScheduleResult[]>();
const [trainIdsToFetch, setTrainIdsToFetch] = useState<number[]>();
const { data: rawTrainSchedules, error: fetchTrainSchedulesError } =
osrdEditoastApi.endpoints.postTrainSchedule.useQuery({
body: {
ids: timetable.train_ids,
},
});
const { data: conflicts } = osrdEditoastApi.endpoints.getTimetableByIdConflicts.useQuery({
id: scenario.timetable_id,
infraId: scenario.infra_id,
});
const projectionPath = usePathProjection(infra);
const simulationResults = useSimulationResults();
const {
trainScheduleSummariesById,
projectedTrainsById,
setTrainScheduleSummariesById,
setProjectedTrainsById,
allTrainsProjected,
} = useLazyLoadTrains({
infraId: scenario.infra_id,
trainIdsToFetch,
setTrainIdsToFetch,
path: projectionPath?.path,
trainSchedules,
});
const trainScheduleSummaries = useMemo(
() => sortBy(Array.from(trainScheduleSummariesById.values()), 'startTime'),
[trainScheduleSummariesById]
);
const projectedTrains = useMemo(
() => Array.from(projectedTrainsById.values()),
[projectedTrainsById]
);
const trainScheduleUsedForProjection = useMemo(
() => trainSchedules?.find((trainSchedule) => trainSchedule.id === trainIdUsedForProjection),
[trainIdUsedForProjection, trainSchedules]
);
useAutoUpdateProjection(infra, timetable.train_ids, trainScheduleSummaries);
useEffect(() => {
if (!rawTrainSchedules) {
setTrainSchedules(undefined);
} else {
const sortedTrainSchedules = sortBy(rawTrainSchedules, 'start_time');
setTrainSchedules(sortedTrainSchedules);
}
}, [rawTrainSchedules]);
// first load of the trainScheduleSummaries
useEffect(() => {
if (trainSchedules && infra.state === 'CACHED' && trainScheduleSummaries.length === 0) {
const trainIds = trainSchedules.map((trainSchedule) => trainSchedule.id);
setTrainIdsToFetch(trainIds);
}
}, [trainSchedules, infra.state]);
useEffect(() => {
if (fetchTrainSchedulesError) {
dispatch(setFailure(castErrorToFailure(fetchTrainSchedulesError)));
}
}, [fetchTrainSchedulesError]);
const upsertTrainSchedules = useCallback(
(trainSchedulesToUpsert: TrainScheduleResult[]) => {
setTrainSchedules((prev) => {
const newTrainSchedulesById = {
...keyBy(prev, 'id'),
...keyBy(trainSchedulesToUpsert, 'id'),
};
const newTrainSchedules = sortBy(Object.values(newTrainSchedulesById), 'start_time');
return newTrainSchedules;
});
setProjectedTrainsById((prev) => {
const newProjectedTrainsById = new Map(prev);
trainSchedulesToUpsert.forEach((trainSchedule) => {
newProjectedTrainsById.delete(trainSchedule.id);
});
return newProjectedTrainsById;
});
const sortedTrainSchedulesToUpsert = sortBy(trainSchedulesToUpsert, 'start_time');
setTrainIdsToFetch(sortedTrainSchedulesToUpsert.map((trainSchedule) => trainSchedule.id));
},
[trainSchedules]
);
const removeTrains = useCallback((_trainIdsToRemove: number[]) => {
setTrainSchedules((prev) => {
const trainSchedulesById = mapBy(prev, 'id');
_trainIdsToRemove.forEach((trainId) => {
trainSchedulesById.delete(trainId);
});
return Array.from(trainSchedulesById.values());
});
setTrainScheduleSummariesById((prev) => {
const newTrainScheduleSummariesById = new Map(prev);
_trainIdsToRemove.forEach((trainId) => {
newTrainScheduleSummariesById.delete(trainId);
});
return newTrainScheduleSummariesById;
});
setProjectedTrainsById((prev) => {
const newProjectedTrainsById = new Map(prev);
_trainIdsToRemove.forEach((trainId) => {
newProjectedTrainsById.delete(trainId);
});
return newProjectedTrainsById;
});
}, []);
return {
selectedTrainId,
trainScheduleSummaries,
trainSchedules,
projectionData:
trainScheduleUsedForProjection && projectionPath
? {
trainSchedule: trainScheduleUsedForProjection,
...projectionPath,
projectedTrains,
allTrainsProjected,
}
: undefined,
simulationResults,
conflicts,
removeTrains,
upsertTrainSchedules,
};
};
export default useScenarioData;