diff --git a/apps/web/src/common/db.js b/apps/web/src/common/db.js index 237d46964..c5299b132 100644 --- a/apps/web/src/common/db.js +++ b/apps/web/src/common/db.js @@ -1,20 +1,18 @@ import { EventSourcePolyfill as EventSource } from "event-source-polyfill"; -import { EVENTS } from "@streetwriters/notesnook-core/common"; -import { TaskManager } from "./task-manager"; -import { initalize, logger } from "@streetwriters/notesnook-core/logger"; +import { NNStorage } from "../interfaces/storage"; +import { logger } from "../utils/logger"; /** * @type {import("@streetwriters/notesnook-core/api").default} */ var db; async function initializeDatabase(persistence) { + logger.measure("Database initialization"); + const { default: Database } = await import( "@streetwriters/notesnook-core/api" ); - const { NNStorage } = await import("../interfaces/storage"); const { default: FS } = await import("../interfaces/fs"); - - initalize(new NNStorage("Logs", persistence)); db = new Database(new NNStorage("Notesnook", persistence), EventSource, FS); // if (isTesting()) { @@ -39,22 +37,18 @@ async function initializeDatabase(persistence) { // }); // } - db.eventManager.subscribe(EVENTS.databaseMigrating, async ({ from, to }) => { - await TaskManager.startTask({ - type: "modal", - title: `Migrating your database`, - subtitle: - "Please do not close your browser/app before the migration is done.", - action: (task) => { - task({ text: `Migrating database from v${from} to v${to}` }); - return new Promise((resolve) => { - db.eventManager.subscribe(EVENTS.databaseMigrated, resolve); - }); - }, - }); - }); + // db.eventManager.subscribe(EVENTS.databaseMigrating, async ({ from, to }) => { + + // }); await db.init(); + + logger.measure("Database initialization"); + + if (db.migrations.required()) { + const { showMigrationDialog } = await import("./dialog-controller"); + await showMigrationDialog(); + } return db; } diff --git a/apps/web/src/common/dialog-controller.tsx b/apps/web/src/common/dialog-controller.tsx index 31722a537..eb87f68e8 100644 --- a/apps/web/src/common/dialog-controller.tsx +++ b/apps/web/src/common/dialog-controller.tsx @@ -278,6 +278,12 @@ export function showEmailVerificationDialog() { )); } +export function showMigrationDialog() { + return showDialog("MigrationDialog", (Dialog, perform) => ( + perform(false)} /> + )); +} + type LoadingDialogProps = { title: string; message?: string; diff --git a/apps/web/src/components/dialogs/index.js b/apps/web/src/components/dialogs/index.js index 553648511..c2df99e7a 100644 --- a/apps/web/src/components/dialogs/index.js +++ b/apps/web/src/components/dialogs/index.js @@ -19,6 +19,7 @@ import OnboardingDialog from "./onboarding-dialog"; import AttachmentsDialog from "./attachmentsdialog"; import { Prompt } from "./prompt"; import { ToolbarConfigDialog } from "./toolbarconfigdialog"; +import { MigrationDialog } from "./migrationdialog"; const Dialogs = { AddNotebookDialog, @@ -43,5 +44,6 @@ const Dialogs = { RecoveryCodesDialog, OnboardingDialog, AttachmentsDialog, + MigrationDialog, }; export default Dialogs; diff --git a/apps/web/src/components/dialogs/migration-dialog.tsx b/apps/web/src/components/dialogs/migration-dialog.tsx new file mode 100644 index 000000000..3688400ae --- /dev/null +++ b/apps/web/src/components/dialogs/migration-dialog.tsx @@ -0,0 +1,66 @@ +import { Text } from "rebass"; +import { createBackup } from "../../common"; +import { db } from "../../common/db"; +import { Perform } from "../../common/dialog-controller"; +import { TaskManager } from "../../common/task-manager"; +import Dialog from "./dialog"; + +export type MigrationDialogProps = { + onClose: Perform; +}; + +export function MigrationDialog(props: MigrationDialogProps) { + return ( + { + await createBackup(true); + await TaskManager.startTask({ + type: "modal", + title: `Migrating your database`, + subtitle: + "Please do NOT close your browser/app during the migration process.", + action: (task) => { + task({ text: `Please wait...` }); + return db.migrations?.migrate(); + }, + }); + props.onClose(true); + }, + }} + > + Read before continuing: + + + It is required that you download & save a backup of + your data. + + + Some merge conflicts in your notes after a migration are + expected. It is recommended that you go through them & + resolve them carefully. + + + But if you are feeling reckless and want to risk losing + some data, you can logout & log back in. + + + + + If you face any other issues or are unsure about what to do, feel free + to reach out to us via{" "} + Discord or email + us at{" "} + support@streetwriters.co + + + + ); +}