Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions packages/compass-components/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,7 @@ export type {
EdgeProps,
NodeProps,
DiagramProps,
FieldId,
DiagramInstance,
NodeField,
NodeGlyph,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ describe('DiagramCard', () => {
displayPosition: [0, 0],
shardKey: {},
jsonSchema: { bsonType: 'object' },
isExpanded: true,
},
],
relationships: [],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,13 +44,15 @@ const storageItems: MongoDBDataModelDescription[] = [
displayPosition: [50, 50],
shardKey: {},
jsonSchema: { bsonType: 'object' },
isExpanded: true,
},
{
ns: 'db1.collection2',
indexes: [],
displayPosition: [150, 150],
shardKey: {},
jsonSchema: { bsonType: 'object' },
isExpanded: true,
},
],
relationships: [],
Expand Down Expand Up @@ -78,13 +80,15 @@ const storageItems: MongoDBDataModelDescription[] = [
displayPosition: [0, 0],
shardKey: {},
jsonSchema: { bsonType: 'object' },
isExpanded: true,
},
{
ns: 'db1.collection2',
indexes: [],
displayPosition: [0, 0],
shardKey: {},
jsonSchema: { bsonType: 'object' },
isExpanded: true,
},
],
relationships: [],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,13 @@ import {
deleteRelationship,
removeField,
renameField,
toggleCollectionExpanded,
} from '../store/diagram';
import type {
EdgeProps,
NodeProps,
DiagramProps,
FieldId,
} from '@mongodb-js/compass-components';
import {
Banner,
Expand Down Expand Up @@ -58,6 +60,7 @@ import {
relationshipToDiagramEdge,
} from '../utils/nodes-and-edges';
import toNS from 'mongodb-ns';
import { usePreference } from 'compass-preferences-model/provider';

const loadingContainerStyles = css({
width: '100%',
Expand Down Expand Up @@ -182,6 +185,7 @@ const DiagramContent: React.FunctionComponent<{
}) => void;
onRelationshipDrawn: () => void;
DiagramComponent?: typeof Diagram;
onToggleCollectionExpanded: (namespace: string) => void;
}> = ({
diagramLabel,
database,
Expand All @@ -204,8 +208,10 @@ const DiagramContent: React.FunctionComponent<{
onDeleteField,
selectedItems,
DiagramComponent = Diagram,
onToggleCollectionExpanded,
}) => {
const isDarkMode = useDarkMode();
const isCollapseFlagEnabled = usePreference('enableDataModelingCollapse');
const diagram = useRef(useDiagram());
const { openDrawer } = useDrawerActions();
const { isDrawerOpen } = useDrawerState();
Expand Down Expand Up @@ -239,6 +245,7 @@ const DiagramContent: React.FunctionComponent<{
: undefined,
selected,
isInRelationshipDrawingMode,
isExpanded: coll.isExpanded,
});
});
}, [
Expand Down Expand Up @@ -331,9 +338,24 @@ const DiagramContent: React.FunctionComponent<{
);

const onFieldClick = useCallback(
(_evt: React.MouseEvent, { id: fieldPath, nodeId: namespace }) => {
(
_evt: React.MouseEvent,
{ id, nodeId: namespace }: { id: FieldId; nodeId: string }
) => {
// Diagramming package accepts both string ids and array of string ids for
// fields (to represent the field path better). While all current code in
// compass always uses array of strings as field id, some older saved
// diagrams might not. Also handling this explicitly is sort of needed
// anyway to convince typescript that we're doing the right thing
const fieldPath = Array.isArray(id)
? id
: typeof id === 'string'
? [id]
: undefined;
if (!fieldPath) {
return;
}
_evt.stopPropagation(); // TODO(COMPASS-9659): should this be handled by the diagramming package?
if (!Array.isArray(fieldPath)) return; // TODO(COMPASS-9659): could be avoided with generics in the diagramming package
onFieldSelect(namespace, fieldPath);
openDrawer(DATA_MODELING_DRAWER_ID);
},
Expand Down Expand Up @@ -398,6 +420,15 @@ const DiagramContent: React.FunctionComponent<{
[onDiagramBackgroundClicked]
);

const handleNodeExpandedToggle = useCallback(
(evt: React.MouseEvent, nodeId: string) => {
evt.preventDefault();
evt.stopPropagation();
onToggleCollectionExpanded(nodeId);
},
[onToggleCollectionExpanded]
);

const diagramProps: DiagramProps = useMemo(
() =>
({
Expand All @@ -414,6 +445,9 @@ const DiagramContent: React.FunctionComponent<{
onFieldNameChange: onRenameField,
onNodeDragStop,
onConnect,
onNodeExpandToggle: isCollapseFlagEnabled
? handleNodeExpandedToggle
: undefined,
} satisfies DiagramProps),
[
isDarkMode,
Expand All @@ -429,6 +463,8 @@ const DiagramContent: React.FunctionComponent<{
onRenameField,
onNodeDragStop,
onConnect,
handleNodeExpandedToggle,
isCollapseFlagEnabled,
]
);

Expand Down Expand Up @@ -496,6 +532,7 @@ const ConnectedDiagramContent = connect(
onDeleteCollection: deleteCollection,
onDeleteRelationship: deleteRelationship,
onDeleteField: removeField,
onToggleCollectionExpanded: toggleCollectionExpanded,
}
)(DiagramContent);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ const storageItems: MongoDBDataModelDescription[] = [
displayPosition: [1, 1],
shardKey: {},
jsonSchema: { bsonType: 'object' },
isExpanded: true,
},
],
relationships: [],
Expand Down Expand Up @@ -57,6 +58,7 @@ const storageItems: MongoDBDataModelDescription[] = [
displayPosition: [2, 2],
shardKey: {},
jsonSchema: { bsonType: 'object' },
isExpanded: true,
},
],
relationships: [],
Expand Down Expand Up @@ -84,6 +86,7 @@ const storageItems: MongoDBDataModelDescription[] = [
displayPosition: [3, 3],
shardKey: {},
jsonSchema: { bsonType: 'object' },
isExpanded: true,
},
],
relationships: [],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ export const RelationshipSchema = z.object({

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

export const DEFAULT_IS_EXPANDED = true;

const CollectionSchema = z.object({
ns: z.string(),
jsonSchema: z.custom<MongoDBJSONSchema>((value) => {
Expand All @@ -37,6 +39,7 @@ const CollectionSchema = z.object({
shardKey: z.record(z.unknown()).optional(),
displayPosition: z.tuple([z.number(), z.number()]),
note: z.string().optional(),
isExpanded: z.boolean().default(DEFAULT_IS_EXPANDED),
});

export type DataModelCollection = z.output<typeof CollectionSchema>;
Expand Down Expand Up @@ -133,6 +136,10 @@ const EditSchemaVariants = z.discriminatedUnion('type', [
field: FieldPathSchema,
jsonSchema: z.custom<MongoDBJSONSchema>(),
}),
z.object({
type: z.literal('ToggleExpandCollection'),
ns: z.string(),
}),
]);

export const EditSchema = z.intersection(EditSchemaBase, EditSchemaVariants);
Expand Down
11 changes: 8 additions & 3 deletions packages/compass-data-modeling/src/store/analysis-process.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@ import type { DataModelingThunkAction } from './reducer';
import { analyzeDocuments, type MongoDBJSONSchema } from 'mongodb-schema';
import { getCurrentDiagramFromState } from './diagram';
import { UUID } from 'bson';
import type { Relationship } from '../services/data-model-storage';
import {
DEFAULT_IS_EXPANDED,
type Relationship,
} from '../services/data-model-storage';
import { applyLayout } from '@mongodb-js/compass-components';
import {
collectionToBaseNodeForLayout,
Expand Down Expand Up @@ -76,6 +79,7 @@ export type AnalysisFinishedAction = {
ns: string;
schema: MongoDBJSONSchema;
position: { x: number; y: number };
isExpanded: boolean;
}[];
relations: Relationship[];
};
Expand Down Expand Up @@ -154,7 +158,7 @@ async function getInitialLayout({
collections,
relations,
}: {
collections: { ns: string; schema: MongoDBJSONSchema }[];
collections: { ns: string; schema: MongoDBJSONSchema; isExpanded: boolean }[];
relations: Relationship[];
}) {
const hasRelations = relations.length > 0;
Expand All @@ -163,6 +167,7 @@ async function getInitialLayout({
ns: coll.ns,
jsonSchema: coll.schema,
displayPosition: [0, 0],
isExpanded: coll.isExpanded,
});
});
return await applyLayout({
Expand Down Expand Up @@ -250,7 +255,7 @@ export function startAnalysis(
type: AnalysisProcessActionTypes.NAMESPACE_SCHEMA_ANALYZED,
namespace: ns,
});
return { ns, schema, sample };
return { ns, schema, sample, isExpanded: DEFAULT_IS_EXPANDED };
})
);

Expand Down
26 changes: 21 additions & 5 deletions packages/compass-data-modeling/src/store/apply-edit.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import type {
DataModelCollection,
Edit,
Relationship,
StaticModel,
import {
DEFAULT_IS_EXPANDED,
type DataModelCollection,
type Edit,
type Relationship,
type StaticModel,
} from '../services/data-model-storage';
import { updateSchema } from '../utils/schema-traversal';
import {
Expand Down Expand Up @@ -60,6 +61,7 @@ export function applyEdit(edit: Edit, model?: StaticModel): StaticModel {
jsonSchema: edit.initialSchema,
displayPosition: edit.position,
indexes: [],
isExpanded: DEFAULT_IS_EXPANDED,
};
return {
...model,
Expand Down Expand Up @@ -271,6 +273,20 @@ export function applyEdit(edit: Edit, model?: StaticModel): StaticModel {
}),
};
}
case 'ToggleExpandCollection': {
return {
...model,
collections: model.collections.map((collection) => {
if (collection.ns !== edit.ns) {
return collection;
}
return {
...collection,
isExpanded: !collection.isExpanded,
};
}),
};
}
default: {
return model;
}
Expand Down
7 changes: 7 additions & 0 deletions packages/compass-data-modeling/src/store/diagram.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,15 @@ const model: StaticModel = {
displayPosition: [0, 0],
shardKey: {},
jsonSchema: { bsonType: 'object' },
isExpanded: true,
},
{
ns: 'db.collection2',
indexes: [],
displayPosition: [1, 1],
shardKey: {},
jsonSchema: { bsonType: 'object' },
isExpanded: true,
},
],
relationships: [
Expand Down Expand Up @@ -92,11 +94,13 @@ describe('Data Modeling store', function () {
ns: 'db.collection1',
schema: model.collections[0].jsonSchema,
position: { x: 0, y: 0 },
isExpanded: true,
},
{
ns: 'db.collection2',
schema: model.collections[1].jsonSchema,
position: { x: 0, y: 0 },
isExpanded: true,
},
],
relations: model.relationships,
Expand Down Expand Up @@ -160,6 +164,7 @@ describe('Data Modeling store', function () {
displayPosition: [0, 0],
shardKey: {},
jsonSchema: { bsonType: 'object' },
isExpanded: true,
},
] as StaticModel['collections'],
relationships: [] as StaticModel['relationships'],
Expand Down Expand Up @@ -455,6 +460,7 @@ describe('Data Modeling store', function () {
field3: { bsonType: 'int' },
},
},
isExpanded: true,
},
],
relationships: [],
Expand All @@ -481,6 +487,7 @@ describe('Data Modeling store', function () {
indexes: [],
displayPosition: [0, 0],
shardKey: {},
isExpanded: true,
jsonSchema: {
bsonType: 'object',
properties: {
Expand Down
Loading
Loading