@@ -17,6 +17,7 @@ import {
1717 selectCollection ,
1818 selectRelationship ,
1919 selectBackground ,
20+ type DiagramState ,
2021} from '../store/diagram' ;
2122import {
2223 Banner ,
@@ -37,7 +38,7 @@ import {
3738 useDiagram ,
3839 applyLayout ,
3940} from '@mongodb-js/diagramming' ;
40- import type { StaticModel } from '../services/data-model-storage' ;
41+ import type { Relationship , StaticModel } from '../services/data-model-storage' ;
4142import DiagramEditorToolbar from './diagram-editor-toolbar' ;
4243import ExportDiagramModal from './export-diagram-modal' ;
4344import { useLogger } from '@mongodb-js/compass-logging/provider' ;
@@ -136,7 +137,7 @@ export const getFieldsFromSchema = (
136137 // types are either direct, or from anyof
137138 // children are either direct (properties), from anyOf, items or items.anyOf
138139 const types : ( string | string [ ] ) [ ] = [ ] ;
139- const children = [ ] ;
140+ const children : ( MongoDBJSONSchema | MongoDBJSONSchema [ ] ) [ ] = [ ] ;
140141 if ( field . bsonType ) types . push ( field . bsonType ) ;
141142 if ( field . properties ) children . push ( field ) ;
142143 if ( field . items )
@@ -182,6 +183,27 @@ export const getFieldsFromSchema = (
182183 return fields ;
183184} ;
184185
186+ const getSelectedFields = (
187+ selectedItems : SelectedItems | null ,
188+ relationships ?: Relationship [ ]
189+ ) => {
190+ if ( ! selectedItems || selectedItems . type !== 'relationship' ) return { } ;
191+ const { id } = selectedItems ;
192+ const relationship = relationships ?. find ( ( rel ) => rel . id === id ) ;
193+ if (
194+ ! relationship ||
195+ ! relationship . relationship [ 0 ] . ns ||
196+ ! relationship . relationship [ 1 ] . ns ||
197+ ! relationship . relationship [ 0 ] . fields ||
198+ ! relationship . relationship [ 1 ] . fields
199+ )
200+ return { } ;
201+ return {
202+ [ relationship . relationship [ 0 ] . ns ] : relationship . relationship [ 0 ] . fields ,
203+ [ relationship . relationship [ 1 ] . ns ] : relationship . relationship [ 1 ] . fields ,
204+ } ;
205+ } ;
206+
185207const modelPreviewContainerStyles = css ( {
186208 display : 'grid' ,
187209 gridTemplateColumns : '100%' ,
@@ -194,6 +216,8 @@ const modelPreviewStyles = css({
194216 minHeight : 0 ,
195217} ) ;
196218
219+ type SelectedItems = NonNullable < DiagramState > [ 'selectedItems' ] ;
220+
197221const DiagramEditor : React . FunctionComponent < {
198222 diagramLabel : string ;
199223 step : DataModelingState [ 'step' ] ;
@@ -206,7 +230,7 @@ const DiagramEditor: React.FunctionComponent<{
206230 onCollectionSelect : ( namespace : string ) => void ;
207231 onRelationshipSelect : ( rId : string ) => void ;
208232 onDiagramBackgroundClicked : ( ) => void ;
209- selectedItems : { type : 'relationship' | 'collection' ; id : string } | null ;
233+ selectedItems : SelectedItems | null ;
210234} > = ( {
211235 diagramLabel,
212236 step,
@@ -254,28 +278,11 @@ const DiagramEditor: React.FunctionComponent<{
254278 } ) ;
255279 } , [ model ?. relationships , selectedItems ] ) ;
256280
257- const selectedFields = useMemo < Record < string , string [ ] > > ( ( ) => {
258- if ( ! selectedItems ) return { } ;
259- const { type, id } = selectedItems ;
260- if ( type === 'relationship' ) {
261- const relationship = model ?. relationships . find ( ( rel ) => rel . id === id ) ;
262- if (
263- ! relationship ||
264- ! relationship . relationship [ 0 ] . ns ||
265- ! relationship . relationship [ 1 ] . ns ||
266- ! relationship . relationship [ 0 ] . fields ||
267- ! relationship . relationship [ 1 ] . fields
268- )
269- return { } ;
270- return {
271- [ relationship . relationship [ 0 ] . ns ] : relationship . relationship [ 0 ] . fields ,
272- [ relationship . relationship [ 1 ] . ns ] : relationship . relationship [ 1 ] . fields ,
273- } ;
274- }
275- return { } ;
276- } , [ model ?. relationships , selectedItems ] ) ;
277-
278281 const nodes = useMemo < NodeProps [ ] > ( ( ) => {
282+ const selectedFields = getSelectedFields (
283+ selectedItems ,
284+ model ?. relationships
285+ ) ;
279286 return ( model ?. collections ?? [ ] ) . map (
280287 ( coll ) : NodeProps => ( {
281288 id : coll . ns ,
@@ -296,7 +303,7 @@ const DiagramEditor: React.FunctionComponent<{
296303 selectedItems . id === coll . ns ,
297304 } )
298305 ) ;
299- } , [ model ?. collections , selectedItems , selectedFields ] ) ;
306+ } , [ model ?. collections , model ?. relationships , selectedItems ] ) ;
300307
301308 const applyInitialLayout = useCallback ( async ( ) => {
302309 try {
0 commit comments