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 prompt before starting the backup restoration process
Adorian Doranc212d5ab1125
diff --git a/apps/client/src/translations/en/translation.json b/apps/client/src/translations/en/translation.jsonindex 08e185a350..433dcd8b18 100644--- a/apps/client/src/translations/en/translation.json+++ b/apps/client/src/translations/en/translation.json@@ -2082,6 +2082,7 @@"create_and_download": "Download backup","upload_and_restore": "Restore backup","restart_for_backup": "Trilium needs to restart to back up your notes. You can go back to them as soon as it's done.\n\nRestart now?",+ "restart_for_restore": "Trilium needs to restart before the restoration process. Once the restoration process is started, you will be prompted to back up your existing data before it's overwritten, so nothing is lost.\n\nRestart now?","no_backup_yet": "no backup yet","download": "Download","options_title": "Backup options",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.tsxindex 312d1dd31c..3bd79e3276 100644--- a/apps/client/src/widgets/type_widgets/options/backup.status.spec.tsx+++ b/apps/client/src/widgets/type_widgets/options/backup.status.spec.tsx@@ -51,15 +51,32 @@ afterEach(() => {}});+/** Lets a click's own promise chain settle, since confirming is asynchronous. */+const settle = () => new Promise((resolve) => setTimeout(resolve, 0));+describe("restoring a backup from the options", () => {- it("sends the app into the setup screen, at the restore step", () => {+ it("asks before restarting, then sends the app into the setup screen's restore step", async () => {renderInto(<BackupStatus backups={[]} refreshCallback={vi.fn()} />);restoreButton()?.click();+ await settle();+ // The same question on every platform that can restart itself, since the restart, and+ // what it leads to, are the same thing everywhere.+ expect(mocks.confirm).toHaveBeenCalledWith("backup.restart_for_restore");expect(mocks.bootToSetup).toHaveBeenCalledWith({ targetScreen: "restore-backup" });});+ it("stays put when the restart is declined", async () => {+ mocks.confirm.mockResolvedValue(false);+ renderInto(<BackupStatus backups={[]} refreshCallback={vi.fn()} />);++ restoreButton()?.click();+ await settle();++ expect(mocks.bootToSetup).not.toHaveBeenCalled();+ });+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);@@ -76,9 +93,6 @@ describe("backing up where a backup is a download (standalone)", () => {return container?.querySelector<HTMLButtonElement>("button[name='backup-database-now-button']") ?? null;}- /** Lets the click's own promise chain settle, since confirming is asynchronous. */- const settle = () => new Promise((resolve) => setTimeout(resolve, 0));-it("asks before restarting, then goes to the setup screen's backup step", async () => {renderInto(<StandaloneBackupSection />);@@ -100,6 +114,16 @@ describe("backing up where a backup is a download (standalone)", () => {expect(mocks.bootToSetup).not.toHaveBeenCalled();});+ it("asks before restarting for a restore, and goes to that step", async () => {+ renderInto(<StandaloneBackupSection />);++ restoreButton()?.click();+ await settle();++ expect(mocks.confirm).toHaveBeenCalledWith("backup.restart_for_restore");+ expect(mocks.bootToSetup).toHaveBeenCalledWith({ targetScreen: "restore-backup" });+ });+it("offers neither action where the app cannot start itself again", () => {// Both of them restart the application, so neither can be honoured without that.mocks.canBootToSetup.mockReturnValue(false);diff --git a/apps/client/src/widgets/type_widgets/options/backup.tsx b/apps/client/src/widgets/type_widgets/options/backup.tsxindex e4947b78eb..75482ac980 100644--- a/apps/client/src/widgets/type_widgets/options/backup.tsx+++ b/apps/client/src/widgets/type_widgets/options/backup.tsx@@ -89,22 +89,6 @@ function StandaloneBackupSettings() {* which is where the database can be held still long enough to be copied or replaced.*/export function StandaloneBackupSection() {- /**- * Restarts into the setup screen, where the backup is actually taken.- *- * A backup is streamed off the live database over minutes, and a page of it read after a write- * would not match the pages read before: the copy has to come from a database nothing is- * touching. Only setup mode gives that — the database is open, but becca, sync and migrations- * are all held back — so the backup is taken there and the instance comes straight back here.- */- async function backUp() {- if (!await dialogService.confirm(t("backup.restart_for_backup"))) {- return;- }-- await bootToSetup({ targetScreen: "backup-database" });- }-return (<div className="options-section standalone-backup"><SetupForm icon="bx bx-data">@@ -117,13 +101,13 @@ export function StandaloneBackupSection() {name="backup-database-now-button"text={t("backup.create_and_download")}kind="primary"- onClick={() => void backUp()}+ onClick={() => void backUpInSetup()}/><Buttonname="restore-backup-button"text={t("backup.upload_and_restore")}- onClick={() => bootToSetup({ targetScreen: "restore-backup" })}+ onClick={() => void restoreInSetup()}/></div>)}@@ -132,6 +116,37 @@ export function StandaloneBackupSection() {);}+/**+ * Restarts into the setup screen, where the backup is actually taken.+ *+ * A backup is streamed off the live database over minutes, and a page of it read after a write+ * would not match the pages read before: the copy has to come from a database nothing is touching.+ * Only setup mode gives that — the database is open, but becca, sync and migrations are all held+ * back — so the backup is taken there and the instance comes straight back here.+ */+async function backUpInSetup() {+ if (!await dialogService.confirm(t("backup.restart_for_backup"))) {+ return;+ }++ await bootToSetup({ targetScreen: "backup-database" });+}++/**+ * Restarts into the setup screen, which is where a backup can replace the database.+ *+ * Asked about on every platform that can restart itself, because the restart is the surprising part+ * and because what follows it reads as destructive until the user knows they will be offered a copy+ * of what is about to be replaced.+ */+async function restoreInSetup() {+ if (!await dialogService.confirm(t("backup.restart_for_restore"))) {+ return;+ }++ await bootToSetup({ targetScreen: "restore-backup" });+}+interface BackupStatusProps {backups: DatabaseBackup[];refreshCallback: () => void;@@ -156,7 +171,7 @@ export function BackupStatus({ backups, refreshCallback }: BackupStatusProps) {name="restore-backup-button"text={t("backup.restore_backup")}size="micro"- onClick={() => bootToSetup({ targetScreen: "restore-backup" })}+ onClick={() => void restoreInSetup()}/>)}