Skip to content

Commit 7dd2d22

Browse files
committed
working version of the state management
1 parent 716de6e commit 7dd2d22

File tree

4 files changed

+167
-79
lines changed

4 files changed

+167
-79
lines changed

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

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import React, { useMemo, useState } from 'react';
1+
import React, { useMemo } from 'react';
22
import {
33
css,
44
spacing,
@@ -78,16 +78,6 @@ function CreateIndexForm({
7878

7979
const track = useTelemetry();
8080

81-
const [coveredQueriesObj, setCoveredQueriesObj] = useState<{
82-
coveredQueries: JSX.Element;
83-
optimalQueries: string | JSX.Element;
84-
showCoveredQueries: boolean;
85-
}>({
86-
coveredQueries: <></>,
87-
optimalQueries: '',
88-
showCoveredQueries: false,
89-
});
90-
9181
const schemaFields = useAutocompleteFields(namespace);
9282
const schemaFieldNames = useMemo(() => {
9383
return schemaFields
@@ -165,8 +155,6 @@ function CreateIndexForm({
165155
onRemoveFieldClick={onRemoveFieldClick}
166156
/>
167157
}
168-
coveredQueriesObj={coveredQueriesObj}
169-
setCoveredQueriesObj={setCoveredQueriesObj}
170158
/>
171159
) : (
172160
// Control UI

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

Lines changed: 34 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,16 @@ import React, { useState, useCallback, useEffect } from 'react';
1717
import {
1818
errorCleared,
1919
errorEncountered,
20+
fetchCoveredQueries,
2021
type Field,
22+
type CoveredQueriesFetchedProps,
2123
} from '../../modules/create-index';
2224
import MDBCodeViewer from './mdb-code-viewer';
2325
import { areAllFieldsFilledIn } from '../../utils/create-index-modal-validation';
2426
import { connect } from 'react-redux';
2527
import type { TrackFunction } from '@mongodb-js/compass-telemetry/provider';
2628
import { useTelemetry } from '@mongodb-js/compass-telemetry/provider';
29+
import type { RootState } from '../../modules';
2730

2831
const flexContainerStyles = css({
2932
display: 'flex',
@@ -107,16 +110,15 @@ export type IndexFlowSectionProps = {
107110
collectionName: string;
108111
onErrorEncountered: (error: string) => void;
109112
onErrorCleared: () => void;
110-
coveredQueriesObj: {
111-
coveredQueries: JSX.Element;
112-
optimalQueries: string | JSX.Element;
113-
showCoveredQueries: boolean;
114-
};
115-
setCoveredQueriesObj: (coveredQueriesObj: {
116-
coveredQueries: JSX.Element;
117-
optimalQueries: string | JSX.Element;
118-
showCoveredQueries: boolean;
119-
}) => void;
113+
onCoveredQueriesFetched: ({
114+
coveredQueries,
115+
optimalQueries,
116+
showCoveredQueries,
117+
}: CoveredQueriesFetchedProps) => void;
118+
coveredQueries: JSX.Element | null;
119+
optimalQueries: string | JSX.Element | null;
120+
showCoveredQueries: boolean;
121+
hasIndexFieldChanges: boolean;
120122
};
121123

122124
const generateCoveredQueries = (
@@ -204,13 +206,15 @@ const IndexFlowSection = ({
204206
collectionName,
205207
onErrorEncountered,
206208
onErrorCleared,
207-
coveredQueriesObj,
208-
setCoveredQueriesObj,
209+
onCoveredQueriesFetched,
210+
coveredQueries,
211+
optimalQueries,
212+
showCoveredQueries,
213+
hasIndexFieldChanges,
209214
}: IndexFlowSectionProps) => {
210215
const darkMode = useDarkMode();
211216
const [isCodeEquivalentToggleChecked, setIsCodeEquivalentToggleChecked] =
212217
useState(false);
213-
const [hasFieldChanges, setHasFieldChanges] = useState(false);
214218

215219
const hasUnsupportedQueryTypes = fields.some((field) => {
216220
return field.type === '2dsphere' || field.type === 'text';
@@ -220,7 +224,7 @@ const IndexFlowSection = ({
220224
const isCoveredQueriesButtonDisabled =
221225
!areAllFieldsFilledIn(fields) ||
222226
hasUnsupportedQueryTypes ||
223-
!hasFieldChanges;
227+
!hasIndexFieldChanges;
224228

225229
const indexNameTypeMap = fields.reduce<Record<string, string>>(
226230
(accumulator, currentValue) => {
@@ -242,26 +246,20 @@ const IndexFlowSection = ({
242246
});
243247

244248
try {
245-
setCoveredQueriesObj({
249+
onCoveredQueriesFetched({
246250
coveredQueries: generateCoveredQueries(coveredQueriesArr, track),
247251
optimalQueries: generateOptimalQueries(coveredQueriesArr),
248252
showCoveredQueries: true,
249253
});
250254
} catch (e) {
251255
onErrorEncountered(e instanceof Error ? e.message : String(e));
252256
}
253-
254-
setHasFieldChanges(false);
255-
}, [fields, onErrorEncountered, setCoveredQueriesObj, track]);
257+
}, [fields, onCoveredQueriesFetched, onErrorEncountered, track]);
256258

257259
useEffect(() => {
258-
setHasFieldChanges(true);
259260
onErrorCleared();
260261
}, [fields, onErrorCleared]);
261262

262-
const { coveredQueries, optimalQueries, showCoveredQueries } =
263-
coveredQueriesObj;
264-
265263
return (
266264
<div>
267265
<div
@@ -424,13 +422,25 @@ const IndexFlowSection = ({
424422
);
425423
};
426424

427-
const mapState = () => {
428-
return {};
425+
const mapState = ({ createIndex }: RootState) => {
426+
const {
427+
coveredQueries,
428+
optimalQueries,
429+
showCoveredQueries,
430+
hasIndexFieldChanges,
431+
} = createIndex;
432+
return {
433+
coveredQueries,
434+
optimalQueries,
435+
showCoveredQueries,
436+
hasIndexFieldChanges,
437+
};
429438
};
430439

431440
const mapDispatch = {
432441
onErrorEncountered: errorEncountered,
433442
onErrorCleared: errorCleared,
443+
onCoveredQueriesFetched: fetchCoveredQueries,
434444
};
435445

436446
export default connect(mapState, mapDispatch)(IndexFlowSection);

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
@@ -22,6 +22,7 @@ import type {
2222
IndexSuggestionState,
2323
SuggestedIndexFetchedProps,
2424
} from '../../modules/create-index';
25+
import { queryUpdated } from '../../modules/create-index';
2526
import { connect } from 'react-redux';
2627
import { parseFilter } from 'mongodb-query-parser';
2728
import { useTelemetry } from '@mongodb-js/compass-telemetry/provider';
@@ -96,25 +97,6 @@ const insightStyles = css({
9697
height: spacing[500],
9798
});
9899

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-
118100
const QueryFlowSection = ({
119101
schemaFields,
120102
serverVersion,
@@ -126,6 +108,8 @@ const QueryFlowSection = ({
126108
initialQuery,
127109
inputQuery,
128110
setInputQuery,
111+
hasQueryChanges,
112+
onQueryUpdated,
129113
}: {
130114
schemaFields: { name: string; description?: string }[];
131115
serverVersion: string;
@@ -141,13 +125,17 @@ const QueryFlowSection = ({
141125
initialQuery: Document | null;
142126
inputQuery: string;
143127
setInputQuery: (query: string) => void;
128+
hasQueryChanges: boolean; // to keep track of the state between the tab flows
129+
onQueryUpdated: () => void;
144130
}) => {
145131
const track = useTelemetry();
146132
const darkMode = useDarkMode();
147133

134+
// if initialQuery isnt null then we should have new changes
148135
const [hasNewChanges, setHasNewChanges] = React.useState(
149-
initialQuery !== null
136+
initialQuery !== null || hasQueryChanges
150137
);
138+
151139
const [isShowSuggestionsButtonDisabled, setIsShowSuggestionsButtonDisabled] =
152140
React.useState(true);
153141

@@ -176,7 +164,7 @@ const QueryFlowSection = ({
176164
inputQuery: sanitizedInputQuery,
177165
});
178166
setHasNewChanges(false);
179-
}, [inputQuery, dbName, collectionName, onSuggestedIndexButtonClick]);
167+
}, [inputQuery, onSuggestedIndexButtonClick, dbName, collectionName]);
180168

181169
const handleSuggestedIndexButtonClick = () => {
182170
generateSuggestedIndexes();
@@ -189,28 +177,28 @@ const QueryFlowSection = ({
189177
(text: string) => {
190178
setInputQuery(text);
191179
setHasNewChanges(true);
180+
onQueryUpdated();
192181
},
193-
[setInputQuery]
182+
[onQueryUpdated, setInputQuery]
194183
);
195184

196185
const isFetchingIndexSuggestions = fetchingSuggestionsState === 'fetching';
197186

198187
useMemo(() => {
199-
const isQueryInvalid = validateQuery({
200-
inputQuery,
201-
defaultState: !hasNewChanges,
202-
});
203-
setIsShowSuggestionsButtonDisabled(isQueryInvalid);
188+
let _isShowSuggestionsButtonDisabled = !hasNewChanges;
189+
try {
190+
parseFilter(inputQuery);
191+
192+
if (!inputQuery.startsWith('{') || !inputQuery.endsWith('}')) {
193+
_isShowSuggestionsButtonDisabled = true;
194+
}
195+
} catch {
196+
_isShowSuggestionsButtonDisabled = true;
197+
} finally {
198+
setIsShowSuggestionsButtonDisabled(_isShowSuggestionsButtonDisabled);
199+
}
204200
}, [hasNewChanges, inputQuery]);
205201

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-
214202
useEffect(() => {
215203
// If there is an initial query from the insights nudge, we generate suggested indexes
216204
if (initialQuery !== null) {
@@ -305,17 +293,23 @@ const QueryFlowSection = ({
305293
};
306294

307295
const mapState = ({ createIndex }: RootState) => {
308-
const { indexSuggestions, sampleDocs, fetchingSuggestionsState } =
309-
createIndex;
296+
const {
297+
indexSuggestions,
298+
sampleDocs,
299+
fetchingSuggestionsState,
300+
hasQueryChanges,
301+
} = createIndex;
310302
return {
311303
indexSuggestions,
312304
sampleDocs,
313305
fetchingSuggestionsState,
306+
hasQueryChanges,
314307
};
315308
};
316309

317310
const mapDispatch = {
318311
onSuggestedIndexButtonClick: fetchIndexSuggestions,
312+
onQueryUpdated: queryUpdated,
319313
};
320314

321315
export default connect(mapState, mapDispatch)(QueryFlowSection);

0 commit comments

Comments
 (0)