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): drills and go to dashcard does not work in dashboard with entity ids (#62953)

Phoomparin Manodbdf07038362
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 01dfcadb156..224f145098f 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
@@ -209,6 +209,46 @@ describe("scenarios > embedding-sdk > interactive-dashboard", () => {
});
});
+ const idTypes = [
+ { idType: "numeric id", dashboardIdAlias: "@dashboardId" },
+ { idType: "entity id", dashboardIdAlias: "@dashboardEntityId" },
+ ];
+
+ 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} />);
+ });
+
+ getSdkRoot().within(() => {
+ H.getDashboardCard().findByText("Orders").click();
+
+ 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");
+ });
+ });
+
+ it(`can drill a question and go back using a ${idType} dashboard (EMB-773)`, () => {
+ 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();
+
+ 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("should only call POST /dataset once when parent component re-renders (EMB-288)", () => {
cy.intercept("POST", "/api/dataset").as("datasetQuery");
diff --git a/enterprise/frontend/src/embedding-sdk-bundle/components/public/dashboard/use-common-dashboard-params.tsx b/enterprise/frontend/src/embedding-sdk-bundle/components/public/dashboard/use-common-dashboard-params.tsx
index af32bab754a..5b11fe126eb 100644
--- a/enterprise/frontend/src/embedding-sdk-bundle/components/public/dashboard/use-common-dashboard-params.tsx
+++ b/enterprise/frontend/src/embedding-sdk-bundle/components/public/dashboard/use-common-dashboard-params.tsx
@@ -13,6 +13,7 @@ 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 { StoreDashboard } from "metabase-types/store";
export const useCommonDashboardParams = ({
dashboardId,
@@ -47,7 +48,12 @@ export const useCommonDashboardParams = ({
const state = store.getState();
const metadata = getMetadata(state);
const { dashboards, parameterValues } = state.dashboard;
- const dashboard = dashboardId && dashboards[dashboardId];
+
+ if (dashboardId === null) {
+ return;
+ }
+
+ const dashboard = findDashboardById(dashboardId, dashboards);
if (dashboard) {
const url = getNewCardUrl({
@@ -86,7 +92,13 @@ export const useCommonDashboardParams = ({
(question: Question) => {
const state = store.getState();
const { dashboards } = state.dashboard;
- const dashboard = dashboardId && dashboards[dashboardId];
+
+ if (dashboardId === null) {
+ return;
+ }
+
+ const dashboard = findDashboardById(dashboardId, dashboards);
+
if (dashboard) {
dispatch({
type: NAVIGATE_TO_NEW_CARD,
@@ -109,3 +121,24 @@ export const useCommonDashboardParams = ({
onNavigateToNewCardFromDashboard: handleNavigateToNewCardFromDashboard,
};
};
+
+const findDashboardById = (
+ dashboardId: DashboardId,
+ dashboards: Record<DashboardId, StoreDashboard>,
+): StoreDashboard | null => {
+ if (typeof dashboardId === "number") {
+ return dashboards[dashboardId] ?? null;
+ }
+
+ // Lookup via entity ids.
+ // Dashboards are a mapping of numeric id to dashboard.
+ if (typeof dashboardId === "string") {
+ return (
+ Object.values(dashboards).find(
+ (dashboard) => dashboard.entity_id === dashboardId,
+ ) ?? null
+ );
+ }
+
+ return null;
+};