Skip to content

Commit 455770f

Browse files
authored
feat: retain state between tab switches and generate suggestions from nudge flow CLOUDP-323832 (#7001)
* retain state between tab switches and generate suggestions from nudge flow * updated the usecallback portion * updated test typing
1 parent 3f4fbbf commit 455770f

File tree

3 files changed

+25
-8
lines changed

3 files changed

+25
-8
lines changed

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,9 @@ function CreateIndexForm({
9393
showIndexesGuidanceVariant && currentTab === 'IndexFlow';
9494
const showIndexesGuidanceQueryFlow =
9595
showIndexesGuidanceVariant && currentTab === 'QueryFlow';
96+
const [inputQuery, setInputQuery] = React.useState(
97+
query ? JSON.stringify(query, null, 2) : ''
98+
);
9699

97100
const { database: dbName, collection: collectionName } = toNS(namespace);
98101

@@ -178,6 +181,8 @@ function CreateIndexForm({
178181
dbName={dbName}
179182
collectionName={collectionName}
180183
initialQuery={query}
184+
inputQuery={inputQuery}
185+
setInputQuery={setInputQuery}
181186
/>
182187
)}
183188

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ describe('QueryFlowSection', () => {
1919
dbName={dbName}
2020
collectionName={collectionName}
2121
initialQuery={null}
22+
inputQuery={''}
23+
setInputQuery={() => {}}
2224
/>
2325
</Provider>
2426
);

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

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import {
99
useDarkMode,
1010
} from '@mongodb-js/compass-components';
1111
import type { Document } from 'mongodb';
12-
import React, { useMemo, useCallback } from 'react';
12+
import React, { useMemo, useCallback, useEffect } from 'react';
1313
import { css, spacing } from '@mongodb-js/compass-components';
1414
import {
1515
CodemirrorMultilineEditor,
@@ -105,6 +105,8 @@ const QueryFlowSection = ({
105105
indexSuggestions,
106106
fetchingSuggestionsState,
107107
initialQuery,
108+
inputQuery,
109+
setInputQuery,
108110
}: {
109111
schemaFields: { name: string; description?: string }[];
110112
serverVersion: string;
@@ -118,12 +120,12 @@ const QueryFlowSection = ({
118120
indexSuggestions: Record<string, number> | null;
119121
fetchingSuggestionsState: IndexSuggestionState;
120122
initialQuery: Document | null;
123+
inputQuery: string;
124+
setInputQuery: (query: string) => void;
121125
}) => {
122126
const track = useTelemetry();
123127
const darkMode = useDarkMode();
124-
const [inputQuery, setInputQuery] = React.useState(
125-
initialQuery ? JSON.stringify(initialQuery, null, 2) : ''
126-
);
128+
127129
const [hasNewChanges, setHasNewChanges] = React.useState(
128130
initialQuery !== null
129131
);
@@ -146,21 +148,23 @@ const QueryFlowSection = ({
146148
radius: editorContainerRadius,
147149
});
148150

149-
const handleSuggestedIndexButtonClick = useCallback(() => {
151+
const generateSuggestedIndexes = useCallback(() => {
150152
const sanitizedInputQuery = inputQuery.trim();
151153

152154
void onSuggestedIndexButtonClick({
153155
dbName,
154156
collectionName,
155157
inputQuery: sanitizedInputQuery,
156158
});
159+
setHasNewChanges(false);
160+
}, [inputQuery, dbName, collectionName, onSuggestedIndexButtonClick]);
157161

162+
const handleSuggestedIndexButtonClick = () => {
163+
generateSuggestedIndexes();
158164
track('Suggested Index Button Clicked', {
159165
context: 'Create Index Modal',
160166
});
161-
162-
setHasNewChanges(false);
163-
}, [inputQuery, dbName, collectionName, onSuggestedIndexButtonClick, track]);
167+
};
164168

165169
const handleQueryInputChange = useCallback((text: string) => {
166170
setInputQuery(text);
@@ -185,6 +189,12 @@ const QueryFlowSection = ({
185189
}
186190
}, [hasNewChanges, inputQuery]);
187191

192+
useEffect(() => {
193+
if (initialQuery !== null) {
194+
generateSuggestedIndexes();
195+
}
196+
}, [initialQuery]);
197+
188198
return (
189199
<>
190200
{initialQuery && (

0 commit comments

Comments
 (0)