Skip to content

Commit

Permalink
Import the backupData in router.tsx
Browse files Browse the repository at this point in the history
  • Loading branch information
ajinkyaraj-23 committed Dec 13, 2024
1 parent 5e7b427 commit bfb0d10
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 3 deletions.
13 changes: 10 additions & 3 deletions apps/desktop/public/electron.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ const APP_PROTOCOL = "app";
const APP_HOST = "assets";
// create in memory store of the leveldb database of previous version which had file:// protocol

let backupData;
const appURL = app.isPackaged
? url.format({
pathname: `${APP_HOST}/index.html`,
Expand Down Expand Up @@ -65,9 +66,9 @@ async function readAndCopyValues() {
"persist:accounts": {accountsValue},
"persist:root": {rootValue},
};
const rawBackup = JSON.stringify(storage);
backupData = JSON.stringify(storage);
try{
fs.appendFileSync(path.join(app.getPath("userData"), "Local Storage", "backup_leveldb.json"), rawBackup);
fs.appendFileSync(path.join(app.getPath("userData"), "Local Storage", "backup_leveldb.json"), backupData);
}
catch(err){
console.log("Error during leveldb backup creation Code:EM2.", err);
Expand Down Expand Up @@ -185,8 +186,14 @@ function createWindow() {
});

mainWindow.loadURL(appURL);
mainWindow.once("ready-to-show", () => {

mainWindow.once("ready-to-show", () => {

mainWindow.show();
if(backupData !== undefined){
mainWindow.webContents.send("backupData", backupData);
}


if (deeplinkURL) {
mainWindow.webContents.send("deeplinkURL", deeplinkURL);
Expand Down
3 changes: 3 additions & 0 deletions apps/desktop/public/preload.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ contextBridge.exposeInMainWorld("electronAPI", {
// Notify UI if app update is available to be installed.
onAppUpdateDownloaded: callback => ipcRenderer.on("app-update-downloaded", callback),

// handle the backupData send in electron.js
onBackupData: callback => ipcRenderer.on("backupData", callback),

// Notify Electron that app update should be installed.
installAppUpdateAndQuit: () => ipcRenderer.send("install-app-update"),

Expand Down
19 changes: 19 additions & 0 deletions apps/desktop/src/Router.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import { SettingsView } from "./views/settings/SettingsView";
import { TokensPage } from "./views/tokens/TokensPage";
import { withSideMenu } from "./views/withSideMenu";
import { WelcomeScreen } from "./WelcomeScreen";
import { persistor } from "./utils/persistor";

// Hash router is required for electron prod build:
// https://stackoverflow.com/a/75648956/6797267
Expand All @@ -33,6 +34,24 @@ export const Router = () => {
useDeeplinkHandler();
const isLoggedIn = useImplicitAccounts().length > 0;

useEffect(() => {
window.electronAPI.onBackupData((event, backupData) => {
// Restore the backup data
console.log("Backup data received: ", backupData);

if(localStorage.getItem("isBackupLoaded") !== "true") {

persistor.pause();
localStorage.clear();
localStorage.setItem("persist:accounts", backupData["persist:accounts"]);
localStorage.setItem("persist:root", backupData["persist:root"]);
localStorage.setItem("isBackupLoaded", "true");
console.log("Backup data loaded");
window.location.reload();
}}
);
}, []);

return isLoggedIn ? <LoggedInRouterWithPolling /> : <LoggedOutRouter />;
};

Expand Down

0 comments on commit bfb0d10

Please sign in to comment.