use leptos::*; use leptos::html::Dialog; use crate::store::{show_toast_with_signal, TorrentStore}; use crate::api; use shared::NotificationLevel; #[component] pub fn AddTorrentModal( #[prop(into)] on_close: Callback<()>, ) -> impl IntoView { let store = use_context::().expect("TorrentStore not provided"); let notifications = store.notifications; let dialog_ref = create_node_ref::(); let (uri, set_uri) = create_signal(String::new()); let (is_loading, set_loading) = create_signal(false); let (error_msg, set_error_msg) = create_signal(Option::::None); create_effect(move |_| { if let Some(dialog) = dialog_ref.get() { let _ = dialog.show_modal(); } }); let handle_submit = move |_| { let uri_val = uri.get(); if uri_val.is_empty() { 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; } set_loading.set(true); set_error_msg.set(None); let uri_val = uri_val; spawn_local(async move { match api::torrent::add(&uri_val).await { Ok(_) => { logging::log!("Torrent added successfully"); show_toast_with_signal(notifications, NotificationLevel::Success, "Torrent eklendi"); set_loading.set(false); if let Some(dialog) = dialog_ref.get() { dialog.close(); } on_close.call(()); } Err(e) => { logging::error!("Failed to add torrent: {:?}", e); show_toast_with_signal(notifications, NotificationLevel::Error, "Torrent eklenemedi"); set_error_msg.set(Some(format!("Error: {:?}", e))); set_loading.set(false); } } }); }; let handle_close = move |_| { if let Some(dialog) = dialog_ref.get() { dialog.close(); } on_close.call(()); }; view! { } }