Build your personal knowledge base with Trilium Notes
TypeScript electronelectron-appknowledge-baseknowledge-graphknowledge-managementknowledge-management-graphlocal-firstnote-managmentnote-takernote-takingnotebooknotesnotes-apppersonal-knowledge-basepersonal-wikiscriptableself-hostedself-hostingwiki
client/options/backup: add a "Restore backup" button
Adorian Doran6e2f2c1ce964
diff --git a/apps/client/src/translations/en/translation.json b/apps/client/src/translations/en/translation.jsonindex d26e70abe5..04fa98190e 100644--- a/apps/client/src/translations/en/translation.json+++ b/apps/client/src/translations/en/translation.json@@ -2071,6 +2071,7 @@"default_location": "Default location","backup_recommendation": "It's recommended to keep the backup turned on, but this can make application startup slow with large databases and/or slow storage devices.","backup_now": "Backup now",+ "restore_backup": "Restore a backup","backups_summary_one": "{{count}} backup, the most recent from {{age}} ago.","backups_summary_other": "{{count}} backups, the most recent from {{age}} ago.","existing_backups": "Existing backups",diff --git a/apps/client/src/widgets/type_widgets/options/backup.status.spec.tsx b/apps/client/src/widgets/type_widgets/options/backup.status.spec.tsxnew file mode 100644index 0000000000..89824e07bb--- /dev/null+++ b/apps/client/src/widgets/type_widgets/options/backup.status.spec.tsx@@ -0,0 +1,68 @@+import { render, VNode } from "preact";+import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";++const mocks = vi.hoisted(() => ({+ bootToSetup: vi.fn(),+ canBootToSetup: vi.fn(() => true)+}));++vi.mock("../../../services/setup_mode", () => ({+ bootToSetup: mocks.bootToSetup,+ canBootToSetup: () => mocks.canBootToSetup()+}));++// i18next is never initialized in the client tests, so its `t` returns undefined; echo the key instead.+vi.mock("../../../services/i18n", async (importOriginal) => ({+ ...(await importOriginal<typeof import("../../../services/i18n")>()),+ t: (key: string) => key+}));++// Import AFTER the mocks (vi.mock is hoisted, but the component import must resolve the mocked deps).+import { BackupStatus } from "./backup";++let container: HTMLDivElement | undefined;++function renderInto(vnode: VNode) {+ container = document.createElement("div");+ document.body.appendChild(container);+ render(vnode, container);++ return container;+}++function restoreButton(): HTMLButtonElement | null {+ return container?.querySelector<HTMLButtonElement>("button[name='restore-backup-button']") ?? null;+}++beforeEach(() => {+ mocks.bootToSetup.mockReset().mockResolvedValue(undefined);+ mocks.canBootToSetup.mockReset().mockReturnValue(true);+});++afterEach(() => {+ if (container) {+ render(null, container);+ container.remove();+ container = undefined;+ }+});++describe("restoring a backup from the options", () => {+ it("sends the app into the setup screen, at the restore step", () => {+ renderInto(<BackupStatus backups={[]} refreshCallback={vi.fn()} />);++ restoreButton()?.click();++ expect(mocks.bootToSetup).toHaveBeenCalledWith({ targetScreen: "restore-backup" });+ });++ it("is not offered where the app cannot start itself again", () => {+ // A browser talking to a server: the reload would leave the server, and its database, up.+ mocks.canBootToSetup.mockReturnValue(false);+ renderInto(<BackupStatus backups={[]} refreshCallback={vi.fn()} />);++ expect(restoreButton()).toBeNull();+ // The action that has always been there is untouched by any of this.+ expect(container?.querySelector("button[name='backup-database-now-button']")).not.toBeNull();+ });+});diff --git a/apps/client/src/widgets/type_widgets/options/backup.tsx b/apps/client/src/widgets/type_widgets/options/backup.tsxindex 9241a05a80..a4382f3786 100644--- a/apps/client/src/widgets/type_widgets/options/backup.tsx+++ b/apps/client/src/widgets/type_widgets/options/backup.tsx@@ -14,6 +14,7 @@ import dialogService from "../../../services/dialog";import { t } from "../../../services/i18n";import options from "../../../services/options";import server from "../../../services/server";+import { bootToSetup, canBootToSetup } from "../../../services/setup_mode";import toast from "../../../services/toast";import { isElectron } from "../../../services/utils";import Button from "../../react/Button";@@ -66,13 +67,24 @@ interface BackupStatusProps {* of the page's own header rather than of the list below it: the list card answers for what it* holds, which is not the same as what the page is for.*/-function BackupStatus({ backups, refreshCallback }: BackupStatusProps) {+export function BackupStatus({ backups, refreshCallback }: BackupStatusProps) {const [backupInProgress, setBackupInProgress] = useState(false);return (<div className="backup-status"><span className="backup-status-summary">{summarizeBackups(backups)}</span>+ {/* Offered only where the app can start itself again, which is what a restore needs:+ it happens in the setup screen, with this database closed. */}+ {canBootToSetup() && (+ <Button+ name="restore-backup-button"+ text={t("backup.restore_backup")}+ size="micro"+ onClick={() => bootToSetup({ targetScreen: "restore-backup" })}+ />+ )}+<Buttonname="backup-database-now-button"text={t("backup.backup_now")}