Skip to content

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

Prevent editing snippets from question editor when remote sync is read-only (#69147)

Andersond38d54ec945d
diff --git a/e2e/test/scenarios/native/snippets.cy.spec.js b/e2e/test/scenarios/native/snippets.cy.spec.js
index c4403e26327..be2968472ec 100644
--- a/e2e/test/scenarios/native/snippets.cy.spec.js
+++ b/e2e/test/scenarios/native/snippets.cy.spec.js
@@ -434,8 +434,8 @@ describe("scenarios > question > snippets (EE)", () => {
// Edit permissions for a snippet folder
H.rightSidebar()
.findByText("Snippet Folder")
- .next()
- .find(".Icon-ellipsis")
+ .parent()
+ .findByRole("button", { name: "Snippet folder options" })
.click({ force: true });
H.popover().findByText("Change permissions").click();
@@ -476,6 +476,60 @@ describe("scenarios > question > snippets (EE)", () => {
});
});
+describe("scenarios > question > read-only snippets", () => {
+ beforeEach(() => {
+ H.restore();
+ cy.signInAsAdmin();
+ H.activateToken("bleeding-edge");
+ H.createSnippet({
+ name: "stuff-snippet",
+ content: "select 'snippet 1'",
+ });
+ H.createSnippetFolder({
+ name: "My favorite snippets",
+ description: "the more you know",
+ });
+ H.setupGitSync();
+ });
+
+ it("should not let you create or edit a snippet", () => {
+ H.configureGitAndPullChanges("read-only");
+ H.startNewNativeQuestion();
+ cy.findByTestId("native-query-editor-action-buttons")
+ .icon("snippet")
+ .click();
+ cy.findByTestId("sidebar-content")
+ .findByText("Create snippet")
+ .should("not.exist");
+
+ cy.findByTestId("sidebar-right").within(() => {
+ cy.findByText("stuff-snippet").click();
+ cy.findByRole("button", { name: /pencil icon edit/i }).should(
+ "not.exist",
+ );
+ });
+ });
+
+ it("should not let you create or edit a snippet folder", () => {
+ H.configureGitAndPullChanges("read-only");
+ H.startNewNativeQuestion();
+ cy.icon("snippet").click();
+
+ cy.log("Menu that allows creating a snippet folder is not rendered");
+ cy.findByTestId("sidebar-right")
+ .as("sidebar")
+ .find(".Icon-add")
+ .should("not.exist");
+
+ cy.findByTestId("sidebar-right").within(() => {
+ cy.findByText("My favorite snippets").should("be.visible");
+ cy.findByRole("button", { name: "Snippet folder options" }).should(
+ "not.exist",
+ );
+ });
+ });
+});
+
function createNestedSnippet() {
cy.signInAsAdmin();
// Create snippet folder via API
diff --git a/enterprise/frontend/src/metabase-enterprise/data-studio/library/snippets/pages/NewSnippetPage/NewSnippetPage.tsx b/enterprise/frontend/src/metabase-enterprise/data-studio/library/snippets/pages/NewSnippetPage/NewSnippetPage.tsx
index 935242c6a3f..7cf56cd84a5 100644
--- a/enterprise/frontend/src/metabase-enterprise/data-studio/library/snippets/pages/NewSnippetPage/NewSnippetPage.tsx
+++ b/enterprise/frontend/src/metabase-enterprise/data-studio/library/snippets/pages/NewSnippetPage/NewSnippetPage.tsx
@@ -8,6 +8,7 @@ import { useCreateSnippetMutation } from "metabase/api";
import { getErrorMessage } from "metabase/api/utils";
import { CodeMirror } from "metabase/common/components/CodeMirror";
import { EditableText } from "metabase/common/components/EditableText";
+import { Unauthorized } from "metabase/common/components/ErrorPages";
import { LeaveRouteConfirmModal } from "metabase/common/components/LeaveConfirmModal";
import { Link } from "metabase/common/components/Link";
import { useToast } from "metabase/common/hooks";
@@ -18,9 +19,9 @@ import {
PaneHeaderActions,
PaneHeaderInput,
} from "metabase/data-studio/common/components/PaneHeader";
-import { useDispatch } from "metabase/lib/redux";
+import { useDispatch, useSelector } from "metabase/lib/redux";
import * as Urls from "metabase/lib/urls";
-import { PLUGIN_SNIPPET_FOLDERS } from "metabase/plugins";
+import { PLUGIN_REMOTE_SYNC, PLUGIN_SNIPPET_FOLDERS } from "metabase/plugins";
import { Card, Flex, Stack } from "metabase/ui";
import type {
NativeQuerySnippet,
@@ -47,6 +48,9 @@ export function NewSnippetPage({ route }: NewSnippetPageProps) {
);
const [createSnippet, { isLoading: isSaving }] = useCreateSnippetMutation();
const isValid = name.length > 0 && content.length > 0;
+ const isRemoteSyncReadOnly = useSelector(
+ PLUGIN_REMOTE_SYNC.getIsRemoteSyncReadOnly,
+ );
const handleCreateSnippet = async (
collectionId: RegularCollectionId | null,
@@ -95,6 +99,10 @@ export function NewSnippetPage({ route }: NewSnippetPageProps) {
const extensions = useMemo(() => [sql()], []);
+ if (isRemoteSyncReadOnly) {
+ return <Unauthorized />;
+ }
+
return (
<>
<PageContainer pos="relative" data-testid="new-snippet-page">
diff --git a/enterprise/frontend/src/metabase-enterprise/snippets/components/CollectionOptionsButton.jsx b/enterprise/frontend/src/metabase-enterprise/snippets/components/CollectionOptionsButton.jsx
deleted file mode 100644
index 82164f1faca..00000000000
--- a/enterprise/frontend/src/metabase-enterprise/snippets/components/CollectionOptionsButton.jsx
+++ /dev/null
@@ -1,89 +0,0 @@
-/* eslint-disable react/prop-types */
-import { Component } from "react";
-import { t } from "ttag";
-
-import { canonicalCollectionId } from "metabase/collections/utils";
-import { AccordionList } from "metabase/common/components/AccordionList";
-import { TippyPopoverWithTrigger } from "metabase/common/components/PopoverWithTrigger/TippyPopoverWithTrigger";
-import CS from "metabase/css/core/index.css";
-import { Icon } from "metabase/ui";
-
-const ICON_SIZE = 16;
-
-// eslint-disable-next-line import/no-default-export -- deprecated usage
-export default class CollectionOptionsButton extends Component {
- render() {
- const items = this.popoverOptions();
- if (items.length === 0) {
- return null;
- }
- const { className } = this.props;
-
- return (
- <div
- className={className}
- // prevent the ellipsis click from selecting the folder also
- onClick={(e) => e.stopPropagation()}
- // cap the large ellipsis so it doesn't increase the row height
- style={{ height: ICON_SIZE }}
- >
- <TippyPopoverWithTrigger
- triggerClasses={CS.hoverChild}
- triggerContent={<Icon name="ellipsis" size={20} />}
- placement="bottom-end"
- popoverContent={({ closePopover }) => (
- <AccordionList
- className={CS.textBrand}
- sections={[{ items }]}
- onChange={(item) => {
- item.onClick(item);
- closePopover();
- }}
- />
- )}
- />
- </div>
- );
- }
-
- popoverOptions = () => {
- const { collection, setSidebarState, user } = this.props;
- if (!collection.can_write) {
- return [];
- }
- if (collection.archived) {
- return [
- {
- name: t`Unarchive`,
- onClick: () => collection.setArchived(false),
- },
- ];
- }
- const onEdit = (collection) =>
- setSidebarState({ modalSnippetCollection: collection });
- const onEditCollectionPermissions = () =>
- setSidebarState({ permissionsModalCollectionId: collection.id });
-
- const options = [];
- const isRoot = canonicalCollectionId(collection.id) === null;
- if (!isRoot) {
- options.push({
- name: t`Edit folder details`,
- onClick: () => onEdit(collection),
- });
- }
- if (user && user.is_superuser) {
- options.push({
- name: t`Change permissions`,
- onClick: onEditCollectionPermissions,
- });
- }
- if (!isRoot) {
- options.push({
- name: t`Archive`,
- onClick: () => collection.setArchived(true),
- });
- }
- return options;
- };
-}
diff --git a/enterprise/frontend/src/metabase-enterprise/snippets/components/CollectionRow.jsx b/enterprise/frontend/src/metabase-enterprise/snippets/components/CollectionRow.jsx
index 275be87780b..d97701318a3 100644
--- a/enterprise/frontend/src/metabase-enterprise/snippets/components/CollectionRow.jsx
+++ b/enterprise/frontend/src/metabase-enterprise/snippets/components/CollectionRow.jsx
@@ -7,14 +7,17 @@ import CS from "metabase/css/core/index.css";
import { SnippetCollections } from "metabase/entities/snippet-collections";
import { Icon } from "metabase/ui";
-import CollectionOptionsButton from "./CollectionOptionsButton";
+import { SnippetCollectionMenu } from "./SnippetCollectionMenu";
const ICON_SIZE = 16;
class CollectionRow extends Component {
render() {
- const { snippetCollection: collection, setSnippetCollectionId } =
- this.props;
+ const {
+ snippetCollection: collection,
+ setSnippetCollectionId,
+ setSidebarState,
+ } = this.props;
const onSelectCollection = () => {
if (setSnippetCollectionId) {
setSnippetCollectionId(collection.id);
@@ -44,10 +47,15 @@ class CollectionRow extends Component {
<Ellipsified className={cx(CS.flexFull, CS.ml1, CS.textBold)} flex={1}>
{collection.name}
</Ellipsified>
- <CollectionOptionsButton
- {...this.props}
- collection={collection}
+ <SnippetCollectionMenu
className={CS.flexNoShrink}
+ collection={collection}
+ onEditDetails={() => {
+ setSidebarState({ modalSnippetCollection: collection });
+ }}
+ onChangePermissions={() => {
+ setSidebarState({ permissionsModalCollectionId: collection.id });
+ }}
/>
</div>
);
diff --git a/enterprise/frontend/src/metabase-enterprise/snippets/index.js b/enterprise/frontend/src/metabase-enterprise/snippets/index.js
index 6fdbeaa79db..1c0af01db8a 100644
--- a/enterprise/frontend/src/metabase-enterprise/snippets/index.js
+++ b/enterprise/frontend/src/metabase-enterprise/snippets/index.js
@@ -12,7 +12,6 @@ import {
} from "metabase/plugins";
import { hasPremiumFeature } from "metabase-enterprise/settings";
-import CollectionOptionsButton from "./components/CollectionOptionsButton";
import CollectionRow from "./components/CollectionRow";
import { MoveSnippetModal } from "./components/MoveSnippetModal";
import SnippetCollectionFormModal from "./components/SnippetCollectionFormModal";
@@ -86,12 +85,17 @@ export function initializePlugin() {
// Add header button
PLUGIN_SNIPPET_SIDEBAR_HEADER_BUTTONS.push((snippetSidebar, props) => {
const collection = snippetSidebar.props.snippetCollection;
+ const setSidebarState = snippetSidebar.setState.bind(snippetSidebar);
+
return (
- <CollectionOptionsButton
- {...snippetSidebar.props}
- {...props}
- setSidebarState={snippetSidebar.setState.bind(snippetSidebar)}
+ <SnippetCollectionMenu
collection={collection}
+ onEditDetails={() => {
+ setSidebarState({ modalSnippetCollection: collection });
+ }}
+ onChangePermissions={() => {
+ setSidebarState({ permissionsModalCollectionId: collection.id });
+ }}
/>
);
});
diff --git a/frontend/src/metabase/data-studio/common/hooks/use-build-snippet-tree/use-build-snippet-tree.ts b/frontend/src/metabase/data-studio/common/hooks/use-build-snippet-tree/use-build-snippet-tree.ts
index 52f981bfdfd..a2f4a86b0a9 100644
--- a/frontend/src/metabase/data-studio/common/hooks/use-build-snippet-tree/use-build-snippet-tree.ts
+++ b/frontend/src/metabase/data-studio/common/hooks/use-build-snippet-tree/use-build-snippet-tree.ts
@@ -2,6 +2,8 @@ import { useMemo } from "react";
import { useListCollectionsQuery, useListSnippetsQuery } from "metabase/api";
import type { TreeItem } from "metabase/data-studio/common/types";
+import { useSelector } from "metabase/lib/redux";
+import { PLUGIN_REMOTE_SYNC } from "metabase/plugins";
import { buildActiveSnippetTree, buildArchivedSnippetTree } from "./utils";
@@ -20,6 +22,9 @@ export const useBuildSnippetTree = ({ archived = false } = {}): {
namespace: "snippets",
archived,
});
+ const isRemoteSyncReadOnly = useSelector(
+ PLUGIN_REMOTE_SYNC.getIsRemoteSyncReadOnly,
+ );
return useMemo(() => {
if (
@@ -40,7 +45,11 @@ export const useBuildSnippetTree = ({ archived = false } = {}): {
error,
tree: archived
? buildArchivedSnippetTree(snippetCollections, snippets)
- : buildActiveSnippetTree(snippetCollections, snippets),
+ : buildActiveSnippetTree(
+ snippetCollections,
+ snippets,
+ !isRemoteSyncReadOnly,
+ ),
};
}, [
loadingSnippets,
@@ -49,5 +58,6 @@ export const useBuildSnippetTree = ({ archived = false } = {}): {
snippetCollections,
error,
archived,
+ isRemoteSyncReadOnly,
]);
};
diff --git a/frontend/src/metabase/data-studio/common/hooks/use-build-snippet-tree/utils.ts b/frontend/src/metabase/data-studio/common/hooks/use-build-snippet-tree/utils.ts
index 3c0ee6ef210..a1f23498bde 100644
--- a/frontend/src/metabase/data-studio/common/hooks/use-build-snippet-tree/utils.ts
+++ b/frontend/src/metabase/data-studio/common/hooks/use-build-snippet-tree/utils.ts
@@ -54,6 +54,7 @@ function buildSnippetCollectionNode(
export function buildActiveSnippetTree(
snippetCollections: Collection[],
snippets: NativeQuerySnippet[],
+ canWriteSnippets: boolean,
): TreeItem[] {
const collections = snippetCollections.filter((c) => !c.archived);
const activeSnippets = snippets.filter((s) => !s.archived);
@@ -74,7 +75,7 @@ export function buildActiveSnippetTree(
const hasContent = activeSnippets.length > 0 || nonRootCollections.length > 0;
const children = hasContent
? rootNode.children
- : [createEmptyStateItem("snippets")];
+ : [createEmptyStateItem("snippets", undefined, !canWriteSnippets)];
return [{ ...rootNode, name: t`SQL snippets`, children }];
}
diff --git a/frontend/src/metabase/data-studio/common/utils/create-empty-space-item.ts b/frontend/src/metabase/data-studio/common/utils/create-empty-space-item.ts
index 8cb68c3507a..b866ddd677d 100644
--- a/frontend/src/metabase/data-studio/common/utils/create-empty-space-item.ts
+++ b/frontend/src/metabase/data-studio/common/utils/create-empty-space-item.ts
@@ -38,14 +38,15 @@ const getEmptyStateConfig = (
export const createEmptyStateItem = (
sectionType: LibrarySectionType,
- metricCollectionId?: CollectionId,
+ collectionId?: CollectionId,
+ hideAction?: boolean,
): TreeItem => {
const config = getEmptyStateConfig(sectionType);
let actionUrl: string | undefined;
- if (sectionType === "metrics" && metricCollectionId) {
- actionUrl = Urls.newDataStudioMetric({ collectionId: metricCollectionId });
- } else if (sectionType === "snippets") {
+ if (sectionType === "metrics" && collectionId && !hideAction) {
+ actionUrl = Urls.newDataStudioMetric({ collectionId: collectionId });
+ } else if (sectionType === "snippets" && !hideAction) {
actionUrl = Urls.newDataStudioSnippet();
}
// "data" section opens a modal, so no actionUrl
diff --git a/frontend/src/metabase/query_builder/components/NativeQueryEditor/NativeQueryEditor.tsx b/frontend/src/metabase/query_builder/components/NativeQueryEditor/NativeQueryEditor.tsx
index 85464e13679..3042e786a94 100644
--- a/frontend/src/metabase/query_builder/components/NativeQueryEditor/NativeQueryEditor.tsx
+++ b/frontend/src/metabase/query_builder/components/NativeQueryEditor/NativeQueryEditor.tsx
@@ -12,6 +12,8 @@ import { useMount } from "react-use";
import { t } from "ttag";
import { useListCollectionsQuery, useListSnippetsQuery } from "metabase/api";
+import { useSelector } from "metabase/lib/redux";
+import { PLUGIN_REMOTE_SYNC } from "metabase/plugins";
import { SnippetFormModal } from "metabase/query_builder/components/template_tags/SnippetFormModal";
import type { QueryModalType } from "metabase/query_builder/constants";
import { useNotebookScreenSize } from "metabase/query_builder/hooks/use-notebook-screen-size";
@@ -167,7 +169,9 @@ export const NativeQueryEditor = forwardRef<
toggleSnippetSidebar,
topBarInnerContent,
} = props;
-
+ const isRemoteSyncReadOnly = useSelector(
+ PLUGIN_REMOTE_SYNC.getIsRemoteSyncReadOnly,
+ );
const { data: snippets = [] } = useListSnippetsQuery();
const { data: snippetCollections = [] } = useListCollectionsQuery({
namespace: "snippets",
@@ -176,9 +180,9 @@ export const NativeQueryEditor = forwardRef<
const editorRef = useRef<CodeMirrorEditorRef>(null);
const { ref: topBarRef, height: topBarHeight } = useElementSize();
- const canSaveSnippets = snippetCollections.some(
- (collection) => collection.can_write,
- );
+ const canSaveSnippets =
+ !isRemoteSyncReadOnly &&
+ snippetCollections.some((collection) => collection.can_write);
const canToggleEditor = typeof setIsNativeEditorOpen === "function";
const shouldShowEditor = isNativeEditorOpen || !canToggleEditor;
diff --git a/frontend/src/metabase/query_builder/components/template_tags/SnippetSidebar/SnippetSidebar.jsx b/frontend/src/metabase/query_builder/components/template_tags/SnippetSidebar/SnippetSidebar.jsx
index 83c23bf13e0..07f64ae8c5d 100644
--- a/frontend/src/metabase/query_builder/components/template_tags/SnippetSidebar/SnippetSidebar.jsx
+++ b/frontend/src/metabase/query_builder/components/template_tags/SnippetSidebar/SnippetSidebar.jsx
@@ -11,6 +11,7 @@ import { SnippetCollections } from "metabase/entities/snippet-collections";
import { Snippets } from "metabase/entities/snippets";
import { connect } from "metabase/lib/redux";
import {
+ PLUGIN_REMOTE_SYNC,
PLUGIN_SNIPPET_SIDEBAR_HEADER_BUTTONS,
PLUGIN_SNIPPET_SIDEBAR_MODALS,
PLUGIN_SNIPPET_SIDEBAR_PLUS_MENU_OPTIONS,
@@ -47,6 +48,7 @@ class SnippetSidebarInner extends React.Component {
setModalSnippet: PropTypes.func.isRequired,
openSnippetModalWithSelectedText: PropTypes.func.isRequired,
insertSnippet: PropTypes.func.isRequired,
+ isRemoteSyncReadOnly: PropTypes.bool.isRequired,
};
searchBox = React.createRef();
@@ -78,6 +80,7 @@ class SnippetSidebarInner extends React.Component {
render() {
const {
+ isRemoteSyncReadOnly,
snippets,
openSnippetModalWithSelectedText,
snippetCollection,
@@ -118,6 +121,8 @@ class SnippetSidebarInner extends React.Component {
this.props.setSnippetCollectionId(targetId);
};
+ const showAddMenu =
+ snippetCollection.can_write && !showSearch && !isRemoteSyncReadOnly;
return (
<SidebarContent footer={this.footer()}>
@@ -125,6 +130,7 @@ class SnippetSidebarInner extends React.Component {
displayedItems.length === 0 &&
snippetCollection.id === "root" ? (
<SnippetSidebarEmptyState
+ areSnippetsReadOnly={isRemoteSyncReadOnly}
onClick={openSnippetModalWithSelectedText}
/>
) : (
@@ -185,7 +191,7 @@ class SnippetSidebarInner extends React.Component {
</Button>
)}
- {snippetCollection.can_write && !showSearch && (
+ {showAddMenu && (
<Menu position="bottom-end">
<Menu.Target>
<Button
@@ -229,7 +235,9 @@ class SnippetSidebarInner extends React.Component {
item={item}
type={item.model || "snippet"}
setSidebarState={this.setState.bind(this)}
- canWrite={snippetCollection.can_write}
+ canWrite={
+ snippetCollection.can_write && !isRemoteSyncReadOnly
+ }
{...this.props}
/>
))}
@@ -257,11 +265,19 @@ export const SnippetSidebar = _.compose(
namespace: "snippets",
}),
}),
+ connect((state, { list }) => ({
+ isRemoteSyncReadOnly: PLUGIN_REMOTE_SYNC.getIsRemoteSyncReadOnly(state),
+ })),
)(SnippetSidebarInner);
function ArchivedSnippetsInner(props) {
- const { onBack, snippets, snippetCollections, archivedSnippetCollections } =
- props;
+ const {
+ onBack,
+ snippets,
+ snippetCollections,
+ archivedSnippetCollections,
+ isRemoteSyncReadOnly,
+ } = props;
const collectionsById = _.indexBy(
snippetCollections.concat(archivedSnippetCollections),
(c) => canonicalCollectionId(c.id),
@@ -278,6 +294,7 @@ function ArchivedSnippetsInner(props) {
key={`collection-${collection.id}`}
item={collection}
type="collection"
+ canWrite={!isRemoteSyncReadOnly}
/>
))}
{snippets.map((snippet) => (
@@ -289,7 +306,7 @@ function ArchivedSnippetsInner(props) {
collectionsById[
// `String` used to appease flow
String(canonicalCollectionId(snippet.collection_id))
- ].can_write
+ ].can_write && !isRemoteSyncReadOnly
}
/>
))}
@@ -299,7 +316,10 @@ function ArchivedSnippetsInner(props) {
const ArchivedSnippets = _.compose(
SnippetCollections.loadList({ query: { archived: true }, wrapped: true }),
- connect((state, { list }) => ({ archivedSnippetCollections: list })),
+ connect((state, { list }) => ({
+ isRemoteSyncReadOnly: PLUGIN_REMOTE_SYNC.getIsRemoteSyncReadOnly(state),
+ archivedSnippetCollections: list,
+ })),
SnippetCollections.loadList(),
Snippets.loadList({ query: { archived: true }, wrapped: true }),
)(ArchivedSnippetsInner);
diff --git a/frontend/src/metabase/query_builder/components/template_tags/SnippetSidebar/SnippetSidebarEmptyState.tsx b/frontend/src/metabase/query_builder/components/template_tags/SnippetSidebar/SnippetSidebarEmptyState.tsx
index 45f630abe64..028f1ae247f 100644
--- a/frontend/src/metabase/query_builder/components/template_tags/SnippetSidebar/SnippetSidebarEmptyState.tsx
+++ b/frontend/src/metabase/query_builder/components/template_tags/SnippetSidebar/SnippetSidebarEmptyState.tsx
@@ -5,8 +5,10 @@ import { Box, Button, Icon, Stack, Text, Title } from "metabase/ui";
export const SnippetSidebarEmptyState = ({
onClick,
+ areSnippetsReadOnly,
}: {
onClick: () => void;
+ areSnippetsReadOnly?: boolean;
}) => {
return (
<Box p="lg" mt="5rem">
@@ -15,27 +17,33 @@ export const SnippetSidebarEmptyState = ({
<img src={EmptySnippet} alt={t`Code snippet bot illustration`} />
</Box>
- <Box>
- <Title
- order={2}
- size="lg"
- mb="sm"
- >{t`Save time with reusable bits of code`}</Title>
- <Text fz="md">
- {c("{0} is the left arrow icon")
- .jt`Create a snippet from scratch or select and right-click existing code. Then use the ${(
- <Icon
- name="arrow_left_to_line"
- key="snippet-icon"
- style={{ verticalAlign: "middle" }}
- />
- )} button to add it to your query.`}
- </Text>
- </Box>
+ {areSnippetsReadOnly ? (
+ <Text fz="md">{t`No snippets to show.`}</Text>
+ ) : (
+ <>
+ <Box>
+ <Title
+ order={2}
+ size="lg"
+ mb="sm"
+ >{t`Save time with reusable bits of code`}</Title>
+ <Text fz="md">
+ {c("{0} is the left arrow icon")
+ .jt`Create a snippet from scratch or select and right-click existing code. Then use the ${(
+ <Icon
+ name="arrow_left_to_line"
+ key="snippet-icon"
+ style={{ verticalAlign: "middle" }}
+ />
+ )} button to add it to your query.`}
+ </Text>
+ </Box>
- <Button variant="subtle" onClick={onClick}>
- {t`Create snippet`}
- </Button>
+ <Button variant="subtle" onClick={onClick}>
+ {t`Create snippet`}
+ </Button>
+ </>
+ )}
</Stack>
</Box>
);
diff --git a/frontend/src/metabase/query_builder/components/template_tags/SnippetSidebar/SnippetSidebarEmptyState.unit.spec.tsx b/frontend/src/metabase/query_builder/components/template_tags/SnippetSidebar/SnippetSidebarEmptyState.unit.spec.tsx
new file mode 100644
index 00000000000..b2cf2299750
--- /dev/null
+++ b/frontend/src/metabase/query_builder/components/template_tags/SnippetSidebar/SnippetSidebarEmptyState.unit.spec.tsx
@@ -0,0 +1,31 @@
+import userEvent from "@testing-library/user-event";
+
+import { render, screen } from "__support__/ui";
+
+import { SnippetSidebarEmptyState } from "./SnippetSidebarEmptyState";
+
+describe("SnippetSidebarEmptyState", () => {
+ it("should render the empty state with Create snippet button", async () => {
+ const onButtonClick = jest.fn();
+ render(<SnippetSidebarEmptyState onClick={onButtonClick} />);
+
+ expect(
+ screen.getByRole("button", { name: "Create snippet" }),
+ ).toBeInTheDocument();
+
+ await userEvent.click(
+ screen.getByRole("button", { name: "Create snippet" }),
+ );
+ expect(onButtonClick).toHaveBeenCalled();
+ });
+
+ it("should not render the Create snippet button when snippets are read-only", () => {
+ render(
+ <SnippetSidebarEmptyState onClick={jest.fn()} areSnippetsReadOnly />,
+ );
+
+ expect(
+ screen.queryByRole("button", { name: "Create snippet" }),
+ ).not.toBeInTheDocument();
+ });
+});
diff --git a/frontend/src/metabase/query_builder/components/template_tags/SnippetSidebar/tests/premium.unit.spec.tsx b/frontend/src/metabase/query_builder/components/template_tags/SnippetSidebar/tests/premium.unit.spec.tsx
index e628ce84c3e..d3552459980 100644
--- a/frontend/src/metabase/query_builder/components/template_tags/SnippetSidebar/tests/premium.unit.spec.tsx
+++ b/frontend/src/metabase/query_builder/components/template_tags/SnippetSidebar/tests/premium.unit.spec.tsx
@@ -1,6 +1,6 @@
import userEvent from "@testing-library/user-event";
-import { getIcon, queryIcon, screen } from "__support__/ui";
+import { getIcon, screen } from "__support__/ui";
// TODO remove this and use proper reset functions once
// plugins initialization functions return proper teardown functions
// eslint-disable-next-line no-restricted-imports
@@ -30,8 +30,9 @@ describe("SnippetSidebar (EE with token feature)", () => {
it("should not display the `Change permissions` menu for non-admin users", async () => {
await setup({ user: { is_superuser: false } });
+ await userEvent.click(getIcon("ellipsis"));
- expect(queryIcon("ellipsis")).not.toBeInTheDocument();
+ expect(screen.queryByText("Change permissions")).not.toBeInTheDocument();
});
it("should display the `New snippet` and the `New folder` option", async () => {