@@ -20,6 +20,7 @@ import type {
2020 SuggestedIndexFetchedProps ,
2121} from '../../modules/create-index' ;
2222import { connect } from 'react-redux' ;
23+ import { parseFilter } from 'mongodb-query-parser' ;
2324
2425const inputQueryContainerStyles = css ( {
2526 display : 'flex' ,
@@ -97,6 +98,8 @@ const QueryFlowSection = ({
9798 fetchingSuggestionsState : IndexSuggestionState ;
9899} ) => {
99100 const [ inputQuery , setInputQuery ] = React . useState ( '' ) ;
101+ const [ hasNewChanges , setHasNewChanges ] = React . useState ( false ) ;
102+
100103 const completer = useMemo (
101104 ( ) =>
102105 createQueryAutocompleter ( {
@@ -121,9 +124,24 @@ const QueryFlowSection = ({
121124 collectionName,
122125 inputQuery : sanitizedInputQuery ,
123126 } ) ;
127+
128+ setHasNewChanges ( false ) ;
124129 } , [ inputQuery , dbName , collectionName , onSuggestedIndexButtonClick ] ) ;
125130
131+ const handleQueryInputChange = useCallback ( ( text : string ) => {
132+ setInputQuery ( text ) ;
133+ setHasNewChanges ( true ) ;
134+ } , [ ] ) ;
135+
126136 const isFetchingIndexSuggestions = fetchingSuggestionsState === 'fetching' ;
137+ let isShowSuggestionsButtonDisabled = ! hasNewChanges ;
138+
139+ // Validate query upon typing
140+ try {
141+ parseFilter ( inputQuery ) ;
142+ } catch ( e ) {
143+ isShowSuggestionsButtonDisabled = true ;
144+ }
127145
128146 return (
129147 < >
@@ -144,7 +162,7 @@ const QueryFlowSection = ({
144162 copyable = { false }
145163 formattable = { false }
146164 text = { inputQuery }
147- onChangeText = { ( text ) => setInputQuery ( text ) }
165+ onChangeText = { ( text ) => handleQueryInputChange ( text ) }
148166 placeholder = "Type a query: { field: 'value' }"
149167 completer = { completer }
150168 className = { codeEditorStyles }
@@ -156,6 +174,7 @@ const QueryFlowSection = ({
156174 onClick = { handleSuggestedIndexButtonClick }
157175 className = { suggestedIndexButtonStyles }
158176 size = "small"
177+ disabled = { isShowSuggestionsButtonDisabled }
159178 >
160179 Show suggested index
161180 </ Button >
0 commit comments