-
Notifications
You must be signed in to change notification settings - Fork 46
/
Copy pathEditor.tsx
384 lines (359 loc) · 12.8 KB
/
Editor.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
import React, { FC, useCallback, useEffect, useMemo, useRef, useState } from 'react';
import { useDispatch, useSelector } from 'react-redux';
import { useTranslation } from 'react-i18next';
import { useNavigate, useParams } from 'react-router-dom';
import cx from 'classnames';
import { isNil, toInteger } from 'lodash';
import { MapRef } from 'react-map-gl/maplibre';
import './Editor.scss';
import 'common/Map/Map.scss';
import { useModal } from 'common/BootstrapSNCF/ModalSNCF';
import { LoaderState } from 'common/Loader';
import { loadDataModel, selectLayers, updateTotalsIssue } from 'reducers/editor';
import { updateInfraID } from 'reducers/osrdconf';
import { updateViewport, Viewport } from 'reducers/map';
import { getInfraID } from 'reducers/osrdconf/selectors';
import useKeyboardShortcuts from 'utils/hooks/useKeyboardShortcuts';
import MapSearch from 'common/Map/Search/MapSearch';
import Tipped from './components/Tipped';
import Map from './Map';
import NavButtons from './nav';
import EditorContext from './context';
import TOOLS from './tools/tools';
import TOOL_TYPES from './tools/toolTypes';
import { EditorState } from './tools/types';
import {
EditorContextType,
ExtendedEditorContextType,
FullTool,
Reducer,
} from './tools/editorContextTypes';
import { switchProps } from './tools/switchProps';
import { CommonToolState } from './tools/commonToolState';
import { useSwitchTypes } from './tools/switchEdition/types';
import InfraErrorMapControl from './components/InfraErrors/InfraErrorMapControl';
const Editor: FC = () => {
const { t } = useTranslation();
const dispatch = useDispatch();
const navigate = useNavigate();
const { openModal, closeModal } = useModal();
const mapRef = useRef<MapRef>(null);
const { urlInfra } = useParams();
const infraID = useSelector(getInfraID);
const editorState = useSelector((state: { editor: EditorState }) => state.editor);
const switchTypes = useSwitchTypes(infraID);
const { register } = useKeyboardShortcuts();
/* eslint-disable @typescript-eslint/no-explicit-any */
const [toolAndState, setToolAndState] = useState<FullTool<any>>({
tool: TOOLS[TOOL_TYPES.SELECTION],
state: TOOLS[TOOL_TYPES.SELECTION].getInitialState({ infraID, switchTypes }),
});
const [isSearchToolOpened, setIsSearchToolOpened] = useState(false);
const [renderingFingerprint, setRenderingFingerprint] = useState(Date.now());
const forceRender = useCallback(() => {
setRenderingFingerprint(Date.now());
}, [setRenderingFingerprint]);
const switchTool = useCallback(
({ toolType, toolState }: switchProps) => {
const tool = TOOLS[toolType];
const state = { ...tool.getInitialState({ infraID, switchTypes }), ...(toolState || {}) };
setToolAndState({
tool,
state,
});
},
[infraID, switchTypes, setToolAndState]
);
const setToolState = useCallback(
<S extends CommonToolState>(stateOrReducer: Partial<S> | Reducer<S>) => {
setToolAndState((s) => ({
...s,
state: {
...s.state,
...(typeof stateOrReducer === 'function' ? stateOrReducer(s.state) : stateOrReducer),
},
}));
},
[setToolAndState]
);
const resetState = useCallback(() => {
switchTool({ toolType: TOOL_TYPES.SELECTION, toolState: {} });
forceRender();
}, [switchTool, forceRender]);
const { mapStyle, viewport } = useSelector(
(state: { map: { mapStyle: string; viewport: Viewport } }) => state.map
);
const setViewport = useCallback(
(value: Partial<Viewport>) => {
dispatch(updateViewport(value));
},
[dispatch]
);
const context = useMemo<EditorContextType<CommonToolState>>(
() => ({
t,
openModal,
closeModal,
activeTool: toolAndState.tool,
state: toolAndState.state,
setState: setToolState,
switchTool,
forceRender,
renderingFingerprint,
}),
[
setToolState,
toolAndState,
openModal,
closeModal,
infraID,
t,
forceRender,
renderingFingerprint,
]
);
const extendedContext = useMemo<ExtendedEditorContextType<CommonToolState>>(
() => ({
...context,
dispatch,
editorState,
infraID,
switchTypes,
mapState: {
viewport,
mapStyle,
},
}),
[context, dispatch, editorState, mapStyle, infraID, switchTypes, viewport]
);
const actionsGroups = useMemo(
() =>
toolAndState.tool.actions
.map((group) =>
group.filter((action) => !action.isHidden || !action.isHidden(extendedContext))
)
.filter((group) => group.length),
[toolAndState.tool]
);
/**
* When the component mount
* => we load the data model
* => we check if url has no infra and the store one => navigate to the good url
*/
useEffect(() => {
// load the data model
dispatch(loadDataModel());
if (isNil(urlInfra) && !isNil(infraID)) {
navigate(`/editor/${infraID}`);
}
}, []);
/**
* When infra change in the url
* => change the state
* => reset editor state
*/
useEffect(() => {
resetState();
if (!isNil(urlInfra)) {
const infradID = toInteger(urlInfra);
dispatch(updateInfraID(infradID));
dispatch(updateTotalsIssue(infradID));
}
}, [urlInfra]);
// Lifecycle events on tools:
useEffect(() => {
if (toolAndState.tool.onMount) toolAndState.tool.onMount(extendedContext);
const layersList = toolAndState.tool.requiredLayers
? new Set([...editorState.editorLayers, ...toolAndState.tool.requiredLayers])
: editorState.editorLayers;
// Remove the errors layer for better visibility in the route tool
if (toolAndState.tool.id === 'route-edition') layersList.delete('errors');
dispatch(selectLayers(layersList));
return () => {
if (toolAndState.tool.onUnmount) toolAndState.tool.onUnmount(extendedContext);
};
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [toolAndState.tool]);
return (
<EditorContext.Provider value={extendedContext as EditorContextType<unknown>}>
<main
className={cx('editor-root mastcontainer mastcontainer-map', infraID && 'infra-selected')}
>
<div className="layout">
<div className="tool-box bg-primary">
{Object.values(TOOL_TYPES).map((toolType: TOOL_TYPES) => {
const tool = TOOLS[toolType];
const { id, icon: IconComponent, labelTranslationKey } = tool;
const label = t(labelTranslationKey);
return (
<Tipped key={id} mode="right">
<button
type="button"
className={cx(
'btn-rounded',
id === toolAndState.tool.id && 'active',
'editor-btn'
)}
onClick={() => {
switchTool({ toolType, toolState: {} });
}}
>
<span className="sr-only">{label}</span>
<IconComponent />
</button>
<span>{label}</span>
</Tipped>
);
})}
</div>
<div className="actions-box">
{actionsGroups.flatMap((actionsGroup, i, a) => {
const actions = actionsGroup.map((action) => {
const {
id,
icon: IconComponent,
labelTranslationKey,
isDisabled,
isActive,
onClick,
} = action;
const label = t(labelTranslationKey);
return (
<Tipped key={id} mode="right">
<button
key={id}
type="button"
className={cx(
'editor-btn',
'btn-rounded',
isActive && isActive(extendedContext) ? 'active' : ''
)}
onClick={() => {
if (onClick) {
onClick(extendedContext);
}
}}
disabled={isDisabled && isDisabled(extendedContext)}
>
<span className="sr-only">{label}</span>
<IconComponent />
</button>
<span>{label}</span>
</Tipped>
);
});
return i < a.length - 1
? [...actions, <div key={`separator-${i}`} className="separator" />]
: actions;
})}
</div>
{toolAndState.tool.leftPanelComponent && (
<div className="panel-box">
<toolAndState.tool.leftPanelComponent />
</div>
)}
<div className="map-wrapper">
<div className="map">
<Map
{...{
mapRef,
mapStyle,
viewport,
setViewport,
toolState: toolAndState.state,
activeTool: toolAndState.tool,
setToolState,
}}
/>
{isSearchToolOpened && (
<MapSearch
map={mapRef.current!}
closeMapSearchPopUp={() => setIsSearchToolOpened(false)}
/>
)}
<div className="nav-box">
{NavButtons.flatMap((navButtons, i, a) => {
const buttons = navButtons.map((navButton) => {
const {
id,
icon: IconComponent,
labelTranslationKey,
shortcut,
isDisabled,
isActive,
isBlink,
onClick,
} = navButton;
const label = t(labelTranslationKey);
const clickFunction = () => {
if (onClick && mapRef.current !== null) {
onClick(
{
navigate,
dispatch,
setViewport,
viewport,
openModal,
closeModal,
setIsSearchToolOpened,
editorState,
mapRef: mapRef.current,
},
{
activeTool: toolAndState.tool,
toolState: toolAndState.state,
setToolState,
switchTool,
}
);
}
};
if (shortcut) register({ ...shortcut, handler: clickFunction });
return (
<Tipped key={id} mode="left">
<button
id={id}
type="button"
className={cx(
'editor-btn',
'btn-rounded',
'shadow',
isActive && isActive(editorState) ? 'active' : '',
isBlink && isBlink(editorState, infraID)
? 'btn-map-infras-blinking'
: ''
)}
onClick={clickFunction}
disabled={isDisabled && isDisabled(editorState)}
>
<span className="sr-only">{label}</span>
<IconComponent />
</button>
<span>{label}</span>
</Tipped>
);
});
if (i < a.length - 1)
return buttons.concat([<div key={`separator-${i}`} className="separator" />]);
return buttons;
})}
</div>
{mapRef.current && editorState.editorLayers.has('errors') && (
<div className="error-box">
<InfraErrorMapControl mapRef={mapRef.current} switchTool={switchTool} />
</div>
)}
</div>
<div className="messages-bar border-left">
<div className="px-1">
{toolAndState.tool.messagesComponent && <toolAndState.tool.messagesComponent />}
</div>
</div>
</div>
</div>
<LoaderState />
</main>
</EditorContext.Provider>
);
};
export default Editor;