Skip to content

Commit

Permalink
Add redux-persist migrations
Browse files Browse the repository at this point in the history
  • Loading branch information
OKendigelyan committed Dec 17, 2024
1 parent c56c91c commit dbf0117
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 45 deletions.
50 changes: 25 additions & 25 deletions apps/desktop/public/electron.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ const url = require("url");
const process = require("process");
const { autoUpdater } = require("electron-updater");
const { Level } = require("level");
const log = require('electron-log');
const fs = require('fs');
const log = require("electron-log");
const fs = require("fs");

const APP_PROTOCOL = "app";
const APP_HOST = "assets";
Expand All @@ -33,7 +33,7 @@ protocol.registerSchemesAsPrivileged([
]);

// Configure electron-log
log.transports.file.file = path.join(app.getPath('userData'), 'Local Storage', 'umami-desktop.log');
log.transports.file.file = path.join(app.getPath("userData"), "Local Storage", "umami-desktop.log");

async function readAndCopyValues() {
// Path to the LevelDB database
Expand All @@ -56,29 +56,30 @@ async function readAndCopyValues() {
try {
const accountsValue = await db.get("_file://\x00\x01persist:accounts");
let rootValue = await db.get("_file://\x00\x01persist:root");
if ( !accountsValue || !rootValue) {
if (!accountsValue || !rootValue) {
log.info("No data found in the database. Code:EM03");
return;
}
console.log(accountsValue);
console.log(rootValue.length);
const storage = {
"persist:accounts": {accountsValue},
"persist:root": {rootValue},
};
backupData = JSON.stringify(storage);
try{
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);
}
const storage = {
"persist:accounts": { accountsValue },
"persist:root": { rootValue },
};
backupData = JSON.stringify(storage);
try {
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);
}
console.log("Migration done successfully");
}
catch (err) {
} catch (err) {
log.error("Error during key migration Code:EM4.", err);
} finally {
db.close().catch((err) => {
db.close().catch(err => {
log.error("Error closing the database. Code: EM5", err);
});
}
Expand Down Expand Up @@ -187,13 +188,8 @@ 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 Expand Up @@ -288,7 +284,7 @@ function start() {
// is ready to create the browser windows.
// Some APIs can only be used after this event occurs.
app.whenReady().then(async () => {
// Execute readAndCopyValues at the beginning
// Execute readAndCopyValues at the beginning
await readAndCopyValues();
createWindow();
});
Expand All @@ -311,6 +307,10 @@ 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: 2 additions & 0 deletions apps/desktop/public/preload.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ 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
19 changes: 0 additions & 19 deletions apps/desktop/src/Router.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ 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 @@ -34,24 +33,6 @@ 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
32 changes: 31 additions & 1 deletion packages/state/src/migrations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,17 @@ import { fromPairs, identity } from "lodash";

import { announcementInitialState as announcementsInitialState } from "./slices/announcement";

export const VERSION = 9;
function handleBackupData(callback: (data: any) => void) {
// @ts-ignore
window.electronAPI.triggerBackupData();
// @ts-ignore
window.electronAPI.onBackupData(backupData => {
console.log(backupData);
callback(backupData);
});
}

export const VERSION = 10;

export const mainStoreMigrations = {
0: (state: any) =>
Expand Down Expand Up @@ -80,6 +90,16 @@ 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 @@ -120,4 +140,14 @@ 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 dbf0117

Please sign in to comment.