Skip to content

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

fix(sdk): open SQL editor when Metabot navigates to native hash URL (#76738)

Mahatthana (Kelvin) Nomsawadi2e33d53027ec
diff --git a/e2e/test-component/scenarios/embedding-sdk/metabot-question.cy.spec.tsx b/e2e/test-component/scenarios/embedding-sdk/metabot-question.cy.spec.tsx
index 4bc2292f5ab..c99ffc99467 100644
--- a/e2e/test-component/scenarios/embedding-sdk/metabot-question.cy.spec.tsx
+++ b/e2e/test-component/scenarios/embedding-sdk/metabot-question.cy.spec.tsx
@@ -35,6 +35,11 @@ const metabotResponseWithNavigateTo = `${metabotResponse}
const metabotRetryResponse = `0:"Retry: Here is the [question link](${adHocQuestionPath})"`;
+// Captured navigate_to event verbatim from a real embedding Metabot response
+// (database id swapped from 2 to 1 to match this env's Sample Database).
+const metabotResponseWithSqlEditor = `0:"I'll direct you to the SQL editor for the Sample Database so you can write a query for the most popular products!"
+2:{"type":"navigate_to","version":1,"value":"/question#eyJkYXRhc2V0X3F1ZXJ5Ijp7ImRhdGFiYXNlIjoxLCJ0eXBlIjoibmF0aXZlIiwibmF0aXZlIjp7InF1ZXJ5IjoiIn19fQ=="}`;
+
describe("scenarios > embedding-sdk > metabot-question", () => {
const setup = (response: string) => {
signInAsAdminAndEnableEmbeddingSdk();
@@ -71,6 +76,17 @@ describe("scenarios > embedding-sdk > metabot-question", () => {
mockAuthProviderAndJwtSignIn();
};
+ it("should show the SQL editor when Metabot navigates to the SQL editor page", () => {
+ setup(metabotResponseWithSqlEditor);
+
+ mountSdkContent(<MetabotQuestion />);
+
+ getSdkRoot().within(() => {
+ cy.findByTestId("metabot-chat-input").type("Open the SQL editor {enter}");
+ cy.findByTestId("native-query-editor-container").should("be.visible");
+ });
+ });
+
it("should show drill-through results after drilling from a metabot question", () => {
setup(metabotResponseWithNavigateTo);
diff --git a/frontend/src/embedding-sdk-bundle/components/private/SdkAdHocQuestion/SdkAdHocQuestion.tsx b/frontend/src/embedding-sdk-bundle/components/private/SdkAdHocQuestion/SdkAdHocQuestion.tsx
index ae5281da29a..0cac87f8bd4 100644
--- a/frontend/src/embedding-sdk-bundle/components/private/SdkAdHocQuestion/SdkAdHocQuestion.tsx
+++ b/frontend/src/embedding-sdk-bundle/components/private/SdkAdHocQuestion/SdkAdHocQuestion.tsx
@@ -2,6 +2,7 @@ import { useMemo } from "react";
import type { SdkQuestionProps } from "embedding-sdk-bundle/components/public/SdkQuestion/SdkQuestion";
import { SdkQuestion } from "embedding-sdk-bundle/components/public/SdkQuestion/SdkQuestion";
+import type { SdkQuestionId } from "embedding-sdk-bundle/types/question";
import { deserializeCard, parseHash } from "metabase/common/utils/card";
import * as Urls from "metabase/urls";
@@ -60,9 +61,6 @@ export const SdkAdHocQuestion = ({
[questionPath],
);
- // If we cannot extract an entity ID from the slug, assume we are creating a new question.
- const questionId = Urls.extractEntityId(params.slug) ?? null;
-
const { options, deserializedCard } = useMemo(() => {
const { options, serializedCard } = parseHash(location.hash);
const deserializedCard = serializedCard
@@ -72,6 +70,11 @@ export const SdkAdHocQuestion = ({
return { options, deserializedCard };
}, [location.hash]);
+ const questionId = resolveQuestionId(
+ params.slug,
+ deserializedCard as { dataset_query?: { type?: string } } | undefined,
+ );
+
return (
<SdkQuestion
questionId={questionId}
@@ -101,6 +104,25 @@ export const SdkAdHocQuestion = ({
);
};
+/**
+ * Derives the questionId from URL slug and deserialized card.
+ * Returns "new-native" for hash-encoded native cards (e.g. Metabot SQL editor navigation),
+ * a numeric/string ID for saved questions, or null for new notebook questions.
+ */
+export function resolveQuestionId(
+ slug: string | undefined,
+ deserializedCard: { dataset_query?: { type?: string } } | undefined,
+): SdkQuestionId | null {
+ const extractedId = Urls.extractEntityId(slug) ?? null;
+ if (extractedId !== null) {
+ return extractedId;
+ }
+ if (deserializedCard?.dataset_query?.type === "native") {
+ return "new-native";
+ }
+ return null;
+}
+
/**
* This generates route parameters based on the provided URL path
* to load the interactive questions. See [use-load-question.ts]
diff --git a/frontend/src/embedding-sdk-bundle/components/private/SdkAdHocQuestion/SdkAdHocQuestion.unit.spec.tsx b/frontend/src/embedding-sdk-bundle/components/private/SdkAdHocQuestion/SdkAdHocQuestion.unit.spec.tsx
index 37ece96c890..de2dd63cc94 100644
--- a/frontend/src/embedding-sdk-bundle/components/private/SdkAdHocQuestion/SdkAdHocQuestion.unit.spec.tsx
+++ b/frontend/src/embedding-sdk-bundle/components/private/SdkAdHocQuestion/SdkAdHocQuestion.unit.spec.tsx
@@ -1,4 +1,4 @@
-import { getQuestionParameters } from "./SdkAdHocQuestion";
+import { getQuestionParameters, resolveQuestionId } from "./SdkAdHocQuestion";
describe("getQuestionParameters", () => {
it("should generate proper URL params for ad-hoc question", () => {
@@ -28,3 +28,39 @@ describe("getQuestionParameters", () => {
});
});
});
+
+describe("resolveQuestionId", () => {
+ it("returns 'new-native' for a native card hash with no slug", () => {
+ expect(
+ resolveQuestionId(undefined, {
+ dataset_query: { type: "native" },
+ }),
+ ).toBe("new-native");
+ });
+
+ it("returns null for a structured (MBQL) card hash with no slug", () => {
+ expect(
+ resolveQuestionId(undefined, {
+ dataset_query: { type: "query" },
+ }),
+ ).toBeNull();
+ });
+
+ it("returns numeric ID for a saved question slug", () => {
+ expect(
+ resolveQuestionId("109-days-when-orders-were-added", undefined),
+ ).toBe(109);
+ });
+
+ it("returns null when there is no slug and no deserialized card", () => {
+ expect(resolveQuestionId(undefined, undefined)).toBeNull();
+ });
+
+ it("numeric slug takes precedence over a native card", () => {
+ expect(
+ resolveQuestionId("42-my-question", {
+ dataset_query: { type: "native" },
+ }),
+ ).toBe(42);
+ });
+});