@@ -137,7 +137,7 @@ const DiagramEditor: React.FunctionComponent<{
137137 const isDarkMode = useDarkMode ( ) ;
138138 const diagramContainerRef = useRef < HTMLDivElement | null > ( null ) ;
139139 const diagram = useDiagram ( ) ;
140- const [ nodes , setNodes ] = useState < NodeProps [ ] > ( [ ] ) ;
140+ const [ areNodesReady , setAreNodesReady ] = useState ( false ) ;
141141
142142 const setDiagramContainerRef = useCallback (
143143 ( ref : HTMLDivElement | null ) => {
@@ -211,34 +211,31 @@ const DiagramEditor: React.FunctionComponent<{
211211 } ) ;
212212 } , [ model ?. relationships ] ) ;
213213
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 ] ) ;
239236
240- useEffect ( ( ) => {
241- const storedNodes = ( model ?. collections ?? [ ] ) . map (
237+ const nodes = useMemo < NodeProps [ ] > ( ( ) => {
238+ return ( model ?. collections ?? [ ] ) . map (
242239 ( coll ) : NodeProps => ( {
243240 id : coll . ns ,
244241 type : 'collection' ,
@@ -265,24 +262,22 @@ const DiagramEditor: React.FunctionComponent<{
265262 ) ,
266263 } )
267264 ) ;
268- const isInitialState = storedNodes . every (
265+ } , [ model ?. collections ] ) ;
266+
267+ useEffect ( ( ) => {
268+ if ( nodes . length === 0 ) return ;
269+ const isInitialState = nodes . every (
269270 ( node ) => node . position . x === - 1 && node . position . y === - 1
270271 ) ;
271272 if ( isInitialState ) {
272- void applyInitialLayout ( storedNodes ) ;
273+ void applyInitialLayout ( ) ;
273274 return ;
274275 }
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 ) {
282277 void diagram . fitView ( ) ;
283- nodesLoaded . current = true ;
278+ setAreNodesReady ( true ) ;
284279 }
285- } , [ nodesLoaded , nodes , diagram ] ) ;
280+ } , [ areNodesReady , nodes , diagram , applyInitialLayout ] ) ;
286281
287282 let content ;
288283
@@ -331,7 +326,7 @@ const DiagramEditor: React.FunctionComponent<{
331326 isDarkMode = { isDarkMode }
332327 title = { diagramLabel }
333328 edges = { edges }
334- nodes = { nodes }
329+ nodes = { areNodesReady ? nodes : [ ] }
335330 fitViewOptions = { {
336331 maxZoom : 1 ,
337332 minZoom : 0.25 ,
0 commit comments