Skip to content

The easy-to-use open source Business Intelligence and Embedded Analytics tool that lets everyone work with data :bar_chart:

fix: changing db clears drafted inline sql prompt (#74515)

Maksym Yakubychc1f058ba961e
diff --git a/e2e/test/scenarios/metabot/native-sql-generation.cy.spec.ts b/e2e/test/scenarios/metabot/native-sql-generation.cy.spec.ts
index 786c0d18e91..f76aa4a50a2 100644
--- a/e2e/test/scenarios/metabot/native-sql-generation.cy.spec.ts
+++ b/e2e/test/scenarios/metabot/native-sql-generation.cy.spec.ts
@@ -173,15 +173,14 @@ describe("Native SQL generation", () => {
});
acceptButton().should("be.visible");
- // change the selected database, should close the input
+ // change the selected database, should keep the input open
rejectButton().click();
toggleInlineSQLPrompt();
inlinePrompt().should("be.visible");
H.NativeEditor.selectDataSource("QA Postgres12");
- inlinePrompt().should("not.exist");
+ inlinePrompt().should("be.visible");
- // open again, send a prompt, req.body.history should be empty
- toggleInlineSQLPrompt();
+ // send a prompt, req.body.history should be empty
inlinePromptInput().click();
cy.realType("select something", { pressDelay: 10 });
H.mockMetabotResponse({
diff --git a/frontend/src/metabase/metabot/components/MetabotInlineSQLPrompt/useInlineSQLPrompt.tsx b/frontend/src/metabase/metabot/components/MetabotInlineSQLPrompt/useInlineSQLPrompt.tsx
index 98b90f0f45b..0d98c12299d 100644
--- a/frontend/src/metabase/metabot/components/MetabotInlineSQLPrompt/useInlineSQLPrompt.tsx
+++ b/frontend/src/metabase/metabot/components/MetabotInlineSQLPrompt/useInlineSQLPrompt.tsx
@@ -88,14 +88,6 @@ export function useInlineSQLPrompt(
return portalTarget?.view?.state.doc.toString() ?? "";
}, [portalTarget?.view]);
- const prevDatabaseIdRef = useRef(databaseId);
- useEffect(() => {
- if (prevDatabaseIdRef.current !== databaseId) {
- setPromptValue("");
- prevDatabaseIdRef.current = databaseId;
- }
- }, [databaseId]);
-
const generatedSqlRef = useRef(generatedSource);
generatedSqlRef.current = generatedSource;
@@ -123,14 +115,22 @@ export function useInlineSQLPrompt(
[generatedSource, hideInput],
);
+ const prevDatabaseIdRef = useRef(databaseId);
+ useEffect(() => {
+ if (prevDatabaseIdRef.current !== databaseId) {
+ resetSuggestionState();
+ prevDatabaseIdRef.current = databaseId;
+ }
+ }, [databaseId, resetSuggestionState]);
+
useEffect(
- function resetOnDbChangeAndUnmount() {
+ function resetOnUnmount() {
return () => {
hideInputRef.current();
resetSuggestionState();
};
},
- [resetSuggestionState, databaseId],
+ [resetSuggestionState],
);
const resetInput = useCallback(() => {