From 3b4da046fb4d8c4dcc50c26df48cb8334e73fb42 Mon Sep 17 00:00:00 2001 From: Oleg Chendighelean Date: Fri, 20 Dec 2024 23:03:05 +0000 Subject: [PATCH] Handle redux-persist init --- apps/desktop-e2e/tsconfig.json | 6 ++-- apps/desktop/src/Router.tsx | 29 ++++----------- packages/state/src/reducer.ts | 66 +--------------------------------- 3 files changed, 12 insertions(+), 89 deletions(-) diff --git a/apps/desktop-e2e/tsconfig.json b/apps/desktop-e2e/tsconfig.json index 39ef672fda..240053e442 100644 --- a/apps/desktop-e2e/tsconfig.json +++ b/apps/desktop-e2e/tsconfig.json @@ -13,10 +13,12 @@ "isolatedModules": true, "noEmit": true, "jsx": "react-jsx", - "customConditions": ["@umami/source"] + "customConditions": ["@umami/source"], + "preserveSymlinks": true }, "ts-node": { - "esm": true + "esm": true, + "files": true }, "include": ["src", ".eslintrc.cjs", "cucumber.cjs"] } diff --git a/apps/desktop/src/Router.tsx b/apps/desktop/src/Router.tsx index 7263fa423c..c5d0458c45 100644 --- a/apps/desktop/src/Router.tsx +++ b/apps/desktop/src/Router.tsx @@ -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 ( diff --git a/packages/state/src/reducer.ts b/packages/state/src/reducer.ts index 7c7bf4768d..f516ea652f 100644 --- a/packages/state/src/reducer.ts +++ b/packages/state/src/reducer.ts @@ -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"; @@ -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): Promise => { - 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,