88 ParagraphSkeleton ,
99 useDarkMode ,
1010} from '@mongodb-js/compass-components' ;
11- import type { Document } from 'mongodb' ;
12- import React , { useMemo , useCallback , useEffect } from 'react' ;
11+ import React , { useMemo , useCallback } from 'react' ;
1312import { css , spacing } from '@mongodb-js/compass-components' ;
1413import {
1514 CodemirrorMultilineEditor ,
@@ -20,6 +19,7 @@ import type { RootState } from '../../modules';
2019import { fetchIndexSuggestions } from '../../modules/create-index' ;
2120import type {
2221 IndexSuggestionState ,
22+ QueryUpdatedProps ,
2323 SuggestedIndexFetchedProps ,
2424} from '../../modules/create-index' ;
2525import { queryUpdated } from '../../modules/create-index' ;
@@ -106,8 +106,7 @@ const QueryFlowSection = ({
106106 indexSuggestions,
107107 fetchingSuggestionsState,
108108 initialQuery,
109- inputQuery,
110- setInputQuery,
109+ query,
111110 hasQueryChanges,
112111 onQueryUpdated,
113112} : {
@@ -118,24 +117,18 @@ const QueryFlowSection = ({
118117 onSuggestedIndexButtonClick : ( {
119118 dbName,
120119 collectionName,
121- inputQuery ,
120+ query ,
122121 } : SuggestedIndexFetchedProps ) => Promise < void > ;
123122 indexSuggestions : Record < string , number > | null ;
124123 fetchingSuggestionsState : IndexSuggestionState ;
125- initialQuery : Document | null ;
126- inputQuery : string ;
127- setInputQuery : ( query : string ) => void ;
124+ initialQuery : string | null ;
125+ query : string ;
128126 hasQueryChanges : boolean ; // to keep track of the state between the tab flows
129- onQueryUpdated : ( ) => void ;
127+ onQueryUpdated : ( { query , hasQueryChanges } : QueryUpdatedProps ) => void ;
130128} ) => {
131129 const track = useTelemetry ( ) ;
132130 const darkMode = useDarkMode ( ) ;
133131
134- // if initialQuery isnt null then we should have new changes
135- const [ hasNewChanges , setHasNewChanges ] = React . useState (
136- initialQuery !== null || hasQueryChanges
137- ) ;
138-
139132 const [ isShowSuggestionsButtonDisabled , setIsShowSuggestionsButtonDisabled ] =
140133 React . useState ( true ) ;
141134
@@ -156,15 +149,14 @@ const QueryFlowSection = ({
156149 } ) ;
157150
158151 const generateSuggestedIndexes = useCallback ( ( ) => {
159- const sanitizedInputQuery = inputQuery . trim ( ) ;
152+ const sanitizedInputQuery = query . trim ( ) ;
160153
161154 void onSuggestedIndexButtonClick ( {
162155 dbName,
163156 collectionName,
164- inputQuery : sanitizedInputQuery ,
157+ query : sanitizedInputQuery ,
165158 } ) ;
166- setHasNewChanges ( false ) ;
167- } , [ inputQuery , onSuggestedIndexButtonClick , dbName , collectionName ] ) ;
159+ } , [ query , onSuggestedIndexButtonClick , dbName , collectionName ] ) ;
168160
169161 const handleSuggestedIndexButtonClick = ( ) => {
170162 generateSuggestedIndexes ( ) ;
@@ -175,39 +167,37 @@ const QueryFlowSection = ({
175167
176168 const handleQueryInputChange = useCallback (
177169 ( text : string ) => {
178- setInputQuery ( text ) ;
179- setHasNewChanges ( true ) ;
180- onQueryUpdated ( ) ;
170+ onQueryUpdated ( { query : text , hasQueryChanges : true } ) ;
181171 } ,
182- [ onQueryUpdated , setInputQuery ]
172+ [ onQueryUpdated ]
183173 ) ;
184174
185175 const isFetchingIndexSuggestions = fetchingSuggestionsState === 'fetching' ;
186176
187177 useMemo ( ( ) => {
188- let _isShowSuggestionsButtonDisabled = ! hasNewChanges ;
178+ let _isShowSuggestionsButtonDisabled = ! hasQueryChanges ;
189179 try {
190- parseFilter ( inputQuery ) ;
180+ parseFilter ( query ) ;
191181
192- if ( ! inputQuery . startsWith ( '{' ) || ! inputQuery . endsWith ( '}' ) ) {
182+ if ( ! query . startsWith ( '{' ) || ! query . endsWith ( '}' ) ) {
193183 _isShowSuggestionsButtonDisabled = true ;
194184 }
195185 } catch {
196186 _isShowSuggestionsButtonDisabled = true ;
197187 } finally {
198188 setIsShowSuggestionsButtonDisabled ( _isShowSuggestionsButtonDisabled ) ;
199189 }
200- } , [ hasNewChanges , inputQuery ] ) ;
201-
202- useEffect ( ( ) => {
203- // If there is an initial query from the insights nudge, we generate suggested indexes
204- if ( initialQuery !== null ) {
205- generateSuggestedIndexes ( ) ;
206- }
207- // we do not want to update this when the initialQuery changes
208- // this should just be done when the component first renders
209- // eslint-disable-next-line
210- } , [ ] ) ;
190+ } , [ hasQueryChanges , query ] ) ;
191+
192+ // useEffect(() => {
193+ // // If there is an initial query from the insights nudge, we generate suggested indexes
194+ // if (initialQuery !== null) {
195+ // generateSuggestedIndexes();
196+ // }
197+ // // we do not want to update this when the initialQuery changes
198+ // // this should just be done when the component first renders
199+ // // eslint-disable-next-line
200+ // }, []);
211201
212202 return (
213203 < >
@@ -235,7 +225,7 @@ const QueryFlowSection = ({
235225 showAnnotationsGutter = { false }
236226 copyable = { false }
237227 formattable = { false }
238- text = { inputQuery }
228+ text = { query }
239229 onChangeText = { ( text ) => handleQueryInputChange ( text ) }
240230 placeholder = "Type a query: { field: 'value' }"
241231 completer = { completer }
@@ -300,12 +290,16 @@ const mapState = ({ createIndex }: RootState) => {
300290 indexSuggestions,
301291 sampleDocs,
302292 fetchingSuggestionsState,
293+ query,
294+ initialQuery,
303295 hasQueryChanges,
304296 } = createIndex ;
305297 return {
306298 indexSuggestions,
307299 sampleDocs,
308300 fetchingSuggestionsState,
301+ query,
302+ initialQuery,
309303 hasQueryChanges,
310304 } ;
311305} ;
0 commit comments