mirror of
https://github.com/streetwriters/notesnook.git
synced 2025-12-23 23:19:40 +01:00
web: take automatic backups in background
This commit is contained in:
committed by
Abdullah Atta
parent
759fba06ae
commit
b938c58b67
@@ -83,9 +83,10 @@ export async function createBackup(
|
|||||||
rescueMode?: boolean;
|
rescueMode?: boolean;
|
||||||
noVerify?: boolean;
|
noVerify?: boolean;
|
||||||
mode?: "full" | "partial";
|
mode?: "full" | "partial";
|
||||||
|
background?: boolean;
|
||||||
} = { mode: "partial" }
|
} = { mode: "partial" }
|
||||||
) {
|
) {
|
||||||
const { rescueMode, noVerify, mode } = options;
|
const { rescueMode, noVerify, mode, background } = options;
|
||||||
const { isLoggedIn } = useUserStore.getState();
|
const { isLoggedIn } = useUserStore.getState();
|
||||||
const { encryptBackups, toggleEncryptBackups } = useSettingStore.getState();
|
const { encryptBackups, toggleEncryptBackups } = useSettingStore.getState();
|
||||||
if (!isLoggedIn && encryptBackups) toggleEncryptBackups();
|
if (!isLoggedIn && encryptBackups) toggleEncryptBackups();
|
||||||
@@ -115,7 +116,8 @@ export async function createBackup(
|
|||||||
|
|
||||||
const encoder = new TextEncoder();
|
const encoder = new TextEncoder();
|
||||||
const error = await TaskManager.startTask<Error | void>({
|
const error = await TaskManager.startTask<Error | void>({
|
||||||
type: "modal",
|
type: background ? "status" : "modal",
|
||||||
|
id: "creating-backup",
|
||||||
title: "Creating backup",
|
title: "Creating backup",
|
||||||
subtitle: "We are creating a backup of your data. Please wait...",
|
subtitle: "We are creating a backup of your data. Please wait...",
|
||||||
action: async (report) => {
|
action: async (report) => {
|
||||||
|
|||||||
@@ -46,6 +46,7 @@ async function moveNotesToTrash(ids: string[], confirm = true) {
|
|||||||
await TaskManager.startTask({
|
await TaskManager.startTask({
|
||||||
type: "status",
|
type: "status",
|
||||||
id: "deleteNotes",
|
id: "deleteNotes",
|
||||||
|
title: "Deleting notes",
|
||||||
action: async (report) => {
|
action: async (report) => {
|
||||||
report({
|
report({
|
||||||
text: `Deleting ${pluralize(items.length, "note")}...`
|
text: `Deleting ${pluralize(items.length, "note")}...`
|
||||||
@@ -66,6 +67,7 @@ async function moveNotebooksToTrash(ids: string[]) {
|
|||||||
await TaskManager.startTask({
|
await TaskManager.startTask({
|
||||||
type: "status",
|
type: "status",
|
||||||
id: "deleteNotebooks",
|
id: "deleteNotebooks",
|
||||||
|
title: "Deleting notebooks",
|
||||||
action: async (report) => {
|
action: async (report) => {
|
||||||
report({
|
report({
|
||||||
text: `Deleting ${pluralize(ids.length, "notebook")}...`
|
text: `Deleting ${pluralize(ids.length, "notebook")}...`
|
||||||
@@ -92,6 +94,7 @@ async function deleteAttachments(ids: string[]) {
|
|||||||
await TaskManager.startTask({
|
await TaskManager.startTask({
|
||||||
type: "status",
|
type: "status",
|
||||||
id: "deleteAttachments",
|
id: "deleteAttachments",
|
||||||
|
title: "Deleting attachments",
|
||||||
action: async (report) => {
|
action: async (report) => {
|
||||||
for (let i = 0; i < ids.length; ++i) {
|
for (let i = 0; i < ids.length; ++i) {
|
||||||
const id = ids[i];
|
const id = ids[i];
|
||||||
@@ -119,6 +122,7 @@ async function moveRemindersToTrash(ids: string[]) {
|
|||||||
await TaskManager.startTask({
|
await TaskManager.startTask({
|
||||||
type: "status",
|
type: "status",
|
||||||
id: "deleteReminders",
|
id: "deleteReminders",
|
||||||
|
title: "Deleting reminders",
|
||||||
action: async (report) => {
|
action: async (report) => {
|
||||||
report({
|
report({
|
||||||
text: `Deleting ${pluralize(ids.length, "reminder")}...`
|
text: `Deleting ${pluralize(ids.length, "reminder")}...`
|
||||||
|
|||||||
@@ -200,7 +200,7 @@ function isIgnored(key: keyof typeof NoticesData) {
|
|||||||
let openedToast: { hide: () => void } | null = null;
|
let openedToast: { hide: () => void } | null = null;
|
||||||
async function saveBackup(mode: "full" | "partial" = "partial") {
|
async function saveBackup(mode: "full" | "partial" = "partial") {
|
||||||
if (IS_DESKTOP_APP) {
|
if (IS_DESKTOP_APP) {
|
||||||
await createBackup({ noVerify: true, mode });
|
await createBackup({ noVerify: true, mode, background: true });
|
||||||
} else if (isUserPremium() && !IS_TESTING) {
|
} else if (isUserPremium() && !IS_TESTING) {
|
||||||
if (openedToast !== null) return;
|
if (openedToast !== null) return;
|
||||||
openedToast = showToast(
|
openedToast = showToast(
|
||||||
|
|||||||
@@ -21,7 +21,7 @@ import { ProgressDialog } from "../dialogs/progress-dialog";
|
|||||||
import { removeStatus, updateStatus } from "../hooks/use-status";
|
import { removeStatus, updateStatus } from "../hooks/use-status";
|
||||||
|
|
||||||
type TaskType = "status" | "modal";
|
type TaskType = "status" | "modal";
|
||||||
type TaskAction<T> = (report: ProgressReportCallback) => T | Promise<T>;
|
export type TaskAction<T> = (report: ProgressReportCallback) => T | Promise<T>;
|
||||||
type BaseTaskDefinition<TTaskType extends TaskType, TReturnType> = {
|
type BaseTaskDefinition<TTaskType extends TaskType, TReturnType> = {
|
||||||
type: TTaskType;
|
type: TTaskType;
|
||||||
action: TaskAction<TReturnType>;
|
action: TaskAction<TReturnType>;
|
||||||
@@ -31,6 +31,7 @@ type StatusTaskDefinition<TReturnType> = BaseTaskDefinition<
|
|||||||
"status",
|
"status",
|
||||||
TReturnType
|
TReturnType
|
||||||
> & {
|
> & {
|
||||||
|
title: string;
|
||||||
id: string;
|
id: string;
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -39,7 +40,7 @@ type ModalTaskDefinition<TReturnType> = BaseTaskDefinition<
|
|||||||
TReturnType
|
TReturnType
|
||||||
> & {
|
> & {
|
||||||
title: string;
|
title: string;
|
||||||
subtitle: string;
|
subtitle?: string;
|
||||||
};
|
};
|
||||||
|
|
||||||
type TaskDefinition<TReturnType> =
|
type TaskDefinition<TReturnType> =
|
||||||
@@ -59,6 +60,10 @@ export class TaskManager {
|
|||||||
switch (task.type) {
|
switch (task.type) {
|
||||||
case "status": {
|
case "status": {
|
||||||
const statusTask = task;
|
const statusTask = task;
|
||||||
|
updateStatus({
|
||||||
|
key: statusTask.id,
|
||||||
|
status: task.title
|
||||||
|
});
|
||||||
const result = await statusTask.action((progress) => {
|
const result = await statusTask.action((progress) => {
|
||||||
let percentage: number | undefined = undefined;
|
let percentage: number | undefined = undefined;
|
||||||
if (progress.current && progress.total)
|
if (progress.current && progress.total)
|
||||||
|
|||||||
@@ -175,12 +175,8 @@ function SideBar(props: SideBarProps) {
|
|||||||
const result = await TaskManager.startTask({
|
const result = await TaskManager.startTask({
|
||||||
type: "status",
|
type: "status",
|
||||||
id: "trialActivation",
|
id: "trialActivation",
|
||||||
action: (report) => {
|
title: "Activating trial",
|
||||||
report({
|
action: () => db.user.activateTrial()
|
||||||
text: "Activating trial"
|
|
||||||
});
|
|
||||||
return db.user.activateTrial();
|
|
||||||
}
|
|
||||||
});
|
});
|
||||||
if (result) onClose();
|
if (result) onClose();
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
|
|||||||
@@ -513,12 +513,8 @@ function TrialOffer({ onClose }: { onClose: () => void }) {
|
|||||||
const result = await TaskManager.startTask({
|
const result = await TaskManager.startTask({
|
||||||
type: "status",
|
type: "status",
|
||||||
id: "trialActivation",
|
id: "trialActivation",
|
||||||
action: (report) => {
|
title: "Activating trial",
|
||||||
report({
|
action: () => db.user.activateTrial()
|
||||||
text: "Activating trial"
|
|
||||||
});
|
|
||||||
return db.user.activateTrial();
|
|
||||||
}
|
|
||||||
});
|
});
|
||||||
if (result) onClose();
|
if (result) onClose();
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
|
|||||||
@@ -29,7 +29,7 @@ type Progress = {
|
|||||||
};
|
};
|
||||||
type ProgressDialogProps<T> = BaseDialogProps<T | Error> & {
|
type ProgressDialogProps<T> = BaseDialogProps<T | Error> & {
|
||||||
title: string;
|
title: string;
|
||||||
subtitle: string;
|
subtitle?: string;
|
||||||
action: (report: (progress: Progress) => void) => T;
|
action: (report: (progress: Progress) => void) => T;
|
||||||
};
|
};
|
||||||
export const ProgressDialog = DialogManager.register(function ProgressDialog<T>(
|
export const ProgressDialog = DialogManager.register(function ProgressDialog<T>(
|
||||||
|
|||||||
Reference in New Issue
Block a user