Skip to content

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

Apply global dashboardCardMenu plugin from MetabaseProvider to dashboards (#77500)

Sébastien9f42bcf3233b
diff --git a/frontend/src/embedding-sdk-bundle/components/public/dashboard/SdkDashboard.tsx b/frontend/src/embedding-sdk-bundle/components/public/dashboard/SdkDashboard.tsx
index 3ccbe1d066c..2711307e718 100644
--- a/frontend/src/embedding-sdk-bundle/components/public/dashboard/SdkDashboard.tsx
+++ b/frontend/src/embedding-sdk-bundle/components/public/dashboard/SdkDashboard.tsx
@@ -37,6 +37,7 @@ import { useSdkDispatch, useSdkSelector } from "embedding-sdk-bundle/store";
import { setInitialGuestToken } from "embedding-sdk-bundle/store/guest-embed";
import {
getIsGuestEmbed,
+ getPlugins,
getSessionTokenState,
} from "embedding-sdk-bundle/store/selectors";
import type { MetabaseQuestion } from "embedding-sdk-bundle/types";
@@ -325,8 +326,15 @@ const SdkDashboardInner = ({
dashboardId,
});
+ // `dashboardCardMenu` can be supplied either directly on the dashboard via
+ // `plugins`, or globally through `MetabaseProvider` (stored in the SDK Redux
+ // state). The local prop takes precedence, then the global plugin, and
+ // finally the component's built-in default menu.
+ const globalPlugins = useSdkSelector(getPlugins);
const finalDashcardMenu =
- plugins?.dashboard?.dashboardCardMenu ?? dashcardMenu;
+ plugins?.dashboard?.dashboardCardMenu ??
+ globalPlugins?.dashboard?.dashboardCardMenu ??
+ dashcardMenu;
const [renderModeState, setRenderMode] = useState<
"dashboard" | "queryBuilder"
diff --git a/frontend/src/embedding-sdk-bundle/components/public/dashboard/tests/SdkDashboard.unit.spec.tsx b/frontend/src/embedding-sdk-bundle/components/public/dashboard/tests/SdkDashboard.unit.spec.tsx
index a87ceef5f28..7c28421fec8 100644
--- a/frontend/src/embedding-sdk-bundle/components/public/dashboard/tests/SdkDashboard.unit.spec.tsx
+++ b/frontend/src/embedding-sdk-bundle/components/public/dashboard/tests/SdkDashboard.unit.spec.tsx
@@ -182,4 +182,55 @@ describe("SdkDashboard", () => {
expect(dashcardMenuPopover).not.toBeInTheDocument();
expect(onClickCustomAction).toHaveBeenCalled();
});
+
+ it("should render a custom dashcard menu if one is provided globally via MetabaseProvider (metabase#EMB-2049)", async () => {
+ const onClickCustomAction = jest.fn();
+ await setup({
+ props: {
+ withDownloads: true,
+ },
+ providerProps: {
+ pluginsConfig: {
+ dashboard: {
+ dashboardCardMenu: {
+ withDownloads: true,
+ withEditLink: true,
+ customItems: [
+ {
+ iconName: "chevronright",
+ label: "Custom Action",
+ onClick: onClickCustomAction,
+ },
+ ],
+ },
+ },
+ },
+ },
+ });
+
+ expect(await screen.findByTestId("dashboard-grid")).toBeInTheDocument();
+
+ const dashcard = screen.getAllByTestId("dashcard").at(0);
+ expect(dashcard).toBeInTheDocument();
+
+ await userEvent.click(within(dashcard!).getByTestId("dashcard-menu"));
+
+ const dashcardMenuPopover = await screen.findByRole("menu");
+ expect(
+ within(dashcardMenuPopover).getByText("Download results"),
+ ).toBeInTheDocument();
+ expect(
+ within(dashcardMenuPopover).getByText("Edit question"),
+ ).toBeInTheDocument();
+ expect(
+ within(dashcardMenuPopover).getByText("Custom Action"),
+ ).toBeInTheDocument();
+
+ await userEvent.click(
+ within(dashcardMenuPopover).getByText("Custom Action"),
+ );
+
+ expect(dashcardMenuPopover).not.toBeInTheDocument();
+ expect(onClickCustomAction).toHaveBeenCalled();
+ });
});