From 7d46dbd43701ca938b1cd9c067b216e6f00e3ccc Mon Sep 17 00:00:00 2001 From: spinline Date: Sun, 8 Feb 2026 20:02:01 +0300 Subject: [PATCH] =?UTF-8?q?refactor:=20tema=20y=C3=B6netimi=20leptos-use::?= =?UTF-8?q?use=5Flocal=5Fstorage=20ile=20reaktif=20hale=20getirildi?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- frontend/Cargo.toml | 1 + frontend/src/components/layout/statusbar.rs | 45 ++++++--------------- 2 files changed, 13 insertions(+), 33 deletions(-) diff --git a/frontend/Cargo.toml b/frontend/Cargo.toml index d9f1b79..e4d556b 100644 --- a/frontend/Cargo.toml +++ b/frontend/Cargo.toml @@ -55,3 +55,4 @@ js-sys = "0.3.85" base64 = "0.22.1" serde-wasm-bindgen = "0.6.5" leptos-use = "0.13" +codee = "0.2" diff --git a/frontend/src/components/layout/statusbar.rs b/frontend/src/components/layout/statusbar.rs index d2c9971..0d16938 100644 --- a/frontend/src/components/layout/statusbar.rs +++ b/frontend/src/components/layout/statusbar.rs @@ -1,4 +1,6 @@ use leptos::*; +use leptos_use::storage::use_local_storage; +use codee::string::FromToStringCodec; use shared::GlobalLimitRequest; fn format_bytes(bytes: i64) -> String { @@ -26,34 +28,19 @@ pub fn StatusBar() -> impl IntoView { let store = use_context::().expect("store not provided"); let stats = store.global_stats; - let initial_theme = if let Some(win) = web_sys::window() { - if let Some(doc) = win.document() { - doc.document_element() - .and_then(|el| el.get_attribute("data-theme")) - .unwrap_or_else(|| "dark".to_string()) - } else { - "dark".to_string() - } - } else { - "dark".to_string() - }; + // Use leptos-use for reactive localStorage management + let (current_theme, set_current_theme, _) = use_local_storage::("vibetorrent_theme"); - let (current_theme, set_current_theme) = create_signal(initial_theme); + // Initialize with default if empty + if current_theme.get_untracked().is_empty() { + set_current_theme.set("dark".to_string()); + } + // Automatically sync theme to document attribute create_effect(move |_| { - if let Some(win) = web_sys::window() { - if let Some(storage) = win.local_storage().ok().flatten() { - if let Ok(Some(stored_theme)) = storage.get_item("vibetorrent_theme") { - let theme = stored_theme.to_lowercase(); - set_current_theme.set(theme.clone()); - if let Some(doc) = win.document() { - let _ = doc - .document_element() - .unwrap() - .set_attribute("data-theme", &theme); - } - } - } + let theme = current_theme.get().to_lowercase(); + if let Some(doc) = document().document_element() { + let _ = doc.set_attribute("data-theme", &theme); } }); @@ -275,14 +262,6 @@ pub fn StatusBar() -> impl IntoView { on:pointerdown=move |e| { e.stop_propagation(); set_current_theme.set(theme.to_string()); - if let Some(win) = web_sys::window() { - if let Some(doc) = win.document() { - let _ = doc.document_element().unwrap().set_attribute("data-theme", theme); - } - if let Some(storage) = win.local_storage().ok().flatten() { - let _ = storage.set_item("vibetorrent_theme", theme); - } - } close_all(); } >