Skip to content

Commit ebb4b7f

Browse files
Anemypaula-stacho
authored andcommitted
fixup: seperate the two node creation functions
1 parent 915cb75 commit ebb4b7f

File tree

2 files changed

+57
-33
lines changed

2 files changed

+57
-33
lines changed

packages/compass-data-modeling/src/store/analysis-process.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import type { Document } from 'bson';
77
import type { AggregationCursor } from 'mongodb';
88
import type { Relationship } from '../services/data-model-storage';
99
import { applyLayout } from '@mongodb-js/diagramming';
10-
import { collectionToDiagramNode } from '../utils/nodes-and-edges';
10+
import { collectionToBaseNodeForLayout } from '../utils/nodes-and-edges';
1111

1212
export type AnalysisProcessState = {
1313
currentAnalysisOptions:
@@ -207,13 +207,13 @@ export function startAnalysis(
207207
}
208208

209209
const positioned = await applyLayout(
210-
collections.map((coll) => {
211-
return collectionToDiagramNode({
210+
collections.map((coll) =>
211+
collectionToBaseNodeForLayout({
212212
ns: coll.ns,
213213
jsonSchema: coll.schema,
214214
displayPosition: [0, 0],
215-
});
216-
}),
215+
})
216+
),
217217
[],
218218
'LEFT_RIGHT'
219219
);

packages/compass-data-modeling/src/utils/nodes-and-edges.tsx

Lines changed: 52 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import {
66
InlineDefinition,
77
css,
88
} from '@mongodb-js/compass-components';
9-
import type { NodeProps, EdgeProps } from '@mongodb-js/diagramming';
9+
import type { NodeProps, EdgeProps, BaseNode } from '@mongodb-js/diagramming';
1010
import type { MongoDBJSONSchema } from 'mongodb-schema';
1111
import type { SelectedItems } from '../store/diagram';
1212
import type {
@@ -126,37 +126,64 @@ export const getFieldsFromSchema = ({
126126
return fields;
127127
};
128128

129-
export function collectionToDiagramNode(
130-
coll: Pick<DataModelCollection, 'ns' | 'jsonSchema' | 'displayPosition'>,
131-
options: {
132-
highlightedFields?: Record<string, FieldPath[] | undefined>;
133-
selectedField?: FieldPath;
134-
onClickAddNewFieldToCollection?: () => void;
135-
selected?: boolean;
136-
isInRelationshipDrawingMode?: boolean;
137-
} = {}
138-
): NodeProps {
139-
const {
140-
highlightedFields = {},
141-
selectedField,
142-
onClickAddNewFieldToCollection,
143-
selected = false,
144-
isInRelationshipDrawingMode = false,
145-
} = options;
129+
/**
130+
* Create a base node to be used for positioning and measuring in node layouts.
131+
*/
132+
export function collectionToBaseNodeForLayout({
133+
ns,
134+
jsonSchema,
135+
displayPosition,
136+
}: Pick<
137+
DataModelCollection,
138+
'ns' | 'jsonSchema' | 'displayPosition'
139+
>): BaseNode & Pick<NodeProps, 'fields'> {
140+
return {
141+
id: ns,
142+
position: {
143+
x: displayPosition[0],
144+
y: displayPosition[1],
145+
},
146+
fields: getFieldsFromSchema({ jsonSchema }),
147+
};
148+
}
149+
150+
type CollectionWithRenderOptions = Pick<
151+
DataModelCollection,
152+
'ns' | 'jsonSchema' | 'displayPosition'
153+
> & {
154+
highlightedFields: Record<string, FieldPath[] | undefined>;
155+
selectedField?: FieldPath;
156+
selected: boolean;
157+
isInRelationshipDrawingMode: boolean;
158+
onClickAddNewFieldToCollection: () => void;
159+
};
146160

161+
export function collectionToDiagramNode({
162+
ns,
163+
jsonSchema,
164+
displayPosition,
165+
selectedField,
166+
highlightedFields,
167+
selected,
168+
isInRelationshipDrawingMode,
169+
onClickAddNewFieldToCollection,
170+
}: CollectionWithRenderOptions): NodeProps {
147171
return {
148-
id: coll.ns,
172+
id: ns,
149173
type: 'collection',
150174
position: {
151-
x: coll.displayPosition[0],
152-
y: coll.displayPosition[1],
175+
x: displayPosition[0],
176+
y: displayPosition[1],
153177
},
154-
title: toNS(coll.ns).collection,
178+
title: toNS(ns).collection,
155179
fields: getFieldsFromSchema({
156-
jsonSchema: coll.jsonSchema,
157-
highlightedFields: highlightedFields[coll.ns] ?? undefined,
180+
jsonSchema: jsonSchema,
181+
highlightedFields: highlightedFields[ns] ?? undefined,
158182
selectedField,
159183
}),
184+
selected,
185+
connectable: isInRelationshipDrawingMode,
186+
draggable: !isInRelationshipDrawingMode,
160187
actions: onClickAddNewFieldToCollection ? (
161188
<IconButton
162189
aria-label="Add Field"
@@ -170,9 +197,6 @@ export function collectionToDiagramNode(
170197
<PlusWithSquare />
171198
</IconButton>
172199
) : undefined,
173-
selected,
174-
connectable: isInRelationshipDrawingMode,
175-
draggable: !isInRelationshipDrawingMode,
176200
};
177201
}
178202

@@ -189,4 +213,4 @@ export function relationshipToDiagramEdge(
189213
markerEnd: target.cardinality === 1 ? 'one' : 'many',
190214
selected,
191215
};
192-
}
216+
}

0 commit comments

Comments
 (0)