//! Share creation dialog and the "Your shares" management panel (milestone 6). use leptos::prelude::*; use wasm_bindgen::JsCast; use wasm_bindgen_futures::spawn_local; use crate::api::{self, ShareInfo}; use crate::components::icon::Icon; use crate::components::modal::Modal; use crate::components::toast::{ToastMsg, show, show_error}; use crate::i18n; use crate::icons::IconName; use crate::router::{dav_share_url, share_url}; use crate::util::format_date; use crate::util::{select_input, select_value}; // --------------------------------------------------------------------------- // Expiry helpers (compute the RFC 3339 expiry client-side) // --------------------------------------------------------------------------- /// Format a millisecond timestamp as an RFC 3339 UTC string (ISO 8601). fn ms_to_rfc3339(ms: f64) -> Option { if ms.is_nan() { return None; } Some( js_sys::Date::new(&wasm_bindgen::JsValue::from_f64(ms)) .to_iso_string() .into(), ) } /// Convert the user's expiry choice into an RFC 3339 timestamp, or `None` for /// "never". Presets are computed from "now"; "custom" parses the /// datetime-local value as the browser's local time. fn expiry_to_rfc3339(choice: &str, custom: &str) -> Option { let now = js_sys::Date::now(); let h = 60.0 * 60.0 * 1000.0; let target_ms: Option = match choice { "never" => return None, "1h" => Some(now + h), "1d" => Some(now + 24.0 * h), "1w" => Some(now + 7.0 * 24.0 * h), "1mo" => Some(now + 30.0 * 24.0 * h), "custom" => { if custom.is_empty() { None } else { // `Date.parse` treats a bare datetime as local time, so the // result is already the correct UTC instant. Some(js_sys::Date::parse(custom)) } } _ => return None, }; target_ms.and_then(ms_to_rfc3339) } fn copy_to_clipboard(text: &str, toast: ToastMsg) { let Some(win) = web_sys::window() else { show_error(toast, i18n::t(i18n::k::COPY_MANUAL).to_string()); return; }; let cb = win.navigator().clipboard(); let t = text.to_string(); spawn_local(async move { let _ = wasm_bindgen_futures::JsFuture::from(cb.write_text(&t)).await; show(toast, i18n::t(i18n::k::LINK_COPIED).to_string()); }); } // --------------------------------------------------------------------------- // Share creation dialog (two-phase: form -> link) // --------------------------------------------------------------------------- #[component] pub fn ShareDialog( name: String, root_id: i64, path: String, allow_writable: bool, root_writable: bool, close: Callback<()>, ) -> impl IntoView { let toast = use_context::().expect("toast context"); let (writable, set_writable) = signal(false); let (choice, set_choice) = signal("never".to_string()); let (custom, set_custom) = signal(String::new()); let (password, set_password) = signal(String::new()); let (created, set_created) = signal(Option::::None); let (busy, set_busy) = signal(false); let name_msg = name.clone(); let do_create = move |_| { if busy.get() { return; } set_busy.set(true); let exp = expiry_to_rfc3339(&choice.get(), &custom.get()); let wr = writable.get(); let rid = root_id; let p = path.clone(); // An empty field means "no password", not a password of "". let pw = password.get().trim().to_string(); let pw = (!pw.is_empty()).then_some(pw); spawn_local(async move { match api::create_share(rid, &p, wr, exp.as_deref(), pw.as_deref()).await { Ok(info) => set_created.set(Some(info)), Err(e) => { show( toast, i18n::t_fmt(i18n::k::SHARE_CREATE_ERR, &e.to_string()), ); set_busy.set(false); } } }); }; view! { } } // --------------------------------------------------------------------------- // "Your shares" manage view // --------------------------------------------------------------------------- #[component] pub fn SharesView() -> impl IntoView { let toast = use_context::().expect("toast context"); let (shares, set_shares) = signal(Option::>::None); // Set when the list could not be loaded; the error card offers a retry. let (load_err, set_load_err) = signal(Option::::None); let (confirm_all, set_confirm_all) = signal(false); let (busy_all, set_busy_all) = signal(false); let load = move || { set_load_err.set(None); spawn_local(async move { match api::list_shares().await { Ok(s) => set_shares.set(Some(s)), Err(e) => set_load_err.set(Some(e.to_string())), } }); }; load(); // Delete every share of the current user (no server-side bulk endpoint). let delete_all = move |_| { set_confirm_all.set(false); let Some(list) = shares.get() else { return; }; if list.is_empty() { return; } let ids: Vec = list.iter().map(|s| s.id).collect(); set_busy_all.set(true); let write = set_shares; let toast2 = toast; spawn_local(async move { let mut failed = 0usize; for id in &ids { if api::delete_share(*id).await.is_err() { failed += 1; } } if failed == 0 { write.set(Some(Vec::new())); show( toast2, i18n::t(i18n::k::DELETED_N_SHARES) .to_string() .replace("{}", &ids.len().to_string()), ); } else if failed < ids.len() { // Partial failure: re-read what is actually left. if let Ok(s) = api::list_shares().await { write.set(Some(s)); } show( toast2, i18n::t(i18n::k::DELETE_PARTIAL) .replace("{}", &failed.to_string()) .replace("{}", &ids.len().to_string()), ); } else { show_error(toast2, i18n::t(i18n::k::SHARES_DELETE_ERR).to_string()); } set_busy_all.set(false); }); }; view! {

{i18n::tr(i18n::k::YOUR_SHARES)}

{move || { let n = shares.get().map(|s| s.len()).unwrap_or(0); if n > 0 { view! { } .into_view() .into_any() } else { view! {}.into_any() } }}

{i18n::tr(i18n::k::SHARES_VIEW_HINT)}

{move || match shares.get() { None => match load_err.get() { Some(msg) => view! {
{msg}
} .into_view() .into_any(), None => view! {

{i18n::tr(i18n::k::LOADING)}

} .into_view() .into_any(), }, Some(ref list) if list.is_empty() => view! {

{i18n::tr(i18n::k::NO_SHARES)}

{i18n::tr(i18n::k::SHARES_EMPTY_HINT)}

} .into_view() .into_any(), Some(ref list) => { let items = list .iter() .map(|s| { let s2 = s.clone(); let link = share_url(&s.token); let dav_link = dav_share_url(&s.token); let is_file = s.is_file; let glyph = if s.is_file { IconName::File } else { IconName::Folder }; let mut meta = format!( "{} · {}", s.target, if s.writable { i18n::t(i18n::k::MODE_RW) } else { i18n::t(i18n::k::MODE_RO) } ); if let Some(e) = &s.expires_at { meta.push_str(&format!( " · {}", i18n::t_fmt(i18n::k::META_EXPIRES, &format_date(e)) )); } meta.push_str(&format!( " · {}", i18n::t_fmt(i18n::k::META_CREATED, &format_date(&s.created_at)) )); if s.has_password { meta.push_str(&format!(" · {}", i18n::t(i18n::k::SHARE_PROTECTED))); } view! { } }) .collect::>(); view! { } .into_view() .into_any() } }} // Delete-all confirmation. {move || { if confirm_all.get() { let n = shares.get().map(|s| s.len()).unwrap_or(0); view! { } .into_view() .into_any() } else { view! {}.into_any() } }}
} }