Skip to content

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

Don't close InteractiveQuestion editor on Filters change (#67517)

Timofey Kachalove3f05d0cbb62
diff --git a/e2e/test-component/scenarios/embedding-sdk/create-question.cy.spec.tsx b/e2e/test-component/scenarios/embedding-sdk/create-question.cy.spec.tsx
index feb130601ff..3c2e0722aea 100644
--- a/e2e/test-component/scenarios/embedding-sdk/create-question.cy.spec.tsx
+++ b/e2e/test-component/scenarios/embedding-sdk/create-question.cy.spec.tsx
@@ -101,6 +101,7 @@ describe("scenarios > embedding-sdk > interactive-question > creating a question
});
getSdkRoot().findByText(expectedQuestionName).should("be.visible");
+ getSdkRoot().findByTestId("visualization-root").should("be.visible");
});
it("can save a question in a dashboard", () => {
diff --git a/e2e/test-component/scenarios/embedding-sdk/sdk-question.cy.spec.tsx b/e2e/test-component/scenarios/embedding-sdk/sdk-question.cy.spec.tsx
index 3e82dedfdee..0a52c5b2112 100644
--- a/e2e/test-component/scenarios/embedding-sdk/sdk-question.cy.spec.tsx
+++ b/e2e/test-component/scenarios/embedding-sdk/sdk-question.cy.spec.tsx
@@ -632,4 +632,108 @@ describe("scenarios > embedding-sdk > interactive-question", () => {
expect(interception.response?.statusCode).to.equal(200);
});
});
+
+ it("should stay in editor mode after adding a filter for the first time for an existing saved question (EMB-1077)", () => {
+ cy.get<string>("@questionId").then((questionId) => {
+ mountSdkContent(<InteractiveQuestion questionId={questionId} />);
+ });
+
+ getSdkRoot().within(() => {
+ cy.findByTestId("notebook-button").click();
+
+ cy.findByRole("button", { name: "Visualize" }).should("exist");
+
+ cy.findByTestId("step-data-0-0").within(() => {
+ cy.findAllByTestId("action-buttons").find(".Icon-filter").click();
+ });
+ });
+
+ H.popover().within(() => {
+ cy.findByText("Created At").click();
+ cy.findByText("Previous 7 days").click();
+ });
+
+ getSdkRoot().within(() => {
+ cy.findByRole("button", { name: "Visualize" }).should("exist");
+ });
+ });
+
+ it("should close the editor after modifying and saving an existing question in-place", () => {
+ cy.get<number>("@questionId").then((questionId) => {
+ mountSdkContent(
+ <InteractiveQuestion questionId={questionId} isSaveEnabled />,
+ );
+ });
+
+ getSdkRoot().within(() => {
+ cy.findByTestId("visualization-root").should("be.visible");
+ cy.findByTestId("notebook-button").click();
+
+ cy.findByTestId("step-data-0-0").within(() => {
+ cy.findAllByTestId("action-buttons").find(".Icon-filter").click();
+ });
+ });
+
+ H.popover().findByText("Product ID").click();
+ H.popover().within(() => {
+ cy.findByPlaceholderText("Enter an ID").type("1");
+ cy.findByText("Add filter").click();
+ });
+
+ getSdkRoot().within(() => {
+ cy.findByText("Back to visualization").should("be.visible");
+ cy.findByRole("button", { name: "Save" }).click();
+ });
+
+ H.modal().within(() => {
+ cy.findByText(/Replace original question/).click();
+ cy.findByRole("button", { name: "Save" }).click();
+ });
+
+ getSdkRoot().within(() => {
+ cy.findByText("Back to visualization").should("not.exist");
+ cy.findByTestId("visualization-root").should("be.visible");
+ });
+ });
+
+ it("should close the editor after modifying and saving an existing question as a new question", () => {
+ cy.get<number>("@questionId").then((questionId) => {
+ mountSdkContent(
+ <InteractiveQuestion questionId={questionId} isSaveEnabled />,
+ );
+ });
+
+ getSdkRoot().within(() => {
+ cy.findByTestId("visualization-root").should("be.visible");
+ cy.findByTestId("notebook-button").click();
+
+ cy.findByTestId("step-data-0-0").within(() => {
+ cy.findAllByTestId("action-buttons").find(".Icon-filter").click();
+ });
+ });
+
+ H.popover().findByText("Product ID").click();
+ H.popover().within(() => {
+ cy.findByPlaceholderText("Enter an ID").type("1");
+ cy.findByText("Add filter").click();
+ });
+
+ getSdkRoot().within(() => {
+ cy.findByText("Back to visualization").should("be.visible");
+ cy.findByRole("button", { name: "Save" }).click();
+ });
+
+ H.modal().within(() => {
+ cy.findByText("Save as new question").click();
+ cy.findByPlaceholderText("What is the name of your question?")
+ .clear()
+ .type("Orders Copy");
+ cy.findByRole("button", { name: "Save" }).click();
+ });
+
+ getSdkRoot().within(() => {
+ cy.findByText("Back to visualization").should("not.exist");
+ cy.findByTestId("visualization-root").should("be.visible");
+ });
+ });
});
diff --git a/frontend/src/embedding-sdk-bundle/components/private/SdkQuestionDefaultView/SdkQuestionDefaultView.tsx b/frontend/src/embedding-sdk-bundle/components/private/SdkQuestionDefaultView/SdkQuestionDefaultView.tsx
index 951d964e578..7749e5f5a5b 100644
--- a/frontend/src/embedding-sdk-bundle/components/private/SdkQuestionDefaultView/SdkQuestionDefaultView.tsx
+++ b/frontend/src/embedding-sdk-bundle/components/private/SdkQuestionDefaultView/SdkQuestionDefaultView.tsx
@@ -11,6 +11,7 @@ import {
} from "embedding-sdk-bundle/components/private/PublicComponentWrapper";
import { QuestionVisualization } from "embedding-sdk-bundle/components/private/SdkQuestion/components/Visualization";
import { SdkQuestion } from "embedding-sdk-bundle/components/public/SdkQuestion";
+import { useQuestionEditorSync } from "embedding-sdk-bundle/hooks/private/use-question-editor-sync";
import { useSdkBreadcrumbs } from "embedding-sdk-bundle/hooks/private/use-sdk-breadcrumb";
import { shouldRunCardQuery } from "embedding-sdk-bundle/lib/sdk-question";
import { useSdkSelector } from "embedding-sdk-bundle/store";
@@ -88,18 +89,20 @@ export const SdkQuestionDefaultView = ({
withDownloads,
onReset,
onNavigateBack,
+ queryQuestion,
} = useSdkQuestionContext();
const { isBreadcrumbEnabled, reportLocation } = useSdkBreadcrumbs();
const isGuestEmbed = useSdkSelector(getIsGuestEmbed);
- const isNewQuestion = originalId === "new";
const isQuestionSaved = question?.isSaved();
- const [
- isEditorOpen,
- { close: closeEditor, toggle: toggleEditor, open: openEditor },
- ] = useDisclosure(isNewQuestion && !isQuestionSaved);
+ const { isEditorOpen, closeEditor, toggleEditor } = useQuestionEditorSync({
+ originalId,
+ isQuestionSaved,
+ queryResults,
+ queryQuestion,
+ });
const [isSaveModalOpen, { open: openSaveModal, close: closeSaveModal }] =
useDisclosure(false);
@@ -114,17 +117,6 @@ export const SdkQuestionDefaultView = ({
return isNative;
}, [question]);
- useEffect(() => {
- if (isNewQuestion && !isQuestionSaved) {
- // When switching to new question, open the notebook editor
- openEditor();
- } else if (!isNewQuestion) {
- // When no longer in a notebook editor, switch back to visualization.
- // When a question is saved, also switch back to visualization.
- closeEditor();
- }
- }, [isNewQuestion, isQuestionSaved, openEditor, closeEditor]);
-
// When visualizing a question for the first time, there is no query result yet.
const isQueryResultLoading =
question && shouldRunCardQuery({ question, isGuestEmbed }) && !queryResults;
diff --git a/frontend/src/embedding-sdk-bundle/hooks/private/use-existing-question-editor-sync.ts b/frontend/src/embedding-sdk-bundle/hooks/private/use-existing-question-editor-sync.ts
new file mode 100644
index 00000000000..acb45cffd1d
--- /dev/null
+++ b/frontend/src/embedding-sdk-bundle/hooks/private/use-existing-question-editor-sync.ts
@@ -0,0 +1,45 @@
+import { useEffect } from "react";
+import { usePrevious } from "react-use";
+
+interface UseExistingQuestionEditorSyncOptions {
+ enabled: boolean;
+ originalId: string | number | null | undefined;
+ isQuestionSaved: boolean | undefined;
+ closeEditor: () => void;
+}
+
+/**
+ * Manages editor state for existing questions (originalId !== "new").
+ * - Closes the editor when transitioning from a new question to an existing one
+ * - Closes the editor when an existing question finishes loading
+ */
+export function useExistingQuestionEditorSync({
+ enabled,
+ originalId,
+ isQuestionSaved,
+ closeEditor,
+}: UseExistingQuestionEditorSyncOptions) {
+ const prevOriginalId = usePrevious(originalId);
+ const isPrevQuestionSaved = usePrevious(isQuestionSaved);
+
+ useEffect(() => {
+ if (!enabled) {
+ return;
+ }
+
+ const isTransitionToExistingQuestion =
+ prevOriginalId === "new" && originalId !== "new";
+ const isTransitionToSaved = !isPrevQuestionSaved && isQuestionSaved;
+
+ if (isTransitionToExistingQuestion || isTransitionToSaved) {
+ closeEditor();
+ }
+ }, [
+ prevOriginalId,
+ isPrevQuestionSaved,
+ enabled,
+ isQuestionSaved,
+ closeEditor,
+ originalId,
+ ]);
+}
diff --git a/frontend/src/embedding-sdk-bundle/hooks/private/use-new-question-editor-sync.ts b/frontend/src/embedding-sdk-bundle/hooks/private/use-new-question-editor-sync.ts
new file mode 100644
index 00000000000..ca70c9012c2
--- /dev/null
+++ b/frontend/src/embedding-sdk-bundle/hooks/private/use-new-question-editor-sync.ts
@@ -0,0 +1,57 @@
+import { useEffect } from "react";
+import { useLatest, usePrevious } from "react-use";
+
+interface UseNewQuestionEditorSyncOptions {
+ enabled: boolean;
+ isQuestionSaved: boolean | undefined;
+ queryResults: unknown;
+ queryQuestion: () => Promise<unknown>;
+ openEditor: () => void;
+ closeEditor: () => void;
+}
+
+/**
+ * Manages editor state for new questions
+ * - Opens the editor automatically when the question is unsaved
+ * - Closes the editor when the question is saved
+ * - Runs the query after saving if no results are available yet
+ * (handles the case when user saves without visualizing first)
+ */
+export function useNewQuestionEditorSync({
+ enabled,
+ isQuestionSaved,
+ queryResults,
+ queryQuestion,
+ openEditor,
+ closeEditor,
+}: UseNewQuestionEditorSyncOptions) {
+ const isPrevQuestionSaved = usePrevious(isQuestionSaved);
+ const queryResultsRef = useLatest(queryResults);
+ const queryQuestionRef = useLatest(queryQuestion);
+
+ useEffect(() => {
+ if (!enabled) {
+ return;
+ }
+
+ const isTransitionToSaved = !isPrevQuestionSaved && isQuestionSaved;
+
+ if (!isQuestionSaved) {
+ openEditor();
+ } else if (isTransitionToSaved) {
+ closeEditor();
+
+ if (!queryResultsRef.current) {
+ queryQuestionRef.current();
+ }
+ }
+ }, [
+ enabled,
+ isQuestionSaved,
+ isPrevQuestionSaved,
+ openEditor,
+ closeEditor,
+ queryResultsRef,
+ queryQuestionRef,
+ ]);
+}
diff --git a/frontend/src/embedding-sdk-bundle/hooks/private/use-question-editor-sync.ts b/frontend/src/embedding-sdk-bundle/hooks/private/use-question-editor-sync.ts
new file mode 100644
index 00000000000..15d9bc31add
--- /dev/null
+++ b/frontend/src/embedding-sdk-bundle/hooks/private/use-question-editor-sync.ts
@@ -0,0 +1,54 @@
+import { useDisclosure } from "@mantine/hooks";
+
+import { useExistingQuestionEditorSync } from "./use-existing-question-editor-sync";
+import { useNewQuestionEditorSync } from "./use-new-question-editor-sync";
+
+interface UseQuestionEditorSyncOptions {
+ originalId: string | number | null | undefined;
+ isQuestionSaved: boolean | undefined;
+ queryResults: unknown;
+ queryQuestion: () => Promise<unknown>;
+}
+
+/**
+ * Synchronizes the notebook editor open/close state with the question lifecycle.
+ *
+ * The hook manages automatic editor state transitions based on whether the user
+ * is working with a new question or an existing one:
+ */
+export function useQuestionEditorSync({
+ originalId,
+ isQuestionSaved,
+ queryResults,
+ queryQuestion,
+}: UseQuestionEditorSyncOptions) {
+ const isNewQuestion = originalId === "new";
+
+ const [
+ isEditorOpen,
+ { close: closeEditor, toggle: toggleEditor, open: openEditor },
+ ] = useDisclosure(isNewQuestion && !isQuestionSaved);
+
+ useNewQuestionEditorSync({
+ enabled: isNewQuestion,
+ isQuestionSaved,
+ queryResults,
+ queryQuestion,
+ openEditor,
+ closeEditor,
+ });
+
+ useExistingQuestionEditorSync({
+ enabled: !isNewQuestion,
+ originalId,
+ isQuestionSaved,
+ closeEditor,
+ });
+
+ return {
+ isEditorOpen,
+ openEditor,
+ closeEditor,
+ toggleEditor,
+ };
+}