Skip to content

Commit

Permalink
test
Browse files Browse the repository at this point in the history
  • Loading branch information
OKendigelyan committed Dec 17, 2024
1 parent dbf0117 commit c678fe1
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 35 deletions.
15 changes: 5 additions & 10 deletions apps/desktop/public/electron.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,6 @@ async function readAndCopyValues() {
log.info("LevelDB database not found at path. Code:EM01", dbPath);
return;
}
// check if backup file exists
if (fs.existsSync(path.join(app.getPath("userData"), "Local Storage", "backup_leveldb.json"))) {
log.info("Backup file already exists. Code:EM02");
return;
}

// Open the LevelDB database
const db = new Level(dbPath, { valueEncoding: "utf-8" });
Expand All @@ -66,7 +61,7 @@ async function readAndCopyValues() {
"persist:accounts": { accountsValue },
"persist:root": { rootValue },
};
backupData = JSON.stringify(storage);
backupData = storage;
try {
fs.appendFileSync(
path.join(app.getPath("userData"), "Local Storage", "backup_leveldb.json"),
Expand Down Expand Up @@ -191,6 +186,10 @@ function createWindow() {
mainWindow.once("ready-to-show", () => {
mainWindow.show();

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

if (deeplinkURL) {
mainWindow.webContents.send("deeplinkURL", deeplinkURL);
deeplinkURL = null;
Expand Down Expand Up @@ -307,10 +306,6 @@ function start() {
// Listen to install-app-update event from UI, start update on getting the event.
ipcMain.on("install-app-update", () => autoUpdater.quitAndInstall());

ipcMain.on("sendBackupData", (_, backupData) => {
mainWindow.webContents.send("backupData", backupData);
});

ipcMain.on("clipboard-write", (_, text) => {
clipboard.writeText(text);
});
Expand Down
2 changes: 0 additions & 2 deletions apps/desktop/public/preload.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,6 @@ contextBridge.exposeInMainWorld("electronAPI", {
// Notify UI if app update is available to be installed.
onAppUpdateDownloaded: callback => ipcRenderer.on("app-update-downloaded", callback),

triggerBackupData: callback => ipcRenderer.send("sendBackupData", callback),

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

Expand Down
48 changes: 47 additions & 1 deletion apps/desktop/src/Router.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,13 @@ import {
useResetBeaconConnections,
} from "@umami/state";
import { noop } from "lodash";
import { useEffect } from "react";
import { useEffect, useState } from "react";
import { HashRouter, Navigate, Route, Routes } from "react-router-dom";

import { AnnouncementBanner } from "./components/AnnouncementBanner";
import { SocialLoginWarningModal } from "./components/SocialLoginWarningModal/SocialLoginWarningModal";
import { BeaconProvider } from "./utils/beacon/BeaconProvider";
import { persistor } from "./utils/persistor";
import { useDeeplinkHandler } from "./utils/useDeeplinkHandler";
import { AddressBookView } from "./views/addressBook/AddressBookView";
import { BatchPage } from "./views/batch/BatchPage";
Expand All @@ -33,6 +34,51 @@ export const Router = () => {
useDeeplinkHandler();
const isLoggedIn = useImplicitAccounts().length > 0;

const [backupData, setBackupData] = useState({});

useEffect(() => {
// @ts-ignore
window.electronAPI.onBackupData((_, backupData) => {
console.log(backupData);
setBackupData(backupData);
});
}, []);

useEffect(() => {
if (Object.keys(backupData).length > 0) {
try {
const accountsValue = backupData["persist:accounts"].accountsValue.slice(1); // Remove the \u0001 prefix
const rootValue = backupData["persist:root"].rootValue.slice(1); // Remove the \u0001 prefix
// Step 2: Parse the outer JSON string
const parsedAccounts = JSON.parse(accountsValue);
const sanitizedRootValue = backupData["persist:root"].rootValue.replaceAll(
/[\u0000-\u001F\u007F-\u009F]/g,
""
);
const parsedRootValue = JSON.parse(sanitizedRootValue);

// Step 3: Parse the inner strings as needed
parsedAccounts.items = JSON.parse(parsedAccounts.items);
parsedAccounts.seedPhrases = JSON.parse(parsedAccounts.seedPhrases);
parsedAccounts.secretKeys = JSON.parse(parsedAccounts.secretKeys);
parsedAccounts._persist = JSON.parse(parsedAccounts._persist);
parsedAccounts.current = JSON.parse(parsedAccounts.current);


console.log(parsedAccounts, "parsedAccounts");
console.log(parsedRootValue, "parsedRootValue");

persistor.pause();
// localStorage.clear();
localStorage.setItem("persist:accounts", JSON.stringify(parsedAccounts));
localStorage.setItem("persist:root", JSON.stringify(parsedRootValue));
// window.location.reload();
} catch (error) {
console.error("Error during backup restoration", error);
}
}
}, [backupData]);

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

Expand Down
28 changes: 6 additions & 22 deletions packages/state/src/migrations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,19 @@ import { announcementInitialState as announcementsInitialState } from "./slices/

function handleBackupData(callback: (data: any) => void) {
// @ts-ignore
window.electronAPI.triggerBackupData();
// window.electronAPI.triggerBackupData();
// @ts-ignore
localStorage.setItem("test", "true");
// @ts-ignore
window.electronAPI.onBackupData(backupData => {
console.log(backupData);
localStorage.setItem("backup-received", "true");
localStorage.setItem("backupData", backupData);
callback(backupData);
});
}

export const VERSION = 10;
export const VERSION = 9;

export const mainStoreMigrations = {
0: (state: any) =>
Expand Down Expand Up @@ -90,16 +94,6 @@ export const mainStoreMigrations = {
}
}
}),
10: (state: any) =>
produce(state, (draft: any) => {
handleBackupData(backupData => {
const parsedData = JSON.parse(backupData);

Object.keys(parsedData["persist:root"]).forEach(key => {
draft[key] = parsedData["persist:root"][key];
});
});
}),
} as any;

export const accountsMigrations = {
Expand Down Expand Up @@ -140,14 +134,4 @@ export const accountsMigrations = {
});
}),
9: identity,
10: (state: any) =>
produce(state, (draft: any) => {
handleBackupData(backupData => {
const parsedData = JSON.parse(backupData);

Object.keys(parsedData["persist:accounts"]).forEach(key => {
draft[key] = parsedData["persist:accounts"][key];
});
});
}),
} as any;

0 comments on commit c678fe1

Please sign in to comment.