mirror of
https://github.com/streetwriters/notesnook.git
synced 2025-12-24 07:29:30 +01:00
web: refactor backup verification logic
This commit is contained in:
committed by
Abdullah Atta
parent
aa3d778ad1
commit
d5adc3bbac
@@ -27,7 +27,8 @@ import Config from "../utils/config";
|
||||
import { hashNavigate, getCurrentHash } from "../navigation";
|
||||
import { db } from "./db";
|
||||
import { sanitizeFilename } from "@notesnook/common";
|
||||
import { store as userstore } from "../stores/user-store";
|
||||
import { useStore as useUserStore } from "../stores/user-store";
|
||||
import { useStore as useSettingStore } from "../stores/setting-store";
|
||||
import { showToast } from "../utils/toast";
|
||||
import { SUBSCRIPTION_STATUS } from "./constants";
|
||||
import { readFile, showFilePicker } from "../utils/file-picker";
|
||||
@@ -79,8 +80,17 @@ export async function introduceFeatures() {
|
||||
export const DEFAULT_CONTEXT = { colors: [], tags: [], notebook: {} };
|
||||
|
||||
export async function createBackup() {
|
||||
const encryptBackups =
|
||||
userstore.get().isLoggedIn && Config.get("encryptBackups", false);
|
||||
const { isLoggedIn } = useUserStore.getState();
|
||||
const { encryptBackups, toggleEncryptBackups } = useSettingStore.getState();
|
||||
if (!isLoggedIn && encryptBackups) toggleEncryptBackups();
|
||||
|
||||
const verified = encryptBackups || (await verifyAccount());
|
||||
if (!verified) {
|
||||
showToast("error", "Could not create a backup: user verification failed.");
|
||||
return;
|
||||
}
|
||||
|
||||
const encryptedBackups = isLoggedIn && encryptBackups;
|
||||
|
||||
const filename = sanitizeFilename(
|
||||
`notesnook-backup-${getFormattedDate(Date.now())}`
|
||||
@@ -102,7 +112,7 @@ export async function createBackup() {
|
||||
await new ReadableStream({
|
||||
start() {},
|
||||
async pull(controller) {
|
||||
for await (const file of db.backup!.export("web", encryptBackups)) {
|
||||
for await (const file of db.backup!.export("web", encryptedBackups)) {
|
||||
report({
|
||||
text: `Saving chunk ${file.path}`
|
||||
});
|
||||
@@ -273,7 +283,7 @@ export function totalSubscriptionConsumed(user: User) {
|
||||
export async function showUpgradeReminderDialogs() {
|
||||
if (IS_TESTING) return;
|
||||
|
||||
const user = userstore.get().user;
|
||||
const user = useUserStore.getState().user;
|
||||
if (!user || !user.subscription || user.subscription?.expiry === 0) return;
|
||||
|
||||
const consumed = totalSubscriptionConsumed(user);
|
||||
|
||||
@@ -43,17 +43,7 @@ export const BackupExportSettings: SettingsGroup[] = [
|
||||
{
|
||||
type: "button",
|
||||
title: "Create backup",
|
||||
action: async () => {
|
||||
if (
|
||||
!useUserStore.getState().isLoggedIn &&
|
||||
useSettingStore.getState().encryptBackups
|
||||
)
|
||||
useSettingStore.getState().toggleEncryptBackups();
|
||||
const verified =
|
||||
useSettingStore.getState().encryptBackups ||
|
||||
(await verifyAccount());
|
||||
if (verified) await createBackup();
|
||||
},
|
||||
action: createBackup,
|
||||
variant: "secondary"
|
||||
}
|
||||
]
|
||||
|
||||
Reference in New Issue
Block a user