99 useDarkMode ,
1010} from '@mongodb-js/compass-components' ;
1111import type { Document } from 'mongodb' ;
12- import React , { useMemo , useCallback } from 'react' ;
12+ import React , { useMemo , useCallback , useEffect } from 'react' ;
1313import { css , spacing } from '@mongodb-js/compass-components' ;
1414import {
1515 CodemirrorMultilineEditor ,
@@ -105,6 +105,8 @@ const QueryFlowSection = ({
105105 indexSuggestions,
106106 fetchingSuggestionsState,
107107 initialQuery,
108+ inputQuery,
109+ setInputQuery,
108110} : {
109111 schemaFields : { name : string ; description ?: string } [ ] ;
110112 serverVersion : string ;
@@ -118,12 +120,12 @@ const QueryFlowSection = ({
118120 indexSuggestions : Record < string , number > | null ;
119121 fetchingSuggestionsState : IndexSuggestionState ;
120122 initialQuery : Document | null ;
123+ inputQuery : string ;
124+ setInputQuery : ( query : string ) => void ;
121125} ) => {
122126 const track = useTelemetry ( ) ;
123127 const darkMode = useDarkMode ( ) ;
124- const [ inputQuery , setInputQuery ] = React . useState (
125- initialQuery ? JSON . stringify ( initialQuery , null , 2 ) : ''
126- ) ;
128+
127129 const [ hasNewChanges , setHasNewChanges ] = React . useState (
128130 initialQuery !== null
129131 ) ;
@@ -146,21 +148,23 @@ const QueryFlowSection = ({
146148 radius : editorContainerRadius ,
147149 } ) ;
148150
149- const handleSuggestedIndexButtonClick = useCallback ( ( ) => {
151+ const generateSuggestedIndexes = useCallback ( ( ) => {
150152 const sanitizedInputQuery = inputQuery . trim ( ) ;
151153
152154 void onSuggestedIndexButtonClick ( {
153155 dbName,
154156 collectionName,
155157 inputQuery : sanitizedInputQuery ,
156158 } ) ;
159+ setHasNewChanges ( false ) ;
160+ } , [ inputQuery , dbName , collectionName , onSuggestedIndexButtonClick ] ) ;
157161
162+ const handleSuggestedIndexButtonClick = ( ) => {
163+ generateSuggestedIndexes ( ) ;
158164 track ( 'Suggested Index Button Clicked' , {
159165 context : 'Create Index Modal' ,
160166 } ) ;
161-
162- setHasNewChanges ( false ) ;
163- } , [ inputQuery , dbName , collectionName , onSuggestedIndexButtonClick , track ] ) ;
167+ } ;
164168
165169 const handleQueryInputChange = useCallback ( ( text : string ) => {
166170 setInputQuery ( text ) ;
@@ -185,6 +189,12 @@ const QueryFlowSection = ({
185189 }
186190 } , [ hasNewChanges , inputQuery ] ) ;
187191
192+ useEffect ( ( ) => {
193+ if ( initialQuery !== null ) {
194+ generateSuggestedIndexes ( ) ;
195+ }
196+ } , [ initialQuery ] ) ;
197+
188198 return (
189199 < >
190200 { initialQuery && (
0 commit comments