Skip to content

Commit b8f8fec

Browse files
committed
fitView, deps
1 parent a4d96d8 commit b8f8fec

File tree

1 file changed

+23
-5
lines changed

1 file changed

+23
-5
lines changed

packages/compass-data-modeling/src/components/diagram-editor.tsx

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ import {
3939
} from '@mongodb-js/diagramming';
4040
import type { Edit, StaticModel } from '../services/data-model-storage';
4141
import { UUID } from 'bson';
42+
import { useLogger } from '@mongodb-js/compass-logging/provider';
4243

4344
const loadingContainerStyles = css({
4445
width: '100%',
@@ -142,6 +143,7 @@ const DiagramEditor: React.FunctionComponent<{
142143
onApplyClick,
143144
onApplyInitialLayout,
144145
}) => {
146+
const { log, mongoLogId } = useLogger('COMPASS-DATA-MODELING-DIAGRAM-EDITOR');
145147
const isDarkMode = useDarkMode();
146148
const diagramContainerRef = useRef<HTMLDivElement | null>(null);
147149
const diagram = useDiagram();
@@ -227,19 +229,22 @@ const DiagramEditor: React.FunctionComponent<{
227229
edges,
228230
'LEFT_RIGHT'
229231
);
230-
setNodes(positionedNodes);
231232
onApplyInitialLayout(
232233
positionedNodes.reduce((obj, node) => {
233234
obj[node.id] = [node.position.x, node.position.y];
234235
return obj;
235236
}, {} as Record<string, [number, number]>)
236237
);
237238
} catch (err) {
238-
// eslint-disable-next-line no-console
239-
console.error('Error applying layout:', err);
239+
log.error(
240+
mongoLogId(1_001_000_001),
241+
'DiagramEditor',
242+
'Error applying layout:',
243+
err
244+
);
240245
}
241246
},
242-
[setNodes]
247+
[edges, log, mongoLogId, onApplyInitialLayout]
243248
);
244249

245250
useEffect(() => {
@@ -278,7 +283,16 @@ const DiagramEditor: React.FunctionComponent<{
278283
return;
279284
}
280285
setNodes(storedNodes);
281-
}, [model?.collections]);
286+
}, [model?.collections, edges, diagram, applyInitialLayout]);
287+
288+
const nodesLoaded = useRef(false);
289+
useEffect(() => {
290+
// call fitView once the nodes are ready
291+
if (!nodesLoaded.current && nodes.length > 0) {
292+
void diagram.fitView();
293+
nodesLoaded.current = true;
294+
}
295+
}, [nodesLoaded, nodes, diagram]);
282296

283297
let content;
284298

@@ -328,6 +342,10 @@ const DiagramEditor: React.FunctionComponent<{
328342
title={diagramLabel}
329343
edges={edges}
330344
nodes={nodes}
345+
fitViewOptions={{
346+
maxZoom: 1,
347+
minZoom: 0.25,
348+
}}
331349
onEdgeClick={(evt, edge) => {
332350
setApplyInput(
333351
JSON.stringify(

0 commit comments

Comments
 (0)