Skip to content

Commit acae18a

Browse files
authored
chore(data-modeling): memoize functions passed to diagramming (#7349)
1 parent 7f8d06c commit acae18a

File tree

1 file changed

+59
-28
lines changed

1 file changed

+59
-28
lines changed

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

Lines changed: 59 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,11 @@ const modelPreviewStyles = css({
104104
},
105105
});
106106

107+
const ZOOM_OPTIONS = {
108+
maxZoom: 1,
109+
minZoom: 0.25,
110+
};
111+
107112
type SelectedItems = NonNullable<DiagramState>['selectedItems'];
108113

109114
const DiagramContent: React.FunctionComponent<{
@@ -248,6 +253,53 @@ const DiagramContent: React.FunctionComponent<{
248253
[onRelationshipDrawn, onCreateNewRelationship]
249254
);
250255

256+
const onNodeClick = useCallback(
257+
(_evt: React.MouseEvent, node: NodeProps) => {
258+
if (node.type !== 'collection') {
259+
return;
260+
}
261+
onCollectionSelect(node.id);
262+
openDrawer(DATA_MODELING_DRAWER_ID);
263+
},
264+
[onCollectionSelect, openDrawer]
265+
);
266+
267+
const onEdgeClick = useCallback(
268+
(_evt: React.MouseEvent, edge: EdgeProps) => {
269+
onRelationshipSelect(edge.id);
270+
openDrawer(DATA_MODELING_DRAWER_ID);
271+
},
272+
[onRelationshipSelect, openDrawer]
273+
);
274+
275+
const onFieldClick = useCallback(
276+
(_evt: React.MouseEvent, { id: fieldPath, nodeId: namespace }) => {
277+
_evt.stopPropagation(); // TODO(COMPASS-9659): should this be handled by the diagramming package?
278+
if (!Array.isArray(fieldPath)) return; // TODO(COMPASS-9659): could be avoided with generics in the diagramming package
279+
onFieldSelect(namespace, fieldPath);
280+
openDrawer(DATA_MODELING_DRAWER_ID);
281+
},
282+
[onFieldSelect, openDrawer]
283+
);
284+
285+
const onNodeDragStop = useCallback(
286+
(evt: React.MouseEvent, node: NodeProps) => {
287+
onMoveCollection(node.id, [node.position.x, node.position.y]);
288+
},
289+
[onMoveCollection]
290+
);
291+
292+
const onPaneClick = useCallback(() => {
293+
onDiagramBackgroundClicked();
294+
}, [onDiagramBackgroundClicked]);
295+
296+
const onConnect = useCallback(
297+
({ source, target }: { source: string; target: string }) => {
298+
handleNodesConnect(source, target);
299+
},
300+
[handleNodesConnect]
301+
);
302+
251303
return (
252304
<div
253305
ref={setDiagramContainerRef}
@@ -263,34 +315,13 @@ const DiagramContent: React.FunctionComponent<{
263315
// With threshold too low clicking sometimes gets confused with
264316
// dragging
265317
nodeDragThreshold={5}
266-
onNodeClick={(_evt, node) => {
267-
if (node.type !== 'collection') {
268-
return;
269-
}
270-
onCollectionSelect(node.id);
271-
openDrawer(DATA_MODELING_DRAWER_ID);
272-
}}
273-
onPaneClick={onDiagramBackgroundClicked}
274-
onEdgeClick={(_evt, edge) => {
275-
onRelationshipSelect(edge.id);
276-
openDrawer(DATA_MODELING_DRAWER_ID);
277-
}}
278-
onFieldClick={(_evt, { id: fieldPath, nodeId: namespace }) => {
279-
_evt.stopPropagation(); // TODO(COMPASS-9659): should this be handled by the diagramming package?
280-
if (!Array.isArray(fieldPath)) return; // TODO(COMPASS-9659): could be avoided with generics in the diagramming package
281-
onFieldSelect(namespace, fieldPath);
282-
openDrawer(DATA_MODELING_DRAWER_ID);
283-
}}
284-
fitViewOptions={{
285-
maxZoom: 1,
286-
minZoom: 0.25,
287-
}}
288-
onNodeDragStop={(evt, node) => {
289-
onMoveCollection(node.id, [node.position.x, node.position.y]);
290-
}}
291-
onConnect={({ source, target }) => {
292-
handleNodesConnect(source, target);
293-
}}
318+
onNodeClick={onNodeClick}
319+
onPaneClick={onPaneClick}
320+
onEdgeClick={onEdgeClick}
321+
onFieldClick={onFieldClick}
322+
fitViewOptions={ZOOM_OPTIONS}
323+
onNodeDragStop={onNodeDragStop}
324+
onConnect={onConnect}
294325
/>
295326
</div>
296327
</div>

0 commit comments

Comments
 (0)