-
Notifications
You must be signed in to change notification settings - Fork 46
/
Copy pathuseScenarioData.ts
211 lines (183 loc) · 6.69 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
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
/* eslint-disable @typescript-eslint/no-non-null-asserted-optional-chain */
import { useCallback, useEffect, useMemo, useState } from 'react';
import { keyBy, sortBy } from 'lodash';
import { useSelector } from 'react-redux';
import { osrdEditoastApi, type TrainScheduleResult } from 'common/api/osrdEditoastApi';
import useInfraStatus from 'modules/pathfinding/hooks/useInfraStatus';
import { setFailure } from 'reducers/main';
import {
updateSelectedTrainId,
updateTrainIdUsedForProjection,
} from 'reducers/osrdsimulation/actions';
import { getSelectedTrainId, getTrainIdUsedForProjection } from 'reducers/osrdsimulation/selectors';
import { useAppDispatch } from 'store';
import { castErrorToFailure } from 'utils/error';
import { mapBy } from 'utils/types';
import useLazyLoadTrains from './useLazyLoadTrains';
import useScenario from './useScenario';
import useSimulationResults from './useSimulationResults';
import selectTrainAndPathForProjection from '../helpers/selectTrainAndPathForProjection';
const useScenarioData = () => {
const dispatch = useAppDispatch();
const trainIdUsedForProjection = useSelector(getTrainIdUsedForProjection);
const selectedTrainId = useSelector(getSelectedTrainId);
const [trainSchedules, setTrainSchedules] = useState<TrainScheduleResult[]>();
const [trainIdsToFetch, setTrainIdsToFetch] = useState<number[]>();
const scenario = useScenario();
const { infra, isInfraLoaded, reloadCount } = useInfraStatus();
const { data: timetable } = osrdEditoastApi.endpoints.getTimetableById.useQuery(
{ id: scenario?.timetable_id! },
{
skip: !scenario,
}
);
const { data: rawTrainSchedules, error: fetchTrainSchedulesError } =
osrdEditoastApi.endpoints.postTrainSchedule.useQuery(
{
body: {
ids: timetable?.train_ids!,
},
},
{
skip: !timetable || !timetable.train_ids.length,
}
);
const { data: conflicts } = osrdEditoastApi.endpoints.getTimetableByIdConflicts.useQuery(
{ id: scenario?.timetable_id!, infraId: scenario?.infra_id! },
{ skip: !scenario }
);
const { data: projectionPath } = osrdEditoastApi.endpoints.getTrainScheduleByIdPath.useQuery(
{
id: trainIdUsedForProjection!,
infraId: scenario?.infra_id!,
},
{
skip: !scenario || !trainIdUsedForProjection,
}
);
const simulationResults = useSimulationResults();
const {
trainScheduleSummariesById,
projectedTrainsById,
setTrainScheduleSummariesById,
setProjectedTrainsById,
} = useLazyLoadTrains({
infraId: scenario?.infra_id,
trainIdsToFetch,
setTrainIdsToFetch,
path: projectionPath?.status === 'success' ? projectionPath : undefined,
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]
);
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]);
// TODO: refacto selectTrainAndPathForProjection
useEffect(() => {
if (trainSchedules && infra?.state === 'CACHED' && trainSchedules.length > 0) {
selectTrainAndPathForProjection(
trainSchedules.map((trainSchedule) => trainSchedule.id),
(id: number) => dispatch(updateSelectedTrainId(id)),
(id: number) => dispatch(updateTrainIdUsedForProjection(id)),
trainIdUsedForProjection,
selectedTrainId
);
}
}, [trainSchedules, infra]);
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 = (_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;
});
};
useEffect(() => {
dispatch(updateTrainIdUsedForProjection(undefined));
dispatch(updateSelectedTrainId(undefined));
}, []);
return scenario && timetable
? {
scenario,
timetable,
infraId: scenario.infra_id,
selectedTrainId,
infra: { infra, isInfraLoaded, reloadCount },
trainScheduleSummaries,
trainSchedules,
trainScheduleUsedForProjection,
trainIdUsedForProjection,
projectedTrains,
simulationResults,
conflicts,
removeTrains,
upsertTrainSchedules,
}
: undefined;
};
export default useScenarioData;