Skip to content

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

Implement read-only mode on Snippet detail page (#68268)

Andersona37b3fb0ec51
diff --git a/enterprise/frontend/src/metabase-enterprise/data-studio/snippets/components/SnippetDescriptionSection/SnippetDescriptionSection.tsx b/enterprise/frontend/src/metabase-enterprise/data-studio/snippets/components/SnippetDescriptionSection/SnippetDescriptionSection.tsx
index d615466170a..617b87fb128 100644
--- a/enterprise/frontend/src/metabase-enterprise/data-studio/snippets/components/SnippetDescriptionSection/SnippetDescriptionSection.tsx
+++ b/enterprise/frontend/src/metabase-enterprise/data-studio/snippets/components/SnippetDescriptionSection/SnippetDescriptionSection.tsx
@@ -7,10 +7,12 @@ import type { NativeQuerySnippet } from "metabase-types/api";
type SnippetDescriptionSectionProps = {
snippet: NativeQuerySnippet;
+ isDisabled?: boolean;
};
export function SnippetDescriptionSection({
snippet,
+ isDisabled,
}: SnippetDescriptionSectionProps) {
const [updateSnippet] = useUpdateSnippetMutation();
const [sendToast] = useToast();
@@ -40,6 +42,7 @@ export function SnippetDescriptionSection({
placeholder={t`No description`}
isMarkdown
onChange={handleChange}
+ isDisabled={isDisabled}
/>
);
}
diff --git a/enterprise/frontend/src/metabase-enterprise/data-studio/snippets/components/SnippetDescriptionSection/SnippetDescriptionSection.unit.spec.tsx b/enterprise/frontend/src/metabase-enterprise/data-studio/snippets/components/SnippetDescriptionSection/SnippetDescriptionSection.unit.spec.tsx
new file mode 100644
index 00000000000..fb390db74ee
--- /dev/null
+++ b/enterprise/frontend/src/metabase-enterprise/data-studio/snippets/components/SnippetDescriptionSection/SnippetDescriptionSection.unit.spec.tsx
@@ -0,0 +1,41 @@
+import userEvent from "@testing-library/user-event";
+
+import { renderWithProviders, screen } from "__support__/ui";
+import { createMockNativeQuerySnippet } from "metabase-types/api/mocks";
+
+import { SnippetDescriptionSection } from "./SnippetDescriptionSection";
+
+type SetupProps = {
+ description: string;
+ isDisabled?: boolean;
+};
+
+const setup = ({ description, isDisabled }: SetupProps) => {
+ const snippet = createMockNativeQuerySnippet({ description });
+ renderWithProviders(
+ <SnippetDescriptionSection isDisabled={isDisabled} snippet={snippet} />,
+ );
+};
+
+describe("SnippetDescriptionSection", () => {
+ it("renders editable text with snippet description", async () => {
+ setup({ description: "My snippet description" });
+
+ expect(screen.getByTestId("editable-text")).toBeInTheDocument();
+ await userEvent.click(screen.getByTestId("editable-text"));
+
+ expect(screen.getByPlaceholderText("No description")).toBeEnabled();
+ expect(screen.getByPlaceholderText("No description")).toHaveValue(
+ "My snippet description",
+ );
+ });
+
+ it("renders a disabled input when isDisabled is true", async () => {
+ setup({ description: "My snippet description", isDisabled: true });
+
+ expect(screen.getByTestId("editable-text")).toBeInTheDocument();
+ await userEvent.click(screen.getByTestId("editable-text"));
+
+ expect(screen.getByPlaceholderText("No description")).toBeDisabled();
+ });
+});
diff --git a/enterprise/frontend/src/metabase-enterprise/data-studio/snippets/components/SnippetHeader/SnippetHeader.tsx b/enterprise/frontend/src/metabase-enterprise/data-studio/snippets/components/SnippetHeader/SnippetHeader.tsx
index ea7e95caef9..befdcaf5c89 100644
--- a/enterprise/frontend/src/metabase-enterprise/data-studio/snippets/components/SnippetHeader/SnippetHeader.tsx
+++ b/enterprise/frontend/src/metabase-enterprise/data-studio/snippets/components/SnippetHeader/SnippetHeader.tsx
@@ -6,11 +6,12 @@ import { t } from "ttag";
import { useUpdateSnippetMutation } from "metabase/api";
import { getErrorMessage } from "metabase/api/utils";
import { useToast } from "metabase/common/hooks";
-import { useDispatch } from "metabase/lib/redux";
+import { useDispatch, useSelector } from "metabase/lib/redux";
import * as Urls from "metabase/lib/urls";
import { PLUGIN_DEPENDENCIES } from "metabase/plugins";
import { DataStudioBreadcrumbs } from "metabase-enterprise/data-studio/common/components/DataStudioBreadcrumbs";
import { useCollectionPath } from "metabase-enterprise/data-studio/common/hooks/use-collection-path/useCollectionPath";
+import { getIsRemoteSyncReadOnly } from "metabase-enterprise/remote_sync/selectors";
import type { NativeQuerySnippet } from "metabase-types/api";
import {
@@ -35,6 +36,7 @@ export function SnippetHeader({
...rest
}: SnippetHeaderProps & PaneHeaderProps) {
const dispatch = useDispatch();
+ const remoteSyncReadOnly = useSelector(getIsRemoteSyncReadOnly);
const handleDelete = () => {
dispatch(push(Urls.dataStudioLibrary()));
@@ -46,8 +48,14 @@ export function SnippetHeader({
return (
<PaneHeader
- title={<SnippetNameInput snippet={snippet} />}
- menu={<SnippetMoreMenu snippet={snippet} onDelete={handleDelete} />}
+ title={
+ <SnippetNameInput snippet={snippet} readOnly={remoteSyncReadOnly} />
+ }
+ menu={
+ remoteSyncReadOnly ? null : (
+ <SnippetMoreMenu snippet={snippet} onDelete={handleDelete} />
+ )
+ }
tabs={<SnippetTabs snippet={snippet} />}
actions={actions}
data-testid="snippet-header"
@@ -75,10 +83,11 @@ export function SnippetHeader({
}
type SnippetNameInputProps = {
+ readOnly: boolean;
snippet: NativeQuerySnippet;
};
-function SnippetNameInput({ snippet }: SnippetNameInputProps) {
+function SnippetNameInput({ readOnly, snippet }: SnippetNameInputProps) {
const [updateSnippet] = useUpdateSnippetMutation();
const [sendToast] = useToast();
@@ -106,6 +115,7 @@ function SnippetNameInput({ snippet }: SnippetNameInputProps) {
initialValue={snippet.name}
maxLength={SNIPPET_NAME_MAX_LENGTH}
onChange={handleChangeName}
+ readOnly={readOnly}
/>
);
}
diff --git a/enterprise/frontend/src/metabase-enterprise/data-studio/snippets/components/SnippetHeader/SnippetHeader.unit.spec.tsx b/enterprise/frontend/src/metabase-enterprise/data-studio/snippets/components/SnippetHeader/SnippetHeader.unit.spec.tsx
new file mode 100644
index 00000000000..f27e4435e46
--- /dev/null
+++ b/enterprise/frontend/src/metabase-enterprise/data-studio/snippets/components/SnippetHeader/SnippetHeader.unit.spec.tsx
@@ -0,0 +1,66 @@
+import { mockSettings } from "__support__/settings";
+import { renderWithProviders, screen } from "__support__/ui";
+import type {
+ EnterpriseSettings,
+ NativeQuerySnippet,
+} from "metabase-types/api";
+import { createMockNativeQuerySnippet } from "metabase-types/api/mocks";
+import { createMockState } from "metabase-types/store/mocks";
+
+import { SnippetHeader } from "./SnippetHeader";
+
+type SetupOps = {
+ snippet?: Partial<NativeQuerySnippet>;
+ remoteSyncType?: EnterpriseSettings["remote-sync-type"];
+};
+
+const setup = ({ snippet = {}, remoteSyncType }: SetupOps) => {
+ const mockSnippet = createMockNativeQuerySnippet(snippet);
+
+ renderWithProviders(
+ <SnippetHeader
+ snippet={mockSnippet}
+ actions={<div data-testid="custom-actions" />}
+ />,
+ {
+ storeInitialState: createMockState({
+ settings: mockSettings({
+ "remote-sync-type": remoteSyncType,
+ "remote-sync-enabled": !!remoteSyncType,
+ }),
+ }),
+ },
+ );
+};
+
+describe("SnippetHeader", () => {
+ it("renders header with input, menu and actions", () => {
+ setup({ snippet: { name: "Batman's snippet" } });
+
+ expect(screen.getByRole("textbox")).toBeEnabled();
+ expect(screen.getByRole("textbox")).toHaveValue("Batman's snippet");
+ expect(
+ screen.getByRole("button", { name: "Snippet menu options" }),
+ ).toBeInTheDocument();
+ expect(screen.getByTestId("custom-actions")).toBeInTheDocument();
+ });
+
+ describe("when remote sync is set to read-only", () => {
+ beforeEach(() => {
+ setup({
+ snippet: { name: "Batman's snippet" },
+ remoteSyncType: "read-only",
+ });
+ });
+
+ it("renders input as disabled", () => {
+ expect(screen.getByRole("textbox")).toBeDisabled();
+ });
+
+ it("renders no menu", () => {
+ expect(
+ screen.queryByRole("button", { name: "Snippet menu options" }),
+ ).not.toBeInTheDocument();
+ });
+ });
+});
diff --git a/enterprise/frontend/src/metabase-enterprise/data-studio/snippets/components/SnippetMoreMenu/SnippetMoreMenu.tsx b/enterprise/frontend/src/metabase-enterprise/data-studio/snippets/components/SnippetMoreMenu/SnippetMoreMenu.tsx
index 46353c3bd80..7a6111f5d5b 100644
--- a/enterprise/frontend/src/metabase-enterprise/data-studio/snippets/components/SnippetMoreMenu/SnippetMoreMenu.tsx
+++ b/enterprise/frontend/src/metabase-enterprise/data-studio/snippets/components/SnippetMoreMenu/SnippetMoreMenu.tsx
@@ -68,7 +68,7 @@ function SnippetMenu({ onOpenModal }: SnippetMenuProps) {
return (
<Menu>
<Menu.Target>
- <ActionIcon size="sm">
+ <ActionIcon size="sm" aria-label={t`Snippet menu options`}>
<Icon name="ellipsis" />
</ActionIcon>
</Menu.Target>
diff --git a/enterprise/frontend/src/metabase-enterprise/data-studio/snippets/pages/EditSnippetPage/EditSnippetPage.tsx b/enterprise/frontend/src/metabase-enterprise/data-studio/snippets/pages/EditSnippetPage/EditSnippetPage.tsx
index 4ec26bf5d39..812f9349645 100644
--- a/enterprise/frontend/src/metabase-enterprise/data-studio/snippets/pages/EditSnippetPage/EditSnippetPage.tsx
+++ b/enterprise/frontend/src/metabase-enterprise/data-studio/snippets/pages/EditSnippetPage/EditSnippetPage.tsx
@@ -14,9 +14,11 @@ import { EntityCreationInfo } from "metabase/common/components/EntityCreationInf
import { LeaveRouteConfirmModal } from "metabase/common/components/LeaveConfirmModal";
import { LoadingAndErrorWrapper } from "metabase/common/components/LoadingAndErrorWrapper";
import { useToast } from "metabase/common/hooks";
+import { useSelector } from "metabase/lib/redux";
import * as Urls from "metabase/lib/urls";
import { Card, Center, Flex, Stack } from "metabase/ui";
import { PageContainer } from "metabase-enterprise/data-studio/common/components/PageContainer";
+import { getIsRemoteSyncReadOnly } from "metabase-enterprise/remote_sync/selectors";
import { PaneHeaderActions } from "../../../common/components/PaneHeader";
import { SnippetDescriptionSection } from "../../components/SnippetDescriptionSection";
@@ -36,6 +38,7 @@ type EditSnippetPageProps = {
export function EditSnippetPage({ params, route }: EditSnippetPageProps) {
const snippetId = Urls.extractEntityId(params.snippetId);
const [sendToast] = useToast();
+ const remoteSyncReadOnly = useSelector(getIsRemoteSyncReadOnly);
const {
data: snippet,
@@ -128,6 +131,7 @@ export function EditSnippetPage({ params, route }: EditSnippetPageProps) {
height="100%"
className={S.editor}
data-testid="snippet-editor"
+ editable={!remoteSyncReadOnly}
basicSetup={{
lineNumbers: true,
foldGutter: true,
@@ -137,7 +141,10 @@ export function EditSnippetPage({ params, route }: EditSnippetPageProps) {
/>
</Card>
<Stack p="md" gap="lg" flex="0 0 20rem">
- <SnippetDescriptionSection snippet={snippet} />
+ <SnippetDescriptionSection
+ snippet={snippet}
+ isDisabled={remoteSyncReadOnly}
+ />
<EntityCreationInfo
createdAt={snippet.created_at}
creator={snippet.creator}
diff --git a/enterprise/frontend/src/metabase-enterprise/data-studio/snippets/pages/EditSnippetPage/EditSnippetPage.unit.spec.tsx b/enterprise/frontend/src/metabase-enterprise/data-studio/snippets/pages/EditSnippetPage/EditSnippetPage.unit.spec.tsx
new file mode 100644
index 00000000000..8e42cd42c77
--- /dev/null
+++ b/enterprise/frontend/src/metabase-enterprise/data-studio/snippets/pages/EditSnippetPage/EditSnippetPage.unit.spec.tsx
@@ -0,0 +1,105 @@
+import userEvent from "@testing-library/user-event";
+import { Route } from "react-router";
+
+import { setupNativeQuerySnippetEndpoints } from "__support__/server-mocks";
+import { mockSettings } from "__support__/settings";
+import { renderWithProviders, screen, within } from "__support__/ui";
+import type {
+ EnterpriseSettings,
+ NativeQuerySnippet,
+} from "metabase-types/api";
+import { createMockNativeQuerySnippet } from "metabase-types/api/mocks";
+import { createMockState } from "metabase-types/store/mocks";
+
+import { EditSnippetPage } from "./EditSnippetPage";
+
+type SetupOps = {
+ snippet?: Partial<NativeQuerySnippet>;
+ remoteSyncType?: EnterpriseSettings["remote-sync-type"];
+};
+
+const setup = async ({ snippet = {}, remoteSyncType }: SetupOps) => {
+ const mockSnippet = createMockNativeQuerySnippet(snippet);
+ setupNativeQuerySnippetEndpoints({ snippets: [mockSnippet] });
+
+ renderWithProviders(
+ <Route component={EditSnippetPage} path="/snippets/:snippetId" />,
+ {
+ initialRoute: `/snippets/${mockSnippet.id}`,
+ storeInitialState: createMockState({
+ settings: mockSettings({
+ "remote-sync-type": remoteSyncType,
+ "remote-sync-enabled": !!remoteSyncType,
+ }),
+ }),
+ withRouter: true,
+ },
+ );
+ expect(await screen.findByTestId("edit-snippet-page")).toBeInTheDocument();
+};
+
+describe("EditSnippetPage", () => {
+ it("renders the snippet header", async () => {
+ await setup({ snippet: { name: "Batman's snippet" } });
+
+ expect(screen.getByTestId("snippet-header")).toBeInTheDocument();
+ expect(
+ within(screen.getByTestId("snippet-header")).getByRole("textbox"),
+ ).toHaveValue("Batman's snippet");
+ });
+
+ it("renders the code editor", async () => {
+ await setup({ snippet: { name: "Batman's snippet" } });
+ expect(await screen.findByTestId("snippet-editor")).toBeInTheDocument();
+ expect(screen.getByTestId("snippet-editor")).toBeEnabled();
+ });
+
+ it("renders the description input", async () => {
+ await setup({
+ snippet: {
+ name: "Batman's snippet",
+ description: "My snippet description",
+ },
+ });
+ await userEvent.click(
+ within(screen.getByTestId("edit-snippet-page")).getByText(
+ "My snippet description",
+ ),
+ );
+ expect(screen.getByPlaceholderText("No description")).toHaveValue(
+ "My snippet description",
+ );
+ expect(screen.getByPlaceholderText("No description")).toBeEnabled();
+ });
+
+ describe("when remote sync is set to read-only", () => {
+ beforeEach(async () => {
+ await setup({
+ snippet: {
+ name: "Batman's snippet",
+ description: "My snippet description",
+ },
+ remoteSyncType: "read-only",
+ });
+ });
+
+ it("renders a disabled header name input", () => {
+ expect(
+ within(screen.getByTestId("snippet-header")).getByRole("textbox"),
+ ).toBeDisabled();
+ });
+
+ it("renders a disabled code editor", () => {
+ expect(screen.getByTestId("snippet-editor")).toBeDisabled();
+ });
+
+ it("renders a disabled description input", async () => {
+ await userEvent.click(
+ within(screen.getByTestId("edit-snippet-page")).getByText(
+ "My snippet description",
+ ),
+ );
+ expect(screen.getByPlaceholderText("No description")).toBeDisabled();
+ });
+ });
+});
diff --git a/frontend/test/__support__/mocks.js b/frontend/test/__support__/mocks.js
index 04ffe36bbcf..379e17285e2 100644
--- a/frontend/test/__support__/mocks.js
+++ b/frontend/test/__support__/mocks.js
@@ -32,7 +32,7 @@ jest.mock("@uiw/react-codemirror", () => {
const { forwardRef } = jest.requireActual("react");
const MockEditor = forwardRef((props, ref) => {
- const { indentWithTab, extensions, ...rest } = props;
+ const { indentWithTab, extensions, basicSetup, editable, ...rest } = props;
return (
// @ts-expect-error: some props types are different on CodeMirror
<textarea
@@ -42,6 +42,7 @@ jest.mock("@uiw/react-codemirror", () => {
// @ts-expect-error: We cannot provide the update argument to onChange
onChange={(evt) => props.onChange?.(evt.target.value, undefined)}
autoFocus
+ disabled={editable === false}
/>
);
});