@@ -137,7 +137,7 @@ const DiagramEditor: React.FunctionComponent<{
137
137
const isDarkMode = useDarkMode ( ) ;
138
138
const diagramContainerRef = useRef < HTMLDivElement | null > ( null ) ;
139
139
const diagram = useDiagram ( ) ;
140
- const [ nodes , setNodes ] = useState < NodeProps [ ] > ( [ ] ) ;
140
+ const [ areNodesReady , setAreNodesReady ] = useState ( false ) ;
141
141
142
142
const setDiagramContainerRef = useCallback (
143
143
( ref : HTMLDivElement | null ) => {
@@ -211,34 +211,31 @@ const DiagramEditor: React.FunctionComponent<{
211
211
} ) ;
212
212
} , [ model ?. relationships ] ) ;
213
213
214
- const applyInitialLayout = useCallback (
215
- async ( storedNodes : NodeProps [ ] ) => {
216
- try {
217
- const { nodes : positionedNodes } = await applyLayout (
218
- storedNodes ,
219
- edges ,
220
- 'LEFT_RIGHT'
221
- ) ;
222
- onApplyInitialLayout (
223
- positionedNodes . reduce ( ( obj , node ) => {
224
- obj [ node . id ] = [ node . position . x , node . position . y ] ;
225
- return obj ;
226
- } , { } as Record < string , [ number , number ] > )
227
- ) ;
228
- } catch ( err ) {
229
- log . error (
230
- mongoLogId ( 1_001_000_361 ) ,
231
- 'DiagramEditor' ,
232
- 'Error applying layout:' ,
233
- err
234
- ) ;
235
- }
236
- } ,
237
- [ edges , log , mongoLogId , onApplyInitialLayout ]
238
- ) ;
214
+ const applyInitialLayout = useCallback ( async ( ) => {
215
+ try {
216
+ const { nodes : positionedNodes } = await applyLayout (
217
+ nodes ,
218
+ edges ,
219
+ 'LEFT_RIGHT'
220
+ ) ;
221
+ onApplyInitialLayout (
222
+ positionedNodes . reduce ( ( obj , node ) => {
223
+ obj [ node . id ] = [ node . position . x , node . position . y ] ;
224
+ return obj ;
225
+ } , { } as Record < string , [ number , number ] > )
226
+ ) ;
227
+ } catch ( err ) {
228
+ log . error (
229
+ mongoLogId ( 1_001_000_361 ) ,
230
+ 'DiagramEditor' ,
231
+ 'Error applying layout:' ,
232
+ err
233
+ ) ;
234
+ }
235
+ } , [ edges , log , mongoLogId , onApplyInitialLayout ] ) ;
239
236
240
- useEffect ( ( ) => {
241
- const storedNodes = ( model ?. collections ?? [ ] ) . map (
237
+ const nodes = useMemo < NodeProps [ ] > ( ( ) => {
238
+ return ( model ?. collections ?? [ ] ) . map (
242
239
( coll ) : NodeProps => ( {
243
240
id : coll . ns ,
244
241
type : 'collection' ,
@@ -265,24 +262,22 @@ const DiagramEditor: React.FunctionComponent<{
265
262
) ,
266
263
} )
267
264
) ;
268
- const isInitialState = storedNodes . every (
265
+ } , [ model ?. collections ] ) ;
266
+
267
+ useEffect ( ( ) => {
268
+ if ( nodes . length === 0 ) return ;
269
+ const isInitialState = nodes . every (
269
270
( node ) => node . position . x === - 1 && node . position . y === - 1
270
271
) ;
271
272
if ( isInitialState ) {
272
- void applyInitialLayout ( storedNodes ) ;
273
+ void applyInitialLayout ( ) ;
273
274
return ;
274
275
}
275
- setNodes ( storedNodes ) ;
276
- } , [ model ?. collections , edges , diagram , applyInitialLayout ] ) ;
277
-
278
- const nodesLoaded = useRef ( false ) ;
279
- useEffect ( ( ) => {
280
- // call fitView once the nodes are ready
281
- if ( ! nodesLoaded . current && nodes . length > 0 ) {
276
+ if ( ! areNodesReady ) {
282
277
void diagram . fitView ( ) ;
283
- nodesLoaded . current = true ;
278
+ setAreNodesReady ( true ) ;
284
279
}
285
- } , [ nodesLoaded , nodes , diagram ] ) ;
280
+ } , [ areNodesReady , nodes , diagram , applyInitialLayout ] ) ;
286
281
287
282
let content ;
288
283
@@ -331,7 +326,7 @@ const DiagramEditor: React.FunctionComponent<{
331
326
isDarkMode = { isDarkMode }
332
327
title = { diagramLabel }
333
328
edges = { edges }
334
- nodes = { nodes }
329
+ nodes = { areNodesReady ? nodes : [ ] }
335
330
fitViewOptions = { {
336
331
maxZoom : 1 ,
337
332
minZoom : 0.25 ,
0 commit comments