Skip to content

Commit 65f545c

Browse files
committed
fix disabled state of suggested index button when switching between tabs
1 parent 8e9c13f commit 65f545c

File tree

1 file changed

+41
-18
lines changed

1 file changed

+41
-18
lines changed

packages/compass-indexes/src/components/create-index-form/query-flow-section.tsx

Lines changed: 41 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,25 @@ const insightStyles = css({
9696
height: spacing[500],
9797
});
9898

99+
const validateQuery = ({
100+
inputQuery,
101+
defaultState,
102+
}: {
103+
inputQuery: string;
104+
defaultState: boolean;
105+
}) => {
106+
let isShowSuggestionsButtonDisabled = defaultState;
107+
try {
108+
parseFilter(inputQuery);
109+
if (!inputQuery.startsWith('{') || !inputQuery.endsWith('}')) {
110+
isShowSuggestionsButtonDisabled = true;
111+
}
112+
} catch {
113+
isShowSuggestionsButtonDisabled = true;
114+
}
115+
return isShowSuggestionsButtonDisabled;
116+
};
117+
99118
const QueryFlowSection = ({
100119
schemaFields,
101120
serverVersion,
@@ -166,34 +185,38 @@ const QueryFlowSection = ({
166185
});
167186
};
168187

169-
const handleQueryInputChange = useCallback((text: string) => {
170-
setInputQuery(text);
171-
setHasNewChanges(true);
172-
}, []);
188+
const handleQueryInputChange = useCallback(
189+
(text: string) => {
190+
setInputQuery(text);
191+
setHasNewChanges(true);
192+
},
193+
[setInputQuery]
194+
);
173195

174196
const isFetchingIndexSuggestions = fetchingSuggestionsState === 'fetching';
175197

176-
// Validate query upon typing
177198
useMemo(() => {
178-
let _isShowSuggestionsButtonDisabled = !hasNewChanges;
179-
try {
180-
parseFilter(inputQuery);
181-
182-
if (!inputQuery.startsWith('{') || !inputQuery.endsWith('}')) {
183-
_isShowSuggestionsButtonDisabled = true;
184-
}
185-
} catch {
186-
_isShowSuggestionsButtonDisabled = true;
187-
} finally {
188-
setIsShowSuggestionsButtonDisabled(_isShowSuggestionsButtonDisabled);
189-
}
199+
const isQueryInvalid = validateQuery({
200+
inputQuery,
201+
defaultState: !hasNewChanges,
202+
});
203+
setIsShowSuggestionsButtonDisabled(isQueryInvalid);
190204
}, [hasNewChanges, inputQuery]);
191205

206+
// We need to validate the changes upon initial render to possibly enable the button
207+
useEffect(() => {
208+
const isQueryInvalid = validateQuery({ inputQuery, defaultState: false });
209+
setIsShowSuggestionsButtonDisabled(isQueryInvalid);
210+
// only run this validation once on mount
211+
// eslint-disable-next-line react-hooks/exhaustive-deps
212+
}, []);
213+
192214
useEffect(() => {
215+
// If there is an initial query from the insights nudge, we generate suggested indexes
193216
if (initialQuery !== null) {
194217
generateSuggestedIndexes();
195218
}
196-
}, [initialQuery]);
219+
}, [generateSuggestedIndexes, initialQuery]);
197220

198221
return (
199222
<>

0 commit comments

Comments
 (0)