feat(web): a Show photo button on the cards of Discover

A suggested artist you do not have shows a letter, because a photo is a lookup at the providers. The card now has a button that asks for the photo of that one artist when it is pressed, says what it sends, and says so when there is no free photo.

Closes #132
This commit is contained in:
tgiachi 2026-11-13 13:01:57 +01:00
commit 3ade5e80d5
5 changed files with 63 additions and 4 deletions

View file

@ -16,11 +16,14 @@ export default function ArtistPhoto({
name,
size,
photo = true,
onMissing,
}: {
artistId: number
name: string
size: number
photo?: boolean
/** Called when the picture could not be had (there is none, or the provider is off). */
onMissing?: () => void
}) {
useAccessToken() // the URL carries the token, so it has to follow a renewal
const src = mediaUrl(`/artists/${artistId}/image`)
@ -37,7 +40,10 @@ export default function ArtistPhoto({
alt={name}
loading="lazy"
className="size-full object-cover"
onError={() => setFailed(src)}
onError={() => {
setFailed(src)
onMissing?.()
}}
/>
) : (
<span data-placeholder aria-hidden="true" className="font-extrabold" style={{ fontSize: size * 0.4 }}>

View file

@ -407,6 +407,12 @@
"inLibrary": "In your library",
"why": "Why this?",
"wishlist": "Wishlist",
"photo": {
"show": "Show photo of {{name}}",
"button": "Show photo",
"hint": "Asks Wikimedia Commons for a free photo of this artist, using its MusicBrainz id. Needs the Commons and MusicBrainz providers to be on.",
"none": "No free photo found."
},
"notInterested": "Not interested",
"invite": "Pick an artist to start from, or let Discover start from your listening.",
"waiting": "Asking Last.fm can take several seconds.",

View file

@ -407,6 +407,12 @@
"inLibrary": "Nella tua libreria",
"why": "Perché questo?",
"wishlist": "Wishlist",
"photo": {
"show": "Mostra la foto di {{name}}",
"button": "Mostra foto",
"hint": "Chiede a Wikimedia Commons una foto libera di questo artista, usando il suo id MusicBrainz. Servono i provider Commons e MusicBrainz accesi.",
"none": "Nessuna foto libera trovata."
},
"notInterested": "Non mi interessa",
"invite": "Scegli un artista da cui partire, oppure lascia che Discover parta dai tuoi ascolti.",
"waiting": "Interrogare Last.fm può richiedere diversi secondi.",

View file

@ -1,5 +1,5 @@
import { QueryClient, QueryClientProvider } from '@tanstack/react-query'
import { render, screen, within } from '@testing-library/react'
import { fireEvent, render, screen, within } from '@testing-library/react'
import userEvent from '@testing-library/user-event'
import { MemoryRouter } from 'react-router-dom'
import { beforeEach, expect, test, vi } from 'vitest'
@ -56,6 +56,29 @@ test('an artist you do not have gets no photo request: looking at a list must no
expect(screen.getByText('P', { selector: '[data-placeholder]' })).toBeInTheDocument()
})
test('a button asks for the photo of one artist you do not have, and the photo comes only after it is pressed', async () => {
renderCard(make({ in_library: false }))
const button = screen.getByRole('button', { name: 'Show photo of Portishead' })
expect(button).toHaveAttribute('title', expect.stringContaining('Wikimedia Commons'))
expect(screen.queryByRole('img')).not.toBeInTheDocument()
await userEvent.click(button)
expect(screen.getByRole('img', { name: 'Portishead' })).toHaveAttribute('src', expect.stringContaining('/artists/7/image'))
expect(screen.queryByRole('button', { name: /Show photo/ })).not.toBeInTheDocument()
})
test('when the artist has no free photo the card says so and keeps the letter', async () => {
renderCard(make({ in_library: false }))
await userEvent.click(screen.getByRole('button', { name: 'Show photo of Portishead' }))
fireEvent.error(screen.getByRole('img', { name: 'Portishead' }))
expect(screen.getByText('No free photo found.')).toBeInTheDocument()
expect(screen.getByText('P', { selector: '[data-placeholder]' })).toBeInTheDocument()
})
test('an artist you have has no such button: its photo is already shown', () => {
renderCard(make({ in_library: true }))
expect(screen.queryByRole('button', { name: /Show photo/ })).not.toBeInTheDocument()
})
test('an artist you have shows its photo', () => {
renderCard(make({ in_library: true }))
expect(screen.getByRole('img', { name: 'Portishead' })).toBeInTheDocument()

View file

@ -1,3 +1,4 @@
import { useState } from 'react'
import { useTranslation } from 'react-i18next'
import { Link } from 'react-router-dom'
import { ApiError } from '@/api/client'
@ -23,13 +24,18 @@ function Part({ label, value, missing }: { label: string; value: number | null;
export default function SuggestionCard({ suggestion }: { suggestion: Suggestion }) {
const { t } = useTranslation()
const marks = useArtistMarks()
// A photo of an artist you do not have is looked up only when asked, one artist at a time.
const [asked, setAsked] = useState(false)
const [noPhoto, setNoPhoto] = useState(false)
const { artist, components: parts } = suggestion
const failure = marks.addToWishlist.error ?? marks.removeFromWishlist.error ?? marks.dismiss.error
return (
<li className="flex flex-col gap-3 rounded-lg bg-card p-4 text-card-foreground">
<div className="flex flex-wrap items-center gap-4">
<ArtistPhoto artistId={artist.id} name={artist.name} size={56} photo={suggestion.in_library} />
<ArtistPhoto artistId={artist.id} name={artist.name} size={56} photo={suggestion.in_library || asked}
onMissing={() => setNoPhoto(true)}
/>
<div className="flex min-w-40 flex-1 flex-col">
<Link to={`/artists/${artist.id}`} className="text-lg font-bold break-words hover:underline">
{artist.name}
@ -38,6 +44,18 @@ export default function SuggestionCard({ suggestion }: { suggestion: Suggestion
<span>{t('discover.score', { percent: Math.round(suggestion.score * 100) })}</span>
{suggestion.in_library && <span>{t('discover.inLibrary')}</span>}
</div>
{!suggestion.in_library && !asked && (
<Button
variant="ghost"
className="-ml-2 self-start px-2 text-sm text-primary"
aria-label={t('discover.photo.show', { name: artist.name })}
title={t('discover.photo.hint')}
onClick={() => setAsked(true)}
>
{t('discover.photo.button')}
</Button>
)}
{noPhoto && <span className="text-sm text-muted-foreground">{t('discover.photo.none')}</span>}
{suggestion.because.length > 0 && (
<p className="text-sm break-words text-muted-foreground">
{t('discover.because', { names: suggestion.because.map((b) => b.artist.name).join(', ') })}