Skip to content

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

feat: add clear & delete vault buttons in settings

thecodrr8fdc5ccc3248
diff --git a/apps/web/src/common/dialogcontroller.js b/apps/web/src/common/dialogcontroller.js
index 606805664..8db5144c8 100644
--- a/apps/web/src/common/dialogcontroller.js
+++ b/apps/web/src/common/dialogcontroller.js
@@ -383,10 +383,26 @@ function getDialogData(type) {
switch (type) {
case "create_vault":
return {
- title: "Create Your Vault",
+ title: "Create your vault",
subtitle: "A vault stores your notes in a password-encrypted storage.",
positiveButtonText: "Create vault",
};
+ case "clear_vault":
+ return {
+ title: "Clear your vault",
+ subtitle:
+ "Enter vault password to unlock and remove all notes from the vault.",
+ positiveButtonText: "Clear vault",
+ };
+ case "delete_vault":
+ return {
+ title: "Delete your vault",
+ subtitle: "Enter your account password to delete your vault.",
+ positiveButtonText: "Delete vault",
+ checks: [
+ { key: "deleteAllLockedNotes", title: "Delete all locked notes?" },
+ ],
+ };
case "lock_note":
return {
title: "Lock note",
@@ -436,12 +452,13 @@ function getDialogData(type) {
}
export function showPasswordDialog(type, validate) {
- const { title, subtitle, positiveButtonText } = getDialogData(type);
+ const { title, subtitle, positiveButtonText, checks } = getDialogData(type);
return showDialog((Dialogs, perform) => (
<Dialogs.PasswordDialog
type={type}
title={title}
subtitle={subtitle}
+ checks={checks}
positiveButtonText={positiveButtonText}
validate={validate}
onClose={() => perform(false)}
diff --git a/apps/web/src/common/vault.js b/apps/web/src/common/vault.js
index caff44b13..ef9c613f9 100644
--- a/apps/web/src/common/vault.js
+++ b/apps/web/src/common/vault.js
@@ -12,6 +12,30 @@ class Vault {
});
}
+ static async clearVault() {
+ if (!(await db.vault.exists())) return false;
+ return await showPasswordDialog("clear_vault", async ({ password }) => {
+ try {
+ await db.vault.clear(password);
+ return true;
+ } catch {
+ return false;
+ }
+ });
+ }
+
+ static async deleteVault() {
+ if (!(await db.vault.exists())) return false;
+ return await showPasswordDialog(
+ "delete_vault",
+ async ({ password, deleteAllLockedNotes }) => {
+ if (!(await db.user.verifyPassword(password))) return false;
+ await db.vault.delete(!!deleteAllLockedNotes);
+ return true;
+ }
+ );
+ }
+
static unlockVault() {
return showPasswordDialog("lock_note", ({ password }) => {
return db.vault
diff --git a/apps/web/src/components/dialogs/dialog.js b/apps/web/src/components/dialogs/dialog.js
index 7951baff2..6d60e8b9e 100644
--- a/apps/web/src/components/dialogs/dialog.js
+++ b/apps/web/src/components/dialogs/dialog.js
@@ -92,7 +92,7 @@ function Dialog(props) {
{props.title}
</Text>
{props.description && (
- <Text variant="body" textAlign="left" color="gray" mt={1}>
+ <Text variant="body" textAlign="left" color="fontTertiary" mt={1}>
{props.description}
</Text>
)}
diff --git a/apps/web/src/components/dialogs/password-dialog.js b/apps/web/src/components/dialogs/password-dialog.js
index de54a87fd..12b38c08f 100644
--- a/apps/web/src/components/dialogs/password-dialog.js
+++ b/apps/web/src/components/dialogs/password-dialog.js
@@ -2,10 +2,11 @@ import React, { useState, useCallback, useMemo } from "react";
import { Box, Text } from "rebass";
import Dialog from "./dialog";
import Field from "../field";
+import { Checkbox, Label } from "@rebass/forms";
const requiredValues = ["password", "newPassword", "oldPassword"];
function PasswordDialog(props) {
- const { type } = props;
+ const { type, checks } = props;
const [error, setError] = useState();
const [isLoading, setIsLoading] = useState(false);
const isChangePasswordDialog = useMemo(() => {
@@ -53,14 +54,8 @@ function PasswordDialog(props) {
as="form"
onSubmit={async (e) => {
e.preventDefault();
- const data = requiredValues.reduce((prev, curr) => {
- if (!e.target[curr]) return prev;
- prev[curr] = e.target[curr].value;
- return prev;
- }, {});
-
+ const data = Object.fromEntries(new FormData(e.target).entries());
setError();
-
await submit(data);
}}
>
@@ -88,6 +83,14 @@ function PasswordDialog(props) {
/>
) : null}
+ {checks &&
+ checks.map((check) => (
+ <Label key={check.key} alignItems="center" fontSize="title" mt={2}>
+ <Checkbox id={check.key} name={check.key} />
+ {check.title}
+ </Label>
+ ))}
+
{error && (
<Text mt={1} variant={"error"}>
{error}
diff --git a/apps/web/src/views/settings.js b/apps/web/src/views/settings.js
index 056dcdb65..5dcab9bd7 100644
--- a/apps/web/src/views/settings.js
+++ b/apps/web/src/views/settings.js
@@ -2,6 +2,7 @@ import React, { useEffect, useMemo } from "react";
import { Button, Flex, Text } from "rebass";
import * as Icon from "../components/icons";
import { useStore as useUserStore } from "../stores/user-store";
+import { useStore as useNoteStore } from "../stores/note-store";
import { useStore as useThemeStore } from "../stores/theme-store";
import { useStore as useSettingStore } from "../stores/setting-store";
import { useStore as useAppStore } from "../stores/app-store";
@@ -33,6 +34,7 @@ import Tip from "../components/tip";
import Toggle from "../components/toggle";
import openLink from "../commands/openLink";
import { isDesktop } from "../utils/platform";
+import Vault from "../common/vault";
function importBackup() {
return new Promise((resolve, reject) => {
@@ -104,7 +106,9 @@ const otherItems = [
function Settings(props) {
const isSystemThemeDark = useSystemTheme();
const isVaultCreated = useAppStore((store) => store.isVaultCreated);
+ const setIsVaultCreated = useAppStore((store) => store.setIsVaultCreated);
const refreshApp = useAppStore((store) => store.refresh);
+ const refreshNotes = useNoteStore((store) => store.refresh);
const sync = useAppStore((store) => store.sync);
const theme = useThemeStore((store) => store.theme);
const toggleNightMode = useThemeStore((store) => store.toggleNightMode);
@@ -244,7 +248,6 @@ function Settings(props) {
/>
</Button>
<Button
- sx={{ borderColor: "error" }}
variant="list"
onClick={async () => {
return showPasswordDialog(
@@ -255,6 +258,10 @@ function Settings(props) {
}
);
}}
+ sx={{ ":hover": { borderColor: "error" } }}
+ bg="errorBg"
+ mx={-2}
+ px={2}
>
<Tip
color="error"
@@ -402,7 +409,44 @@ function Settings(props) {
variant="list"
onClick={() => hashNavigate("/vault/changePassword")}
>
- <Tip text="Change vault password" />
+ <Tip
+ text="Change vault password"
+ tip={"Set a new password for your vault"}
+ />
+ </Button>
+ <Button
+ variant="list"
+ onClick={async () => {
+ if (await Vault.clearVault()) {
+ refreshNotes();
+ showToast("success", "Vault cleared.");
+ }
+ }}
+ >
+ <Tip
+ text="Clear vault"
+ tip="Unlock all locked notes and clear vault"
+ />
+ </Button>
+ <Button
+ variant="list"
+ onClick={async () => {
+ if ((await Vault.deleteVault()) && !(await db.vault.exists())) {
+ setIsVaultCreated(false);
+ await refreshApp();
+ showToast("success", "Vault deleted.");
+ }
+ }}
+ sx={{ ":hover": { borderColor: "error" } }}
+ bg="errorBg"
+ mx={-2}
+ px={2}
+ >
+ <Tip
+ color="error"
+ text="Delete vault"
+ tip="Delete vault (and optionally remove all locked notes)"
+ />
</Button>
</>
) : (