diff --git a/frontend/src/api.rs b/frontend/src/api.rs index 77f59be..195726e 100644 --- a/frontend/src/api.rs +++ b/frontend/src/api.rs @@ -176,6 +176,11 @@ pub async fn add_album( post_json(&format!("{BASE}/albums"), &body).await } +pub async fn watch_track(title: &str, mbid: &str) -> Result { + let body = serde_json::json!({"title": title, "mbid": mbid}).to_string(); + post_json(&format!("{BASE}/tracks/watch"), &body).await +} + // --- Downloads --- pub async fn get_downloads(status: Option<&str>) -> Result, ApiError> { let mut url = format!("{BASE}/downloads/queue"); diff --git a/frontend/src/pages/album.rs b/frontend/src/pages/album.rs index a3e9255..b98e1cc 100644 --- a/frontend/src/pages/album.rs +++ b/frontend/src/pages/album.rs @@ -80,7 +80,7 @@ pub fn album_page(props: &Props) -> Html { - { for d.tracks.iter().map(|t| { + { for d.tracks.iter().enumerate().map(|(idx, t)| { let duration = t.duration_ms .map(&fmt_duration) .unwrap_or_default(); @@ -88,6 +88,7 @@ pub fn album_page(props: &Props) -> Html { let track_key = t.recording_mbid.clone(); let is_active = active_lyrics.as_ref() == Some(&track_key); let cached = lyrics_cache.get(&track_key).cloned(); + let has_status = t.status.is_some(); let on_lyrics_click = { let active = active_lyrics.clone(); @@ -117,6 +118,29 @@ pub fn album_page(props: &Props) -> Html { }) }; + let on_watch_click = { + let detail = detail.clone(); + let title = t.title.clone(); + let mbid = t.recording_mbid.clone(); + Callback::from(move |_: MouseEvent| { + let detail = detail.clone(); + let title = title.clone(); + let mbid = mbid.clone(); + let idx = idx; + wasm_bindgen_futures::spawn_local(async move { + if let Ok(resp) = api::watch_track(&title, &mbid).await { + if let Some(ref d) = *detail { + let mut updated = d.clone(); + if let Some(track) = updated.tracks.get_mut(idx) { + track.status = Some(resp.status); + } + detail.set(Some(updated)); + } + } + }); + }) + }; + html! { <> @@ -130,7 +154,13 @@ pub fn album_page(props: &Props) -> Html { { "\u{2014}" } } - + + if !has_status { + + }