Skip to content

Build your personal knowledge base with Trilium Notes

client/content manager/space usage: add a context menu to erase the deleted notes

Adorian Doran04867fb15014
diff --git a/apps/client/src/widgets/type_widgets/options/content_manager/space_usage/context_menu.spec.ts b/apps/client/src/widgets/type_widgets/options/content_manager/space_usage/context_menu.spec.ts
index 215911df7e..2b72aa9aea 100644
--- a/apps/client/src/widgets/type_widgets/options/content_manager/space_usage/context_menu.spec.ts
+++ b/apps/client/src/widgets/type_widgets/options/content_manager/space_usage/context_menu.spec.ts
@@ -18,7 +18,9 @@ const mocks = vi.hoisted(() => ({
deleteNotes: vi.fn(async (..._args: unknown[]) => true),
downloadFileNote: vi.fn(),
showDetails: vi.fn(),
- contentChanged: vi.fn()
+ contentChanged: vi.fn(),
+ post: vi.fn(async (..._args: unknown[]) => undefined),
+ showMessage: vi.fn()
}));
vi.mock("../../../../../menus/context_menu", () => ({
@@ -45,6 +47,14 @@ vi.mock("../../../../../services/open", () => ({
downloadFileNote: (...args: unknown[]) => mocks.downloadFileNote(...args)
}));
+vi.mock("../../../../../services/server", () => ({
+ default: { post: (...args: unknown[]) => mocks.post(...args) }
+}));
+
+vi.mock("../../../../../services/toast", () => ({
+ default: { showMessage: (...args: unknown[]) => mocks.showMessage(...args) }
+}));
+
vi.mock("../../../../../components/app_context", () => ({
default: {
triggerCommand: mocks.triggerCommand,
@@ -55,7 +65,7 @@ vi.mock("../../../../../components/app_context", () => ({
}
}));
-import { openSpaceUsageContextMenu } from "./context_menu";
+import { openDeletedNotesContextMenu, openSpaceUsageContextMenu } from "./context_menu";
// The translations are uninitialized under the test i18n, so titles come back empty; items are
// identified by their icon, which is what distinguishes them anyway.
@@ -65,6 +75,8 @@ const SHOW_DETAILS = "bx bx-detail";
const DOWNLOAD = "bx bx-download";
const EXPORT = "bx bx-export";
const DELETE = "bx bx-trash destructive-action-icon";
+// The deleted bucket's erase item wears the same destructive trash icon, in a menu of its own.
+const ERASE = DELETE;
function givenNote(noteId: string, type = "text", isContentAvailable = true) {
mocks.notes.set(noteId, {
@@ -201,3 +213,24 @@ describe("openSpaceUsageContextMenu", () => {
expect(mocks.shown).toBeUndefined();
});
});
+
+describe("openDeletedNotesContextMenu", () => {
+ it("erases the deleted notes and asks for a fresh reading once the server is done", async () => {
+ const event = contextMenuEvent();
+
+ await openDeletedNotesContextMenu(event, mocks.contentChanged);
+
+ expect(event.preventDefault).toHaveBeenCalled();
+ expect(event.stopPropagation).toHaveBeenCalled();
+ // The bucket stands for a crowd of notes, not one of them, so erasing is all it offers.
+ expect(icons()).toEqual([ ERASE ]);
+
+ invoke(ERASE);
+ expect(mocks.post).toHaveBeenCalledWith("notes/erase-deleted-notes-now");
+ // Not before the server answers: the bucket keeps its area until the space is really gone.
+ expect(mocks.contentChanged).not.toHaveBeenCalled();
+
+ await vi.waitFor(() => expect(mocks.contentChanged).toHaveBeenCalledTimes(1));
+ expect(mocks.showMessage).toHaveBeenCalled();
+ });
+});
diff --git a/apps/client/src/widgets/type_widgets/options/content_manager/space_usage/context_menu.ts b/apps/client/src/widgets/type_widgets/options/content_manager/space_usage/context_menu.ts
index 2d4ca9440b..6bea2e7d10 100644
--- a/apps/client/src/widgets/type_widgets/options/content_manager/space_usage/context_menu.ts
+++ b/apps/client/src/widgets/type_widgets/options/content_manager/space_usage/context_menu.ts
@@ -8,6 +8,8 @@ import branches from "../../../../../services/branches";
import froca from "../../../../../services/froca";
import { t } from "../../../../../services/i18n";
import { downloadFileNote } from "../../../../../services/open";
+import server from "../../../../../services/server";
+import toast from "../../../../../services/toast";
const ROOT_NOTE_ID = "root";
@@ -131,6 +133,38 @@ export async function openSpaceUsageContextMenu(
});
}
+/**
+ * The menu on the deleted-notes cell, which stands for space the tree has already let go of but the
+ * database still holds until the retention window runs out. Its one action is releasing that space
+ * now — the same erasure the Recent Changes dialog and the options page run, so the wording and the
+ * confirmation-free behaviour are theirs.
+ */
+export async function openDeletedNotesContextMenu(
+ event: ContextMenuEvent,
+ onContentChanged: ContentChangedHandler
+) {
+ event.preventDefault();
+ event.stopPropagation();
+
+ await contextMenu.show({
+ x: event.pageX,
+ y: event.pageY,
+ items: [ {
+ title: t("recent_changes.erase_notes_button"),
+ uiIcon: "bx bx-trash destructive-action-icon",
+ handler: () => {
+ void server.post("notes/erase-deleted-notes-now").then(() => {
+ toast.showMessage(t("recent_changes.deleted_notes_message"));
+ // The cell just lost everything it stood for, so the section has to measure
+ // again — the views take a reading when asked, never on their own.
+ onContentChanged();
+ });
+ }
+ } ],
+ selectMenuItemHandler: () => {}
+ });
+}
+
/**
* The default action on a Space Usage note: the quick-edit popup, which stacks above the settings
* dialog instead of replacing what the user was looking at.
diff --git a/apps/client/src/widgets/type_widgets/options/content_manager/space_usage/overview.spec.tsx b/apps/client/src/widgets/type_widgets/options/content_manager/space_usage/overview.spec.tsx
index 8fbe47b55f..d6f33b981c 100644
--- a/apps/client/src/widgets/type_widgets/options/content_manager/space_usage/overview.spec.tsx
+++ b/apps/client/src/widgets/type_widgets/options/content_manager/space_usage/overview.spec.tsx
@@ -5,6 +5,7 @@ import { afterEach, describe, expect, it, vi } from "vitest";
const mocks = vi.hoisted(() => ({
triggerCommand: vi.fn(),
openContextMenu: vi.fn(),
+ openDeletedNotesMenu: vi.fn(),
showDetails: vi.fn(),
contentChanged: vi.fn()
}));
@@ -16,10 +17,11 @@ vi.mock("../../../../../components/app_context", () => ({
}
}));
-// The menu itself is covered by its own spec; here only the wiring matters.
+// The menus themselves are covered by their own spec; here only the wiring matters.
vi.mock("./context_menu", async (importOriginal) => ({
...await importOriginal<typeof import("./context_menu")>(),
- openSpaceUsageContextMenu: (...args: unknown[]) => mocks.openContextMenu(...args)
+ openSpaceUsageContextMenu: (...args: unknown[]) => mocks.openContextMenu(...args),
+ openDeletedNotesContextMenu: (...args: unknown[]) => mocks.openDeletedNotesMenu(...args)
}));
// The treemap container never gets a real layout under happy-dom.
@@ -68,6 +70,7 @@ afterEach(() => {
}
mocks.triggerCommand.mockClear();
mocks.openContextMenu.mockClear();
+ mocks.openDeletedNotesMenu.mockClear();
mocks.showDetails.mockClear();
});
@@ -109,10 +112,26 @@ describe("Overview", () => {
mocks.triggerCommand.mockClear();
mocks.openContextMenu.mockClear();
- const bucket = probe.querySelector(".treemap-cell-deleted");
+ const bucket = probe.querySelector(".treemap-cell-other");
bucket?.dispatchEvent(new MouseEvent("click", { bubbles: true }));
bucket?.dispatchEvent(new MouseEvent("contextmenu", { bubbles: true }));
expect(mocks.triggerCommand).not.toHaveBeenCalled();
expect(mocks.openContextMenu).not.toHaveBeenCalled();
});
+
+ it("answers the deleted bucket with its own menu, never the note one", () => {
+ const probe = renderOverview();
+ const deleted = probe.querySelector(".treemap-cell-deleted");
+
+ deleted?.dispatchEvent(new MouseEvent("contextmenu", { bubbles: true }));
+
+ // The bucket names no note, so the note menu has nothing to act on; erasing does.
+ expect(mocks.openContextMenu).not.toHaveBeenCalled();
+ expect(mocks.openDeletedNotesMenu)
+ .toHaveBeenCalledWith(expect.anything(), mocks.contentChanged);
+
+ // Clicking it stays inert, like the other buckets.
+ deleted?.dispatchEvent(new MouseEvent("click", { bubbles: true }));
+ expect(mocks.triggerCommand).not.toHaveBeenCalled();
+ });
});
diff --git a/apps/client/src/widgets/type_widgets/options/content_manager/space_usage/overview.tsx b/apps/client/src/widgets/type_widgets/options/content_manager/space_usage/overview.tsx
index 170c4b3005..17b68c90a7 100644
--- a/apps/client/src/widgets/type_widgets/options/content_manager/space_usage/overview.tsx
+++ b/apps/client/src/widgets/type_widgets/options/content_manager/space_usage/overview.tsx
@@ -7,6 +7,7 @@ import { formatSize } from "../../../../../services/utils";
import Treemap, { type TreemapItem } from "../../../../react/charts/Treemap";
import {
type ContentChangedHandler,
+ openDeletedNotesContextMenu,
openSpaceUsageContextMenu,
quickEditNote,
type ShowDetailsHandler
@@ -67,8 +68,17 @@ export default function Overview({ overview, onShowDetails, onContentChanged }:
<Treemap<OverviewCell>
root={model}
onItemClick={(item) => withCellPath(item, quickEditNote)}
- onItemContextMenu={(item, event) => withCellPath(item, (notePath) =>
- void openSpaceUsageContextMenu(event, notePath, onShowDetails, onContentChanged))}
+ onItemContextMenu={(item, event) => {
+ // The deleted bucket names no note, so it answers with what can be done to the
+ // crowd it stands for rather than with the note menu.
+ if (item.data?.bucket === "deleted") {
+ void openDeletedNotesContextMenu(event, onContentChanged);
+ return;
+ }
+
+ withCellPath(item, (notePath) =>
+ void openSpaceUsageContextMenu(event, notePath, onShowDetails, onContentChanged));
+ }}
/>
</div>
);
diff --git a/apps/client/src/widgets/type_widgets/options/content_manager/space_usage/overview_model.spec.ts b/apps/client/src/widgets/type_widgets/options/content_manager/space_usage/overview_model.spec.ts
index 5fb9a8da83..90c30ea72c 100644
--- a/apps/client/src/widgets/type_widgets/options/content_manager/space_usage/overview_model.spec.ts
+++ b/apps/client/src/widgets/type_widgets/options/content_manager/space_usage/overview_model.spec.ts
@@ -162,7 +162,8 @@ describe("buildOverviewModel", () => {
const other = childById(model, "/other-notes");
expect(other.value).toBe(11);
expect(other.hue).toBeUndefined();
- expect(other.data).toEqual({});
+ // Named rather than empty: a bucket's own actions key off which crowd it stands for.
+ expect(other.data).toEqual({ bucket: "other" });
expect(other.tooltip).toBe("Other notes");
// No `data-href`: a crowd of notes has no note preview to show.
expect(other.attributes).toBeUndefined();
@@ -181,6 +182,7 @@ describe("buildOverviewModel", () => {
expect(deleted.className).toBe("treemap-cell-deleted");
expect(deleted.tooltip).toBe("Deleted notes");
expect(deleted.icon).toBe("bx bx-trash-alt");
+ expect(deleted.data).toEqual({ bucket: "deleted" });
});
it("sizes the revisions bucket from the deduplicated content tier, not the per-note figures", () => {
@@ -200,7 +202,7 @@ describe("buildOverviewModel", () => {
expect(revisions.icon).toBe("bx bx-history");
expect(revisions.tooltip).toBe("Revisions");
// Inert like the other buckets: history is not a note to preview or open.
- expect(revisions.data).toEqual({});
+ expect(revisions.data).toEqual({ bucket: "revisions" });
expect(revisions.attributes).toBeUndefined();
});
diff --git a/apps/client/src/widgets/type_widgets/options/content_manager/space_usage/overview_model.ts b/apps/client/src/widgets/type_widgets/options/content_manager/space_usage/overview_model.ts
index 7ba3a30f2c..fbed2b50ac 100644
--- a/apps/client/src/widgets/type_widgets/options/content_manager/space_usage/overview_model.ts
+++ b/apps/client/src/widgets/type_widgets/options/content_manager/space_usage/overview_model.ts
@@ -9,6 +9,11 @@ export interface OverviewCell {
noteId?: string;
/** Note IDs from the root (inclusive) down to the note, as the cell's actions need it. */
notePath?: string[];
+ /**
+ * Which crowd a bucket cell stands for. Named here rather than read back from the cell's class,
+ * so a bucket's own actions — erasing, for the deleted one — key off what it is.
+ */
+ bucket?: "other" | "hidden" | "revisions" | "deleted";
}
interface OverviewModelOptions {
@@ -131,7 +136,7 @@ export function buildOverviewModel(
className: "treemap-cell-other",
icon: "bx bx-dots-horizontal-rounded",
tooltip: otherNotesLabel,
- data: {}
+ data: { bucket: "other" }
},
{
id: "/hidden-notes",
@@ -139,7 +144,7 @@ export function buildOverviewModel(
className: "treemap-cell-hidden",
icon: "bx bx-hide",
tooltip: hiddenNotesLabel,
- data: {}
+ data: { bucket: "hidden" }
},
{
// The deduplicated tier from the content totals, never a sum of the per-note figures:
@@ -150,7 +155,7 @@ export function buildOverviewModel(
className: "treemap-cell-revisions",
icon: "bx bx-history",
tooltip: revisionsLabel,
- data: {}
+ data: { bucket: "revisions" }
},
{
id: "/deleted-notes",
@@ -158,7 +163,7 @@ export function buildOverviewModel(
className: "treemap-cell-deleted",
icon: "bx bx-trash-alt",
tooltip: deletedNotesLabel,
- data: {}
+ data: { bucket: "deleted" }
}
);