9
9
useDarkMode ,
10
10
} from '@mongodb-js/compass-components' ;
11
11
import type { Document } from 'mongodb' ;
12
- import React , { useMemo , useCallback } from 'react' ;
12
+ import React , { useMemo , useCallback , useEffect } from 'react' ;
13
13
import { css , spacing } from '@mongodb-js/compass-components' ;
14
14
import {
15
15
CodemirrorMultilineEditor ,
@@ -105,6 +105,8 @@ const QueryFlowSection = ({
105
105
indexSuggestions,
106
106
fetchingSuggestionsState,
107
107
initialQuery,
108
+ inputQuery,
109
+ setInputQuery,
108
110
} : {
109
111
schemaFields : { name : string ; description ?: string } [ ] ;
110
112
serverVersion : string ;
@@ -118,12 +120,12 @@ const QueryFlowSection = ({
118
120
indexSuggestions : Record < string , number > | null ;
119
121
fetchingSuggestionsState : IndexSuggestionState ;
120
122
initialQuery : Document | null ;
123
+ inputQuery : string ;
124
+ setInputQuery : ( query : string ) => void ;
121
125
} ) => {
122
126
const track = useTelemetry ( ) ;
123
127
const darkMode = useDarkMode ( ) ;
124
- const [ inputQuery , setInputQuery ] = React . useState (
125
- initialQuery ? JSON . stringify ( initialQuery , null , 2 ) : ''
126
- ) ;
128
+
127
129
const [ hasNewChanges , setHasNewChanges ] = React . useState (
128
130
initialQuery !== null
129
131
) ;
@@ -146,21 +148,23 @@ const QueryFlowSection = ({
146
148
radius : editorContainerRadius ,
147
149
} ) ;
148
150
149
- const handleSuggestedIndexButtonClick = useCallback ( ( ) => {
151
+ const generateSuggestedIndexes = useCallback ( ( ) => {
150
152
const sanitizedInputQuery = inputQuery . trim ( ) ;
151
153
152
154
void onSuggestedIndexButtonClick ( {
153
155
dbName,
154
156
collectionName,
155
157
inputQuery : sanitizedInputQuery ,
156
158
} ) ;
159
+ setHasNewChanges ( false ) ;
160
+ } , [ inputQuery , dbName , collectionName , onSuggestedIndexButtonClick ] ) ;
157
161
162
+ const handleSuggestedIndexButtonClick = ( ) => {
163
+ generateSuggestedIndexes ( ) ;
158
164
track ( 'Suggested Index Button Clicked' , {
159
165
context : 'Create Index Modal' ,
160
166
} ) ;
161
-
162
- setHasNewChanges ( false ) ;
163
- } , [ inputQuery , dbName , collectionName , onSuggestedIndexButtonClick , track ] ) ;
167
+ } ;
164
168
165
169
const handleQueryInputChange = useCallback ( ( text : string ) => {
166
170
setInputQuery ( text ) ;
@@ -185,6 +189,12 @@ const QueryFlowSection = ({
185
189
}
186
190
} , [ hasNewChanges , inputQuery ] ) ;
187
191
192
+ useEffect ( ( ) => {
193
+ if ( initialQuery !== null ) {
194
+ generateSuggestedIndexes ( ) ;
195
+ }
196
+ } , [ initialQuery ] ) ;
197
+
188
198
return (
189
199
< >
190
200
{ initialQuery && (
0 commit comments