diff --git a/apps/desktop/public/electron.js b/apps/desktop/public/electron.js index 54f0de58e..5c6fbbe2a 100644 --- a/apps/desktop/public/electron.js +++ b/apps/desktop/public/electron.js @@ -36,6 +36,20 @@ protocol.registerSchemesAsPrivileged([ // Configure electron-log log.transports.file.resolvePathFn = () => path.join(app.getPath("userData"), "umami-desktop.log"); +ipcMain.handle("getBackupData", () => { + const backupPath = path.normalize( + path.join(app.getPath("userData"), "Local Storage", "backup_leveldb.json") + ); + + if (!fs.existsSync(backupPath)) { + return; + } + + const backupData = JSON.parse(fs.readFileSync(backupPath, "utf-8")); + + return backupData; +}); + async function createBackupFromPrevDB() { const dbPath = path.normalize(path.join(app.getPath("userData"), "Local Storage", "leveldb")); const backupPath = path.normalize( diff --git a/apps/desktop/public/preload.js b/apps/desktop/public/preload.js index 2a2eb06be..5628baece 100644 --- a/apps/desktop/public/preload.js +++ b/apps/desktop/public/preload.js @@ -17,6 +17,7 @@ contextBridge.exposeInMainWorld("electronAPI", { // handle the backupData send in electron.js onBackupData: callback => ipcRenderer.on("backupData", callback), + getBackupData: () => ipcRenderer.invoke("getBackupData"), // Notify Electron that app update should be installed. installAppUpdateAndQuit: () => ipcRenderer.send("install-app-update"), diff --git a/apps/desktop/src/Router.tsx b/apps/desktop/src/Router.tsx index 08fd07bc0..7263fa423 100644 --- a/apps/desktop/src/Router.tsx +++ b/apps/desktop/src/Router.tsx @@ -96,24 +96,44 @@ const LoggedOutRouter = () => { persistor.pause(); - if (window.electronAPI) { - window.electronAPI.onBackupData((_, data) => { - if (data) { - setTimeout(() => { - localStorage.clear(); - - localStorage.setItem("migration_2_3_3_to_2_3_4_completed", "true"); - localStorage.setItem("persist:accounts", JSON.stringify(data["persist:accounts"])); - localStorage.setItem("persist:root", JSON.stringify(data["persist:root"])); - - window.location.reload(); - }, 3000); - } else { - setIsDataLoading(false); - localStorage.setItem("migration_2_3_3_to_2_3_4_completed", "true"); - } - }); - } + const getBackupData = async () => { + const backupData = await window.electronAPI.getBackupData(); + + if (!backupData) { + setIsDataLoading(false); + localStorage.setItem("migration_2_3_3_to_2_3_4_completed", "true"); + return; + } + + localStorage.clear(); + + localStorage.setItem("migration_2_3_3_to_2_3_4_completed", "true"); + localStorage.setItem("persist:accounts", JSON.stringify(backupData["persist:accounts"])); + localStorage.setItem("persist:root", JSON.stringify(backupData["persist:root"])); + + window.location.reload(); + }; + + getBackupData(); + + // if (window.electronAPI) { + // window.electronAPI.onBackupData((_, data) => { + // if (data) { + // setTimeout(() => { + // localStorage.clear(); + + // localStorage.setItem("migration_2_3_3_to_2_3_4_completed", "true"); + // localStorage.setItem("persist:accounts", JSON.stringify(data["persist:accounts"])); + // localStorage.setItem("persist:root", JSON.stringify(data["persist:root"])); + + // window.location.reload(); + // }, 3000); + // } else { + // setIsDataLoading(false); + // localStorage.setItem("migration_2_3_3_to_2_3_4_completed", "true"); + // } + // }); + // } }, []); return ( diff --git a/apps/desktop/src/global.d.ts b/apps/desktop/src/global.d.ts index 580ddaf63..b62a71447 100644 --- a/apps/desktop/src/global.d.ts +++ b/apps/desktop/src/global.d.ts @@ -9,6 +9,7 @@ declare global { onAppUpdateDownloaded: (callback: () => void) => void; installAppUpdateAndQuit: () => void; onBackupData: (fn: (event: any, data?: Record) => void) => void; + getBackupData: () => Promise | undefined>; }; } }