Skip to content

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

Add tables and snippets to useSetCollection (#76670)

Nick Fitzpatrickc9f621e291d1
diff --git a/enterprise/frontend/src/metabase-enterprise/data-studio/library/tables/components/TableHeader/TableMoreMenu/TableMoreMenu.tsx b/enterprise/frontend/src/metabase-enterprise/data-studio/library/tables/components/TableHeader/TableMoreMenu/TableMoreMenu.tsx
index ddeb98da701..7e4dd432389 100644
--- a/enterprise/frontend/src/metabase-enterprise/data-studio/library/tables/components/TableHeader/TableMoreMenu/TableMoreMenu.tsx
+++ b/enterprise/frontend/src/metabase-enterprise/data-studio/library/tables/components/TableHeader/TableMoreMenu/TableMoreMenu.tsx
@@ -2,9 +2,10 @@ import { useState } from "react";
import { push } from "react-router-redux";
import { c, t } from "ttag";
-import { collectionApi, useUpdateTableMutation } from "metabase/api";
+import { collectionApi } from "metabase/api";
import { ForwardRefLink } from "metabase/common/components/Link";
import { CollectionPickerModal } from "metabase/common/components/Pickers";
+import { useSetCollection } from "metabase/common/hooks/use-set-collection";
import { PLUGIN_LIBRARY, PLUGIN_REMOTE_SYNC } from "metabase/plugins";
import { useDispatch, useSelector } from "metabase/redux";
import { ActionIcon, Box, FixedSizeIcon, Icon, Menu } from "metabase/ui";
@@ -23,7 +24,7 @@ export type TableMoreMenuProps = {
export function TableMoreMenu({ table, onMoved }: TableMoreMenuProps) {
const dispatch = useDispatch();
const [modalType, setModalType] = useState<TableModalType>();
- const [updateTable] = useUpdateTableMutation();
+ const setCollection = useSetCollection();
const remoteSyncReadOnly = useSelector(
PLUGIN_REMOTE_SYNC.getIsRemoteSyncReadOnly,
);
@@ -37,10 +38,11 @@ export function TableMoreMenu({ table, onMoved }: TableMoreMenuProps) {
const handleMove = async (newCollection: { id: CollectionId }) => {
const sourceCollectionId = table.collection_id;
- await updateTable({
- id: table.id,
- collection_id: newCollection.id,
- }).unwrap();
+ await setCollection(
+ { model: "table", id: table.id },
+ { id: newCollection.id },
+ { notify: false },
+ );
dispatch(
collectionApi.util.invalidateTags([
{ type: "collection", id: `${sourceCollectionId}-items` },
diff --git a/enterprise/frontend/src/metabase-enterprise/snippets/components/MoveSnippetModal/MoveSnippetModal.tsx b/enterprise/frontend/src/metabase-enterprise/snippets/components/MoveSnippetModal/MoveSnippetModal.tsx
index 5beebb1cb94..fe06e370173 100644
--- a/enterprise/frontend/src/metabase-enterprise/snippets/components/MoveSnippetModal/MoveSnippetModal.tsx
+++ b/enterprise/frontend/src/metabase-enterprise/snippets/components/MoveSnippetModal/MoveSnippetModal.tsx
@@ -1,41 +1,41 @@
import { useState } from "react";
import { t } from "ttag";
-import { useUpdateSnippetMutation } from "metabase/api";
import { getErrorMessage } from "metabase/api/utils";
import { useToast } from "metabase/common/hooks";
+import { useSetCollection } from "metabase/common/hooks/use-set-collection";
import type { MoveSnippetModalProps } from "metabase/plugins";
import type { CollectionId } from "metabase-types/api";
import { SnippetCollectionPickerModal } from "../SnippetCollectionPickerModal";
export function MoveSnippetModal({ snippet, onClose }: MoveSnippetModalProps) {
- const [updateSnippet] = useUpdateSnippetMutation();
+ const setCollection = useSetCollection();
const [sendToast] = useToast();
const [isMoving, setIsMoving] = useState(false);
const handleMove = async (collectionId: CollectionId | null) => {
setIsMoving(true);
- const { error } = await updateSnippet({
- id: snippet.id,
- collection_id: collectionId,
- });
-
- if (error) {
- sendToast({
- message: getErrorMessage(error, t`Failed to move snippet`),
- icon: "warning",
- });
- } else {
+ try {
+ await setCollection(
+ { model: "snippet", id: snippet.id },
+ { id: collectionId ?? "root" },
+ { notify: false },
+ );
sendToast({
message: t`Snippet moved`,
icon: "check",
});
onClose();
+ } catch (error) {
+ sendToast({
+ message: getErrorMessage(error, t`Failed to move snippet`),
+ icon: "warning",
+ });
+ } finally {
+ setIsMoving(false);
}
-
- setIsMoving(false);
};
return (
diff --git a/frontend/src/metabase/common/hooks/use-set-collection.ts b/frontend/src/metabase/common/hooks/use-set-collection.ts
index 11efedb3fda..35b12d7ab99 100644
--- a/frontend/src/metabase/common/hooks/use-set-collection.ts
+++ b/frontend/src/metabase/common/hooks/use-set-collection.ts
@@ -8,11 +8,15 @@ import {
collectionApi,
dashboardApi,
documentApi,
+ snippetApi,
+ tableApi,
timelineApi,
useUpdateCardMutation,
useUpdateCollectionMutation,
useUpdateDashboardMutation,
useUpdateDocumentMutation,
+ useUpdateSnippetMutation,
+ useUpdateTableMutation,
useUpdateTimelineMutation,
} from "metabase/api";
import {
@@ -32,6 +36,8 @@ import type {
Dashboard,
DashboardId,
Document,
+ NativeQuerySnippet,
+ Table,
Timeline,
} from "metabase-types/api";
@@ -51,7 +57,9 @@ export type MovableItem =
| Movable<"dashboard", Dashboard>
| Movable<"collection", Collection>
| Movable<"snippet-collection", Collection>
+ | Movable<"snippet", NativeQuerySnippet>
| Movable<"document", Document>
+ | Movable<"table", Table>
| Movable<"timeline", Timeline, "name">;
export type MovableModel = MovableItem["model"];
@@ -63,6 +71,7 @@ const MOVABLE_MODELS = new Set<MovableModel>([
"dashboard",
"collection",
"snippet-collection",
+ "snippet",
"document",
"timeline",
]);
@@ -85,7 +94,9 @@ const LABELS = {
dashboard: () => t`dashboard`,
collection: () => t`collection`,
"snippet-collection": () => t`folder`,
+ snippet: () => t`snippet`,
document: () => t`document`,
+ table: () => t`table`,
timeline: () => t`timeline`,
} as const satisfies Record<MovableModel, () => string>;
@@ -107,6 +118,8 @@ export function useSetCollection() {
const [updateDashboard] = useUpdateDashboardMutation();
const [updateCollection] = useUpdateCollectionMutation();
const [updateDocument] = useUpdateDocumentMutation();
+ const [updateSnippet] = useUpdateSnippetMutation();
+ const [updateTable] = useUpdateTableMutation();
const [updateTimeline] = useUpdateTimelineMutation();
const setCollection = useCallback(
@@ -174,6 +187,28 @@ export function useSetCollection() {
parent_id: canonicalCollectionId(destination.id),
}).unwrap();
})
+ .with({ model: "snippet" }, ({ id }) => {
+ if (!isCollectionDestination(destination)) {
+ throw new Error("Cannot move a snippet into a dashboard");
+ }
+ return updateSnippet({
+ id,
+ collection_id: canonicalCollectionId(destination.id),
+ archived,
+ }).unwrap();
+ })
+ .with({ model: "table" }, ({ id }) => {
+ if (!isCollectionDestination(destination)) {
+ throw new Error("Cannot move a table into a dashboard");
+ }
+ if (archived) {
+ throw new Error("Cannot move a table to the trash");
+ }
+ return updateTable({
+ id,
+ collection_id: canonicalCollectionId(destination.id),
+ }).unwrap();
+ })
.with({ model: "timeline" }, ({ id, name }) => {
if (!isCollectionDestination(destination)) {
throw new Error("Cannot move a timeline into a dashboard");
@@ -192,6 +227,8 @@ export function useSetCollection() {
updateDashboard,
updateCollection,
updateDocument,
+ updateSnippet,
+ updateTable,
updateTimeline,
],
);
@@ -253,6 +290,27 @@ export function useSetCollection() {
});
},
)
+ .with({ model: "snippet" }, async ({ id }) => {
+ const snippet = await dispatch(
+ snippetApi.endpoints.getSnippet.initiate(id),
+ ).unwrap();
+ return () =>
+ updateSnippet({
+ id,
+ collection_id: snippet.collection_id,
+ archived: snippet.archived,
+ });
+ })
+ .with({ model: "table" }, async ({ id }) => {
+ const table = await dispatch(
+ tableApi.endpoints.getTable.initiate({ id }),
+ ).unwrap();
+ return () =>
+ updateTable({
+ id,
+ collection_id: table.collection_id,
+ });
+ })
.with({ model: "timeline" }, async ({ id }) => {
const timeline = await dispatch(
timelineApi.endpoints.getTimeline.initiate({ id }),
@@ -272,6 +330,8 @@ export function useSetCollection() {
updateDashboard,
updateCollection,
updateDocument,
+ updateSnippet,
+ updateTable,
updateTimeline,
],
);