Skip to content

Commit 52f148e

Browse files
authored
feat(data-modeling): allow to expand collapse all fields in the node COMPASS-9504 (#7455)
* feat(data-modeling): allow to expand collapse all fields in the node * fix(data-modeling): correctly handle field id type variation * chore(data-modeling): update types and add a comment * chore(data-modeling, components): update diagramming to latest
1 parent d57efe3 commit 52f148e

File tree

15 files changed

+238
-81
lines changed

15 files changed

+238
-81
lines changed

package-lock.json

Lines changed: 17 additions & 17 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/compass-components/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@
9898
"@lg-chat/suggestions": "^0.2.3",
9999
"@lg-chat/title-bar": "^4.0.7",
100100
"@mongodb-js/compass-context-menu": "^0.2.13",
101-
"@mongodb-js/diagramming": "^1.8.0",
101+
"@mongodb-js/diagramming": "^2.1.0",
102102
"@react-aria/interactions": "^3.9.1",
103103
"@react-aria/utils": "^3.13.1",
104104
"@react-aria/visually-hidden": "^3.3.1",

packages/compass-components/src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -241,6 +241,7 @@ export {
241241
export type {
242242
EdgeProps,
243243
NodeProps,
244+
FieldId,
244245
DiagramInstance,
245246
NodeField,
246247
NodeGlyph,

packages/compass-data-modeling/src/components/diagram-card.spec.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ describe('DiagramCard', () => {
2525
displayPosition: [0, 0],
2626
shardKey: {},
2727
jsonSchema: { bsonType: 'object' },
28+
isExpanded: true,
2829
},
2930
],
3031
relationships: [],

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,13 +44,15 @@ const storageItems: MongoDBDataModelDescription[] = [
4444
displayPosition: [50, 50],
4545
shardKey: {},
4646
jsonSchema: { bsonType: 'object' },
47+
isExpanded: true,
4748
},
4849
{
4950
ns: 'db1.collection2',
5051
indexes: [],
5152
displayPosition: [150, 150],
5253
shardKey: {},
5354
jsonSchema: { bsonType: 'object' },
55+
isExpanded: true,
5456
},
5557
],
5658
relationships: [],
@@ -77,13 +79,15 @@ const storageItems: MongoDBDataModelDescription[] = [
7779
displayPosition: [0, 0],
7880
shardKey: {},
7981
jsonSchema: { bsonType: 'object' },
82+
isExpanded: true,
8083
},
8184
{
8285
ns: 'db1.collection2',
8386
indexes: [],
8487
displayPosition: [0, 0],
8588
shardKey: {},
8689
jsonSchema: { bsonType: 'object' },
90+
isExpanded: true,
8791
},
8892
],
8993
relationships: [],

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

Lines changed: 38 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,13 @@ import {
1919
createNewRelationship,
2020
addCollection,
2121
selectField,
22+
toggleCollectionExpanded,
2223
} from '../store/diagram';
23-
import type { EdgeProps, NodeProps } from '@mongodb-js/compass-components';
24+
import type {
25+
EdgeProps,
26+
NodeProps,
27+
FieldId,
28+
} from '@mongodb-js/compass-components';
2429
import {
2530
Banner,
2631
CancelLoader,
@@ -147,6 +152,7 @@ const DiagramContent: React.FunctionComponent<{
147152
}) => void;
148153
onRelationshipDrawn: () => void;
149154
DiagramComponent?: typeof Diagram;
155+
onToggleCollectionExpanded: (namespace: string) => void;
150156
}> = ({
151157
diagramLabel,
152158
database,
@@ -165,6 +171,7 @@ const DiagramContent: React.FunctionComponent<{
165171
onRelationshipDrawn,
166172
selectedItems,
167173
DiagramComponent = Diagram,
174+
onToggleCollectionExpanded,
168175
}) => {
169176
const isDarkMode = useDarkMode();
170177
const diagram = useRef(useDiagram());
@@ -210,6 +217,7 @@ const DiagramContent: React.FunctionComponent<{
210217
: undefined,
211218
selected,
212219
isInRelationshipDrawingMode,
220+
isExpanded: coll.isExpanded,
213221
});
214222
});
215223
}, [
@@ -292,9 +300,24 @@ const DiagramContent: React.FunctionComponent<{
292300
);
293301

294302
const onFieldClick = useCallback(
295-
(_evt: React.MouseEvent, { id: fieldPath, nodeId: namespace }) => {
303+
(
304+
_evt: React.MouseEvent,
305+
{ id, nodeId: namespace }: { id: FieldId; nodeId: string }
306+
) => {
307+
// Diagramming package accepts both string ids and array of string ids for
308+
// fields (to represent the field path better). While all current code in
309+
// compass always uses array of strings as field id, some older saved
310+
// diagrams might not. Also handling this explicitly is sort of needed
311+
// anyway to convinve typescript that we're doing the right thing
312+
const fieldPath = Array.isArray(id)
313+
? id
314+
: typeof id === 'string'
315+
? [id]
316+
: undefined;
317+
if (!fieldPath) {
318+
return;
319+
}
296320
_evt.stopPropagation(); // TODO(COMPASS-9659): should this be handled by the diagramming package?
297-
if (!Array.isArray(fieldPath)) return; // TODO(COMPASS-9659): could be avoided with generics in the diagramming package
298321
onFieldSelect(namespace, fieldPath);
299322
openDrawer(DATA_MODELING_DRAWER_ID);
300323
},
@@ -334,6 +357,15 @@ const DiagramContent: React.FunctionComponent<{
334357
[onAddFieldToObjectField]
335358
);
336359

360+
const handleNodeExpandedToggle = useCallback(
361+
(evt: React.MouseEvent, nodeId: string) => {
362+
evt.preventDefault();
363+
evt.stopPropagation();
364+
onToggleCollectionExpanded(nodeId);
365+
},
366+
[onToggleCollectionExpanded]
367+
);
368+
337369
const diagramProps = useMemo(
338370
() => ({
339371
isDarkMode,
@@ -348,6 +380,7 @@ const DiagramContent: React.FunctionComponent<{
348380
onFieldClick,
349381
onNodeDragStop,
350382
onConnect,
383+
onNodeExpandToggle: handleNodeExpandedToggle,
351384
}),
352385
[
353386
isDarkMode,
@@ -362,6 +395,7 @@ const DiagramContent: React.FunctionComponent<{
362395
onFieldClick,
363396
onNodeDragStop,
364397
onConnect,
398+
handleNodeExpandedToggle,
365399
]
366400
);
367401

@@ -424,6 +458,7 @@ const ConnectedDiagramContent = connect(
424458
onFieldSelect: selectField,
425459
onDiagramBackgroundClicked: selectBackground,
426460
onCreateNewRelationship: createNewRelationship,
461+
onToggleCollectionExpanded: toggleCollectionExpanded,
427462
}
428463
)(DiagramContent);
429464

packages/compass-data-modeling/src/components/saved-diagrams-list.spec.tsx

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ const storageItems: MongoDBDataModelDescription[] = [
3030
displayPosition: [1, 1],
3131
shardKey: {},
3232
jsonSchema: { bsonType: 'object' },
33+
isExpanded: true,
3334
},
3435
],
3536
relationships: [],
@@ -56,6 +57,7 @@ const storageItems: MongoDBDataModelDescription[] = [
5657
displayPosition: [2, 2],
5758
shardKey: {},
5859
jsonSchema: { bsonType: 'object' },
60+
isExpanded: true,
5961
},
6062
],
6163
relationships: [],
@@ -82,6 +84,7 @@ const storageItems: MongoDBDataModelDescription[] = [
8284
displayPosition: [3, 3],
8385
shardKey: {},
8486
jsonSchema: { bsonType: 'object' },
87+
isExpanded: true,
8588
},
8689
],
8790
relationships: [],

packages/compass-data-modeling/src/services/data-model-storage.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ const CollectionSchema = z.object({
3636
shardKey: z.record(z.unknown()).optional(),
3737
displayPosition: z.tuple([z.number(), z.number()]),
3838
note: z.string().optional(),
39+
isExpanded: z.boolean().default(false),
3940
});
4041

4142
export type DataModelCollection = z.output<typeof CollectionSchema>;
@@ -132,6 +133,10 @@ const EditSchemaVariants = z.discriminatedUnion('type', [
132133
field: FieldPathSchema,
133134
jsonSchema: z.custom<MongoDBJSONSchema>(),
134135
}),
136+
z.object({
137+
type: z.literal('ToggleExpandCollection'),
138+
ns: z.string(),
139+
}),
135140
]);
136141

137142
export const EditSchema = z.intersection(EditSchemaBase, EditSchemaVariants);

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

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -281,17 +281,18 @@ export function startAnalysis(
281281
throw cancelController.signal.reason;
282282
}
283283

284-
const positioned = await applyLayout(
285-
collections.map((coll) => {
284+
const positioned = await applyLayout({
285+
nodes: collections.map((coll) => {
286286
return collectionToBaseNodeForLayout({
287287
ns: coll.ns,
288288
jsonSchema: coll.schema,
289289
displayPosition: [0, 0],
290+
isExpanded: false,
290291
});
291292
}),
292-
[],
293-
'LEFT_RIGHT'
294-
);
293+
edges: [],
294+
direction: 'LEFT_RIGHT',
295+
});
295296

296297
dispatch({
297298
type: AnalysisProcessActionTypes.ANALYSIS_FINISHED,

packages/compass-data-modeling/src/store/apply-edit.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ export function applyEdit(edit: Edit, model?: StaticModel): StaticModel {
4646
jsonSchema: edit.initialSchema,
4747
displayPosition: edit.position,
4848
indexes: [],
49+
isExpanded: false,
4950
};
5051
return {
5152
...model,
@@ -245,6 +246,20 @@ export function applyEdit(edit: Edit, model?: StaticModel): StaticModel {
245246
}),
246247
};
247248
}
249+
case 'ToggleExpandCollection': {
250+
return {
251+
...model,
252+
collections: model.collections.map((collection) => {
253+
if (collection.ns !== edit.ns) {
254+
return collection;
255+
}
256+
return {
257+
...collection,
258+
isExpanded: !collection.isExpanded,
259+
};
260+
}),
261+
};
262+
}
248263
default: {
249264
return model;
250265
}

0 commit comments

Comments
 (0)