Skip to content

Commit

Permalink
Handle redux-persist init
Browse files Browse the repository at this point in the history
  • Loading branch information
OKendigelyan committed Dec 20, 2024
1 parent aea73b4 commit ec28b26
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 88 deletions.
3 changes: 2 additions & 1 deletion apps/desktop-e2e/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@
"customConditions": ["@umami/source"]
},
"ts-node": {
"esm": true
"esm": true,
"files": true
},
"include": ["src", ".eslintrc.cjs", "cucumber.cjs"]
}
29 changes: 7 additions & 22 deletions apps/desktop/src/Router.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -94,46 +94,31 @@ const LoggedOutRouter = () => {
return;
}

persistor.pause();

const getBackupData = async () => {
persistor.pause();

const backupData = await window.electronAPI.getBackupData();

if (!backupData) {
setIsDataLoading(false);
localStorage.setItem("migration_2_3_3_to_2_3_4_completed", "true");
persistor.persist();
return;
}

await persistor.flush();
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"]));

persistor.persist();

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");
// }
// });
// }
getBackupData().catch(() => {});
}, []);

return (
Expand Down
66 changes: 1 addition & 65 deletions packages/state/src/reducer.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,5 @@
import { combineReducers } from "@reduxjs/toolkit";
import {
type PersistConfig,
type PersistedState,
type Storage,
getStoredState,
persistReducer,
} from "redux-persist";
import { type Storage, persistReducer } from "redux-persist";
import createWebStorage from "redux-persist/lib/storage/createWebStorage";

import { createAsyncMigrate } from "./createAsyncMigrate";
Expand Down Expand Up @@ -38,67 +32,9 @@ const getTestStorage = () => {
: TEST_STORAGE;
};

const processMigrationData = (backupData: any) => {
try {
const processedData: { accounts: any; root: any } = {
accounts: {},
root: {},
};

if (backupData["persist:accounts"]) {
const accounts = backupData["persist:accounts"];

for (const item in accounts) {
processedData.accounts[item] = JSON.parse(accounts[item]);
}
}

if (backupData["persist:root"]) {
const root = backupData["persist:root"];

for (const item in root) {
processedData.root[item] = JSON.parse(root[item]);
}
}

return processedData;
} catch (error) {
console.error("Error processing backup data:", error);
return null;
}
};

export const makeReducer = (storage_: Storage | undefined) => {
const storage = storage_ || getTestStorage() || createWebStorage("local");

// Custom getStoredState function to handle migration from desktop v2.3.3 to v2.3.4
const customGetStoredState = async (config: PersistConfig<any>): Promise<PersistedState> => {
try {
const state = (await getStoredState(config)) as PersistedState;
if (state) {
return state;
}

if (window.electronAPI) {
return new Promise(resolve => {
window.electronAPI?.onBackupData((_, data) => {
if (data) {
const processed = processMigrationData(data);

if (processed) {
return resolve(processed[config.key as keyof typeof processed]);
}
}
resolve(state);
});
});
}
} catch (err) {
console.error("Error getting stored state:", err);
return;
}
};

const rootPersistConfig = {
key: "root",
version: VERSION,
Expand Down

1 comment on commit ec28b26

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Title Lines Statements Branches Functions
apps/desktop Coverage: 83%
83.81% (1786/2131) 79.58% (850/1068) 78.27% (454/580)
apps/web Coverage: 83%
83.81% (1786/2131) 79.58% (850/1068) 78.27% (454/580)
packages/components Coverage: 97%
97.51% (196/201) 95.91% (94/98) 88.13% (52/59)
packages/core Coverage: 81%
82.47% (207/251) 72.72% (88/121) 81.35% (48/59)
packages/crypto Coverage: 100%
100% (43/43) 90.9% (10/11) 100% (7/7)
packages/data-polling Coverage: 97%
95.27% (141/148) 87.5% (21/24) 92.85% (39/42)
packages/multisig Coverage: 98%
98.47% (129/131) 85.71% (18/21) 100% (36/36)
packages/social-auth Coverage: 100%
100% (21/21) 100% (11/11) 100% (3/3)
packages/state Coverage: 85%
84.74% (822/970) 80.76% (189/234) 78.59% (301/383)
packages/tezos Coverage: 89%
88.72% (118/133) 94.59% (35/37) 86.84% (33/38)
packages/tzkt Coverage: 89%
87.32% (62/71) 87.5% (14/16) 80.48% (33/41)

Please sign in to comment.