Skip to content

Commit 247d46f

Browse files
committed
fix error clearing, remove suggestions from start (need to fix), move query + initialQuery to redux
1 parent 42eff87 commit 247d46f

File tree

4 files changed

+85
-103
lines changed

4 files changed

+85
-103
lines changed

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

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,6 @@ function CreateIndexForm({
6565
onRemoveFieldClick,
6666
onTabClick,
6767
showIndexesGuidanceVariant,
68-
query,
6968
}: CreateIndexFormProps) {
7069
const { id: connectionId } = useConnectionInfo();
7170
const rollingIndexesFeatureEnabled = !!usePreference('enableRollingIndexes');
@@ -93,9 +92,6 @@ function CreateIndexForm({
9392
showIndexesGuidanceVariant && currentTab === 'IndexFlow';
9493
const showIndexesGuidanceQueryFlow =
9594
showIndexesGuidanceVariant && currentTab === 'QueryFlow';
96-
const [inputQuery, setInputQuery] = React.useState(
97-
query ? JSON.stringify(query, null, 2) : ''
98-
);
9995

10096
const { database: dbName, collection: collectionName } = toNS(namespace);
10197

@@ -180,9 +176,6 @@ function CreateIndexForm({
180176
serverVersion={serverVersion}
181177
dbName={dbName}
182178
collectionName={collectionName}
183-
initialQuery={query}
184-
inputQuery={inputQuery}
185-
setInputQuery={setInputQuery}
186179
/>
187180
)}
188181

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

Lines changed: 4 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,11 @@ import {
1313
Tooltip,
1414
useDarkMode,
1515
} from '@mongodb-js/compass-components';
16-
import React, { useState, useCallback, useEffect } from 'react';
16+
import React, { useState, useCallback } from 'react';
1717
import {
18-
errorCleared,
1918
errorEncountered,
2019
fetchCoveredQueries,
2120
type Field,
22-
type CoveredQueriesFetchedProps,
2321
} from '../../modules/create-index';
2422
import MDBCodeViewer from './mdb-code-viewer';
2523
import { areAllFieldsFilledIn } from '../../utils/create-index-modal-validation';
@@ -109,8 +107,7 @@ export type IndexFlowSectionProps = {
109107
dbName: string;
110108
collectionName: string;
111109
onErrorEncountered: (error: string) => void;
112-
onErrorCleared: () => void;
113-
onCoveredQueriesFetched: ({ fields }: CoveredQueriesFetchedProps) => void;
110+
onCoveredQueriesFetched: () => void;
114111
coveredQueriesArr: Array<Record<string, number>> | null;
115112
hasIndexFieldChanges: boolean;
116113
};
@@ -211,7 +208,6 @@ const IndexFlowSection = ({
211208
dbName,
212209
collectionName,
213210
onErrorEncountered,
214-
onErrorCleared,
215211
onCoveredQueriesFetched,
216212
coveredQueriesArr,
217213
hasIndexFieldChanges,
@@ -246,17 +242,11 @@ const IndexFlowSection = ({
246242
});
247243

248244
try {
249-
onCoveredQueriesFetched({
250-
fields,
251-
});
245+
onCoveredQueriesFetched();
252246
} catch (e) {
253247
onErrorEncountered(e instanceof Error ? e.message : String(e));
254248
}
255-
}, [fields, onCoveredQueriesFetched, onErrorEncountered, track]);
256-
257-
useEffect(() => {
258-
onErrorCleared();
259-
}, [fields, onErrorCleared]);
249+
}, [onCoveredQueriesFetched, onErrorEncountered, track]);
260250

261251
const coveredQueries = generateCoveredQueries(coveredQueriesArr, track);
262252
const optimalQueries = generateOptimalQueries(coveredQueriesArr);
@@ -434,7 +424,6 @@ const mapState = ({ createIndex }: RootState) => {
434424

435425
const mapDispatch = {
436426
onErrorEncountered: errorEncountered,
437-
onErrorCleared: errorCleared,
438427
onCoveredQueriesFetched: fetchCoveredQueries,
439428
};
440429

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

Lines changed: 31 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,7 @@ import {
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';
1312
import { css, spacing } from '@mongodb-js/compass-components';
1413
import {
1514
CodemirrorMultilineEditor,
@@ -20,6 +19,7 @@ import type { RootState } from '../../modules';
2019
import { fetchIndexSuggestions } from '../../modules/create-index';
2120
import type {
2221
IndexSuggestionState,
22+
QueryUpdatedProps,
2323
SuggestedIndexFetchedProps,
2424
} from '../../modules/create-index';
2525
import { 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

Comments
 (0)