|
1 | 1 | use dioxus::prelude::*; |
2 | 2 | use serde::{Deserialize, Serialize}; |
| 3 | + |
| 4 | +#[cfg(feature = "web")] |
3 | 5 | use wasm_bindgen::prelude::*; |
| 6 | +#[cfg(feature = "web")] |
4 | 7 | use web_sys::Storage; |
5 | 8 |
|
6 | 9 | // Define the wallet state |
7 | | -#[derive(Debug, Clone, Serialize, Deserialize)] |
| 10 | +#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)] |
8 | 11 | pub struct WalletState { |
9 | 12 | pub is_connected: bool, |
10 | 13 | pub address: String, |
11 | 14 | } |
12 | 15 |
|
13 | | -// Create a signal for the wallet state |
14 | | -pub fn use_wallet_store() -> Signal<WalletState> { |
15 | | - // Create a static signal to ensure the state persists across renders |
16 | | - static WALLET_STORE: Signal<WalletState> = Signal::new(WalletState { |
17 | | - is_connected: false, |
18 | | - address: String::new(), |
19 | | - }); |
20 | | - |
21 | | - // Initialize the store with data from local storage if available |
22 | | - use_effect(|_| { |
23 | | - if let Some(storage) = get_local_storage() { |
24 | | - if let Ok(Some(stored_data)) = storage.get_item("wallet-storage") { |
25 | | - if let Ok(wallet_state) = serde_json::from_str::<WalletState>(&stored_data) { |
26 | | - WALLET_STORE.set(wallet_state); |
| 16 | +impl Default for WalletState { |
| 17 | + fn default() -> Self { |
| 18 | + Self { |
| 19 | + is_connected: false, |
| 20 | + address: String::new(), |
| 21 | + } |
| 22 | + } |
| 23 | +} |
| 24 | + |
| 25 | +// Create a hook for the wallet state |
| 26 | +pub fn use_wallet_store(cx: &ScopeState) -> &UseState<WalletState> { |
| 27 | + let wallet_state = use_state(cx, || { |
| 28 | + // Try to load from local storage |
| 29 | + #[cfg(feature = "web")] |
| 30 | + { |
| 31 | + if let Some(storage) = get_local_storage() { |
| 32 | + if let Ok(Some(stored_data)) = storage.get_item("wallet-storage") { |
| 33 | + if let Ok(wallet_state) = serde_json::from_str::<WalletState>(&stored_data) { |
| 34 | + return wallet_state; |
| 35 | + } |
27 | 36 | } |
28 | 37 | } |
29 | 38 | } |
30 | | - |
31 | | - || {} |
| 39 | + WalletState::default() |
32 | 40 | }); |
33 | | - |
34 | | - WALLET_STORE |
| 41 | + |
| 42 | + // Save to local storage whenever the state changes |
| 43 | + use_effect(cx, (wallet_state,), |(state,)| { |
| 44 | + let state = state.get(); |
| 45 | + save_to_local_storage(state); |
| 46 | + async move {} |
| 47 | + }); |
| 48 | + |
| 49 | + wallet_state |
35 | 50 | } |
36 | 51 |
|
37 | 52 | // Helper function to get local storage |
| 53 | +#[cfg(feature = "web")] |
38 | 54 | fn get_local_storage() -> Option<Storage> { |
39 | 55 | let window = web_sys::window()?; |
40 | 56 | window.local_storage().ok()? |
41 | 57 | } |
42 | 58 |
|
| 59 | +#[cfg(not(feature = "web"))] |
| 60 | +fn get_local_storage() -> Option<()> { |
| 61 | + None |
| 62 | +} |
| 63 | + |
43 | 64 | // Helper function to save state to local storage |
44 | 65 | fn save_to_local_storage(state: &WalletState) { |
45 | | - if let Some(storage) = get_local_storage() { |
46 | | - if let Ok(json) = serde_json::to_string(state) { |
47 | | - let _ = storage.set_item("wallet-storage", &json); |
| 66 | + #[cfg(feature = "web")] |
| 67 | + { |
| 68 | + if let Some(storage) = get_local_storage() { |
| 69 | + if let Ok(json) = serde_json::to_string(state) { |
| 70 | + let _ = storage.set_item("wallet-storage", &json); |
| 71 | + } |
48 | 72 | } |
49 | 73 | } |
50 | 74 | } |
51 | 75 |
|
52 | 76 | // Connect wallet function |
53 | | -pub fn connect_wallet(wallet: Signal<WalletState>) { |
54 | | - let mut state = wallet.read().clone(); |
| 77 | +pub fn connect_wallet(wallet: &UseState<WalletState>) { |
| 78 | + let mut state = wallet.get().clone(); |
55 | 79 | state.is_connected = true; |
56 | 80 | state.address = "7nzUHcqRVGVsRKLzq9vEG8JqLkQHv6LPCC9JQZy54RVP".to_string(); // Mock address |
57 | 81 | wallet.set(state.clone()); |
58 | 82 | save_to_local_storage(&state); |
59 | 83 | } |
60 | 84 |
|
61 | 85 | // Disconnect wallet function |
62 | | -pub fn disconnect_wallet(wallet: Signal<WalletState>) { |
63 | | - let mut state = wallet.read().clone(); |
| 86 | +pub fn disconnect_wallet(wallet: &UseState<WalletState>) { |
| 87 | + let mut state = wallet.get().clone(); |
64 | 88 | state.is_connected = false; |
65 | 89 | state.address = String::new(); |
66 | 90 | wallet.set(state.clone()); |
|
0 commit comments