feat(gui): Preserve global state across page reloads (#48)

This commit is contained in:
binarybaron 2024-08-29 13:38:11 +02:00 committed by GitHub
parent c7c7cf1886
commit d913206062
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
5 changed files with 35 additions and 11 deletions

View file

@ -26,6 +26,7 @@
"react-qr-code": "^2.0.15", "react-qr-code": "^2.0.15",
"react-redux": "^9.1.2", "react-redux": "^9.1.2",
"react-router-dom": "^6.24.1", "react-router-dom": "^6.24.1",
"redux-persist": "^6.0.0",
"semver": "^7.6.2", "semver": "^7.6.2",
"virtua": "^0.33.2" "virtua": "^0.33.2"
}, },

View file

@ -1,5 +1,6 @@
import { createRoot } from "react-dom/client"; import { createRoot } from "react-dom/client";
import { Provider } from "react-redux"; import { Provider } from "react-redux";
import { PersistGate } from "redux-persist/integration/react";
import { setAlerts } from "store/features/alertsSlice"; import { setAlerts } from "store/features/alertsSlice";
import { setRegistryProviders } from "store/features/providersSlice"; import { setRegistryProviders } from "store/features/providersSlice";
import { setBtcPrice, setXmrPrice } from "store/features/ratesSlice"; import { setBtcPrice, setXmrPrice } from "store/features/ratesSlice";
@ -12,7 +13,7 @@ import {
} from "./api"; } from "./api";
import App from "./components/App"; import App from "./components/App";
import { checkBitcoinBalance, getRawSwapInfos } from "./rpc"; import { checkBitcoinBalance, getRawSwapInfos } from "./rpc";
import { store } from "./store/storeRenderer"; import { persistor, store } from "./store/storeRenderer";
setInterval(() => { setInterval(() => {
checkBitcoinBalance(); checkBitcoinBalance();
@ -23,7 +24,9 @@ const container = document.getElementById("root");
const root = createRoot(container!); const root = createRoot(container!);
root.render( root.render(
<Provider store={store}> <Provider store={store}>
<PersistGate loading={null} persistor={persistor}>
<App /> <App />
</PersistGate>
</Provider>, </Provider>,
); );

View file

@ -1,9 +1,28 @@
import { configureStore } from "@reduxjs/toolkit"; import { combineReducers, configureStore } from "@reduxjs/toolkit";
import { persistReducer, persistStore } from "redux-persist";
import sessionStorage from "redux-persist/lib/storage/session";
import { reducers } from "store/combinedReducer"; import { reducers } from "store/combinedReducer";
// We persist the redux store in sessionStorage
// The point of this is to preserve the store across reloads while not persisting it across GUI restarts
//
// If the user reloads the page, while a swap is running we want to
// continue displaying the correct state of the swap
const persistConfig = {
key: "gui-global-state-store",
storage: sessionStorage,
};
const persistedReducer = persistReducer(
persistConfig,
combineReducers(reducers),
);
export const store = configureStore({ export const store = configureStore({
reducer: reducers, reducer: persistedReducer,
}); });
export const persistor = persistStore(store);
export type AppDispatch = typeof store.dispatch; export type AppDispatch = typeof store.dispatch;
export type RootState = ReturnType<typeof store.getState>; export type RootState = ReturnType<typeof store.getState>;

View file

@ -2803,6 +2803,11 @@ receptacle@^1.3.2:
dependencies: dependencies:
ms "^2.1.1" ms "^2.1.1"
redux-persist@^6.0.0:
version "6.0.0"
resolved "https://registry.yarnpkg.com/redux-persist/-/redux-persist-6.0.0.tgz#b4d2972f9859597c130d40d4b146fecdab51b3a8"
integrity sha512-71LLMbUq2r02ng2We9S215LtPu3fY0KgaGE0k8WRgl6RkqxtGfl7HUozz1Dftwsb0D/5mZ8dwAaPbtnzfvbEwQ==
redux-thunk@^3.1.0: redux-thunk@^3.1.0:
version "3.1.0" version "3.1.0"
resolved "https://registry.yarnpkg.com/redux-thunk/-/redux-thunk-3.1.0.tgz#94aa6e04977c30e14e892eae84978c1af6058ff3" resolved "https://registry.yarnpkg.com/redux-thunk/-/redux-thunk-3.1.0.tgz#94aa6e04977c30e14e892eae84978c1af6058ff3"

View file

@ -1,14 +1,10 @@
/** use crate::{monero, network::quote::BidQuote};
* TOOD: Perhaps we should move this to the `src-tauri` package.
*/
use anyhow::Result; use anyhow::Result;
use bitcoin::Txid; use bitcoin::Txid;
use serde::Serialize; use serde::Serialize;
use typeshare::typeshare; use typeshare::typeshare;
use uuid::Uuid; use uuid::Uuid;
use crate::{monero, network::quote::BidQuote};
static SWAP_PROGRESS_EVENT_NAME: &str = "swap-progress-update"; static SWAP_PROGRESS_EVENT_NAME: &str = "swap-progress-update";
#[derive(Debug, Clone)] #[derive(Debug, Clone)]
@ -29,8 +25,8 @@ impl TauriHandle {
#[allow(unused_variables)] #[allow(unused_variables)]
pub fn emit_tauri_event<S: Serialize + Clone>(&self, event: &str, payload: S) -> Result<()> { pub fn emit_tauri_event<S: Serialize + Clone>(&self, event: &str, payload: S) -> Result<()> {
#[cfg(tauri)] #[cfg(feature = "tauri")]
self.0.emit(event, payload).map_err(|e| e.into())?; tauri::Emitter::emit(self.0.as_ref(), event, payload).map_err(anyhow::Error::from)?;
Ok(()) Ok(())
} }