sonda/web/src/pages/ArtistPage.tsx
tgiachi a93260af05 feat: favourites in the web app
A heart on every song, album and artist, in the player bar and the full-screen
player, and a Favourites page with the songs, albums and artists, newest first.
The heart changes at once and goes back if the server refuses. Hearts are only
for music of this server: a friend's song has none.

- FavoriteButton, useIsFavorite and useSetFavorite
- /favorites page, a Favourites item in the Library group of the menu, on the
  Library page and in the phone Library tab
2026-11-16 10:56:12 +01:00

110 lines
4.1 KiB
TypeScript

import { Play } from 'lucide-react'
import { useTranslation } from 'react-i18next'
import { useParams } from 'react-router-dom'
import { useAlbumList, useArtist, useTrackList } from '@/api/catalog'
import { ApiError } from '@/api/client'
import ArtistPhoto from '@/components/ArtistPhoto'
import FavoriteButton from '@/components/FavoriteButton'
import { AlbumCard, CARD_GRID } from '@/components/cards'
import LidarrButton from '@/components/LidarrButton'
import LoadMore from '@/components/LoadMore'
import { EmptyMessage, ErrorMessage, Loading } from '@/components/Status'
import TrackList from '@/components/TrackList'
import { Button } from '@/components/ui/button'
import { usePlayer } from '@/player/store'
import Section from '@/components/Section'
import { About, PhotoCredit, Similar, Tags } from './ArtistSections'
export default function ArtistPage() {
const { t } = useTranslation()
const id = Number(useParams().id)
if (!Number.isInteger(id) || id < 1) return <EmptyMessage>{t('notFound.artist')}</EmptyMessage>
return <ArtistView id={id} />
}
function ArtistView({ id }: { id: number }) {
const { t } = useTranslation()
const artist = useArtist(id)
const albums = useAlbumList({ artistId: id })
const tracks = useTrackList({ artistId: id })
const playTracks = usePlayer((s) => s.playTracks)
const songs = tracks.data?.items ?? []
if (artist.isError) {
if (artist.error instanceof ApiError && artist.error.status === 404) return <EmptyMessage>{t('notFound.artist')}</EmptyMessage>
return <ErrorMessage error={artist.error} onRetry={() => artist.refetch()} />
}
if (!artist.data) return <Loading />
const a = artist.data
const nothingHere = a.album_count === 0 && a.track_count === 0
return (
<div className="flex flex-col gap-10">
<header className="flex flex-wrap items-end gap-8">
<div className="flex flex-col gap-2">
<ArtistPhoto artistId={a.id} name={a.name} size={160} />
<div className="max-w-40">
<PhotoCredit artistId={a.id} />
</div>
</div>
<div className="flex min-w-0 flex-[1_1_300px] flex-col gap-3">
<h1 className="text-4xl font-extrabold tracking-tight md:text-6xl">{a.name}</h1>
<p className="text-muted-foreground">
{t('artist.albumCount', { count: a.album_count })} · {t('artist.songCount', { count: a.track_count })}
</p>
{!nothingHere && (
<div className="flex items-center gap-3">
<Button className="h-12 gap-2 rounded-md px-6" disabled={songs.length === 0} onClick={() => playTracks(songs, 0)}>
<Play />
{t('player.play')}
</Button>
<FavoriteButton kind="artists" id={a.id} className="size-12" />
</div>
)}
<LidarrButton artist={{ id: a.id, name: a.name }} check />
</div>
</header>
{nothingHere ? (
<EmptyMessage>{t('empty.nothingOfArtist')}</EmptyMessage>
) : (
<>
<Section title={t('artist.albums')}>
{albums.isError ? (
<ErrorMessage error={albums.error} onRetry={() => albums.refetch()} />
) : albums.isPending ? (
<Loading />
) : (
<>
<ul className={CARD_GRID}>
{(albums.data?.items ?? []).map((album) => (
<li key={album.id}>
<AlbumCard album={album} />
</li>
))}
</ul>
<LoadMore query={albums} />
</>
)}
</Section>
<Section title={t('artist.songs')}>
{tracks.isError ? (
<ErrorMessage error={tracks.error} onRetry={() => tracks.refetch()} />
) : tracks.isPending ? (
<Loading />
) : (
<>
<TrackList tracks={songs} />
<LoadMore query={tracks} />
</>
)}
</Section>
</>
)}
<About artistId={a.id} />
<Similar artistId={a.id} />
<Tags artistId={a.id} />
</div>
)
}