@@ -16,6 +16,7 @@ import {
16
16
selectCollections ,
17
17
selectConnection ,
18
18
selectDatabase ,
19
+ toggleInferRelationships ,
19
20
} from '../store/generate-diagram-wizard' ;
20
21
import {
21
22
Banner ,
@@ -35,7 +36,9 @@ import {
35
36
SearchInput ,
36
37
Combobox ,
37
38
ComboboxOption ,
39
+ Checkbox ,
38
40
} from '@mongodb-js/compass-components' ;
41
+ import { usePreference } from 'compass-preferences-model/provider' ;
39
42
40
43
const footerStyles = css ( {
41
44
flexDirection : 'row' ,
@@ -46,12 +49,6 @@ const footerTextStyles = css({ marginRight: 'auto' });
46
49
47
50
const footerActionsStyles = css ( { display : 'flex' , gap : spacing [ 200 ] } ) ;
48
51
49
- const formContainerStyles = css ( {
50
- display : 'flex' ,
51
- flexDirection : 'column' ,
52
- gap : spacing [ 400 ] ,
53
- } ) ;
54
-
55
52
const FormStepContainer : React . FunctionComponent < {
56
53
title : string ;
57
54
description ?: string ;
@@ -112,20 +109,27 @@ const FormStepContainer: React.FunctionComponent<{
112
109
) ;
113
110
} ;
114
111
115
- const SelectListStyles = css ( {
116
- height : 300 ,
112
+ const selectListStyles = css ( {
113
+ maxHeight : 200 ,
117
114
overflow : 'scroll' ,
118
115
} ) ;
119
116
120
117
function SelectCollectionsStep ( {
121
118
collections,
122
119
selectedCollections,
120
+ automaticallyInferRelationships,
123
121
onCollectionsSelect,
122
+ onAutomaticallyInferRelationshipsToggle,
124
123
} : {
125
124
collections : string [ ] ;
126
125
selectedCollections : string [ ] ;
126
+ automaticallyInferRelationships : boolean ;
127
127
onCollectionsSelect : ( colls : string [ ] ) => void ;
128
+ onAutomaticallyInferRelationshipsToggle : ( newVal : boolean ) => void ;
128
129
} ) {
130
+ const showAutoInferOption = usePreference (
131
+ 'enableAutomaticRelationshipInference'
132
+ ) ;
129
133
const [ searchTerm , setSearchTerm ] = useState ( '' ) ;
130
134
const filteredCollections = useMemo ( ( ) => {
131
135
try {
@@ -136,50 +140,80 @@ function SelectCollectionsStep({
136
140
}
137
141
} , [ collections , searchTerm ] ) ;
138
142
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
+ ) ;
169
176
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
+ </ >
183
217
) ;
184
218
}
185
219
@@ -199,6 +233,7 @@ type NewDiagramFormProps = {
199
233
selectedCollections : string [ ] ;
200
234
error : Error | null ;
201
235
analysisInProgress : boolean ;
236
+ automaticallyInferRelationships : boolean ;
202
237
203
238
onCancel : ( ) => void ;
204
239
onNameChange : ( name : string ) => void ;
@@ -212,6 +247,7 @@ type NewDiagramFormProps = {
212
247
onDatabaseConfirmSelection : ( ) => void ;
213
248
onCollectionsSelect : ( collections : string [ ] ) => void ;
214
249
onCollectionsSelectionConfirm : ( ) => void ;
250
+ onAutomaticallyInferRelationshipsToggle : ( newVal : boolean ) => void ;
215
251
} ;
216
252
217
253
const NewDiagramForm : React . FunctionComponent < NewDiagramFormProps > = ( {
@@ -226,6 +262,7 @@ const NewDiagramForm: React.FunctionComponent<NewDiagramFormProps> = ({
226
262
selectedCollections,
227
263
error,
228
264
analysisInProgress,
265
+ automaticallyInferRelationships,
229
266
onCancel,
230
267
onNameChange,
231
268
onNameConfirm,
@@ -238,6 +275,7 @@ const NewDiagramForm: React.FunctionComponent<NewDiagramFormProps> = ({
238
275
onDatabaseConfirmSelection,
239
276
onCollectionsSelect,
240
277
onCollectionsSelectionConfirm,
278
+ onAutomaticallyInferRelationshipsToggle,
241
279
} ) => {
242
280
const connections = useConnectionsList ( ) ;
243
281
const [ activeConnections , otherConnections ] = useMemo ( ( ) => {
@@ -349,7 +387,7 @@ const NewDiagramForm: React.FunctionComponent<NewDiagramFormProps> = ({
349
387
) ;
350
388
case 'select-connection' :
351
389
return (
352
- < FormFieldContainer className = { formContainerStyles } >
390
+ < FormFieldContainer >
353
391
< Combobox
354
392
label = ""
355
393
aria-label = "Select connection"
@@ -419,16 +457,22 @@ const NewDiagramForm: React.FunctionComponent<NewDiagramFormProps> = ({
419
457
collections = { collections }
420
458
onCollectionsSelect = { onCollectionsSelect }
421
459
selectedCollections = { selectedCollections }
460
+ automaticallyInferRelationships = { automaticallyInferRelationships }
461
+ onAutomaticallyInferRelationshipsToggle = {
462
+ onAutomaticallyInferRelationshipsToggle
463
+ }
422
464
/>
423
465
) ;
424
466
}
425
467
} , [
426
468
activeConnections ,
469
+ automaticallyInferRelationships ,
427
470
collections ,
428
471
connections . length ,
429
472
currentStep ,
430
473
databases ,
431
474
diagramName ,
475
+ onAutomaticallyInferRelationshipsToggle ,
432
476
onCollectionsSelect ,
433
477
onConnectionSelect ,
434
478
onDatabaseSelect ,
@@ -516,6 +560,8 @@ export default connect(
516
560
error,
517
561
analysisInProgress :
518
562
state . analysisProgress . analysisProcessStatus === 'in-progress' ,
563
+ automaticallyInferRelationships :
564
+ state . generateDiagramWizard . automaticallyInferRelations ,
519
565
} ;
520
566
} ,
521
567
{
@@ -531,5 +577,6 @@ export default connect(
531
577
onDatabaseConfirmSelection : confirmSelectDatabase ,
532
578
onCollectionsSelect : selectCollections ,
533
579
onCollectionsSelectionConfirm : confirmSelectedCollections ,
580
+ onAutomaticallyInferRelationshipsToggle : toggleInferRelationships ,
534
581
}
535
582
) ( NewDiagramForm ) ;
0 commit comments