fix(frontend): use signal-based toast for async contexts

This commit is contained in:
spinline
2026-02-05 20:48:40 +03:00
parent 6c7379483e
commit 497b39e0ae
3 changed files with 51 additions and 32 deletions

View File

@@ -1,6 +1,7 @@
use leptos::*;
use leptos::html::Dialog;
use crate::store::{toast_success, toast_error, toast_warning};
use crate::store::{show_toast_with_signal, TorrentStore};
use shared::NotificationLevel;
#[component]
@@ -8,6 +9,9 @@ pub fn AddTorrentModal(
#[prop(into)]
on_close: Callback<()>,
) -> impl IntoView {
let store = use_context::<TorrentStore>().expect("TorrentStore not provided");
let notifications = store.notifications;
let dialog_ref = create_node_ref::<Dialog>();
let (uri, set_uri) = create_signal(String::new());
let (is_loading, set_loading) = create_signal(false);
@@ -23,7 +27,7 @@ pub fn AddTorrentModal(
let handle_submit = move |_| {
let uri_val = uri.get();
if uri_val.is_empty() {
toast_warning("Lütfen bir Magnet URI veya URL girin");
show_toast_with_signal(notifications, NotificationLevel::Warning, "Lütfen bir Magnet URI veya URL girin");
set_error_msg.set(Some("Please enter a Magnet URI or URL".to_string()));
return;
}
@@ -44,7 +48,7 @@ pub fn AddTorrentModal(
Ok(resp) => {
if resp.ok() {
logging::log!("Torrent added successfully");
toast_success("Torrent eklendi");
show_toast_with_signal(notifications, NotificationLevel::Success, "Torrent eklendi");
set_loading.set(false);
if let Some(dialog) = dialog_ref.get() {
dialog.close();
@@ -54,14 +58,14 @@ pub fn AddTorrentModal(
let status = resp.status();
let text = resp.text().await.unwrap_or_default();
logging::error!("Failed to add torrent: {} - {}", status, text);
toast_error("Torrent eklenemedi");
show_toast_with_signal(notifications, NotificationLevel::Error, "Torrent eklenemedi");
set_error_msg.set(Some(format!("Error {}: {}", status, text)));
set_loading.set(false);
}
}
Err(e) => {
logging::error!("Network error: {}", e);
toast_error("Bağlantı hatası");
show_toast_with_signal(notifications, NotificationLevel::Error, "Bağlantı hatası");
set_error_msg.set(Some(format!("Network Error: {}", e)));
set_loading.set(false);
}
@@ -69,7 +73,7 @@ pub fn AddTorrentModal(
}
Err(e) => {
logging::error!("Serialization error: {}", e);
toast_error("İstek hatası");
show_toast_with_signal(notifications, NotificationLevel::Error, "İstek hatası");
set_error_msg.set(Some(format!("Request Error: {}", e)));
set_loading.set(false);
}

View File

@@ -1,7 +1,8 @@
use leptos::*;
use wasm_bindgen::closure::Closure;
use wasm_bindgen::JsCast;
use crate::store::{get_action_messages, toast_success, toast_error};
use crate::store::{get_action_messages, show_toast_with_signal};
use shared::NotificationLevel;
fn format_bytes(bytes: i64) -> String {
const UNITS: [&str; 6] = ["B", "KB", "MB", "GB", "TB", "PB"];
@@ -186,6 +187,9 @@ pub fn TorrentTable() -> impl IntoView {
let (success_msg, error_msg) = get_action_messages(&action);
let success_msg = success_msg.to_string();
let error_msg = error_msg.to_string();
// Capture notifications signal before async (use_context unavailable in spawn_local)
let notifications = store.notifications;
spawn_local(async move {
let action_req = if action == "delete_with_data" {
@@ -210,20 +214,20 @@ pub fn TorrentTable() -> impl IntoView {
resp.status(),
resp.status_text()
);
toast_error(error_msg);
show_toast_with_signal(notifications, NotificationLevel::Error, error_msg);
} else {
logging::log!("Action {} executed successfully", action);
toast_success(success_msg);
show_toast_with_signal(notifications, NotificationLevel::Success, success_msg);
}
}
Err(e) => {
logging::error!("Network error executing action: {}", e);
toast_error(format!("{}: Bağlantı hatası", error_msg));
show_toast_with_signal(notifications, NotificationLevel::Error, format!("{}: Bağlantı hatası", error_msg));
}
},
Err(e) => {
logging::error!("Failed to serialize request: {}", e);
toast_error(error_msg);
show_toast_with_signal(notifications, NotificationLevel::Error, error_msg);
}
}
});