Skip to content

Commit d9d4546

Browse files
authored
feat(data-modeling): bring back collapse all under a flag COMPASS-9504 (#7537)
1 parent a8dfc36 commit d9d4546

File tree

15 files changed

+226
-64
lines changed

15 files changed

+226
-64
lines changed

packages/compass-components/src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -244,6 +244,7 @@ export type {
244244
EdgeProps,
245245
NodeProps,
246246
DiagramProps,
247+
FieldId,
247248
DiagramInstance,
248249
NodeField,
249250
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
@@ -26,6 +26,7 @@ describe('DiagramCard', () => {
2626
displayPosition: [0, 0],
2727
shardKey: {},
2828
jsonSchema: { bsonType: 'object' },
29+
isExpanded: true,
2930
},
3031
],
3132
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: [],
@@ -78,13 +80,15 @@ const storageItems: MongoDBDataModelDescription[] = [
7880
displayPosition: [0, 0],
7981
shardKey: {},
8082
jsonSchema: { bsonType: 'object' },
83+
isExpanded: true,
8184
},
8285
{
8386
ns: 'db1.collection2',
8487
indexes: [],
8588
displayPosition: [0, 0],
8689
shardKey: {},
8790
jsonSchema: { bsonType: 'object' },
91+
isExpanded: true,
8892
},
8993
],
9094
relationships: [],

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

Lines changed: 39 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,13 @@ import {
2323
deleteRelationship,
2424
removeField,
2525
renameField,
26+
toggleCollectionExpanded,
2627
} from '../store/diagram';
2728
import type {
2829
EdgeProps,
2930
NodeProps,
3031
DiagramProps,
32+
FieldId,
3133
} from '@mongodb-js/compass-components';
3234
import {
3335
Banner,
@@ -58,6 +60,7 @@ import {
5860
relationshipToDiagramEdge,
5961
} from '../utils/nodes-and-edges';
6062
import toNS from 'mongodb-ns';
63+
import { usePreference } from 'compass-preferences-model/provider';
6164

6265
const loadingContainerStyles = css({
6366
width: '100%',
@@ -182,6 +185,7 @@ const DiagramContent: React.FunctionComponent<{
182185
}) => void;
183186
onRelationshipDrawn: () => void;
184187
DiagramComponent?: typeof Diagram;
188+
onToggleCollectionExpanded: (namespace: string) => void;
185189
}> = ({
186190
diagramLabel,
187191
database,
@@ -204,8 +208,10 @@ const DiagramContent: React.FunctionComponent<{
204208
onDeleteField,
205209
selectedItems,
206210
DiagramComponent = Diagram,
211+
onToggleCollectionExpanded,
207212
}) => {
208213
const isDarkMode = useDarkMode();
214+
const isCollapseFlagEnabled = usePreference('enableDataModelingCollapse');
209215
const diagram = useRef(useDiagram());
210216
const { openDrawer } = useDrawerActions();
211217
const { isDrawerOpen } = useDrawerState();
@@ -239,6 +245,7 @@ const DiagramContent: React.FunctionComponent<{
239245
: undefined,
240246
selected,
241247
isInRelationshipDrawingMode,
248+
isExpanded: coll.isExpanded,
242249
});
243250
});
244251
}, [
@@ -331,9 +338,24 @@ const DiagramContent: React.FunctionComponent<{
331338
);
332339

333340
const onFieldClick = useCallback(
334-
(_evt: React.MouseEvent, { id: fieldPath, nodeId: namespace }) => {
341+
(
342+
_evt: React.MouseEvent,
343+
{ id, nodeId: namespace }: { id: FieldId; nodeId: string }
344+
) => {
345+
// Diagramming package accepts both string ids and array of string ids for
346+
// fields (to represent the field path better). While all current code in
347+
// compass always uses array of strings as field id, some older saved
348+
// diagrams might not. Also handling this explicitly is sort of needed
349+
// anyway to convince typescript that we're doing the right thing
350+
const fieldPath = Array.isArray(id)
351+
? id
352+
: typeof id === 'string'
353+
? [id]
354+
: undefined;
355+
if (!fieldPath) {
356+
return;
357+
}
335358
_evt.stopPropagation(); // TODO(COMPASS-9659): should this be handled by the diagramming package?
336-
if (!Array.isArray(fieldPath)) return; // TODO(COMPASS-9659): could be avoided with generics in the diagramming package
337359
onFieldSelect(namespace, fieldPath);
338360
openDrawer(DATA_MODELING_DRAWER_ID);
339361
},
@@ -398,6 +420,15 @@ const DiagramContent: React.FunctionComponent<{
398420
[onDiagramBackgroundClicked]
399421
);
400422

423+
const handleNodeExpandedToggle = useCallback(
424+
(evt: React.MouseEvent, nodeId: string) => {
425+
evt.preventDefault();
426+
evt.stopPropagation();
427+
onToggleCollectionExpanded(nodeId);
428+
},
429+
[onToggleCollectionExpanded]
430+
);
431+
401432
const diagramProps: DiagramProps = useMemo(
402433
() =>
403434
({
@@ -414,6 +445,9 @@ const DiagramContent: React.FunctionComponent<{
414445
onFieldNameChange: onRenameField,
415446
onNodeDragStop,
416447
onConnect,
448+
onNodeExpandToggle: isCollapseFlagEnabled
449+
? handleNodeExpandedToggle
450+
: undefined,
417451
} satisfies DiagramProps),
418452
[
419453
isDarkMode,
@@ -429,6 +463,8 @@ const DiagramContent: React.FunctionComponent<{
429463
onRenameField,
430464
onNodeDragStop,
431465
onConnect,
466+
handleNodeExpandedToggle,
467+
isCollapseFlagEnabled,
432468
]
433469
);
434470

@@ -496,6 +532,7 @@ const ConnectedDiagramContent = connect(
496532
onDeleteCollection: deleteCollection,
497533
onDeleteRelationship: deleteRelationship,
498534
onDeleteField: removeField,
535+
onToggleCollectionExpanded: toggleCollectionExpanded,
499536
}
500537
)(DiagramContent);
501538

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: [],
@@ -57,6 +58,7 @@ const storageItems: MongoDBDataModelDescription[] = [
5758
displayPosition: [2, 2],
5859
shardKey: {},
5960
jsonSchema: { bsonType: 'object' },
61+
isExpanded: true,
6062
},
6163
],
6264
relationships: [],
@@ -84,6 +86,7 @@ const storageItems: MongoDBDataModelDescription[] = [
8486
displayPosition: [3, 3],
8587
shardKey: {},
8688
jsonSchema: { bsonType: 'object' },
89+
isExpanded: true,
8790
},
8891
],
8992
relationships: [],

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@ export const RelationshipSchema = z.object({
2727

2828
export type Relationship = z.output<typeof RelationshipSchema>;
2929

30+
export const DEFAULT_IS_EXPANDED = true;
31+
3032
const CollectionSchema = z.object({
3133
ns: z.string(),
3234
jsonSchema: z.custom<MongoDBJSONSchema>((value) => {
@@ -37,6 +39,7 @@ const CollectionSchema = z.object({
3739
shardKey: z.record(z.unknown()).optional(),
3840
displayPosition: z.tuple([z.number(), z.number()]),
3941
note: z.string().optional(),
42+
isExpanded: z.boolean().default(DEFAULT_IS_EXPANDED),
4043
});
4144

4245
export type DataModelCollection = z.output<typeof CollectionSchema>;
@@ -133,6 +136,10 @@ const EditSchemaVariants = z.discriminatedUnion('type', [
133136
field: FieldPathSchema,
134137
jsonSchema: z.custom<MongoDBJSONSchema>(),
135138
}),
139+
z.object({
140+
type: z.literal('ToggleExpandCollection'),
141+
ns: z.string(),
142+
}),
136143
]);
137144

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

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

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,10 @@ import type { DataModelingThunkAction } from './reducer';
44
import { analyzeDocuments, type MongoDBJSONSchema } from 'mongodb-schema';
55
import { getCurrentDiagramFromState } from './diagram';
66
import { UUID } from 'bson';
7-
import type { Relationship } from '../services/data-model-storage';
7+
import {
8+
DEFAULT_IS_EXPANDED,
9+
type Relationship,
10+
} from '../services/data-model-storage';
811
import { applyLayout } from '@mongodb-js/compass-components';
912
import {
1013
collectionToBaseNodeForLayout,
@@ -76,6 +79,7 @@ export type AnalysisFinishedAction = {
7679
ns: string;
7780
schema: MongoDBJSONSchema;
7881
position: { x: number; y: number };
82+
isExpanded: boolean;
7983
}[];
8084
relations: Relationship[];
8185
};
@@ -154,7 +158,7 @@ async function getInitialLayout({
154158
collections,
155159
relations,
156160
}: {
157-
collections: { ns: string; schema: MongoDBJSONSchema }[];
161+
collections: { ns: string; schema: MongoDBJSONSchema; isExpanded: boolean }[];
158162
relations: Relationship[];
159163
}) {
160164
const hasRelations = relations.length > 0;
@@ -163,6 +167,7 @@ async function getInitialLayout({
163167
ns: coll.ns,
164168
jsonSchema: coll.schema,
165169
displayPosition: [0, 0],
170+
isExpanded: coll.isExpanded,
166171
});
167172
});
168173
return await applyLayout({
@@ -250,7 +255,7 @@ export function startAnalysis(
250255
type: AnalysisProcessActionTypes.NAMESPACE_SCHEMA_ANALYZED,
251256
namespace: ns,
252257
});
253-
return { ns, schema, sample };
258+
return { ns, schema, sample, isExpanded: DEFAULT_IS_EXPANDED };
254259
})
255260
);
256261

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

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
1-
import type {
2-
DataModelCollection,
3-
Edit,
4-
Relationship,
5-
StaticModel,
1+
import {
2+
DEFAULT_IS_EXPANDED,
3+
type DataModelCollection,
4+
type Edit,
5+
type Relationship,
6+
type StaticModel,
67
} from '../services/data-model-storage';
78
import { updateSchema } from '../utils/schema-traversal';
89
import {
@@ -60,6 +61,7 @@ export function applyEdit(edit: Edit, model?: StaticModel): StaticModel {
6061
jsonSchema: edit.initialSchema,
6162
displayPosition: edit.position,
6263
indexes: [],
64+
isExpanded: DEFAULT_IS_EXPANDED,
6365
};
6466
return {
6567
...model,
@@ -271,6 +273,20 @@ export function applyEdit(edit: Edit, model?: StaticModel): StaticModel {
271273
}),
272274
};
273275
}
276+
case 'ToggleExpandCollection': {
277+
return {
278+
...model,
279+
collections: model.collections.map((collection) => {
280+
if (collection.ns !== edit.ns) {
281+
return collection;
282+
}
283+
return {
284+
...collection,
285+
isExpanded: !collection.isExpanded,
286+
};
287+
}),
288+
};
289+
}
274290
default: {
275291
return model;
276292
}

packages/compass-data-modeling/src/store/diagram.spec.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,13 +32,15 @@ const model: StaticModel = {
3232
displayPosition: [0, 0],
3333
shardKey: {},
3434
jsonSchema: { bsonType: 'object' },
35+
isExpanded: true,
3536
},
3637
{
3738
ns: 'db.collection2',
3839
indexes: [],
3940
displayPosition: [1, 1],
4041
shardKey: {},
4142
jsonSchema: { bsonType: 'object' },
43+
isExpanded: true,
4244
},
4345
],
4446
relationships: [
@@ -92,11 +94,13 @@ describe('Data Modeling store', function () {
9294
ns: 'db.collection1',
9395
schema: model.collections[0].jsonSchema,
9496
position: { x: 0, y: 0 },
97+
isExpanded: true,
9598
},
9699
{
97100
ns: 'db.collection2',
98101
schema: model.collections[1].jsonSchema,
99102
position: { x: 0, y: 0 },
103+
isExpanded: true,
100104
},
101105
],
102106
relations: model.relationships,
@@ -160,6 +164,7 @@ describe('Data Modeling store', function () {
160164
displayPosition: [0, 0],
161165
shardKey: {},
162166
jsonSchema: { bsonType: 'object' },
167+
isExpanded: true,
163168
},
164169
] as StaticModel['collections'],
165170
relationships: [] as StaticModel['relationships'],
@@ -455,6 +460,7 @@ describe('Data Modeling store', function () {
455460
field3: { bsonType: 'int' },
456461
},
457462
},
463+
isExpanded: true,
458464
},
459465
],
460466
relationships: [],
@@ -481,6 +487,7 @@ describe('Data Modeling store', function () {
481487
indexes: [],
482488
displayPosition: [0, 0],
483489
shardKey: {},
490+
isExpanded: true,
484491
jsonSchema: {
485492
bsonType: 'object',
486493
properties: {

0 commit comments

Comments
 (0)