The easy-to-use open source Business Intelligence and Embedded Analytics tool that lets everyone work with data :bar_chart:
TypeScript analyticsbibusiness-intelligencebusinessintelligenceclojuredashboarddatadata-analysisdata-visualizationdatabasemetabasemysqlpostgrespostgresqlreportingslacksql-editorvisualization
fix(sdk): non-editable dashboard should not have "add a chart" button and title should be read-only (#65102)
Phoomparin Mano186df5d80220
diff --git a/.loki/reference/chrome_laptop_App_Embed_PublicOrEmbeddedDashboardView_Light_Theme_No_Results.png b/.loki/reference/chrome_laptop_App_Embed_PublicOrEmbeddedDashboardView_Light_Theme_No_Results.pngindex 300a0acf805..af31c0e6e5d 100644Binary files a/.loki/reference/chrome_laptop_App_Embed_PublicOrEmbeddedDashboardView_Light_Theme_No_Results.png and b/.loki/reference/chrome_laptop_App_Embed_PublicOrEmbeddedDashboardView_Light_Theme_No_Results.png differdiff --git a/.loki/reference/chrome_laptop_App_Embed_PublicOrEmbeddedDashboardView_Narrow_With_Many_Tabs.png b/.loki/reference/chrome_laptop_App_Embed_PublicOrEmbeddedDashboardView_Narrow_With_Many_Tabs.pngindex 81fcb668866..6e62afdf400 100644Binary files a/.loki/reference/chrome_laptop_App_Embed_PublicOrEmbeddedDashboardView_Narrow_With_Many_Tabs.png and b/.loki/reference/chrome_laptop_App_Embed_PublicOrEmbeddedDashboardView_Narrow_With_Many_Tabs.png differdiff --git a/e2e/test-component/scenarios/embedding-sdk/interactive-dashboard.cy.spec.tsx b/e2e/test-component/scenarios/embedding-sdk/interactive-dashboard.cy.spec.tsxindex 224f145098f..3204b58dd88 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@@ -292,3 +292,40 @@ describe("scenarios > embedding-sdk > interactive-dashboard", () => {cy.get("@datasetQuery.all").should("have.length", 1);});});++describe("scenarios > embedding-sdk > interactive-dashboard > tabs", () => {+ it("should not show add a chart button on empty dashboard with many tabs (metabase#65001)", () => {+ signInAsAdminAndEnableEmbeddingSdk();++ const TAB_WITH_CARDS = { id: 1, name: "Tab with cards" };+ const EMPTY_TAB = { id: 2, name: "Empty tab" };++ const questionCard: Partial<DashboardCard> = {+ id: ORDERS_DASHBOARD_DASHCARD_ID,+ dashboard_tab_id: TAB_WITH_CARDS.id,+ card_id: ORDERS_QUESTION_ID,+ row: 0,+ col: 0,+ size_x: 16,+ size_y: 8,+ };++ H.createDashboardWithTabs({+ name: "Dashboard with empty tab",+ tabs: [TAB_WITH_CARDS, EMPTY_TAB],+ dashcards: [questionCard],+ }).then(({ id: dashboardId }) => {+ cy.signOut();+ mockAuthProviderAndJwtSignIn();++ mountSdkContent(<InteractiveDashboard dashboardId={dashboardId} />);+ });++ getSdkRoot().within(() => {+ cy.findByText("Orders").should("be.visible");+ cy.findByRole("tab", { name: EMPTY_TAB.name }).click();+ cy.findByTestId("dashboard-empty-state").should("be.visible");+ cy.findByText("Add a chart").should("not.exist");+ });+ });+});diff --git a/enterprise/frontend/src/embedding-sdk-bundle/components/public/dashboard/EditableDashboard/EditableDashboard.unit.spec.tsx b/enterprise/frontend/src/embedding-sdk-bundle/components/public/dashboard/EditableDashboard/EditableDashboard.unit.spec.tsxindex 42e82e67b67..9e82458fcce 100644--- a/enterprise/frontend/src/embedding-sdk-bundle/components/public/dashboard/EditableDashboard/EditableDashboard.unit.spec.tsx+++ b/enterprise/frontend/src/embedding-sdk-bundle/components/public/dashboard/EditableDashboard/EditableDashboard.unit.spec.tsx@@ -196,6 +196,19 @@ describe("EditableDashboard", () => {expect(dataPickerDataCallUrl).toContain("models=dataset");expect(dataPickerDataCallUrl).not.toContain("models=table");});++ it("should show 'Add a chart' button on empty dashboards", async () => {+ await setup({ dashcards: [] });++ expect(screen.getByText("This dashboard is empty")).toBeInTheDocument();+ expect(screen.getByText("Add a chart")).toBeInTheDocument();+ });++ it("should allow editing the dashboard title", async () => {+ await setup();++ expect(screen.getByTestId("dashboard-name-heading")).toBeEnabled();+ });});function setupSimpleDataPickerEndpoints() {diff --git a/enterprise/frontend/src/embedding-sdk-bundle/components/public/dashboard/InteractiveDashboard/InteractiveDashboard.unit.spec.tsx b/enterprise/frontend/src/embedding-sdk-bundle/components/public/dashboard/InteractiveDashboard/InteractiveDashboard.unit.spec.tsxindex 2ddb6d55bce..243b2e7e88c 100644--- a/enterprise/frontend/src/embedding-sdk-bundle/components/public/dashboard/InteractiveDashboard/InteractiveDashboard.unit.spec.tsx+++ b/enterprise/frontend/src/embedding-sdk-bundle/components/public/dashboard/InteractiveDashboard/InteractiveDashboard.unit.spec.tsx@@ -131,4 +131,17 @@ describe("InteractiveDashboard", () => {dashboardHeader.queryByTestId("dashboard-header-row-button"),).not.toBeInTheDocument();});++ it("should not show 'Add a chart' button on empty dashboards", async () => {+ await setup({ dashcards: [] });++ expect(screen.getByText("This dashboard is empty")).toBeInTheDocument();+ expect(screen.queryByText("Add a chart")).not.toBeInTheDocument();+ });++ it("should not allow editing the dashboard title", async () => {+ await setup();++ expect(screen.getByTestId("dashboard-name-heading")).toBeDisabled();+ });});diff --git a/enterprise/frontend/src/embedding-sdk-bundle/components/public/dashboard/StaticDashboard/StaticDashboard.unit.spec.tsx b/enterprise/frontend/src/embedding-sdk-bundle/components/public/dashboard/StaticDashboard/StaticDashboard.unit.spec.tsxindex e8a7a9cf0bb..b90604bcae9 100644--- a/enterprise/frontend/src/embedding-sdk-bundle/components/public/dashboard/StaticDashboard/StaticDashboard.unit.spec.tsx+++ b/enterprise/frontend/src/embedding-sdk-bundle/components/public/dashboard/StaticDashboard/StaticDashboard.unit.spec.tsx@@ -100,4 +100,17 @@ describe("StaticDashboard", () => {dashboardHeader.queryByTestId("dashboard-header-row-button"),).not.toBeInTheDocument();});++ it("should not show 'Add a chart' button on empty dashboards", async () => {+ await setup({ dashcards: [] });++ expect(screen.getByText("This dashboard is empty")).toBeInTheDocument();+ expect(screen.queryByText("Add a chart")).not.toBeInTheDocument();+ });++ it("should not allow editing the dashboard title", async () => {+ await setup();++ expect(screen.getByTestId("dashboard-name-heading")).toBeDisabled();+ });});diff --git a/enterprise/frontend/src/embedding-sdk-bundle/components/public/dashboard/tests/setup.tsx b/enterprise/frontend/src/embedding-sdk-bundle/components/public/dashboard/tests/setup.tsxindex e21fdb35807..a89430117c3 100644--- a/enterprise/frontend/src/embedding-sdk-bundle/components/public/dashboard/tests/setup.tsx+++ b/enterprise/frontend/src/embedding-sdk-bundle/components/public/dashboard/tests/setup.tsx@@ -23,6 +23,7 @@ import { setupSdkState } from "embedding-sdk-bundle/test/server-mocks/sdk-init";import type { MetabaseProviderProps } from "embedding-sdk-bundle/types/metabase-provider";import { useLocale } from "metabase/common/hooks/use-locale";import { Box } from "metabase/ui";+import type { DashboardCard } from "metabase-types/api";import {createMockCard,createMockCardQueryMetadata,@@ -102,7 +103,11 @@ export const textDashcard2 = createMockTextDashboardCard({dashboard_tab_id: dashboardTabs[1].id,});-export const dashcards = [tableDashcard, textDashcard, textDashcard2];+export const DEFAULT_DASHCARDS: DashboardCard[] = [+ tableDashcard,+ textDashcard,+ textDashcard2,+];export interface SetupSdkDashboardOptions {props?: Partial<SdkDashboardProps>;@@ -111,6 +116,7 @@ export interface SetupSdkDashboardOptions {component: React.ComponentType<SdkDashboardProps>;dashboardName?: string;dataPickerProps?: EditableDashboardProps["dataPickerProps"];+ dashcards?: DashboardCard[];}jest.mock("metabase/common/hooks/use-locale", () => ({@@ -124,6 +130,7 @@ export const setupSdkDashboard = async ({component: Component,dashboardName = "Dashboard",dataPickerProps,+ dashcards = DEFAULT_DASHCARDS,}: SetupSdkDashboardOptions) => {const useLocaleMock = useLocale as jest.Mock;useLocaleMock.mockReturnValue({ isLocaleLoading });@@ -212,7 +219,14 @@ export const setupSdkDashboard = async ({);if (!isLocaleLoading) {- expect(await screen.findByTestId("dashboard-grid")).toBeInTheDocument();+ if (dashcards.length === 0) {+ // For empty dashboards, wait for the empty state instead of the grid+ expect(+ await screen.findByTestId("dashboard-empty-state"),+ ).toBeInTheDocument();+ } else {+ expect(await screen.findByTestId("dashboard-grid")).toBeInTheDocument();+ }}return {diff --git a/frontend/src/metabase/dashboard/components/Dashboard/components/Grid.tsx b/frontend/src/metabase/dashboard/components/Dashboard/components/Grid.tsxindex 817112cf222..a08a550699a 100644--- a/frontend/src/metabase/dashboard/components/Dashboard/components/Grid.tsx+++ b/frontend/src/metabase/dashboard/components/Dashboard/components/Grid.tsx@@ -30,9 +30,9 @@ export const Grid = ({onRefreshPeriodChange,isLoadingWithoutCards,onAddQuestion,+ isEditableDashboard,} = useDashboardContext();- const canWrite = Boolean(dashboard?.can_write);const currentTabDashcards = useMemo(() => {if (!dashboard || !Array.isArray(dashboard.dashcards)) {return [];@@ -92,7 +92,7 @@ export const Grid = ({if (isEmpty) {if (!dashboardHasCards) {- return canWrite ? (+ return isEditableDashboard ? (<DashboardEmptyStatecanCreateQuestions={canCreateQuestions}addQuestion={handleAddQuestion}@@ -105,7 +105,7 @@ export const Grid = ({}if (dashboardHasCards && !tabHasCards) {- return canWrite ? (+ return isEditableDashboard ? (<DashboardEmptyStatecanCreateQuestions={canCreateQuestions}addQuestion={handleAddQuestion}diff --git a/frontend/src/metabase/dashboard/components/DashboardTitle/DashboardTitle.tsx b/frontend/src/metabase/dashboard/components/DashboardTitle/DashboardTitle.tsxindex e5a5539ddcc..9669c29a08a 100644--- a/frontend/src/metabase/dashboard/components/DashboardTitle/DashboardTitle.tsx+++ b/frontend/src/metabase/dashboard/components/DashboardTitle/DashboardTitle.tsx@@ -7,7 +7,7 @@ import { useDashboardTitle } from "metabase/dashboard/hooks/use-dashboard-title"export const DashboardTitle = ({ className }: { className?: string }) => {const [title, setTitle] = useDashboardTitle();- const { dashboard } = useDashboardContext();+ const { dashboard, isEditableDashboard } = useDashboardContext();if (!dashboard) {return null;@@ -19,7 +19,7 @@ export const DashboardTitle = ({ className }: { className?: string }) => {key={title}initialValue={title}placeholder={t`Add title`}- isDisabled={!dashboard?.can_write}+ isDisabled={!isEditableDashboard}data-testid="dashboard-name-heading"onChange={setTitle}maxLength={DASHBOARD_NAME_MAX_LENGTH}diff --git a/frontend/src/metabase/dashboard/context/context.tsx b/frontend/src/metabase/dashboard/context/context.tsxindex 73fa549a2b5..71daf494ffc 100644--- a/frontend/src/metabase/dashboard/context/context.tsx+++ b/frontend/src/metabase/dashboard/context/context.tsx@@ -80,6 +80,7 @@ export type DashboardContextOwnProps = {export type DashboardContextOwnResult = {dashboardId: DashboardId | null;dashboardActions?: DashboardActionButtonList;+ isEditableDashboard: boolean;};export type DashboardControls = UseAutoScrollToDashcardResult &@@ -348,6 +349,23 @@ const DashboardContextProviderInner = forwardRef(? initDashboardActions({ isEditing, downloadsEnabled }): (initDashboardActions ?? null);+ // Determine if the dashboard is editable.+ // This lets us distinguish read-only dashboards (e.g. public embeds, InteractiveDashboard)+ // from editable dashboards (e.g. main app, EditableDashboard).+ const isEditableDashboard = useMemo(() => {+ // When the dashboard is being edited, the "edit" action won't exist.+ if (isEditing) {+ return true;+ }++ // A dashboard is editable if it has the edit action and the user has write permissions.+ const hasEditDashboardAction = (dashboardActions ?? []).includes(+ "EDIT_DASHBOARD",+ );++ return hasEditDashboardAction && Boolean(dashboard?.can_write);+ }, [isEditing, dashboardActions, dashboard?.can_write]);+return (<DashboardContext.Providervalue={{@@ -359,6 +377,7 @@ const DashboardContextProviderInner = forwardRef(dashcardMenu,dashboardActions,onNewQuestion,+ isEditableDashboard,navigateToNewCardFromDashboard,isLoading,