Skip to content

Commit 91ed7d3

Browse files
Copilotlarp0
andcommitted
Fix Rust compilation errors: proper feature gating and trait bounds
Co-authored-by: larp0 <[email protected]>
1 parent 700a8a1 commit 91ed7d3

File tree

6 files changed

+106
-62
lines changed

6 files changed

+106
-62
lines changed

opensvm-dioxus/src/components/copy_button.rs

Lines changed: 31 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,15 @@
11
use dioxus::prelude::*;
22
use dioxus_free_icons::icons::lucide_icons::{Copy, Check};
33
use dioxus_free_icons::Icon;
4+
5+
#[cfg(feature = "web")]
46
use wasm_bindgen::prelude::*;
7+
#[cfg(feature = "web")]
58
use web_sys::window;
9+
#[cfg(feature = "web")]
610
use gloo::timers::callback::Timeout;
711

8-
#[derive(Props)]
12+
#[derive(Props, PartialEq)]
913
pub struct CopyButtonProps<'a> {
1014
pub text: &'a str,
1115
}
@@ -15,24 +19,34 @@ pub fn CopyButton<'a>(cx: Scope<'a, CopyButtonProps<'a>>) -> Element {
1519
let copied = use_state(cx, || false);
1620

1721
let handle_copy = move |_| {
18-
let text = cx.props.text.to_string();
19-
20-
// Copy to clipboard using the Clipboard API
21-
if let Some(window) = window() {
22-
if let Ok(Some(navigator)) = window.navigator().dyn_into::<web_sys::Navigator>() {
23-
if let Some(clipboard) = navigator.clipboard() {
24-
let _ = clipboard.write_text(&text);
25-
copied.set(true);
26-
27-
// Reset copied state after 2 seconds
28-
let copied_clone = copied.clone();
29-
let timeout = Timeout::new(2000, move || {
30-
copied_clone.set(false);
31-
});
32-
timeout.forget();
22+
#[cfg(feature = "web")]
23+
{
24+
let text = cx.props.text.to_string();
25+
26+
// Copy to clipboard using the Clipboard API
27+
if let Some(window) = window() {
28+
if let Ok(Some(navigator)) = window.navigator().dyn_into::<web_sys::Navigator>() {
29+
if let Some(clipboard) = navigator.clipboard() {
30+
let _ = clipboard.write_text(&text);
31+
copied.set(true);
32+
33+
// Reset copied state after 2 seconds
34+
let copied_clone = copied.clone();
35+
let timeout = Timeout::new(2000, move || {
36+
copied_clone.set(false);
37+
});
38+
timeout.forget();
39+
}
3340
}
3441
}
3542
}
43+
44+
#[cfg(not(feature = "web"))]
45+
{
46+
// For desktop/mobile, could implement platform-specific clipboard handling
47+
copied.set(true);
48+
// Would need platform-specific implementation here
49+
}
3650
};
3751

3852
cx.render(rsx! {
@@ -51,7 +65,7 @@ pub fn CopyButton<'a>(cx: Scope<'a, CopyButtonProps<'a>>) -> Element {
5165
} else {
5266
rsx! {
5367
Icon {
54-
icon: FaCopy,
68+
icon: Copy,
5569
width: 20,
5670
height: 20,
5771
fill: "var(--text-secondary)"

opensvm-dioxus/src/components/network_stats.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ pub fn StatsCard<'a>(cx: Scope<'a, StatsCardProps<'a>>) -> Element {
3333
}
3434

3535
// Network stats component
36-
#[derive(Props)]
36+
#[derive(Props, PartialEq)]
3737
pub struct NetworkStatsProps {
3838
pub blocks_processed: u64,
3939
pub active_validators: u64,

opensvm-dioxus/src/components/validator_analytics.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ pub fn ValidatorAnalytics(cx: Scope) -> Element {
140140

141141
StatsCard {
142142
icon: cx.render(rsx! {
143-
Icon { icon: FaWallet, width: 18, height: 18, fill: "var(--primary)" }
143+
Icon { icon: Wallet, width: 18, height: 18, fill: "var(--primary)" }
144144
}),
145145
label: "Total Stake",
146146
value: &format!("{} SOL", format_number(stats.total_stake)),

opensvm-dioxus/src/components/wallet_button.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,36 +8,36 @@ use crate::app::Route;
88

99
#[component]
1010
pub fn WalletButton(cx: Scope) -> Element {
11-
let wallet = use_wallet_store();
11+
let wallet = use_wallet_store(cx);
1212
let navigator = use_navigator(cx);
1313

1414
let handle_wallet_click = move |_| {
15-
if wallet.read().is_connected {
15+
if wallet.get().is_connected {
1616
// If already connected, navigate to wallet page
1717
navigator.push(Route::Wallet {});
1818
} else {
1919
// If not connected, connect wallet
20-
connect_wallet(wallet.clone());
20+
connect_wallet(wallet);
2121
navigator.push(Route::Wallet {});
2222
}
2323
};
2424

2525
cx.render(rsx! {
2626
button {
2727
class: "btn",
28-
class: if wallet.read().is_connected { "btn-secondary" } else { "btn-primary" },
28+
class: if wallet.get().is_connected { "btn-secondary" } else { "btn-primary" },
2929
onclick: handle_wallet_click,
3030

3131
Icon {
32-
icon: FaWallet,
32+
icon: Wallet,
3333
width: 16,
3434
height: 16,
35-
fill: if wallet.read().is_connected { "var(--text)" } else { "white" }
35+
fill: if wallet.get().is_connected { "var(--text)" } else { "white" }
3636
}
3737

3838
span {
3939
style: "margin-left: 8px;",
40-
if wallet.read().is_connected { "Wallet" } else { "Connect Wallet" }
40+
if wallet.get().is_connected { "Wallet" } else { "Connect Wallet" }
4141
}
4242
}
4343
})

opensvm-dioxus/src/stores/theme_store.rs

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
use dioxus::prelude::*;
22
use serde::{Deserialize, Serialize};
3+
4+
#[cfg(feature = "web")]
35
use web_sys::Storage;
46

57
// Define the theme options
@@ -28,10 +30,13 @@ impl Default for ThemeState {
2830
pub fn use_theme_store(cx: &ScopeState) -> &UseState<ThemeState> {
2931
let theme_state = use_state(cx, || {
3032
// Try to load from local storage
31-
if let Some(storage) = get_local_storage() {
32-
if let Ok(Some(stored_data)) = storage.get_item("theme-storage") {
33-
if let Ok(theme_state) = serde_json::from_str::<ThemeState>(&stored_data) {
34-
return theme_state;
33+
#[cfg(feature = "web")]
34+
{
35+
if let Some(storage) = get_local_storage() {
36+
if let Ok(Some(stored_data)) = storage.get_item("theme-storage") {
37+
if let Ok(theme_state) = serde_json::from_str::<ThemeState>(&stored_data) {
38+
return theme_state;
39+
}
3540
}
3641
}
3742
}
@@ -49,13 +54,14 @@ pub fn use_theme_store(cx: &ScopeState) -> &UseState<ThemeState> {
4954
}
5055

5156
// Helper function to get local storage
57+
#[cfg(feature = "web")]
5258
fn get_local_storage() -> Option<Storage> {
53-
#[cfg(feature = "web")]
54-
{
55-
let window = web_sys::window()?;
56-
window.local_storage().ok()?
57-
}
58-
#[cfg(not(feature = "web"))]
59+
let window = web_sys::window()?;
60+
window.local_storage().ok()?
61+
}
62+
63+
#[cfg(not(feature = "web"))]
64+
fn get_local_storage() -> Option<()> {
5965
None
6066
}
6167

opensvm-dioxus/src/stores/wallet_store.rs

Lines changed: 50 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,66 +1,90 @@
11
use dioxus::prelude::*;
22
use serde::{Deserialize, Serialize};
3+
4+
#[cfg(feature = "web")]
35
use wasm_bindgen::prelude::*;
6+
#[cfg(feature = "web")]
47
use web_sys::Storage;
58

69
// Define the wallet state
7-
#[derive(Debug, Clone, Serialize, Deserialize)]
10+
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
811
pub struct WalletState {
912
pub is_connected: bool,
1013
pub address: String,
1114
}
1215

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+
}
2736
}
2837
}
2938
}
30-
31-
|| {}
39+
WalletState::default()
3240
});
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
3550
}
3651

3752
// Helper function to get local storage
53+
#[cfg(feature = "web")]
3854
fn get_local_storage() -> Option<Storage> {
3955
let window = web_sys::window()?;
4056
window.local_storage().ok()?
4157
}
4258

59+
#[cfg(not(feature = "web"))]
60+
fn get_local_storage() -> Option<()> {
61+
None
62+
}
63+
4364
// Helper function to save state to local storage
4465
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+
}
4872
}
4973
}
5074
}
5175

5276
// 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();
5579
state.is_connected = true;
5680
state.address = "7nzUHcqRVGVsRKLzq9vEG8JqLkQHv6LPCC9JQZy54RVP".to_string(); // Mock address
5781
wallet.set(state.clone());
5882
save_to_local_storage(&state);
5983
}
6084

6185
// 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();
6488
state.is_connected = false;
6589
state.address = String::new();
6690
wallet.set(state.clone());

0 commit comments

Comments
 (0)