Skip to content

Commit

Permalink
Add getBackupData func to receive data from main process
Browse files Browse the repository at this point in the history
  • Loading branch information
OKendigelyan committed Dec 20, 2024
1 parent 76b2886 commit aea73b4
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 18 deletions.
14 changes: 14 additions & 0 deletions apps/desktop/public/electron.js
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
1 change: 1 addition & 0 deletions apps/desktop/public/preload.js
Original file line number Diff line number Diff line change
Expand Up @@ -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"),
Expand Down
56 changes: 38 additions & 18 deletions apps/desktop/src/Router.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand Down
1 change: 1 addition & 0 deletions apps/desktop/src/global.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ declare global {
onAppUpdateDownloaded: (callback: () => void) => void;
installAppUpdateAndQuit: () => void;
onBackupData: (fn: (event: any, data?: Record<string, string>) => void) => void;
getBackupData: () => Promise<Record<string, string> | undefined>;
};
}
}

0 comments on commit aea73b4

Please sign in to comment.