@@ -11,7 +11,7 @@ import './Editor.scss';
11
11
12
12
import { LoaderState } from '../../common/Loader' ;
13
13
import { NotificationsState } from '../../common/Notifications' ;
14
- import { EditorState , loadDataModel } from '../../reducers/editor' ;
14
+ import { EditorState , loadDataModel , reset } from '../../reducers/editor' ;
15
15
import { MainState , setFailure } from '../../reducers/main' ;
16
16
import { updateViewport } from '../../reducers/map' ;
17
17
import { updateInfraID } from '../../reducers/osrdconf' ;
@@ -24,6 +24,7 @@ import {
24
24
CommonToolState ,
25
25
EditorContextType ,
26
26
ExtendedEditorContextType ,
27
+ FullTool ,
27
28
ModalRequest ,
28
29
OSRDConf ,
29
30
Tool ,
@@ -36,8 +37,10 @@ const EditorUnplugged: FC<{ t: TFunction }> = ({ t }) => {
36
37
const editorState = useSelector ( ( state : { editor : EditorState } ) => state . editor ) ;
37
38
const { fullscreen } = useSelector ( ( state : { main : MainState } ) => state . main ) ;
38
39
/* eslint-disable @typescript-eslint/no-explicit-any */
39
- const [ activeTool , activateTool ] = useState < Tool < any > > ( TOOLS [ 0 ] ) ;
40
- const [ toolState , setToolState ] = useState < any > ( activeTool . getInitialState ( { osrdConf } ) ) ;
40
+ const [ toolAndState , setToolAndState ] = useState < FullTool < any > > ( {
41
+ tool : TOOLS [ 0 ] ,
42
+ state : TOOLS [ 0 ] . getInitialState ( { osrdConf } ) ,
43
+ } ) ;
41
44
const [ modal , setModal ] = useState < ModalRequest < any , any > | null > ( null ) ;
42
45
/* eslint-enable @typescript-eslint/no-explicit-any */
43
46
const openModal = useCallback (
@@ -48,6 +51,29 @@ const EditorUnplugged: FC<{ t: TFunction }> = ({ t }) => {
48
51
} ,
49
52
[ setModal ]
50
53
) ;
54
+ const switchTool = useCallback (
55
+ < S extends CommonToolState > ( tool : Tool < S > , partialState ?: Partial < S > ) => {
56
+ const state = { ...tool . getInitialState ( { osrdConf } ) , ...( partialState || { } ) } ;
57
+ setToolAndState ( {
58
+ tool,
59
+ state,
60
+ } ) ;
61
+ } ,
62
+ [ osrdConf , setToolAndState ]
63
+ ) ;
64
+ const setToolState = useCallback (
65
+ < S extends CommonToolState > ( state ?: S ) => {
66
+ setToolAndState ( ( s ) => ( {
67
+ ...s ,
68
+ state,
69
+ } ) ) ;
70
+ } ,
71
+ [ setToolAndState ]
72
+ ) ;
73
+ const resetState = useCallback ( ( ) => {
74
+ switchTool ( TOOLS [ 0 ] ) ;
75
+ dispatch ( reset ( ) ) ;
76
+ } , [ ] ) ;
51
77
52
78
const { infra } = useParams < { infra ?: string } > ( ) ;
53
79
const { mapStyle, viewport } = useSelector (
@@ -68,16 +94,12 @@ const EditorUnplugged: FC<{ t: TFunction }> = ({ t }) => {
68
94
closeModal : ( ) => {
69
95
setModal ( null ) ;
70
96
} ,
71
- activeTool,
72
- state : toolState ,
97
+ activeTool : toolAndState . tool ,
98
+ state : toolAndState . state ,
73
99
setState : setToolState ,
74
- switchTool < S extends CommonToolState > ( tool : Tool < S > , state ?: Partial < S > ) {
75
- const fullState = { ...tool . getInitialState ( { osrdConf } ) , ...( state || { } ) } ;
76
- activateTool ( tool ) ;
77
- setToolState ( fullState ) ;
78
- } ,
100
+ switchTool,
79
101
} ) ,
80
- [ activeTool , modal , osrdConf , t , toolState ]
102
+ [ toolAndState , modal , osrdConf , t ]
81
103
) ;
82
104
const extendedContext = useMemo < ExtendedEditorContextType < CommonToolState > > (
83
105
( ) => ( {
@@ -93,9 +115,15 @@ const EditorUnplugged: FC<{ t: TFunction }> = ({ t }) => {
93
115
[ context , dispatch , editorState , mapStyle , osrdConf , viewport ]
94
116
) ;
95
117
96
- const actionsGroups = activeTool . actions
97
- . map ( ( group ) => group . filter ( ( action ) => ! action . isHidden || ! action . isHidden ( extendedContext ) ) )
98
- . filter ( ( group ) => group . length ) ;
118
+ const actionsGroups = useMemo (
119
+ ( ) =>
120
+ toolAndState . tool . actions
121
+ . map ( ( group ) =>
122
+ group . filter ( ( action ) => ! action . isHidden || ! action . isHidden ( extendedContext ) )
123
+ )
124
+ . filter ( ( group ) => group . length ) ,
125
+ [ toolAndState . tool ]
126
+ ) ;
99
127
100
128
// Initial viewport:
101
129
useEffect ( ( ) => {
@@ -109,20 +137,23 @@ const EditorUnplugged: FC<{ t: TFunction }> = ({ t }) => {
109
137
useEffect ( ( ) => {
110
138
if ( infra && parseInt ( infra , 10 ) > 0 ) {
111
139
getInfrastructure ( parseInt ( infra , 10 ) )
112
- . then ( ( infrastructure ) => dispatch ( updateInfraID ( infrastructure . id ) ) )
140
+ . then ( ( infrastructure ) => {
141
+ dispatch ( updateInfraID ( infrastructure . id ) ) ;
142
+ resetState ( ) ;
143
+ } )
113
144
. catch ( ( ) => {
114
145
dispatch ( setFailure ( new Error ( t ( 'Editor.errors.infra-not-found' , { id : infra } ) ) ) ) ;
115
- dispatch ( updateViewport ( { } , `/editor/` ) ) ;
146
+ dispatch ( updateViewport ( { } , `/editor/` , false ) ) ;
116
147
} ) ;
117
148
} else if ( osrdConf . infraID ) {
118
- dispatch ( updateViewport ( { } , `/editor/${ osrdConf . infraID } ` ) ) ;
149
+ dispatch ( updateViewport ( { } , `/editor/${ osrdConf . infraID } ` , false ) ) ;
119
150
} else {
120
151
getInfrastructures ( )
121
152
. then ( ( infras ) => {
122
153
if ( infras && infras . length > 0 ) {
123
154
const infrastructure = infras [ 0 ] ;
124
155
dispatch ( updateInfraID ( infrastructure . id ) ) ;
125
- dispatch ( updateViewport ( { } , `/editor/${ infrastructure . id } ` ) ) ;
156
+ dispatch ( updateViewport ( { } , `/editor/${ infrastructure . id } ` , false ) ) ;
126
157
} else {
127
158
dispatch ( setFailure ( new Error ( t ( 'Editor.errors.no-infra-available' ) ) ) ) ;
128
159
}
@@ -135,13 +166,13 @@ const EditorUnplugged: FC<{ t: TFunction }> = ({ t }) => {
135
166
136
167
// Lifecycle events on tools:
137
168
useEffect ( ( ) => {
138
- if ( activeTool . onMount ) activeTool . onMount ( extendedContext ) ;
169
+ if ( toolAndState . tool . onMount ) toolAndState . tool . onMount ( extendedContext ) ;
139
170
140
171
return ( ) => {
141
- if ( activeTool . onUnmount ) activeTool . onUnmount ( extendedContext ) ;
172
+ if ( toolAndState . tool . onUnmount ) toolAndState . tool . onUnmount ( extendedContext ) ;
142
173
} ;
143
174
// eslint-disable-next-line react-hooks/exhaustive-deps
144
- } , [ activeTool ] ) ;
175
+ } , [ toolAndState . tool ] ) ;
145
176
146
177
return (
147
178
< EditorContext . Provider value = { extendedContext as EditorContextType < unknown > } >
@@ -158,10 +189,13 @@ const EditorUnplugged: FC<{ t: TFunction }> = ({ t }) => {
158
189
< Tipped key = { id } mode = "right" >
159
190
< button
160
191
type = "button"
161
- className = { cx ( 'btn-rounded' , id === activeTool . id && 'active' , 'editor-btn' ) }
192
+ className = { cx (
193
+ 'btn-rounded' ,
194
+ id === toolAndState . tool . id && 'active' ,
195
+ 'editor-btn'
196
+ ) }
162
197
onClick = { ( ) => {
163
- activateTool ( tool ) ;
164
- setToolState ( tool . getInitialState ( { osrdConf } ) ) ;
198
+ switchTool ( tool ) ;
165
199
} }
166
200
disabled = { isDisabled && isDisabled ( extendedContext ) }
167
201
>
@@ -216,21 +250,21 @@ const EditorUnplugged: FC<{ t: TFunction }> = ({ t }) => {
216
250
: actions ;
217
251
} ) }
218
252
</ div >
219
- { activeTool . leftPanelComponent && (
253
+ { toolAndState . tool . leftPanelComponent && (
220
254
< div className = "panel-box" >
221
- < activeTool . leftPanelComponent />
255
+ < toolAndState . tool . leftPanelComponent />
222
256
</ div >
223
257
) }
224
258
< div className = "map-wrapper" >
225
259
< div className = "map" >
226
260
< Map
227
261
{ ...{
228
- toolState,
229
- setToolState,
262
+ mapStyle,
230
263
viewport,
231
264
setViewport,
232
- activeTool,
233
- mapStyle,
265
+ toolState : toolAndState . state ,
266
+ activeTool : toolAndState . tool ,
267
+ setToolState,
234
268
} }
235
269
/>
236
270
@@ -279,7 +313,7 @@ const EditorUnplugged: FC<{ t: TFunction }> = ({ t }) => {
279
313
</ div >
280
314
</ div >
281
315
< div className = "messages-bar" >
282
- { activeTool . messagesComponent && < activeTool . messagesComponent /> }
316
+ { toolAndState . tool . messagesComponent && < toolAndState . tool . messagesComponent /> }
283
317
</ div >
284
318
</ div >
285
319
</ div >
0 commit comments