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-bundle): Fix can't drill numeric string ID dashboard (#66819)

Mahatthana (Kelvin) Nomsawadi354d5da0d6f6
diff --git a/e2e/test-component/scenarios/embedding-sdk/interactive-dashboard.cy.spec.tsx b/e2e/test-component/scenarios/embedding-sdk/interactive-dashboard.cy.spec.tsx
index 72b11893dab..106b65b923b 100644
--- a/e2e/test-component/scenarios/embedding-sdk/interactive-dashboard.cy.spec.tsx
+++ b/e2e/test-component/scenarios/embedding-sdk/interactive-dashboard.cy.spec.tsx
@@ -63,6 +63,7 @@ describe("scenarios > embedding-sdk > interactive-dashboard", () => {
parameters: [DATE_FILTER],
}).then(({ body: dashboard }) => {
cy.wrap(dashboard.id).as("dashboardId");
+ cy.wrap(String(dashboard.id)).as("dashboardNumericStringId");
cy.wrap(dashboard.entity_id).as("dashboardEntityId");
});
@@ -210,41 +211,56 @@ describe("scenarios > embedding-sdk > interactive-dashboard", () => {
});
const idTypes = [
- { idType: "numeric id", dashboardIdAlias: "@dashboardId" },
- { idType: "entity id", dashboardIdAlias: "@dashboardEntityId" },
+ {
+ idType: "numeric ID",
+ dashboardIdAlias: "@dashboardId",
+ issueId: "(EMB-773)",
+ },
+ {
+ idType: "numeric string ID",
+ dashboardIdAlias: "@dashboardNumericStringId",
+ issueId: "(EMB-1120)",
+ },
+ {
+ idType: "entity ID",
+ dashboardIdAlias: "@dashboardEntityId",
+ issueId: "(EMB-773)",
+ },
];
- idTypes.forEach(({ idType, dashboardIdAlias }) => {
- it(`can go to dashcard and go back using a ${idType} dashboard (EMB-773)`, () => {
- cy.get(dashboardIdAlias).then((dashboardId) => {
- mountSdkContent(<InteractiveDashboard dashboardId={dashboardId} />);
- });
+ describe("Dashboard ID types", () => {
+ idTypes.forEach(({ idType, dashboardIdAlias, issueId }) => {
+ it(`can go to dashcard and go back using a ${idType} dashboard ${issueId}`, () => {
+ cy.get(dashboardIdAlias).then((dashboardId) => {
+ mountSdkContent(<InteractiveDashboard dashboardId={dashboardId} />);
+ });
- getSdkRoot().within(() => {
- H.getDashboardCard().findByText("Orders").click();
+ getSdkRoot().within(() => {
+ H.getDashboardCard().findByText("Orders").click();
- cy.findByTestId("interactive-question-result-toolbar").should(
- "be.visible",
- );
+ cy.findByTestId("interactive-question-result-toolbar").should(
+ "be.visible",
+ );
- cy.findByLabelText("Back to Orders in a dashboard").click();
- cy.findByText("Orders in a dashboard").should("be.visible");
- cy.findByText("Back to Orders in a dashboard").should("not.exist");
+ cy.findByLabelText("Back to Orders in a dashboard").click();
+ cy.findByText("Orders in a dashboard").should("be.visible");
+ cy.findByText("Back to Orders in a dashboard").should("not.exist");
+ });
});
- });
- it(`can drill a question and go back using a ${idType} dashboard (EMB-773)`, () => {
- cy.get(dashboardIdAlias).then((dashboardId) => {
- mountSdkContent(<InteractiveDashboard dashboardId={dashboardId} />);
- });
+ it(`can drill a question and go back using a ${idType} dashboard ${issueId}`, () => {
+ cy.get(dashboardIdAlias).then((dashboardId) => {
+ mountSdkContent(<InteractiveDashboard dashboardId={dashboardId} />);
+ });
- getSdkRoot().within(() => {
- cy.findByText("123").first().click();
- H.popover().findByText("View this Product's Orders").click();
+ getSdkRoot().within(() => {
+ cy.findByText("123").first().click();
+ H.popover().findByText("View this Product's Orders").click();
- cy.findByLabelText("Back to Orders in a dashboard").click();
- cy.findByText("Orders in a dashboard").should("be.visible");
- cy.findByText("Back to Orders in a dashboard").should("not.exist");
+ cy.findByLabelText("Back to Orders in a dashboard").click();
+ cy.findByText("Orders in a dashboard").should("be.visible");
+ cy.findByText("Back to Orders in a dashboard").should("not.exist");
+ });
});
});
});
diff --git a/frontend/src/embedding-sdk-bundle/components/public/dashboard/use-common-dashboard-params.tsx b/frontend/src/embedding-sdk-bundle/components/public/dashboard/use-common-dashboard-params.tsx
index 8012475aa30..7e2ffd41a63 100644
--- a/frontend/src/embedding-sdk-bundle/components/public/dashboard/use-common-dashboard-params.tsx
+++ b/frontend/src/embedding-sdk-bundle/components/public/dashboard/use-common-dashboard-params.tsx
@@ -8,12 +8,17 @@ import {
} from "metabase/dashboard/actions";
import { getNewCardUrl } from "metabase/dashboard/actions/getNewCardUrl";
import type { NavigateToNewCardFromDashboardOpts } from "metabase/dashboard/components/DashCard/types";
+import { parseNumber } from "metabase/lib/number";
import * as Urls from "metabase/lib/urls";
import { isJWT } from "metabase/lib/utils";
import { navigateBackToDashboard } from "metabase/query_builder/actions";
import { getMetadata } from "metabase/selectors/metadata";
import type Question from "metabase-lib/v1/Question";
-import type { DashboardId, QuestionDashboardCard } from "metabase-types/api";
+import {
+ type DashboardId,
+ type QuestionDashboardCard,
+ isBaseEntityID,
+} from "metabase-types/api";
import type { StoreDashboard } from "metabase-types/store";
export const useCommonDashboardParams = ({
@@ -127,13 +132,9 @@ const findDashboardById = (
dashboardId: DashboardId,
dashboards: Record<DashboardId, StoreDashboard>,
): StoreDashboard | null => {
- if (typeof dashboardId === "number" || isJWT(dashboardId)) {
- return dashboards[dashboardId] ?? null;
- }
-
// Lookup via entity ids.
// Dashboards are a mapping of numeric id to dashboard.
- if (typeof dashboardId === "string") {
+ if (isBaseEntityID(dashboardId)) {
return (
Object.values(dashboards).find(
(dashboard) => dashboard.entity_id === dashboardId,
@@ -141,5 +142,13 @@ const findDashboardById = (
);
}
+ if (isNumericStringOrNumber(dashboardId) || isJWT(dashboardId)) {
+ return dashboards[dashboardId] ?? null;
+ }
+
return null;
};
+
+function isNumericStringOrNumber(value: string | number): boolean {
+ return parseNumber(String(value)) !== null;
+}