@@ -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,80 @@ 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+ Compass will try to automatically discover relationships in
206+ selected collections. This operation will run multiple find
207+ requests against indexed fields of the collections and{ ' ' }
208+ < strong >
209+ will take additional time per collection being analyzed.
210+ </ strong >
211+ </ >
212+ }
213+ > </ Checkbox >
214+ </ FormFieldContainer >
215+ ) }
216+ </ >
183217 ) ;
184218}
185219
@@ -199,6 +233,7 @@ type NewDiagramFormProps = {
199233 selectedCollections : string [ ] ;
200234 error : Error | null ;
201235 analysisInProgress : boolean ;
236+ automaticallyInferRelationships : boolean ;
202237
203238 onCancel : ( ) => void ;
204239 onNameChange : ( name : string ) => void ;
@@ -212,6 +247,7 @@ type NewDiagramFormProps = {
212247 onDatabaseConfirmSelection : ( ) => void ;
213248 onCollectionsSelect : ( collections : string [ ] ) => void ;
214249 onCollectionsSelectionConfirm : ( ) => void ;
250+ onAutomaticallyInferRelationshipsToggle : ( newVal : boolean ) => void ;
215251} ;
216252
217253const NewDiagramForm : React . FunctionComponent < NewDiagramFormProps > = ( {
@@ -226,6 +262,7 @@ const NewDiagramForm: React.FunctionComponent<NewDiagramFormProps> = ({
226262 selectedCollections,
227263 error,
228264 analysisInProgress,
265+ automaticallyInferRelationships,
229266 onCancel,
230267 onNameChange,
231268 onNameConfirm,
@@ -238,6 +275,7 @@ const NewDiagramForm: React.FunctionComponent<NewDiagramFormProps> = ({
238275 onDatabaseConfirmSelection,
239276 onCollectionsSelect,
240277 onCollectionsSelectionConfirm,
278+ onAutomaticallyInferRelationshipsToggle,
241279} ) => {
242280 const connections = useConnectionsList ( ) ;
243281 const [ activeConnections , otherConnections ] = useMemo ( ( ) => {
@@ -349,7 +387,7 @@ const NewDiagramForm: React.FunctionComponent<NewDiagramFormProps> = ({
349387 ) ;
350388 case 'select-connection' :
351389 return (
352- < FormFieldContainer className = { formContainerStyles } >
390+ < FormFieldContainer >
353391 < Combobox
354392 label = ""
355393 aria-label = "Select connection"
@@ -419,16 +457,22 @@ const NewDiagramForm: React.FunctionComponent<NewDiagramFormProps> = ({
419457 collections = { collections }
420458 onCollectionsSelect = { onCollectionsSelect }
421459 selectedCollections = { selectedCollections }
460+ automaticallyInferRelationships = { automaticallyInferRelationships }
461+ onAutomaticallyInferRelationshipsToggle = {
462+ onAutomaticallyInferRelationshipsToggle
463+ }
422464 />
423465 ) ;
424466 }
425467 } , [
426468 activeConnections ,
469+ automaticallyInferRelationships ,
427470 collections ,
428471 connections . length ,
429472 currentStep ,
430473 databases ,
431474 diagramName ,
475+ onAutomaticallyInferRelationshipsToggle ,
432476 onCollectionsSelect ,
433477 onConnectionSelect ,
434478 onDatabaseSelect ,
@@ -516,6 +560,8 @@ export default connect(
516560 error,
517561 analysisInProgress :
518562 state . analysisProgress . analysisProcessStatus === 'in-progress' ,
563+ automaticallyInferRelationships :
564+ state . generateDiagramWizard . automaticallyInferRelations ,
519565 } ;
520566 } ,
521567 {
@@ -531,5 +577,6 @@ export default connect(
531577 onDatabaseConfirmSelection : confirmSelectDatabase ,
532578 onCollectionsSelect : selectCollections ,
533579 onCollectionsSelectionConfirm : confirmSelectedCollections ,
580+ onAutomaticallyInferRelationshipsToggle : toggleInferRelationships ,
534581 }
535582) ( NewDiagramForm ) ;
0 commit comments