Skip to content

Commit e2b01c7

Browse files
committed
remove relationship
1 parent e427663 commit e2b01c7

File tree

2 files changed

+72
-16
lines changed

2 files changed

+72
-16
lines changed

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import DiagramEditor from './diagram-editor';
44
import SavedDiagramsList from './saved-diagrams-list';
55
import NewDiagramFormModal from './new-diagram-form';
66
import type { DataModelingState } from '../store/reducer';
7-
7+
import { DiagramProvider } from '@mongodb-js/diagramming';
88
type DataModelingPluginInitialProps = {
99
showList: boolean;
1010
};
@@ -17,7 +17,9 @@ const DataModeling: React.FunctionComponent<DataModelingPluginInitialProps> = ({
1717
{showList ? (
1818
<SavedDiagramsList></SavedDiagramsList>
1919
) : (
20-
<DiagramEditor></DiagramEditor>
20+
<DiagramProvider>
21+
<DiagramEditor />
22+
</DiagramProvider>
2123
)}
2224
<NewDiagramFormModal></NewDiagramFormModal>
2325
</>

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

Lines changed: 68 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,14 @@ import {
1919
Button,
2020
palette,
2121
ErrorSummary,
22+
useDarkMode,
2223
} from '@mongodb-js/compass-components';
2324
import { CodemirrorMultilineEditor } from '@mongodb-js/compass-editor';
2425
import { cancelAnalysis, retryAnalysis } from '../store/analysis-process';
2526
import {
2627
Diagram,
27-
DiagramProvider,
2828
type NodeProps,
29+
type EdgeProps,
2930
} from '@mongodb-js/diagramming';
3031
import type { Edit, StaticModel } from '../services/data-model-storage';
3132
import { UUID } from 'bson';
@@ -88,13 +89,12 @@ const editorContainerStyles = css({
8889
});
8990

9091
const editorContainerApplyContainerStyles = css({
91-
paddingLeft: 8,
92-
paddingRight: 8,
92+
padding: spacing[200],
9393
justifyContent: 'flex-end',
9494
gap: spacing[200],
9595
display: 'flex',
9696
width: '100%',
97-
height: spacing[100],
97+
alignItems: 'center',
9898
});
9999

100100
const editorContainerPlaceholderButtonStyles = css({
@@ -107,6 +107,7 @@ const editorContainerPlaceholderButtonStyles = css({
107107
});
108108

109109
const DiagramEditor: React.FunctionComponent<{
110+
diagramLabel: string;
110111
step: DataModelingState['step'];
111112
hasUndo: boolean;
112113
onUndoClick: () => void;
@@ -118,6 +119,7 @@ const DiagramEditor: React.FunctionComponent<{
118119
onCancelClick: () => void;
119120
onApplyClick: (edit: Omit<Edit, 'id' | 'timestamp'>) => void;
120121
}> = ({
122+
diagramLabel,
121123
step,
122124
hasUndo,
123125
onUndoClick,
@@ -129,6 +131,7 @@ const DiagramEditor: React.FunctionComponent<{
129131
onCancelClick,
130132
onApplyClick,
131133
}) => {
134+
const isDarkMode = useDarkMode();
132135
const [applyInput, setApplyInput] = useState('{}');
133136
const isEditValid = useMemo(() => {
134137
try {
@@ -176,9 +179,47 @@ const DiagramEditor: React.FunctionComponent<{
176179
setApplyInput(JSON.stringify(placeholder, null, 2));
177180
};
178181

179-
const modelStr = useMemo(() => {
180-
return JSON.stringify(model, null, 2);
181-
}, [model]);
182+
const edges = useMemo(() => {
183+
return (model?.relationships ?? []).map((relationship): EdgeProps => {
184+
const [source, target] = relationship.relationship;
185+
return {
186+
id: relationship.id,
187+
source: source.ns,
188+
target: target.ns,
189+
markerStart: 'one',
190+
markerEnd: 'many',
191+
};
192+
});
193+
}, [model?.relationships]);
194+
195+
const nodes = useMemo(() => {
196+
return (model?.collections ?? []).map(
197+
(coll): NodeProps => ({
198+
id: coll.ns,
199+
type: 'collection',
200+
position: {
201+
x: Math.floor(Math.random() * 1000),
202+
y: Math.floor(Math.random() * 1000),
203+
},
204+
title: coll.ns,
205+
fields: Object.entries(coll.jsonSchema.properties ?? {}).map(
206+
([name, field]) => {
207+
const type =
208+
field.bsonType === undefined
209+
? 'Unknown'
210+
: typeof field.bsonType === 'string'
211+
? field.bsonType
212+
: field.bsonType[0];
213+
return {
214+
name: name,
215+
type,
216+
glyphs: type === 'objectId' ? ['key'] : [],
217+
};
218+
}
219+
),
220+
})
221+
);
222+
}, [model?.collections]);
182223

183224
let content;
184225

@@ -222,12 +263,24 @@ const DiagramEditor: React.FunctionComponent<{
222263
data-testid="diagram-editor-container"
223264
>
224265
<div className={modelPreviewStyles} data-testid="model-preview">
225-
<CodemirrorMultilineEditor
226-
language="json"
227-
text={modelStr}
228-
readOnly
229-
initialJSONFoldAll={false}
230-
></CodemirrorMultilineEditor>
266+
<Diagram
267+
isDarkMode={isDarkMode}
268+
title={diagramLabel}
269+
edges={edges}
270+
nodes={nodes}
271+
onEdgeClick={(evt, edge) => {
272+
setApplyInput(
273+
JSON.stringify(
274+
{
275+
type: 'RemoveRelationship',
276+
relationshipId: edge.id,
277+
},
278+
null,
279+
2
280+
)
281+
);
282+
}}
283+
/>
231284
</div>
232285
<div className={editorContainerStyles} data-testid="apply-editor">
233286
<div className={editorContainerPlaceholderButtonStyles}>
@@ -296,7 +349,7 @@ const DiagramEditor: React.FunctionComponent<{
296349
);
297350
}}
298351
>
299-
<DiagramProvider>{content}</DiagramProvider>
352+
{content}
300353
</WorkspaceContainer>
301354
);
302355
};
@@ -312,6 +365,7 @@ export default connect(
312365
? selectCurrentModel(getCurrentDiagramFromState(state))
313366
: null,
314367
editErrors: diagram?.editErrors,
368+
diagramLabel: diagram?.name || 'Schema Preview',
315369
};
316370
},
317371
{

0 commit comments

Comments
 (0)