Skip to content

Build your personal knowledge base with Trilium Notes

client/cleanup tool: show an in-progress toast during the cleanup operation

Adorian Doran83a7566267b2
diff --git a/apps/client/src/translations/en/translation.json b/apps/client/src/translations/en/translation.json
index 400021ae19..cd24af26e9 100644
--- a/apps/client/src/translations/en/translation.json
+++ b/apps/client/src/translations/en/translation.json
@@ -2892,7 +2892,9 @@
"cleanup_clean": "Clean",
"cleanup_confirm": "This permanently deletes the selected data. It cannot be undone.",
"cleanup_done": "Cleanup completed, reclaiming {{size}}.",
- "cleanup_compact_database": "Compact database"
+ "cleanup_compact_database": "Compact database",
+ "cleanup_running": "Cleaning up…",
+ "cleanup_compacting": "Compacting the database. This may take several minutes."
},
"note_language": {
"not_set": "No language set",
diff --git a/apps/client/src/widgets/type_widgets/options/content_manager/space_usage/cleanup_dialog.spec.tsx b/apps/client/src/widgets/type_widgets/options/content_manager/space_usage/cleanup_dialog.spec.tsx
index c8e5b22458..143075c605 100644
--- a/apps/client/src/widgets/type_widgets/options/content_manager/space_usage/cleanup_dialog.spec.tsx
+++ b/apps/client/src/widgets/type_widgets/options/content_manager/space_usage/cleanup_dialog.spec.tsx
@@ -8,7 +8,8 @@ import { formatSize } from "../../../../../services/utils";
// Hoisted with the mocks that read them: a `vi.mock` factory runs before any plain module constant.
const {
- REVISIONS_ALL, REVISIONS_TRIMMED, DELETED, UNUSED, FREE_PAGES, MEASURED_BEFORE, MEASURED_AFTER
+ REVISIONS_ALL, REVISIONS_TRIMMED, DELETED, UNUSED, FREE_PAGES,
+ VACUUM_BEFORE, VACUUM_AFTER, MEASURED_BEFORE, MEASURED_AFTER
} = vi.hoisted(() => ({
REVISIONS_ALL: 900,
REVISIONS_TRIMMED: 300,
@@ -16,6 +17,9 @@ const {
UNUSED: 20,
/** Pages already free in the file, which only a rebuild returns. */
FREE_PAGES: 640,
+ /** What the rebuild reports for itself, which is the figure a compacting run goes by. */
+ VACUUM_BEFORE: 8000,
+ VACUUM_AFTER: 3000,
// Deliberately unlike any estimate above, so a toast quoting a prediction cannot pass for one
// quoting the measurement.
MEASURED_BEFORE: 7777,
@@ -29,9 +33,13 @@ const mocks = vi.hoisted(() => ({
weighed: [] as number[],
save: vi.fn(async () => {}),
get: vi.fn(),
- post: vi.fn(async (_url: string, _body?: object) => {}),
+ post: vi.fn(async (url: string, _body?: object) => (
+ url === "database/vacuum-database" ? { sizeBefore: VACUUM_BEFORE, sizeAfter: VACUUM_AFTER } : undefined
+ )),
confirm: vi.fn(async () => true),
- showMessage: vi.fn()
+ showMessage: vi.fn(),
+ showPersistent: vi.fn(),
+ closePersistent: vi.fn()
}));
vi.mock("../../../../../services/options", () => ({
@@ -85,7 +93,13 @@ vi.mock("../../../../../services/server", () => {
});
vi.mock("../../../../../services/dialog", () => ({ default: { confirm: mocks.confirm } }));
-vi.mock("../../../../../services/toast", () => ({ default: { showMessage: mocks.showMessage } }));
+vi.mock("../../../../../services/toast", () => ({
+ default: {
+ showMessage: mocks.showMessage,
+ showPersistent: mocks.showPersistent,
+ closePersistent: mocks.closePersistent
+ }
+}));
// The real modal reports its close once the animation is done; the stub reports it as soon as it is
// told to hide, which is the signal the dialog's own flow keys off.
@@ -106,7 +120,7 @@ vi.mock("../../../../react/Modal", () => ({
}
}));
-import { showCleanupDialog } from "./cleanup_dialog";
+import { CLEANUP_TOAST_ID, showCleanupDialog } from "./cleanup_dialog";
function rows() {
return Array.from(document.body.querySelectorAll<HTMLElement>(".cleanup-item:not(.cleanup-item-nested)"));
@@ -259,6 +273,31 @@ describe("showCleanupDialog", () => {
expect(cleanButton()?.disabled).toBe(false);
});
+ it("keeps a spinner up for the run, naming the rebuild while it holds the server", async () => {
+ mocks.storedOption = JSON.stringify({ unusedAttachments: true, compactDatabase: true });
+ const { closed } = await openDialog();
+
+ await click(cleanButton());
+ // The rebuild's own before/after is what a compacting run reports, not the content readings.
+ await expect(closed).resolves.toBe(VACUUM_BEFORE - VACUUM_AFTER);
+
+ const raised = mocks.showPersistent.mock.calls.map(([ options ]) => options);
+ // One toast, its message swapped as the run moves on — the id is what keeps it the same one.
+ expect(raised.map((options) => options.id)).toEqual([ CLEANUP_TOAST_ID, CLEANUP_TOAST_ID ]);
+ // Compared against the same t() calls; unlike DOM text these are the raw values, so no
+ // empty-string coercion to allow for.
+ expect(raised.map((options) => options.message)).toEqual([
+ t("space_usage.cleanup_running"),
+ t("space_usage.cleanup_compacting")
+ ]);
+ // A spinner rather than a bar: none of this can say how far along it is. And no close
+ // button, there being nothing here to cancel.
+ expect(raised[0].icon).toBe("bx bx-loader-circle bx-spin");
+ expect(raised[0].dismissible).toBe(false);
+
+ expect(mocks.closePersistent).toHaveBeenCalledWith(CLEANUP_TOAST_ID);
+ });
+
it("erases nothing when the confirmation is declined, and stays open to be reconsidered", async () => {
mocks.storedOption = JSON.stringify({ unusedAttachments: true });
await openDialog();
diff --git a/apps/client/src/widgets/type_widgets/options/content_manager/space_usage/cleanup_dialog.tsx b/apps/client/src/widgets/type_widgets/options/content_manager/space_usage/cleanup_dialog.tsx
index c84862fb47..3587e5df40 100644
--- a/apps/client/src/widgets/type_widgets/options/content_manager/space_usage/cleanup_dialog.tsx
+++ b/apps/client/src/widgets/type_widgets/options/content_manager/space_usage/cleanup_dialog.tsx
@@ -18,6 +18,7 @@ import { useTriliumOptionJson } from "../../../../react/hooks";
import Modal from "../../../../react/Modal";
import {
CLEANUP_ITEMS,
+ type CleanupPhase,
type CleanupToolOptions,
computeCleanupSizes,
hasWorkToDo,
@@ -149,9 +150,10 @@ function CleanupDialog({ onFinished }: { onFinished: (reclaimed: number | null)
let reclaimed = work.reclaimed;
try {
- reclaimed = await runCleanup(work.options);
+ reclaimed = await runCleanup(work.options, showCleanupProgress);
toast.showMessage(t("space_usage.cleanup_done", { size: formatSize(reclaimed) }));
} finally {
+ toast.closePersistent(CLEANUP_TOAST_ID);
// Settled either way: a run that failed part-way still changed the database, so whatever
// was watching has to re-measure rather than keep the figures from before it. The
// failure itself is already reported by the request layer.
@@ -254,6 +256,28 @@ function CleanupDialog({ onFinished }: { onFinished: (reclaimed: number | null)
/** Both figures come from the root, whose subtree is the whole visible database. */
const ROOT_USAGE_URL = "space-usage/note/root";
+/** Shared by every phase of a run, so the message is swapped in place rather than stacked. */
+export const CLEANUP_TOAST_ID = "content-manager-cleanup";
+
+/**
+ * The app's in-progress toast, the same one printing raises: a spinning icon rather than a bar,
+ * since none of this can say how far along it is. Compacting names itself, being the step that can
+ * hold the server for minutes with nothing at all to show meanwhile.
+ *
+ * Raised again per phase rather than replaced — the toast service updates the one already carrying
+ * this id, so the message changes in place instead of stacking a second toast beside the first.
+ */
+function showCleanupProgress(phase: CleanupPhase) {
+ toast.showPersistent({
+ id: CLEANUP_TOAST_ID,
+ icon: "bx bx-loader-circle bx-spin",
+ message: t(phase === "compacting" ? "space_usage.cleanup_compacting" : "space_usage.cleanup_running"),
+ // Nothing here to cancel: the server carries on whatever the client does, so a close button
+ // would read as a stop that stops nothing.
+ dismissible: false
+ });
+}
+
/**
* The width the dialog starts at, narrow on purpose: a short list of choices, read top to bottom
* rather than scanned across. A floor rather than a size — item titles never wrap, so a long one
diff --git a/apps/client/src/widgets/type_widgets/options/content_manager/space_usage/cleanup_operation.spec.ts b/apps/client/src/widgets/type_widgets/options/content_manager/space_usage/cleanup_operation.spec.ts
index b3fc0391d1..6f194a0b73 100644
--- a/apps/client/src/widgets/type_widgets/options/content_manager/space_usage/cleanup_operation.spec.ts
+++ b/apps/client/src/widgets/type_widgets/options/content_manager/space_usage/cleanup_operation.spec.ts
@@ -206,6 +206,29 @@ describe("runCleanup", () => {
.toEqual([ "database/vacuum-database", "space-usage/cleanup-completed" ]);
});
+ it("reports the phase it is in, naming the rebuild separately from the erasures", async () => {
+ const phases = vi.fn();
+ await runCleanup({ ...ALL_PICKED, compactDatabase: true }, phases);
+ expect(phases.mock.calls.flat()).toEqual([ "erasing", "compacting" ]);
+
+ // Only what actually happens is announced: a run that erases nothing never claims to.
+ phases.mockClear();
+ mocks.occupied = [ 5000, 5000 ];
+ await runCleanup({
+ ...ALL_PICKED,
+ deletedEntities: false,
+ unusedAttachments: false,
+ revisionSnapshots: false,
+ compactDatabase: true
+ }, phases);
+ expect(phases.mock.calls.flat()).toEqual([ "compacting" ]);
+
+ phases.mockClear();
+ mocks.occupied = [ 5000, 4000 ];
+ await runCleanup(ALL_PICKED, phases);
+ expect(phases.mock.calls.flat()).toEqual([ "erasing" ]);
+ });
+
it("asks for nothing that was not picked", async () => {
await runCleanup({ ...ALL_PICKED, revisionSnapshots: false, deletedEntities: false });
expect(mocks.post.mock.calls.map(([ url ]) => url))
diff --git a/apps/client/src/widgets/type_widgets/options/content_manager/space_usage/cleanup_operation.ts b/apps/client/src/widgets/type_widgets/options/content_manager/space_usage/cleanup_operation.ts
index 73e8333e56..a5bfe875c8 100644
--- a/apps/client/src/widgets/type_widgets/options/content_manager/space_usage/cleanup_operation.ts
+++ b/apps/client/src/widgets/type_widgets/options/content_manager/space_usage/cleanup_operation.ts
@@ -17,6 +17,13 @@ export const CLEANUP_ITEMS = [
export type CleanupItemId = (typeof CLEANUP_ITEMS)[number]["id"];
+/**
+ * What a run is doing, reported as it moves between the two. Erasing is over in seconds on an
+ * ordinary database; compacting is the one that can hold the server for minutes, so it says so
+ * rather than leaving the same message up throughout.
+ */
+export type CleanupPhase = "erasing" | "compacting";
+
/**
* What the cleanup tool is set to erase, persisted as JSON in the `cleanupToolOptions` option so the
* next run opens on the last answer rather than on a fresh set of defaults.
@@ -150,7 +157,16 @@ export function computeCleanupSizes(
* @returns bytes reclaimed, never negative: a note saved by another client mid-run must not read as
* the cleanup having given space back.
*/
-export async function runCleanup(options: CleanupToolOptions): Promise<number> {
+export async function runCleanup(
+ options: CleanupToolOptions,
+ onPhase: (phase: CleanupPhase) => void = () => {}
+): Promise<number> {
+ const erasing = options.revisionSnapshots || options.unusedAttachments || options.deletedEntities;
+
+ if (erasing) {
+ onPhase("erasing");
+ }
+
// Only weighed when the file is not being rebuilt afterwards; a rebuild reports its own figure,
// which is the better one and makes these two readings of the whole database wasted work.
const before = options.compactDatabase ? 0 : await measureOccupiedBytes();
@@ -170,6 +186,10 @@ export async function runCleanup(options: CleanupToolOptions): Promise<number> {
await server.postWithTimeout("notes/erase-deleted-notes-now", CLEANUP_TIMEOUT_MS);
}
+ if (options.compactDatabase) {
+ onPhase("compacting");
+ }
+
const reclaimed = options.compactDatabase
// Last, with everything already erased: the rebuild closes around whatever is left, and its
// own before/after is the truest figure there is — the file itself, rather than an account