Skip to content

Commit f6f473b

Browse files
committed
type edits as a non empty array
1 parent fa7579e commit f6f473b

File tree

6 files changed

+74
-17
lines changed

6 files changed

+74
-17
lines changed

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

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,33 @@ import React from 'react';
22
import { expect } from 'chai';
33
import { render, screen } from '@mongodb-js/testing-library-compass';
44
import { DiagramCard } from './diagram-card';
5+
import type { Edit } from '../services/data-model-storage';
56

67
describe('DiagramCard', () => {
78
const props = {
89
diagram: {
910
id: 'test-diagram',
1011
connectionId: 'test-connection',
1112
name: 'Test Diagram',
12-
edits: [],
13+
edits: [
14+
{
15+
id: 'edit-id',
16+
timestamp: '2023-10-01T00:00:00.000Z',
17+
type: 'SetModel',
18+
model: {
19+
collections: [
20+
{
21+
ns: 'db.collection',
22+
indexes: [],
23+
displayPosition: [0, 0],
24+
shardKey: {},
25+
jsonSchema: { bsonType: 'object' },
26+
},
27+
],
28+
relationships: [],
29+
},
30+
},
31+
] as [Edit],
1332
lastModified: new Date('2025-01-01').getTime(),
1433
databases: 'someDatabase',
1534
},

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -79,8 +79,8 @@ export function DiagramCard({
7979
onDelete,
8080
}: {
8181
diagram: MongoDBDataModelDescription & {
82-
lastModified?: number;
83-
databases?: string;
82+
lastModified: number;
83+
databases: string;
8484
};
8585
onOpen: (diagram: MongoDBDataModelDescription) => void;
8686
onRename: (id: string) => void;

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

Lines changed: 40 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,23 +15,41 @@ const storageItems: MongoDBDataModelDescription[] = [
1515
{
1616
id: '1',
1717
name: 'One',
18-
edits: [],
18+
edits: [
19+
{
20+
id: 'edit-id-1',
21+
timestamp: '2023-10-01T00:00:00.000Z',
22+
type: 'SetModel',
23+
model: {
24+
collections: [
25+
{
26+
ns: 'db1.collection1',
27+
indexes: [],
28+
displayPosition: [1, 1],
29+
shardKey: {},
30+
jsonSchema: { bsonType: 'object' },
31+
},
32+
],
33+
relationships: [],
34+
},
35+
},
36+
],
1937
connectionId: null,
2038
},
2139
{
2240
id: '2',
2341
name: 'Two',
2442
edits: [
2543
{
26-
id: 'edit-id',
44+
id: 'edit-id-2',
2745
timestamp: '2023-10-01T00:00:00.000Z',
2846
type: 'SetModel',
2947
model: {
3048
collections: [
3149
{
3250
ns: 'db2.collection2',
3351
indexes: [],
34-
displayPosition: [0, 0],
52+
displayPosition: [2, 2],
3553
shardKey: {},
3654
jsonSchema: { bsonType: 'object' },
3755
},
@@ -45,7 +63,25 @@ const storageItems: MongoDBDataModelDescription[] = [
4563
{
4664
id: '3',
4765
name: 'Three',
48-
edits: [],
66+
edits: [
67+
{
68+
id: 'edit-id-3',
69+
timestamp: '2023-10-01T00:00:00.000Z',
70+
type: 'SetModel',
71+
model: {
72+
collections: [
73+
{
74+
ns: 'db3.collection3',
75+
indexes: [],
76+
displayPosition: [3, 3],
77+
shardKey: {},
78+
jsonSchema: { bsonType: 'object' },
79+
},
80+
],
81+
relationships: [],
82+
},
83+
},
84+
],
4985
connectionId: null,
5086
},
5187
];

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

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -141,13 +141,11 @@ export const SavedDiagramsList: React.FunctionComponent<{
141141
const { items, status } = useDataModelSavedItems();
142142
const decoratedItems = useMemo<
143143
(MongoDBDataModelDescription & {
144-
lastModified?: number;
145-
databases?: string;
144+
lastModified: number;
145+
databases: string;
146146
})[]
147147
>(() => {
148148
return items.map((item) => {
149-
if (!item.edits.length) return item;
150-
151149
const databases = new Set(
152150
getCurrentModel(item).collections.map(({ ns }) => toNS(ns).database)
153151
);

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ export const MongoDBDataModelDescriptionSchema = z.object({
8888
*/
8989
connectionId: z.string().nullable(),
9090

91-
edits: z.array(EditSchema).default([]),
91+
edits: z.array(EditSchema).nonempty(),
9292
});
9393

9494
export type MongoDBDataModelDescription = z.output<

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

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,15 @@ import { memoize } from 'lodash';
1212
import type { DataModelingState, DataModelingThunkAction } from './reducer';
1313
import { showConfirmation, showPrompt } from '@mongodb-js/compass-components';
1414

15+
function isNonEmptyArray<T>(arr: T[]): arr is [T, ...T[]] {
16+
return Array.isArray(arr) && arr.length > 0;
17+
}
18+
1519
export type DiagramState =
1620
| (Omit<MongoDBDataModelDescription, 'edits'> & {
1721
edits: {
1822
prev: Edit[][];
19-
current: Edit[];
23+
current: [Edit, ...Edit[]];
2024
next: Edit[][];
2125
};
2226
editErrors?: string[];
@@ -154,8 +158,8 @@ export const diagramReducer: Reducer<DiagramState> = (
154158
};
155159
}
156160
if (isAction(action, DiagramActionTypes.UNDO_EDIT)) {
157-
const newCurrent = state.edits.prev.pop();
158-
if (!newCurrent) {
161+
const newCurrent = state.edits.prev.pop() || [];
162+
if (!isNonEmptyArray(newCurrent)) {
159163
return state;
160164
}
161165
return {
@@ -168,8 +172,8 @@ export const diagramReducer: Reducer<DiagramState> = (
168172
};
169173
}
170174
if (isAction(action, DiagramActionTypes.REDO_EDIT)) {
171-
const newCurrent = state.edits.next.pop();
172-
if (!newCurrent) {
175+
const newCurrent = state.edits.next.pop() || [];
176+
if (!isNonEmptyArray(newCurrent)) {
173177
return state;
174178
}
175179
return {

0 commit comments

Comments
 (0)