Files
LibreTunes/src/pages/artist.rs
Ethan Girouard 16bc79aef4
Some checks failed
Push Workflows / rustfmt (push) Successful in 4s
Push Workflows / docs (push) Successful in 29s
Push Workflows / clippy (push) Successful in 25s
Push Workflows / leptos-test (push) Successful in 54s
Push Workflows / test (push) Successful in 1m2s
Push Workflows / build (push) Successful in 1m52s
Push Workflows / docker-build (push) Failing after 8m18s
Push Workflows / nix-build (push) Successful in 13m53s
Fix redundant closures (Clippy)
2025-04-29 22:41:47 +00:00

190 lines
5.8 KiB
Rust

use leptos::either::*;
use leptos::prelude::*;
use leptos_icons::*;
use leptos_router::hooks::use_params_map;
use server_fn::error::NoCustomError;
use crate::models::backend::Artist;
use crate::components::error::*;
use crate::components::loading::*;
use crate::components::song_list::*;
use crate::api::artists::*;
#[component]
pub fn ArtistPage() -> impl IntoView {
let params = use_params_map();
view! {
{move || params.with(|params| {
match params.get("id").map(|id| id.parse::<i32>()) {
Some(Ok(id)) => {
Either::Left(view! { <ArtistIdProfile id /> })
},
Some(Err(e)) => {
Either::Right(view! {
<Error<String>
title="Invalid Artist ID"
error=e.to_string()
/>
})
},
None => {
Either::Right(view! {
<Error<String>
title="No Artist ID"
message="You must specify an artist ID to view their page."
/>
})
}
}
})}
}
}
#[component]
fn ArtistIdProfile(#[prop(into)] id: Signal<i32>) -> impl IntoView {
let artist_info = Resource::new(move || id.get(), get_artist_by_id);
let show_details = RwSignal::new(false);
view! {
<Transition
fallback=move || view! { <LoadingPage /> }
>
{move || artist_info.get().map(|artist| {
match artist {
Ok(Some(artist)) => {
show_details.set(true);
EitherOf3::A(view! { <ArtistProfile artist /> })
},
Ok(None) => EitherOf3::B(view! {
<Error<String>
title="Artist Not Found"
message=format!("Artist with ID {} not found", id.get())
/>
}),
Err(error) => EitherOf3::C(view! {
<ServerError<NoCustomError>
title="Error Getting Artist"
error
/>
}),
}
})}
</Transition>
<div hidden={move || !show_details.get()}>
<TopSongsByArtist artist_id=id />
<AlbumsByArtist artist_id=id />
</div>
}
}
#[component]
fn ArtistProfile(artist: Artist) -> impl IntoView {
let artist_id = artist.id.unwrap();
let profile_image_path = format!("/assets/images/artist/{artist_id}.webp");
leptos::logging::log!("Artist name: {}", artist.name);
view! {
<div class="flex">
<object class="w-35 h-35 rounded-full p-5" data={profile_image_path.clone()} type="image/webp">
<Icon icon={icondata::CgProfile} width="100" height="100" {..} class="artist-image" />
</object>
<h1 class="text-4xl self-center">{artist.name}</h1>
</div>
}
}
#[component]
fn TopSongsByArtist(#[prop(into)] artist_id: Signal<i32>) -> impl IntoView {
let top_songs = Resource::new(
move || artist_id.get(),
|artist_id| async move {
let top_songs = top_songs_by_artist(artist_id, Some(10)).await;
top_songs.map(|top_songs| {
top_songs
.into_iter()
.map(|(song, plays)| {
let plays = if plays == 1 {
"1 play".to_string()
} else {
format!("{plays} plays")
};
(song, plays)
})
.collect::<Vec<_>>()
})
},
);
view! {
<h2 class="text-xl font-bold">"Top Songs"</h2>
<Transition
fallback=move || view! { <Loading /> }
>
<ErrorBoundary
fallback=|errors| view! {
{move || errors.get()
.into_iter()
.map(|(_, e)| view! { <p>{e.to_string()}</p>})
.collect_view()
}
}
>
{move || top_songs.get().map(|top_songs| {
top_songs.map(|top_songs| {
view! { <SongListExtra songs=top_songs /> }
})
})}
</ErrorBoundary>
</Transition>
}
}
#[component]
fn AlbumsByArtist(#[prop(into)] artist_id: Signal<i32>) -> impl IntoView {
use crate::components::dashboard_row::*;
let albums = Resource::new(
move || artist_id.get(),
|artist_id| async move {
let albums = albums_by_artist(artist_id, None).await;
albums.map(|albums| albums.into_iter().collect::<Vec<_>>())
},
);
view! {
<Transition
fallback=move || view! { <Loading /> }
>
<ErrorBoundary
fallback=|errors| view! {
{move || errors.get()
.into_iter()
.map(|(_, e)| view! { <p>{e.to_string()}</p>})
.collect_view()
}
}
>
{move || albums.get().map(|albums| {
albums.map(|albums| {
let tiles = albums.into_iter().map(|album| {
album.into()
}).collect::<Vec<_>>();
view! {
<DashboardRow title="Albums" tiles />
}
})
})}
</ErrorBoundary>
</Transition>
}
}