@@ -16,6 +16,7 @@ import {
1616 selectCollections ,
1717 selectConnection ,
1818 selectDatabase ,
19+ toggleInferRelationships ,
1920} from '../store/generate-diagram-wizard' ;
2021import {
2122 Banner ,
@@ -35,7 +36,9 @@ import {
3536 SearchInput ,
3637 Combobox ,
3738 ComboboxOption ,
39+ Checkbox ,
3840} from '@mongodb-js/compass-components' ;
41+ import { usePreference } from 'compass-preferences-model/provider' ;
3942
4043const footerStyles = css ( {
4144 flexDirection : 'row' ,
@@ -46,12 +49,6 @@ const footerTextStyles = css({ marginRight: 'auto' });
4649
4750const footerActionsStyles = css ( { display : 'flex' , gap : spacing [ 200 ] } ) ;
4851
49- const formContainerStyles = css ( {
50- display : 'flex' ,
51- flexDirection : 'column' ,
52- gap : spacing [ 400 ] ,
53- } ) ;
54-
5552const FormStepContainer : React . FunctionComponent < {
5653 title : string ;
5754 description ?: string ;
@@ -112,20 +109,27 @@ const FormStepContainer: React.FunctionComponent<{
112109 ) ;
113110} ;
114111
115- const SelectListStyles = css ( {
116- height : 300 ,
112+ const selectListStyles = css ( {
113+ maxHeight : 200 ,
117114 overflow : 'scroll' ,
118115} ) ;
119116
120117function SelectCollectionsStep ( {
121118 collections,
122119 selectedCollections,
120+ automaticallyInferRelationships,
123121 onCollectionsSelect,
122+ onAutomaticallyInferRelationshipsToggle,
124123} : {
125124 collections : string [ ] ;
126125 selectedCollections : string [ ] ;
126+ automaticallyInferRelationships : boolean ;
127127 onCollectionsSelect : ( colls : string [ ] ) => void ;
128+ onAutomaticallyInferRelationshipsToggle : ( newVal : boolean ) => void ;
128129} ) {
130+ const showAutoInferOption = usePreference (
131+ 'enableAutomaticRelationshipInference'
132+ ) ;
129133 const [ searchTerm , setSearchTerm ] = useState ( '' ) ;
130134 const filteredCollections = useMemo ( ( ) => {
131135 try {
@@ -136,50 +140,79 @@ function SelectCollectionsStep({
136140 }
137141 } , [ collections , searchTerm ] ) ;
138142 return (
139- < FormFieldContainer className = { formContainerStyles } >
140- < SearchInput
141- aria-label = "Search collections"
142- value = { searchTerm }
143- data-testid = "new-diagram-search-collections"
144- onChange = { ( e ) => {
145- setSearchTerm ( e . target . value ) ;
146- } }
147- />
148- < SelectList
149- className = { SelectListStyles }
150- items = { filteredCollections . map ( ( collName ) => {
151- return {
152- id : collName ,
153- selected : selectedCollections . includes ( collName ) ,
154- 'data-testid' : `new-diagram-collection-checkbox-${ collName } ` ,
155- } ;
156- } ) }
157- label = { { displayLabelKey : 'id' , name : 'Collection Name' } }
158- onChange = { ( items : { id : string ; selected : boolean } [ ] ) => {
159- // When a user is searching, less collections are shown to the user
160- // and we need to keep existing selected collections selected.
161- const currentSelectedItems = selectedCollections . filter (
162- ( collName ) => {
163- const item = items . find ( ( x ) => x . id === collName ) ;
164- // The already selected item was not shown to the user (using search),
165- // and we have to keep it selected.
166- return item ? item . selected : true ;
167- }
168- ) ;
143+ < >
144+ < FormFieldContainer >
145+ < SearchInput
146+ aria-label = "Search collections"
147+ value = { searchTerm }
148+ data-testid = "new-diagram-search-collections"
149+ onChange = { ( e ) => {
150+ setSearchTerm ( e . target . value ) ;
151+ } }
152+ />
153+ </ FormFieldContainer >
154+ < FormFieldContainer >
155+ < SelectList
156+ className = { selectListStyles }
157+ items = { filteredCollections . map ( ( collName ) => {
158+ return {
159+ id : collName ,
160+ selected : selectedCollections . includes ( collName ) ,
161+ 'data-testid' : `new-diagram-collection-checkbox-${ collName } ` ,
162+ } ;
163+ } ) }
164+ label = { { displayLabelKey : 'id' , name : 'Collection Name' } }
165+ onChange = { ( items ) => {
166+ // When a user is searching, less collections are shown to the user
167+ // and we need to keep existing selected collections selected.
168+ const currentSelectedItems = selectedCollections . filter (
169+ ( collName ) => {
170+ const item = items . find ( ( x ) => x . id === collName ) ;
171+ // The already selected item was not shown to the user (using search),
172+ // and we have to keep it selected.
173+ return item ? item . selected : true ;
174+ }
175+ ) ;
169176
170- const newSelectedItems = items
171- . filter ( ( item ) => {
172- return item . selected ;
173- } )
174- . map ( ( item ) => {
175- return item . id ;
176- } ) ;
177- onCollectionsSelect (
178- Array . from ( new Set ( [ ...newSelectedItems , ...currentSelectedItems ] ) )
179- ) ;
180- } }
181- > </ SelectList >
182- </ FormFieldContainer >
177+ const newSelectedItems = items
178+ . filter ( ( item ) => {
179+ return item . selected ;
180+ } )
181+ . map ( ( item ) => {
182+ return item . id ;
183+ } ) ;
184+ onCollectionsSelect (
185+ Array . from (
186+ new Set ( [ ...newSelectedItems , ...currentSelectedItems ] )
187+ )
188+ ) ;
189+ } }
190+ > </ SelectList >
191+ </ FormFieldContainer >
192+ { showAutoInferOption && (
193+ < FormFieldContainer >
194+ < Checkbox
195+ checked = { automaticallyInferRelationships }
196+ onChange = { ( evt ) => {
197+ onAutomaticallyInferRelationshipsToggle (
198+ evt . currentTarget . checked
199+ ) ;
200+ } }
201+ label = "Automatically infer relationships"
202+ // @ts -expect-error Element is accepted, but not typed correctly
203+ description = {
204+ < >
205+ Using a special algorithm, Compass will try to automatically
206+ discover relationships in selected collections. This operation
207+ will run multiple find requests against{ ' ' }
208+ < strong > indexed fields</ strong > of the collections and might
209+ take some extra time.
210+ </ >
211+ }
212+ > </ Checkbox >
213+ </ FormFieldContainer >
214+ ) }
215+ </ >
183216 ) ;
184217}
185218
@@ -199,6 +232,7 @@ type NewDiagramFormProps = {
199232 selectedCollections : string [ ] ;
200233 error : Error | null ;
201234 analysisInProgress : boolean ;
235+ automaticallyInferRelationships : boolean ;
202236
203237 onCancel : ( ) => void ;
204238 onNameChange : ( name : string ) => void ;
@@ -212,6 +246,7 @@ type NewDiagramFormProps = {
212246 onDatabaseConfirmSelection : ( ) => void ;
213247 onCollectionsSelect : ( collections : string [ ] ) => void ;
214248 onCollectionsSelectionConfirm : ( ) => void ;
249+ onAutomaticallyInferRelationshipsToggle : ( newVal : boolean ) => void ;
215250} ;
216251
217252const NewDiagramForm : React . FunctionComponent < NewDiagramFormProps > = ( {
@@ -226,6 +261,7 @@ const NewDiagramForm: React.FunctionComponent<NewDiagramFormProps> = ({
226261 selectedCollections,
227262 error,
228263 analysisInProgress,
264+ automaticallyInferRelationships,
229265 onCancel,
230266 onNameChange,
231267 onNameConfirm,
@@ -238,6 +274,7 @@ const NewDiagramForm: React.FunctionComponent<NewDiagramFormProps> = ({
238274 onDatabaseConfirmSelection,
239275 onCollectionsSelect,
240276 onCollectionsSelectionConfirm,
277+ onAutomaticallyInferRelationshipsToggle,
241278} ) => {
242279 const connections = useConnectionsList ( ) ;
243280 const [ activeConnections , otherConnections ] = useMemo ( ( ) => {
@@ -349,7 +386,7 @@ const NewDiagramForm: React.FunctionComponent<NewDiagramFormProps> = ({
349386 ) ;
350387 case 'select-connection' :
351388 return (
352- < FormFieldContainer className = { formContainerStyles } >
389+ < FormFieldContainer >
353390 < Combobox
354391 label = ""
355392 aria-label = "Select connection"
@@ -419,16 +456,22 @@ const NewDiagramForm: React.FunctionComponent<NewDiagramFormProps> = ({
419456 collections = { collections }
420457 onCollectionsSelect = { onCollectionsSelect }
421458 selectedCollections = { selectedCollections }
459+ automaticallyInferRelationships = { automaticallyInferRelationships }
460+ onAutomaticallyInferRelationshipsToggle = {
461+ onAutomaticallyInferRelationshipsToggle
462+ }
422463 />
423464 ) ;
424465 }
425466 } , [
426467 activeConnections ,
468+ automaticallyInferRelationships ,
427469 collections ,
428470 connections . length ,
429471 currentStep ,
430472 databases ,
431473 diagramName ,
474+ onAutomaticallyInferRelationshipsToggle ,
432475 onCollectionsSelect ,
433476 onConnectionSelect ,
434477 onDatabaseSelect ,
@@ -516,6 +559,8 @@ export default connect(
516559 error,
517560 analysisInProgress :
518561 state . analysisProgress . analysisProcessStatus === 'in-progress' ,
562+ automaticallyInferRelationships :
563+ state . generateDiagramWizard . automaticallyInferRelations ,
519564 } ;
520565 } ,
521566 {
@@ -531,5 +576,6 @@ export default connect(
531576 onDatabaseConfirmSelection : confirmSelectDatabase ,
532577 onCollectionsSelect : selectCollections ,
533578 onCollectionsSelectionConfirm : confirmSelectedCollections ,
579+ onAutomaticallyInferRelationshipsToggle : toggleInferRelationships ,
534580 }
535581) ( NewDiagramForm ) ;
0 commit comments