Skip to content

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

Show the archived-entity banner on trashed explorations (#83144)

Denis Berezinb04e88210a1f
diff --git a/e2e/test/scenarios/explorations/explorations.cy.spec.ts b/e2e/test/scenarios/explorations/explorations.cy.spec.ts
index 720335f65c0..081e1574a88 100644
--- a/e2e/test/scenarios/explorations/explorations.cy.spec.ts
+++ b/e2e/test/scenarios/explorations/explorations.cy.spec.ts
@@ -1367,6 +1367,65 @@ describe("scenarios > explorations > collection placement + archive", () => {
);
});
+ it("shows the trash banner on an archived exploration and restores or permanently deletes it from there", () => {
+ const explorationName = "Exploration-page trash banner fixture";
+
+ H.createExplorationViaApi({ name: explorationName }).then(
+ (explorationId) => {
+ cy.request("PUT", `/api/exploration/${explorationId}`, {
+ archived: true,
+ });
+
+ cy.log("An archived exploration opens with the trash banner");
+ cy.intercept("PUT", `/api/exploration/${explorationId}`).as(
+ "restoreExploration",
+ );
+ H.visitExploration(explorationId);
+ cy.findByTestId("archive-banner")
+ .should("contain", "This research is in the trash.")
+ .findByRole("button", { name: /Restore/ })
+ .click();
+
+ cy.wait("@restoreExploration").then(({ request, response }) => {
+ expect(request.body).to.deep.eq({ archived: false });
+ expect(response?.statusCode).to.eq(200);
+ });
+ cy.findByTestId("exploration-page-sidebar").should("be.visible");
+ cy.findByTestId("archive-banner").should("not.exist");
+
+ cy.log("Delete permanently from the banner lands on the trash page");
+ cy.request("PUT", `/api/exploration/${explorationId}`, {
+ archived: true,
+ });
+ cy.intercept("DELETE", `/api/exploration/${explorationId}`).as(
+ "deleteExploration",
+ );
+ cy.reload();
+ cy.findByTestId("archive-banner")
+ .findByRole("button", { name: /Delete permanently/ })
+ .click();
+ H.modal()
+ .findByRole("button", { name: /Delete permanently/i })
+ .click();
+
+ cy.wait("@deleteExploration")
+ .its("response.statusCode")
+ .should("eq", 204);
+ cy.location("pathname").should("eq", "/trash");
+ H.undoToast()
+ .findByText("This item has been permanently deleted.")
+ .should("be.visible");
+ cy.request({
+ method: "GET",
+ url: `/api/exploration/${explorationId}`,
+ failOnStatusCode: false,
+ })
+ .its("status")
+ .should("eq", 404);
+ },
+ );
+ });
+
it("restores an archived exploration via the Restore action on the /trash page", () => {
// `useSetArchive`'s undo toast is one path back, but the trash
// page exposes a separate `Restore` action that runs through
diff --git a/frontend/src/metabase-types/api/exploration.ts b/frontend/src/metabase-types/api/exploration.ts
index 2e711b8c723..c9825a8e398 100644
--- a/frontend/src/metabase-types/api/exploration.ts
+++ b/frontend/src/metabase-types/api/exploration.ts
@@ -428,6 +428,8 @@ export interface Exploration {
creator?: ExplorationCreator;
threads?: ExplorationThread[];
can_write: boolean;
+ can_restore: boolean;
+ can_delete: boolean;
document?: ExplorationDocument | null;
}
diff --git a/frontend/src/metabase/explorations/components/ExplorationArchivedEntityBanner.tsx b/frontend/src/metabase/explorations/components/ExplorationArchivedEntityBanner.tsx
new file mode 100644
index 00000000000..d4f5c1f15b4
--- /dev/null
+++ b/frontend/src/metabase/explorations/components/ExplorationArchivedEntityBanner.tsx
@@ -0,0 +1,47 @@
+import { t } from "ttag";
+
+import { useDeleteExplorationMutation } from "metabase/api";
+import { ArchivedEntityBanner } from "metabase/archive/components/ArchivedEntityBanner";
+import { useSetArchive } from "metabase/archive/hooks";
+import { useSetCollection } from "metabase/common/hooks";
+import { useDispatch } from "metabase/redux";
+import { addUndo } from "metabase/redux/undo";
+import { useNavigate } from "metabase/router";
+import type { Exploration } from "metabase-types/api";
+
+interface ExplorationArchivedEntityBannerProps {
+ exploration: Exploration;
+}
+
+export const ExplorationArchivedEntityBanner = ({
+ exploration,
+}: ExplorationArchivedEntityBannerProps) => {
+ const dispatch = useDispatch();
+ const navigate = useNavigate();
+ const archive = useSetArchive();
+ const setCollection = useSetCollection();
+ const [deleteExploration] = useDeleteExplorationMutation();
+
+ return (
+ <ArchivedEntityBanner
+ name={exploration.name}
+ entityType={t`research`}
+ canMove={exploration.can_write}
+ canRestore={exploration.can_restore}
+ canDelete={exploration.can_delete}
+ onUnarchive={() =>
+ archive({ id: exploration.id, model: "exploration" }, false)
+ }
+ onMove={({ id }) =>
+ setCollection({ model: "exploration", id: exploration.id }, { id })
+ }
+ onDeletePermanently={async () => {
+ await deleteExploration(exploration.id).unwrap();
+ navigate("/trash");
+ dispatch(
+ addUndo({ message: t`This item has been permanently deleted.` }),
+ );
+ }}
+ />
+ );
+};
diff --git a/frontend/src/metabase/explorations/pages/ExplorationPage.tsx b/frontend/src/metabase/explorations/pages/ExplorationPage.tsx
index 4da69b6b415..908129763f0 100644
--- a/frontend/src/metabase/explorations/pages/ExplorationPage.tsx
+++ b/frontend/src/metabase/explorations/pages/ExplorationPage.tsx
@@ -31,6 +31,7 @@ import {
isTerminalExplorationThreadStatus,
} from "metabase-types/api";
+import { ExplorationArchivedEntityBanner } from "../components/ExplorationArchivedEntityBanner";
import {
ExplorationSidebar,
ExplorationTitle,
@@ -583,10 +584,13 @@ function ExplorationPageForId({ view }: ExplorationPageProps) {
}
return (
- <Group h="100%" align="stretch" gap={0}>
+ <Stack h="100%" gap={0}>
+ {exploration.archived && (
+ <ExplorationArchivedEntityBanner exploration={exploration} />
+ )}
<Stack
- h="100%"
flex={1}
+ mih={0}
bg="background-secondary"
pl="1.5rem"
pt="1rem"
@@ -663,6 +667,6 @@ function ExplorationPageForId({ view }: ExplorationPageProps) {
shouldPoll && <ExplorationChartAreaSkeleton />}
</Group>
</Stack>
- </Group>
+ </Stack>
);
}
diff --git a/frontend/src/metabase/explorations/pages/ExplorationPage.unit.spec.tsx b/frontend/src/metabase/explorations/pages/ExplorationPage.unit.spec.tsx
index 70b04413bb5..652a4a07da3 100644
--- a/frontend/src/metabase/explorations/pages/ExplorationPage.unit.spec.tsx
+++ b/frontend/src/metabase/explorations/pages/ExplorationPage.unit.spec.tsx
@@ -7,6 +7,7 @@ import {
renderWithProviders,
screen,
waitFor,
+ within,
} from "__support__/ui";
import {
createBlock,
@@ -513,3 +514,38 @@ describe("ExplorationPage thread-ready toasts", () => {
).toHaveLength(1);
});
});
+
+describe("archived exploration", () => {
+ it("shows the trash banner and restores the exploration from it", async () => {
+ explorationData = {
+ ...makeMultiPageExploration(),
+ archived: true,
+ can_restore: true,
+ can_delete: true,
+ };
+ const updateUrl = `path:/api/exploration/${explorationData.id}`;
+ fetchMock.put(updateUrl, { ...explorationData, archived: false });
+
+ renderExplorationPage();
+
+ const banner = await screen.findByTestId("archive-banner");
+ expect(banner).toHaveTextContent("This research is in the trash.");
+
+ fireEvent.click(within(banner).getByText("Restore"));
+
+ await waitFor(() => {
+ expect(fetchMock.callHistory.called(updateUrl)).toBe(true);
+ });
+ expect(
+ await fetchMock.callHistory.lastCall(updateUrl)?.request?.json(),
+ ).toEqual({ archived: false });
+ });
+
+ it("does not show the trash banner for an exploration that is not archived", () => {
+ explorationData = makeMultiPageExploration();
+
+ renderExplorationPage();
+
+ expect(screen.queryByTestId("archive-banner")).not.toBeInTheDocument();
+ });
+});
diff --git a/frontend/src/metabase/explorations/test-utils.ts b/frontend/src/metabase/explorations/test-utils.ts
index 508cce7d337..cc90fb1e561 100644
--- a/frontend/src/metabase/explorations/test-utils.ts
+++ b/frontend/src/metabase/explorations/test-utils.ts
@@ -193,6 +193,8 @@ export function createExploration({
description: null,
creator_id: 1,
can_write: true,
+ can_restore: false,
+ can_delete: false,
archived: false,
collection_id: null,
collection_position: null,
diff --git a/src/metabase/explorations/api.clj b/src/metabase/explorations/api.clj
index e474ad291e9..4c75c38da59 100644
--- a/src/metabase/explorations/api.clj
+++ b/src/metabase/explorations/api.clj
@@ -221,7 +221,7 @@
(defn- hydrate-exploration [exploration]
(-> exploration
- (t2/hydrate :creator :can_write :collection :document [:threads :queries :timelines])
+ (t2/hydrate :creator :can_write :can_restore :can_delete :collection :document [:threads :queries :timelines])
(update :threads
#(some->> %
(mapv (comp redact-thread-query-errors attach-thread-status))
@@ -480,6 +480,9 @@
[:creator {:optional true} [:maybe :map]]
[:collection_id {:optional true} [:maybe ms/PositiveInt]]
[:archived {:optional true} :boolean]
+ [:can_write {:optional true} :boolean]
+ [:can_restore {:optional true} :boolean]
+ [:can_delete {:optional true} :boolean]
[:document {:optional true} [:maybe ::ExplorationDocument]]
[:threads {:optional true} [:maybe [:sequential ::HydratedThread]]]
[:created_at {:optional true} [:maybe :any]]
diff --git a/test/metabase/explorations/api_test.clj b/test/metabase/explorations/api_test.clj
index b02622b226a..5b11ef9409e 100644
--- a/test/metabase/explorations/api_test.clj
+++ b/test/metabase/explorations/api_test.clj
@@ -943,6 +943,19 @@
(mt/with-temp [:model/User u {:email "[email protected]"}]
(mt/user-http-request u :post 404 "exploration/thread/9999999/restart"))))
+(deftest exploration-get-hydrates-trash-permissions-test
+ (testing "GET /api/exploration/:id hydrates :can_restore and :can_delete"
+ (mt/with-temp [:model/Exploration {trashed-id :id} {:name "trashed"
+ :creator_id (mt/user->id :crowberto)
+ :archived true
+ :archived_directly true}
+ :model/Exploration {live-id :id} {:name "live"
+ :creator_id (mt/user->id :crowberto)}]
+ (is (=? {:archived true, :can_restore true, :can_delete true}
+ (mt/user-http-request :crowberto :get 200 (format "exploration/%d" trashed-id))))
+ (is (=? {:archived false, :can_restore false, :can_delete false}
+ (mt/user-http-request :crowberto :get 200 (format "exploration/%d" live-id)))))))
+
(deftest exploration-get-permissions-test
(testing "Only the creator (or a superuser) can GET an exploration"
(mt/with-temp [:model/User owner {:email "[email protected]"}