diff --git a/crates/qbz-slint/src/artist.rs b/crates/qbz-slint/src/artist.rs index a130fff69..518bb37ac 100644 --- a/crates/qbz-slint/src/artist.rs +++ b/crates/qbz-slint/src/artist.rs @@ -19,7 +19,7 @@ use crate::artwork::{ArtworkJob, ArtworkTarget}; use crate::home::CardData; use crate::{ AlbumCardItem, AlbumTrackItem, AppWindow, ArtistState, DiscoverSection, LabelEntry, - MbOriginData, NetworkSidebarState, SimilarEntry, + MbOriginData, MbRelationship, MbRelationshipsData, NetworkSidebarState, SimilarEntry, }; /// Plain, `Send` artist data produced on the worker thread. @@ -671,6 +671,174 @@ pub fn apply_mb_unavailable(window: &AppWindow) { state.set_discovery_loading(false); } +// ----- MB relationships ------------------------------------------------- + +/// Plain, `Send` mapped relationships ready to push into Slint. Members +/// here are the still-active ones (ended members already moved to +/// past_members on the qbz-core side, and Tauri's sidebar renders only +/// members — see groupedMembers in ArtistDetailView). +pub struct MbRelationshipsRowData { + pub members: Vec, + pub groups: Vec, + pub collaborators: Vec, + pub has_data: bool, +} + +pub struct MbRelationshipRow { + pub mbid: String, + pub name: String, + /// Primary role for the musician-click callback. Defaults to "Band + /// Member" / "Band" / "Collaborator" by section when MB has no + /// attributes for the relation. + pub role: String, + /// Tooltip — roles joined with ", " plus the period in parens when + /// present. Falls back to the period string or the name. + pub tooltip: String, +} + +/// Fetch MB relationships for `mbid` and map into the Slint-friendly +/// row shape. Groups members by mbid combining their roles, mirroring +/// Tauri's `groupMembersByMbid` plus the per-section role defaults. +pub async fn load_mb_relationships( + runtime: &Arc>, + mbid: &str, +) -> Result +where + A: FrontendAdapter + Send + Sync + 'static, +{ + let relations = runtime + .core() + .musicbrainz_get_artist_relationships(mbid) + .await + .map_err(|e| e.to_string())?; + Ok(map_relationships(relations)) +} + +fn map_relationships( + rels: qbz_integrations::musicbrainz::ArtistRelationships, +) -> MbRelationshipsRowData { + let members = group_relations(rels.members, "Band Member"); + let groups = group_relations(rels.groups, "Band"); + let collaborators = group_relations(rels.collaborators, "Collaborator"); + let has_data = + !members.is_empty() || !groups.is_empty() || !collaborators.is_empty(); + MbRelationshipsRowData { + members, + groups, + collaborators, + has_data, + } +} + +fn group_relations( + rels: Vec, + default_role: &str, +) -> Vec { + use std::collections::HashMap; + struct Pending { + name: String, + roles: Vec, + begin: Option, + end: Option, + } + let mut by_mbid: HashMap = HashMap::new(); + let mut order: Vec = Vec::new(); + for r in rels { + let begin = r.period.as_ref().and_then(|p| p.begin.clone()); + let end = r.period.as_ref().and_then(|p| p.end.clone()); + match by_mbid.get_mut(&r.mbid) { + Some(existing) => { + if let Some(role) = r.role.clone() { + if !existing.roles.iter().any(|rr| rr == &role) { + existing.roles.push(role); + } + } + } + None => { + order.push(r.mbid.clone()); + let mut roles = Vec::new(); + if let Some(role) = r.role.clone() { + roles.push(role); + } + by_mbid.insert( + r.mbid.clone(), + Pending { + name: r.name, + roles, + begin, + end, + }, + ); + } + } + } + order + .into_iter() + .filter_map(|mbid| by_mbid.remove(&mbid).map(|p| (mbid, p))) + .map(|(mbid, p)| { + let period = format_period(p.begin.as_deref(), p.end.as_deref()); + let tooltip = if !p.roles.is_empty() { + let roles_joined = p.roles.join(", "); + if period.is_empty() { + roles_joined + } else { + format!("{} ({})", roles_joined, period) + } + } else if !period.is_empty() { + period.clone() + } else { + p.name.clone() + }; + let role = p + .roles + .first() + .cloned() + .unwrap_or_else(|| default_role.to_string()); + MbRelationshipRow { + mbid, + name: p.name, + role, + tooltip, + } + }) + .collect() +} + +fn format_period(begin: Option<&str>, end: Option<&str>) -> String { + if begin.is_some() || end.is_some() { + let b = begin.unwrap_or("?"); + let e = end.unwrap_or("present"); + format!("{} - {}", b, e) + } else { + String::new() + } +} + +/// Apply MB relationships to NetworkSidebarState. Runs on the Slint +/// event loop. +pub fn apply_mb_relationships(window: &AppWindow, data: MbRelationshipsRowData) { + let to_slint = |rows: Vec| -> ModelRc { + ModelRc::new(VecModel::from( + rows.into_iter() + .map(|r| MbRelationship { + mbid: r.mbid.into(), + name: r.name.into(), + role: r.role.into(), + tooltip: r.tooltip.into(), + }) + .collect::>(), + )) + }; + let state = window.global::(); + state.set_relationships(MbRelationshipsData { + members: to_slint(data.members), + groups: to_slint(data.groups), + collaborators: to_slint(data.collaborators), + has_data: data.has_data, + }); + state.set_relationships_loading(false); +} + /// Format a MusicBrainz partial date into a short human string — /// "1990", "May 1990", or "May 14, 1990" — matching Tauri's /// formatMbDate_v2 output when the locale is en-US. diff --git a/crates/qbz-slint/src/main.rs b/crates/qbz-slint/src/main.rs index 1731cff08..915ee82e0 100644 --- a/crates/qbz-slint/src/main.rs +++ b/crates/qbz-slint/src/main.rs @@ -238,19 +238,39 @@ fn navigate_artist( artwork::spawn_loads(jobs, weak.clone(), image_cache.clone()); // Network sidebar — kick the MB enrichment off in - // parallel with artwork. Origin section shows a loading - // state until the MB resolve + metadata calls return. + // parallel with artwork. Origin shows a loading state + // until the resolve + metadata calls return; on success + // the resolved mbid is used to fetch relationships in a + // second round-trip (the V2 cache, when wired, will + // make this single-shot for repeat visits). let runtime_mb = runtime.clone(); let weak_mb = weak.clone(); tokio::spawn(async move { let _ = weak_mb.upgrade_in_event_loop(|w| { - w.global::().set_origin_loading(true); + let state = w.global::(); + state.set_origin_loading(true); + state.set_relationships_loading(true); }); match artist::load_mb_metadata(&runtime_mb, &artist_name).await { Ok(Some(meta)) => { + let mbid = meta.mbid.clone(); let _ = weak_mb.upgrade_in_event_loop(move |w| { artist::apply_mb_metadata(&w, meta); }); + match artist::load_mb_relationships(&runtime_mb, &mbid).await { + Ok(data) => { + let _ = weak_mb.upgrade_in_event_loop(move |w| { + artist::apply_mb_relationships(&w, data); + }); + } + Err(e) => { + log::warn!("[qbz-slint] MB relationships failed: {e}"); + let _ = weak_mb.upgrade_in_event_loop(|w| { + w.global::() + .set_relationships_loading(false); + }); + } + } } Ok(None) => { let _ = weak_mb.upgrade_in_event_loop(|w| { diff --git a/crates/qbz-slint/ui/artist/ArtistPageView.slint b/crates/qbz-slint/ui/artist/ArtistPageView.slint index 6f27d2f48..961e67d1f 100644 --- a/crates/qbz-slint/ui/artist/ArtistPageView.slint +++ b/crates/qbz-slint/ui/artist/ArtistPageView.slint @@ -887,6 +887,100 @@ export component ArtistPageView inherits Rectangle { } } } + + // --- Relationships (MB) -------------------------- + // Section is gated by mb-available so a + // disabled MB or unmatched artist hides it. + if NetworkSidebarState.mb-available + && (NetworkSidebarState.relationships-loading + || NetworkSidebarState.relationships.has-data): + VerticalLayout { + padding-left: 14px; + padding-right: 14px; + padding-top: 12px; + padding-bottom: 6px; + spacing: 6px; + + Text { + text: @tr("RELATIONSHIPS"); + color: Theme.text-muted; + font-size: 11px; + font-weight: Typography.semibold; + letter-spacing: 0.5px; + } + + if NetworkSidebarState.relationships-loading: Text { + text: @tr("Loading…"); + color: Theme.text-muted; + font-size: 12px; + } + + if !NetworkSidebarState.relationships-loading + && NetworkSidebarState.relationships.members.length > 0: + VerticalLayout { + spacing: 2px; + padding-top: 2px; + Text { + text: @tr("Members & Former"); + color: Theme.text-muted; + font-size: 11px; + } + for member in NetworkSidebarState.relationships.members: + SidebarLink { + label: member.name; + icon: @image-url("../assets/icons/user.svg"); + tooltip: member.tooltip; + clicked => { + NetworkSidebarActions.musician-clicked( + member.name, member.role); + } + } + } + + if !NetworkSidebarState.relationships-loading + && NetworkSidebarState.relationships.groups.length > 0: + VerticalLayout { + spacing: 2px; + padding-top: 2px; + Text { + text: @tr("Member Of"); + color: Theme.text-muted; + font-size: 11px; + } + for group in NetworkSidebarState.relationships.groups: + SidebarLink { + label: group.name; + icon: @image-url("../assets/icons/music.svg"); + tooltip: group.tooltip; + clicked => { + NetworkSidebarActions.musician-clicked( + group.name, group.role); + } + } + } + + if !NetworkSidebarState.relationships-loading + && NetworkSidebarState.relationships.collaborators.length > 0: + VerticalLayout { + spacing: 2px; + padding-top: 2px; + Text { + text: @tr("Collaborators"); + color: Theme.text-muted; + font-size: 11px; + } + for collab in NetworkSidebarState.relationships.collaborators: + SidebarLink { + label: collab.name; + icon: @image-url("../assets/icons/user.svg"); + tooltip: collab.tooltip; + clicked => { + NetworkSidebarActions.musician-clicked( + collab.name, collab.role); + } + } + } + } } } }