Skip to content

Build your personal knowledge base with Trilium Notes

client/cleanup tool: add a notice about slow compacting operation

Adorian Doran554a06e77319
diff --git a/apps/client/src/services/dialog.ts b/apps/client/src/services/dialog.ts
index 7f85b74027..06c8d527db 100644
--- a/apps/client/src/services/dialog.ts
+++ b/apps/client/src/services/dialog.ts
@@ -102,10 +102,12 @@ async function info(message: MessageType, extraProps?: InfoExtraProps) {
/**
* Displays a confirmation dialog with the given message.
*
- * @param message the message to display in the dialog.
+ * @param message the message to display in the dialog. A string is rendered as HTML; pass an element
+ * where the wording needs structure the dialog should not have to parse — an
+ * admonition warning about what the action costs, say.
* @returns A promise that resolves to true if the user confirmed, false otherwise.
*/
-async function confirm(message: string) {
+async function confirm(message: MessageType) {
return new Promise<boolean>((res) =>
appContext.triggerCommand("showConfirmDialog", <ConfirmWithMessageOptions>{
message,
diff --git a/apps/client/src/translations/en/translation.json b/apps/client/src/translations/en/translation.json
index cd24af26e9..d05bb187f4 100644
--- a/apps/client/src/translations/en/translation.json
+++ b/apps/client/src/translations/en/translation.json
@@ -2894,7 +2894,10 @@
"cleanup_done": "Cleanup completed, reclaiming {{size}}.",
"cleanup_compact_database": "Compact database",
"cleanup_running": "Cleaning up…",
- "cleanup_compacting": "Compacting the database. This may take several minutes."
+ "cleanup_compacting": "Compacting the database. This may take several minutes.",
+ "cleanup_confirm_interrupted": "If something interrupts it (such as a crash, a restart or a power cut) nothing beyond the selected items is lost.",
+ "cleanup_confirm_compacting_title": "Compacting database is a slow operation",
+ "cleanup_confirm_compacting": "Rebuilding the database can take several minutes, depending on its size. Trilium will not respond until it finishes."
},
"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 143075c605..796d652737 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
@@ -1,4 +1,4 @@
-import type { ComponentChildren } from "preact";
+import { type ComponentChildren, render, type VNode } from "preact";
import { useEffect } from "preact/hooks";
import { act } from "preact/test-utils";
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
@@ -36,7 +36,8 @@ const mocks = vi.hoisted(() => ({
post: vi.fn(async (url: string, _body?: object) => (
url === "database/vacuum-database" ? { sizeBefore: VACUUM_BEFORE, sizeAfter: VACUUM_AFTER } : undefined
)),
- confirm: vi.fn(async () => true),
+ /** Takes the confirmation element, which one test renders to read what it says. */
+ confirm: vi.fn(async (_message?: unknown) => true),
showMessage: vi.fn(),
showPersistent: vi.fn(),
closePersistent: vi.fn()
@@ -298,6 +299,32 @@ describe("showCleanupDialog", () => {
expect(mocks.closePersistent).toHaveBeenCalledWith(CLEANUP_TOAST_ID);
});
+ it("warns what compacting costs, only when it is being asked for", async () => {
+ /** The confirmation is handed over as an element; rendering it is how its content is read. */
+ const confirmationHtml = async () => {
+ const [ message ] = mocks.confirm.mock.calls.at(-1) ?? [];
+ const host = document.createElement("div");
+ render(message as VNode, host);
+ await settle();
+ return host.innerHTML;
+ };
+
+ mocks.storedOption = JSON.stringify({ unusedAttachments: true });
+ await openDialog();
+ await click(cleanButton());
+
+ // An erasure-only run is over in seconds; there is nothing to warn about waiting for.
+ expect(await confirmationHtml()).not.toContain("admonition");
+
+ mocks.storedOption = JSON.stringify({ unusedAttachments: true, compactDatabase: true });
+ await openDialog();
+ await click(cleanButton());
+
+ const warned = await confirmationHtml();
+ expect(warned).toContain("admonition");
+ expect(warned).toContain("warning");
+ });
+
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 3587e5df40..18157e5a35 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
@@ -9,6 +9,7 @@ import dialogService from "../../../../../services/dialog";
import { t } from "../../../../../services/i18n";
import toast from "../../../../../services/toast";
import { formatSize, isStandalone } from "../../../../../services/utils";
+import { ExtendedAdmonition } from "../../../../react/Admonition";
import Button from "../../../../react/Button";
import { Card, CardSection } from "../../../../react/Card";
import DonutChart, { type DonutRing } from "../../../../react/charts/DonutChart";
@@ -130,7 +131,7 @@ function CleanupDialog({ onFinished }: { onFinished: (reclaimed: number | null)
const pending = useRef<{ options: CleanupToolOptions, reclaimed: number } | null>(null);
async function confirmAndClose() {
- if (!await dialogService.confirm(t("space_usage.cleanup_confirm"))) {
+ if (!await dialogService.confirm(<CleanupConfirmation compacting={options.compactDatabase} />)) {
return;
}
@@ -253,6 +254,34 @@ function CleanupDialog({ onFinished }: { onFinished: (reclaimed: number | null)
);
}
+/**
+ * What the user is agreeing to. The first line is the whole of it for an ordinary run; the rest
+ * qualifies rather than repeats — what compacting costs, when it is being asked for, and the one
+ * reassurance worth giving about an operation whose first sentence is that it deletes.
+ */
+function CleanupConfirmation({ compacting }: { compacting: boolean }) {
+ return (
+ <>
+ <p>{t("space_usage.cleanup_confirm")}</p>
+
+ {compacting && (
+ <ExtendedAdmonition
+ type="warning"
+ icon="bx bx-time-five"
+ title={t("space_usage.cleanup_confirm_compacting_title")}
+ >
+ {t("space_usage.cleanup_confirm_compacting")}
+ </ExtendedAdmonition>
+ )}
+
+ {/* After the warning, answering what it raises: what becomes of a database the machine
+ dies in the middle of rebuilding. Worded as something that happens *to* the run
+ rather than something to do about it — there is no way to call it off from here. */}
+ <p>{t("space_usage.cleanup_confirm_interrupted")}</p>
+ </>
+ );
+}
+
/** Both figures come from the root, whose subtree is the whole visible database. */
const ROOT_USAGE_URL = "space-usage/note/root";