use yew::prelude::*; use yew_router::prelude::*; use crate::api; use crate::pages::Route; use crate::types::ArtistListItem; #[function_component(LibraryPage)] pub fn library_page() -> Html { let artists = use_state(|| None::>); let error = use_state(|| None::); let fetch_artists = { let artists = artists.clone(); let error = error.clone(); Callback::from(move |_: ()| { let artists = artists.clone(); let error = error.clone(); wasm_bindgen_futures::spawn_local(async move { match api::list_artists(200, 0).await { Ok(a) => artists.set(Some(a)), Err(e) => error.set(Some(e.0)), } }); }) }; { let fetch = fetch_artists.clone(); use_effect_with((), move |_| { fetch.emit(()); }); } if let Some(ref err) = *error { return html! {
{ format!("Error: {err}") }
}; } let Some(ref artists) = *artists else { return html! {

{ "Loading..." }

}; }; html! {
if artists.is_empty() {

{ "No artists in library. Use Search to add some!" }

} else { { for artists.iter().map(|a| { let artist_id = a.id; let error = error.clone(); let fetch = fetch_artists.clone(); let on_remove = Callback::from(move |_: MouseEvent| { let error = error.clone(); let fetch = fetch.clone(); wasm_bindgen_futures::spawn_local(async move { match api::delete_artist(artist_id).await { Ok(_) => fetch.emit(()), Err(e) => error.set(Some(e.0)), } }); }); html! { if a.enriched { } else { } } })}
{ "Name" } { "Monitored" } { "Owned" } { "Watched" } { "Tracks" }
to={Route::Artist { id: a.id.to_string() }}> { &a.name } > if a.monitored { { "\u{2713}" } } = a.total_watched && a.total_watched > 0 { "color: var(--success);" } else if a.total_owned > 0 { "color: var(--warning);" } else { "color: var(--text-muted);" } }> { format!("{}/{}", a.total_owned, a.total_watched) } 0 { "color: var(--accent);" } else { "color: var(--text-muted);" } }> { format!("{}/{}", a.total_watched, a.total_items) } { "Awaiting artist enrichment..." } if a.enriched && a.total_items > 0 { { a.total_items } }
}
} }