Skip to content

A fully open source & end-to-end encrypted note taking alternative to Evernote.

feat: show toast on backup/restore

thecodrrbfcc13755178
diff --git a/apps/web/src/components/dialogs/loading-dialog.js b/apps/web/src/components/dialogs/loading-dialog.js
index aceb52e94..ffb01684c 100644
--- a/apps/web/src/components/dialogs/loading-dialog.js
+++ b/apps/web/src/components/dialogs/loading-dialog.js
@@ -6,8 +6,11 @@ import * as Icon from "../icons";
function LoadingDialog(props) {
useEffect(() => {
(async function () {
- await props.action();
- props.onDone();
+ try {
+ props.onDone(await props.action());
+ } catch (e) {
+ props.onDone(e);
+ }
})();
}, [props]);
diff --git a/apps/web/src/utils/toast.js b/apps/web/src/utils/toast.js
index cdafe66c4..52b571a9b 100644
--- a/apps/web/src/utils/toast.js
+++ b/apps/web/src/utils/toast.js
@@ -17,7 +17,7 @@ function showToast(type, message, actions) {
<ToastContainer type={type} message={message} actions={actions} />,
{
position: isMobile() ? "bottom-center" : "top-right",
- hideAfter: actions ? 5 : 3,
+ hideAfter: actions ? 5 : type === "error" ? 5 : 3,
bar: { size: "0px" },
renderIcon: () => {
return (
@@ -34,7 +34,12 @@ function ToastContainer(props) {
const { type, message, actions } = props;
return (
<ThemeProvider>
- <Flex data-test-id="toast" justifyContent="center" alignContent="center">
+ <Flex
+ data-test-id="toast"
+ justifyContent="center"
+ alignContent="center"
+ my={1}
+ >
<Text
data-test-id="toast-message"
variant="body"
diff --git a/apps/web/src/views/settings.js b/apps/web/src/views/settings.js
index c44fa7c5c..571f2ddab 100644
--- a/apps/web/src/views/settings.js
+++ b/apps/web/src/views/settings.js
@@ -18,6 +18,7 @@ import { showBuyDialog } from "../components/dialogs/buy-dialog";
import Vault from "../common/vault";
import ScrollContainer from "../components/scroll-container";
import { showLoadingDialog } from "../components/dialogs/loadingdialog";
+import { showToast } from "../utils/toast";
function importBackup() {
return new Promise((resolve, reject) => {
@@ -26,9 +27,6 @@ function importBackup() {
importFileElem.onchange = function () {
const file = importFileElem.files[0];
if (!file.name.endsWith(".nnbackup")) {
- alert(
- "Invalid backup file provided. Make sure it has an .nnbackup extension."
- );
return reject(
"The given file does not have .nnbackup extension. Only files with .nnbackup extension are supported."
);
@@ -250,11 +248,24 @@ function Settings(props) {
<Button
variant="list"
onClick={async () => {
+ const data = await showLoadingDialog({
+ title: "Creating backup",
+ subtitle: "We are creating a backup of your data. Please wait...",
+ action: async () => {
+ return await db.backup.export("web", encryptBackups);
+ },
+ message: (
+ <Text color="error">
+ Please do NOT close your browser or shut down your PC.
+ </Text>
+ ),
+ });
download(
`notesnook-backup-${new Date().toLocaleString("en")}`,
- await db.backup.export("web", encryptBackups),
+ data,
"nnbackup"
);
+ await showToast("success", "Backup created!");
}}
>
<TextWithTip
@@ -271,19 +282,31 @@ function Settings(props) {
<Button
variant="list"
onClick={async () => {
- const backupData = JSON.stringify(await importBackup());
- showLoadingDialog({
- title: "Restoring backup",
- subtitle: "We are restoring your backup. Please wait...",
- action: async () => {
- await db.backup.import(backupData);
- },
- message: (
- <Text color="error">
- Please do NOT close your browser or shut down your PC.
- </Text>
- ),
- });
+ try {
+ const backupData = JSON.stringify(await importBackup());
+ const error = await showLoadingDialog({
+ title: "Restoring backup",
+ subtitle: "We are restoring your backup. Please wait...",
+ action: async () => {
+ await db.backup.import(backupData);
+ },
+ message: (
+ <Text color="error">
+ Please do NOT close your browser or shut down your PC.
+ </Text>
+ ),
+ });
+ if (!error) {
+ await showToast("success", "Backup restored!");
+ } else {
+ throw new Error(error);
+ }
+ } catch (e) {
+ await showToast(
+ "error",
+ `Could not restore the backup. Error message: ${e.message || e}`
+ );
+ }
}}
>
<TextWithTip