Skip to content

Commit f3e5188

Browse files
committed
fitView, deps
1 parent c52ee56 commit f3e5188

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
@@ -37,6 +37,7 @@ import type { Edit, StaticModel } from '../services/data-model-storage';
3737
import { UUID } from 'bson';
3838
import DiagramEditorToolbar from './diagram-editor-toolbar';
3939
import ExportDiagramModal from './export-diagram-modal';
40+
import { useLogger } from '@mongodb-js/compass-logging/provider';
4041

4142
const loadingContainerStyles = css({
4243
width: '100%',
@@ -132,6 +133,7 @@ const DiagramEditor: React.FunctionComponent<{
132133
onApplyClick,
133134
onApplyInitialLayout,
134135
}) => {
136+
const { log, mongoLogId } = useLogger('COMPASS-DATA-MODELING-DIAGRAM-EDITOR');
135137
const isDarkMode = useDarkMode();
136138
const diagramContainerRef = useRef<HTMLDivElement | null>(null);
137139
const diagram = useDiagram();
@@ -217,19 +219,22 @@ const DiagramEditor: React.FunctionComponent<{
217219
edges,
218220
'LEFT_RIGHT'
219221
);
220-
setNodes(positionedNodes);
221222
onApplyInitialLayout(
222223
positionedNodes.reduce((obj, node) => {
223224
obj[node.id] = [node.position.x, node.position.y];
224225
return obj;
225226
}, {} as Record<string, [number, number]>)
226227
);
227228
} catch (err) {
228-
// eslint-disable-next-line no-console
229-
console.error('Error applying layout:', err);
229+
log.error(
230+
mongoLogId(1_001_000_001),
231+
'DiagramEditor',
232+
'Error applying layout:',
233+
err
234+
);
230235
}
231236
},
232-
[setNodes]
237+
[edges, log, mongoLogId, onApplyInitialLayout]
233238
);
234239

235240
useEffect(() => {
@@ -268,7 +273,16 @@ const DiagramEditor: React.FunctionComponent<{
268273
return;
269274
}
270275
setNodes(storedNodes);
271-
}, [model?.collections]);
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) {
282+
void diagram.fitView();
283+
nodesLoaded.current = true;
284+
}
285+
}, [nodesLoaded, nodes, diagram]);
272286

273287
let content;
274288

@@ -318,6 +332,10 @@ const DiagramEditor: React.FunctionComponent<{
318332
title={diagramLabel}
319333
edges={edges}
320334
nodes={nodes}
335+
fitViewOptions={{
336+
maxZoom: 1,
337+
minZoom: 0.25,
338+
}}
321339
onEdgeClick={(evt, edge) => {
322340
setApplyInput(
323341
JSON.stringify(

0 commit comments

Comments
 (0)