Skip to content

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

feat(data-studio): edit Library dashboard name and description inline (#84612)

Aleksandr Lesnenkod54f58b19eb4
diff --git a/enterprise/frontend/src/metabase-enterprise/data-studio/library/dashboards/components/DashboardHeader/DashboardHeader.tsx b/enterprise/frontend/src/metabase-enterprise/data-studio/library/dashboards/components/DashboardHeader/DashboardHeader.tsx
index d6799a1c35e..bbbeebe0d3d 100644
--- a/enterprise/frontend/src/metabase-enterprise/data-studio/library/dashboards/components/DashboardHeader/DashboardHeader.tsx
+++ b/enterprise/frontend/src/metabase-enterprise/data-studio/library/dashboards/components/DashboardHeader/DashboardHeader.tsx
@@ -2,10 +2,7 @@ import { t } from "ttag";
import { Link } from "metabase/common/components/Link";
import { DataStudioBreadcrumbs } from "metabase/common/data-studio/components/DataStudioBreadcrumbs";
-import {
- PaneHeader,
- PanelHeaderTitle,
-} from "metabase/common/data-studio/components/PaneHeader";
+import { PaneHeader } from "metabase/common/data-studio/components/PaneHeader";
import { useCollectionPath } from "metabase/common/data-studio/hooks/use-collection-path/useCollectionPath";
import * as Urls from "metabase/urls";
import type { Dashboard } from "metabase-types/api";
@@ -14,6 +11,7 @@ import type { DashboardTabsInfo } from "../../hooks/use-dashboard-tabs-info";
import { DashboardArchivedBanner } from "./DashboardArchivedBanner";
import { DashboardHeaderActions } from "./DashboardHeaderActions";
+import { DashboardNameInput } from "./DashboardNameInput";
import { DashboardTabs } from "./DashboardTabs";
import { getDashboardFolders } from "./utils";
@@ -33,7 +31,7 @@ export function DashboardHeader({ dashboard, tabsInfo }: DashboardHeaderProps) {
{dashboard.archived && <DashboardArchivedBanner dashboard={dashboard} />}
<PaneHeader
data-testid="dashboard-pane-header"
- title={<PanelHeaderTitle>{dashboard.name}</PanelHeaderTitle>}
+ title={<DashboardNameInput dashboard={dashboard} />}
icon="dashboard"
tabs={<DashboardTabs dashboardId={dashboard.id} tabsInfo={tabsInfo} />}
actions={<DashboardHeaderActions dashboard={dashboard} />}
diff --git a/enterprise/frontend/src/metabase-enterprise/data-studio/library/dashboards/components/DashboardHeader/DashboardNameInput.tsx b/enterprise/frontend/src/metabase-enterprise/data-studio/library/dashboards/components/DashboardHeader/DashboardNameInput.tsx
new file mode 100644
index 00000000000..9d740a8c5d9
--- /dev/null
+++ b/enterprise/frontend/src/metabase-enterprise/data-studio/library/dashboards/components/DashboardHeader/DashboardNameInput.tsx
@@ -0,0 +1,48 @@
+import { t } from "ttag";
+
+import { useUpdateDashboardMutation } from "metabase/api";
+import {
+ PaneHeaderInput,
+ PanelHeaderTitle,
+} from "metabase/common/data-studio/components/PaneHeader";
+import { useMetadataToasts } from "metabase/common/hooks";
+import { DASHBOARD_NAME_MAX_LENGTH } from "metabase/common/utils/dashboard";
+import type { Dashboard } from "metabase-types/api";
+
+import { useCanEditDashboard } from "../../hooks/use-can-edit-dashboard";
+
+type DashboardNameInputProps = {
+ dashboard: Pick<Dashboard, "id" | "name" | "can_write" | "archived">;
+};
+
+export function DashboardNameInput({ dashboard }: DashboardNameInputProps) {
+ const canEdit = useCanEditDashboard(dashboard);
+ const [updateDashboard] = useUpdateDashboardMutation();
+ const { sendSuccessToast, sendErrorToast } = useMetadataToasts();
+
+ if (!canEdit) {
+ return <PanelHeaderTitle>{dashboard.name}</PanelHeaderTitle>;
+ }
+
+ const handleChangeName = async (newName: string) => {
+ const { error } = await updateDashboard({
+ id: dashboard.id,
+ name: newName,
+ });
+
+ if (error) {
+ sendErrorToast(t`Failed to update dashboard name`);
+ } else {
+ sendSuccessToast(t`Dashboard name updated`);
+ }
+ };
+
+ return (
+ <PaneHeaderInput
+ initialValue={dashboard.name}
+ maxLength={DASHBOARD_NAME_MAX_LENGTH}
+ data-testid="dashboard-name-input"
+ onChange={handleChangeName}
+ />
+ );
+}
diff --git a/enterprise/frontend/src/metabase-enterprise/data-studio/library/dashboards/pages/DashboardOverviewPage/DashboardInfo/DashboardDescription.tsx b/enterprise/frontend/src/metabase-enterprise/data-studio/library/dashboards/pages/DashboardOverviewPage/DashboardInfo/DashboardDescription.tsx
new file mode 100644
index 00000000000..7a254d2dfae
--- /dev/null
+++ b/enterprise/frontend/src/metabase-enterprise/data-studio/library/dashboards/pages/DashboardOverviewPage/DashboardInfo/DashboardDescription.tsx
@@ -0,0 +1,55 @@
+import { t } from "ttag";
+
+import { useUpdateDashboardMutation } from "metabase/api";
+import { EditableText } from "metabase/common/components/EditableText";
+import { Markdown } from "metabase/common/components/Markdown";
+import { useMetadataToasts } from "metabase/common/hooks";
+import { DASHBOARD_DESCRIPTION_MAX_LENGTH } from "metabase/common/utils/dashboard";
+import { Text } from "metabase/ui";
+import type { Dashboard } from "metabase-types/api";
+
+import { useCanEditDashboard } from "../../../hooks/use-can-edit-dashboard";
+
+type DashboardDescriptionProps = {
+ dashboard: Pick<Dashboard, "id" | "description" | "can_write" | "archived">;
+};
+
+export function DashboardDescription({ dashboard }: DashboardDescriptionProps) {
+ const canEdit = useCanEditDashboard(dashboard);
+ const [updateDashboard] = useUpdateDashboardMutation();
+ const { sendSuccessToast, sendErrorToast } = useMetadataToasts();
+
+ if (!canEdit) {
+ return dashboard.description ? (
+ <Markdown>{dashboard.description}</Markdown>
+ ) : (
+ <Text c="text-secondary">{t`No description`}</Text>
+ );
+ }
+
+ const handleChange = async (newValue: string) => {
+ const newDescription = newValue.trim();
+ const { error } = await updateDashboard({
+ id: dashboard.id,
+ description: newDescription.length > 0 ? newDescription : null,
+ });
+ if (error) {
+ sendErrorToast(t`Failed to update dashboard description`);
+ } else {
+ sendSuccessToast(t`Dashboard description updated`);
+ }
+ };
+
+ return (
+ <EditableText
+ initialValue={dashboard.description ?? ""}
+ placeholder={t`No description`}
+ maxLength={DASHBOARD_DESCRIPTION_MAX_LENGTH}
+ isMarkdown
+ isOptional
+ isMultiline
+ data-testid="dashboard-description-input"
+ onChange={handleChange}
+ />
+ );
+}
diff --git a/enterprise/frontend/src/metabase-enterprise/data-studio/library/dashboards/pages/DashboardOverviewPage/DashboardInfo/DashboardInfo.tsx b/enterprise/frontend/src/metabase-enterprise/data-studio/library/dashboards/pages/DashboardOverviewPage/DashboardInfo/DashboardInfo.tsx
index f38e6a95df3..28ccc61dacd 100644
--- a/enterprise/frontend/src/metabase-enterprise/data-studio/library/dashboards/pages/DashboardOverviewPage/DashboardInfo/DashboardInfo.tsx
+++ b/enterprise/frontend/src/metabase-enterprise/data-studio/library/dashboards/pages/DashboardOverviewPage/DashboardInfo/DashboardInfo.tsx
@@ -2,7 +2,6 @@ import type { ReactNode } from "react";
import { t } from "ttag";
import { DateTime } from "metabase/common/components/DateTime";
-import { Markdown } from "metabase/common/components/Markdown";
import { useNumberFormatter } from "metabase/common/hooks/use-number-formatter";
import { isDashboardCacheable } from "metabase/dashboard/utils";
import { PLUGIN_CACHING } from "metabase/plugins";
@@ -14,6 +13,7 @@ import { getLoadingTimeMs } from "../utils";
import { AutoApplyFiltersStat } from "./AutoApplyFiltersStat";
import { CachePolicyStat } from "./CachePolicyStat";
+import { DashboardDescription } from "./DashboardDescription";
import { DashboardStat } from "./DashboardStat";
type DashboardInfoProps = {
@@ -30,11 +30,7 @@ export function DashboardInfo({ dashboard }: DashboardInfoProps) {
return (
<Stack gap={0} align="stretch" data-testid="dashboard-info">
<Box p="xl">
- {dashboard.description ? (
- <Markdown>{dashboard.description}</Markdown>
- ) : (
- <Text c="text-secondary">{t`No description`}</Text>
- )}
+ <DashboardDescription dashboard={dashboard} />
</Box>
<Card mx="xl" bg="background_page-secondary" shadow="none" radius="lg">
{lastEditInfo && (
diff --git a/enterprise/frontend/src/metabase-enterprise/data-studio/library/dashboards/pages/DashboardOverviewPage/DashboardInfo/DashboardInfo.unit.spec.tsx b/enterprise/frontend/src/metabase-enterprise/data-studio/library/dashboards/pages/DashboardOverviewPage/DashboardInfo/DashboardInfo.unit.spec.tsx
index a663b05c16c..ababd43f9b7 100644
--- a/enterprise/frontend/src/metabase-enterprise/data-studio/library/dashboards/pages/DashboardOverviewPage/DashboardInfo/DashboardInfo.unit.spec.tsx
+++ b/enterprise/frontend/src/metabase-enterprise/data-studio/library/dashboards/pages/DashboardOverviewPage/DashboardInfo/DashboardInfo.unit.spec.tsx
@@ -84,10 +84,19 @@ function setup({
return { router: checkNotNull(router) };
}
-function getLastUpdateBody(): unknown {
- return fetchMock.callHistory.lastCall("path:/api/dashboard/10", {
+function getUpdateCalls() {
+ return fetchMock.callHistory.calls("path:/api/dashboard/10", {
method: "PUT",
- })?.options.body;
+ });
+}
+
+function getLastUpdateBody(): unknown {
+ return getUpdateCalls().at(-1)?.options.body;
+}
+
+async function startEditingDescription(): Promise<HTMLElement> {
+ await userEvent.click(await screen.findByTestId("editable-text"));
+ return screen.getByTestId("dashboard-description-input");
}
describe("DashboardInfo", () => {
@@ -206,6 +215,120 @@ describe("DashboardInfo", () => {
});
});
+ describe("description", () => {
+ it("saves the new description", async () => {
+ setup({
+ dashboard: createMockDashboard({ id: 10, description: "Old" }),
+ });
+
+ const input = await startEditingDescription();
+ await userEvent.clear(input);
+ await userEvent.type(input, "New");
+ await userEvent.tab();
+
+ await waitFor(() =>
+ expect(getLastUpdateBody()).toBe(
+ JSON.stringify({ description: "New" }),
+ ),
+ );
+ });
+
+ it("clears the description when it is emptied", async () => {
+ setup({
+ dashboard: createMockDashboard({ id: 10, description: "Old" }),
+ });
+
+ await userEvent.clear(await startEditingDescription());
+ await userEvent.tab();
+
+ await waitFor(() =>
+ expect(getLastUpdateBody()).toBe(JSON.stringify({ description: null })),
+ );
+ });
+
+ it("trims the description", async () => {
+ setup({
+ dashboard: createMockDashboard({ id: 10, description: "Old" }),
+ });
+
+ const input = await startEditingDescription();
+ await userEvent.clear(input);
+ await userEvent.type(input, " New ");
+ await userEvent.tab();
+
+ await waitFor(() =>
+ expect(getLastUpdateBody()).toBe(
+ JSON.stringify({ description: "New" }),
+ ),
+ );
+ });
+
+ it("shows an error and keeps the unsaved description when the description cannot be saved", async () => {
+ setup({
+ dashboard: createMockDashboard({ id: 10, description: "Old" }),
+ hasUpdateError: true,
+ });
+
+ await userEvent.type(await startEditingDescription(), " text");
+ await userEvent.tab();
+
+ const toast = await screen.findByRole("status");
+ expect(
+ within(toast).getByText("Failed to update dashboard description"),
+ ).toBeInTheDocument();
+ expect(await screen.findByText("Old text")).toBeInTheDocument();
+ expect(getUpdateCalls()).toHaveLength(1);
+ });
+
+ it.each([
+ {
+ name: "the user cannot write the dashboard",
+ opts: {
+ dashboard: createMockDashboard({
+ id: 10,
+ description: "Revenue",
+ can_write: false,
+ }),
+ },
+ },
+ {
+ name: "the dashboard is in the trash",
+ opts: {
+ dashboard: createMockDashboard({
+ id: 10,
+ description: "Revenue",
+ archived: true,
+ }),
+ },
+ },
+ {
+ name: "remote sync is read-only",
+ opts: {
+ dashboard: createMockDashboard({ id: 10, description: "Revenue" }),
+ isRemoteSyncReadOnly: true,
+ },
+ },
+ ])("is read-only when $name", async ({ opts }) => {
+ setup(opts);
+
+ expect(await screen.findByText("Revenue")).toBeInTheDocument();
+ expect(screen.queryByTestId("editable-text")).not.toBeInTheDocument();
+ });
+
+ it("says when a read-only dashboard has no description", async () => {
+ setup({
+ dashboard: createMockDashboard({
+ id: 10,
+ description: null,
+ can_write: false,
+ }),
+ });
+
+ expect(await screen.findByText("No description")).toBeInTheDocument();
+ expect(screen.queryByTestId("editable-text")).not.toBeInTheDocument();
+ });
+ });
+
describe("auto-apply filters", () => {
it("saves the new value", async () => {
setup();
diff --git a/enterprise/frontend/src/metabase-enterprise/data-studio/library/dashboards/pages/DashboardOverviewPage/DashboardOverviewPage.unit.spec.tsx b/enterprise/frontend/src/metabase-enterprise/data-studio/library/dashboards/pages/DashboardOverviewPage/DashboardOverviewPage.unit.spec.tsx
index 1089393dfc0..b3633cc82ad 100644
--- a/enterprise/frontend/src/metabase-enterprise/data-studio/library/dashboards/pages/DashboardOverviewPage/DashboardOverviewPage.unit.spec.tsx
+++ b/enterprise/frontend/src/metabase-enterprise/data-studio/library/dashboards/pages/DashboardOverviewPage/DashboardOverviewPage.unit.spec.tsx
@@ -6,8 +6,11 @@ import {
setupDashboardEndpoints,
setupDashboardNotFoundEndpoint,
setupDashboardQueryMetadataEndpoint,
+ setupUpdateDashboardEndpointWithError,
} from "__support__/server-mocks";
import { setupListSubscriptionsEndpoint } from "__support__/server-mocks/pulse";
+import { mockSettings } from "__support__/settings";
+import { createMockState } from "__support__/state";
import {
renderWithProviders,
screen,
@@ -16,6 +19,7 @@ import {
within,
} from "__support__/ui";
import { ROOT_COLLECTION } from "metabase/common/collections/constants";
+import { UndoListing } from "metabase/common/components/UndoListing";
import { Route } from "metabase/router";
import type { Dashboard } from "metabase-types/api";
import {
@@ -52,9 +56,16 @@ const SALES_COLLECTION = createMockCollection({
type SetupOpts = {
dashboard?: Partial<Dashboard>;
isNotFound?: boolean;
+ hasUpdateError?: boolean;
+ isRemoteSyncReadOnly?: boolean;
};
-function setup({ dashboard: dashboardOpts, isNotFound = false }: SetupOpts) {
+function setup({
+ dashboard: dashboardOpts,
+ isNotFound = false,
+ hasUpdateError = false,
+ isRemoteSyncReadOnly = false,
+}: SetupOpts) {
const dashboard = createMockDashboard({
id: 10,
name: "Sales overview",
@@ -63,6 +74,10 @@ function setup({ dashboard: dashboardOpts, isNotFound = false }: SetupOpts) {
...dashboardOpts,
});
setupListSubscriptionsEndpoint([]);
+ if (hasUpdateError) {
+ // Registered before the default endpoints so this PUT route matches first
+ setupUpdateDashboardEndpointWithError(dashboard.id);
+ }
if (isNotFound) {
setupDashboardNotFoundEndpoint(dashboard);
} else {
@@ -85,11 +100,22 @@ function setup({ dashboard: dashboardOpts, isNotFound = false }: SetupOpts) {
<>
<Route
path="/data-studio/dashboards/:dashboardId"
- element={<DashboardOverviewPage />}
+ element={
+ <>
+ <DashboardOverviewPage />
+ <UndoListing />
+ </>
+ }
/>
<Route path="/dashboard/:slug" element={null} />
</>,
{
+ storeInitialState: createMockState({
+ settings: mockSettings({
+ "remote-sync-enabled": isRemoteSyncReadOnly,
+ "remote-sync-type": isRemoteSyncReadOnly ? "read-only" : undefined,
+ }),
+ }),
withRouter: true,
initialRoute: `/data-studio/dashboards/${dashboard.id}`,
},
@@ -98,6 +124,12 @@ function setup({ dashboard: dashboardOpts, isNotFound = false }: SetupOpts) {
return { router };
}
+function getUpdateCalls() {
+ return fetchMock.callHistory.calls("path:/api/dashboard/10", {
+ method: "PUT",
+ });
+}
+
describe("DashboardOverviewPage", () => {
it("shows a loader, then the dashboard name and its folder breadcrumbs", async () => {
setup({});
@@ -142,9 +174,65 @@ describe("DashboardOverviewPage", () => {
setup({ dashboard: { description: null } });
const info = await screen.findByTestId("dashboard-info");
- expect(within(info).getByText("No description")).toBeInTheDocument();
+ expect(
+ within(info).getByPlaceholderText("No description"),
+ ).toBeInTheDocument();
+ });
+
+ it("renames the dashboard from the header", async () => {
+ setup({});
+
+ const input = await screen.findByTestId("dashboard-name-input");
+ await userEvent.clear(input);
+ await userEvent.type(input, "Sales by region{enter}");
+
+ await waitFor(() => expect(getUpdateCalls()).toHaveLength(1));
+ expect(getUpdateCalls().map((call) => call.options.body)).toEqual([
+ JSON.stringify({ name: "Sales by region" }),
+ ]);
+ });
+
+ it("shows an error and keeps the unsaved name when the rename fails", async () => {
+ setup({ hasUpdateError: true });
+
+ const input = await screen.findByTestId("dashboard-name-input");
+ await userEvent.clear(input);
+ await userEvent.type(input, "Sales by region{enter}");
+
+ const toast = await screen.findByRole("status");
+ expect(
+ within(toast).getByText("Failed to update dashboard name"),
+ ).toBeInTheDocument();
+ expect(input).toHaveValue("Sales by region");
+ expect(getUpdateCalls()).toHaveLength(1);
});
+ it.each([
+ {
+ name: "the user cannot write the dashboard",
+ dashboard: { can_write: false },
+ },
+ {
+ name: "the dashboard is in the trash",
+ dashboard: { archived: true, can_restore: true },
+ },
+ {
+ name: "remote sync is read-only",
+ isRemoteSyncReadOnly: true,
+ },
+ ])(
+ "shows the name as plain text when $name",
+ async ({ dashboard, isRemoteSyncReadOnly }) => {
+ setup({ dashboard, isRemoteSyncReadOnly });
+
+ const header = await screen.findByTestId("dashboard-pane-header");
+ expect(within(header).getAllByText("Sales overview")).toHaveLength(2);
+ expect(
+ screen.queryByTestId("dashboard-name-input"),
+ ).not.toBeInTheDocument();
+ },
+ );
+
it("hides Edit when the user cannot write the dashboard", async () => {
setup({ dashboard: { can_write: false } });