feat(web): a card for a Discover suggestion with its score explained
Shows the score in percent, the seeds it comes from, a part-by-part explanation (a missing genre is not a zero), and the wishlist and not-interested actions.
This commit is contained in:
parent
3cd0ef7150
commit
71b0b39a07
4 changed files with 215 additions and 0 deletions
|
|
@ -60,6 +60,20 @@
|
|||
},
|
||||
"discover": {
|
||||
"title": "Discover",
|
||||
"score": "{{percent}}% match",
|
||||
"because": "Because of {{names}}",
|
||||
"inLibrary": "In your library",
|
||||
"why": "Why this?",
|
||||
"wishlist": "Wishlist",
|
||||
"notInterested": "Not interested",
|
||||
"parts": {
|
||||
"label": "How the score is made",
|
||||
"a": "Similar to your starting artists",
|
||||
"g": "Genres close to your taste",
|
||||
"p": "What you play",
|
||||
"d": "Not played yet",
|
||||
"noGenre": "No genre data"
|
||||
},
|
||||
"listening": {
|
||||
"label": "Start Discover from my listening",
|
||||
"help": "When this is on, the names of the artists you play most go to Last.fm, if an admin turned Last.fm on, to find similar artists. When it is off, Discover starts only from artists you pick.",
|
||||
|
|
|
|||
|
|
@ -60,6 +60,20 @@
|
|||
},
|
||||
"discover": {
|
||||
"title": "Discover",
|
||||
"score": "{{percent}}% di affinità",
|
||||
"because": "Per via di {{names}}",
|
||||
"inLibrary": "Nella tua libreria",
|
||||
"why": "Perché questo?",
|
||||
"wishlist": "Wishlist",
|
||||
"notInterested": "Non mi interessa",
|
||||
"parts": {
|
||||
"label": "Come si calcola il punteggio",
|
||||
"a": "Simile agli artisti di partenza",
|
||||
"g": "Generi vicini ai tuoi gusti",
|
||||
"p": "Quello che ascolti",
|
||||
"d": "Non ancora ascoltato",
|
||||
"noGenre": "Nessun dato sui generi"
|
||||
},
|
||||
"listening": {
|
||||
"label": "Avvia Discover dai miei ascolti",
|
||||
"help": "Se è acceso, i nomi degli artisti che ascolti di più vanno a Last.fm, se un admin l'ha acceso, per trovare artisti simili. Se è spento, Discover parte solo dagli artisti che scegli tu.",
|
||||
|
|
|
|||
110
web/src/pages/discover/SuggestionCard.test.tsx
Normal file
110
web/src/pages/discover/SuggestionCard.test.tsx
Normal file
|
|
@ -0,0 +1,110 @@
|
|||
import { QueryClient, QueryClientProvider } from '@tanstack/react-query'
|
||||
import { 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'
|
||||
import type { Suggestion } from '@/api/discover'
|
||||
import { sessionStore } from '@/auth/session'
|
||||
import { initI18n } from '@/i18n'
|
||||
import { apiError, mockFetch } from '@/test/mockFetch'
|
||||
import SuggestionCard from './SuggestionCard'
|
||||
|
||||
const none = () => new Response(null, { status: 204 })
|
||||
|
||||
function make(over: Partial<Suggestion> = {}): Suggestion {
|
||||
return {
|
||||
artist: { id: 7, name: 'Portishead' },
|
||||
in_library: false,
|
||||
wishlisted: false,
|
||||
score: 0.824,
|
||||
components: { a: 0.6, g: 0.45, p: 0.5, d: 1 },
|
||||
because: [
|
||||
{ artist: { id: 1, name: 'Björk' }, match: 0.9 },
|
||||
{ artist: { id: 2, name: 'Massive Attack' }, match: 0.7 },
|
||||
],
|
||||
...over,
|
||||
}
|
||||
}
|
||||
|
||||
function renderCard(suggestion: Suggestion) {
|
||||
const client = new QueryClient({ defaultOptions: { queries: { retry: false }, mutations: { retry: false } } })
|
||||
return render(
|
||||
<QueryClientProvider client={client}>
|
||||
<MemoryRouter>
|
||||
<SuggestionCard suggestion={suggestion} />
|
||||
</MemoryRouter>
|
||||
</QueryClientProvider>,
|
||||
)
|
||||
}
|
||||
|
||||
beforeEach(async () => {
|
||||
vi.spyOn(navigator, 'language', 'get').mockReturnValue('en-US')
|
||||
await initI18n()
|
||||
sessionStore.setState({ status: 'authenticated', user: { id: 1, username: 'tom', is_admin: false } })
|
||||
})
|
||||
|
||||
test('it names the artist with a link, the score in percent, and what it comes from', () => {
|
||||
renderCard(make())
|
||||
expect(screen.getByRole('link', { name: 'Portishead' })).toHaveAttribute('href', '/artists/7')
|
||||
expect(screen.getByText('82% match')).toBeInTheDocument()
|
||||
expect(screen.getByText('Because of Björk, Massive Attack')).toBeInTheDocument()
|
||||
})
|
||||
|
||||
test('an artist with no reason to give says nothing about one', () => {
|
||||
renderCard(make({ because: [] }))
|
||||
expect(screen.queryByText(/Because of/)).not.toBeInTheDocument()
|
||||
})
|
||||
|
||||
test('an artist you already have says so', () => {
|
||||
renderCard(make({ in_library: true }))
|
||||
expect(screen.getByText('In your library')).toBeInTheDocument()
|
||||
})
|
||||
|
||||
test('the score can be explained part by part, and a missing genre is not a zero', async () => {
|
||||
renderCard(make({ components: { a: 0.6, g: null, p: 0.5, d: 1 } }))
|
||||
await userEvent.click(screen.getByText('Why this?'))
|
||||
const parts = screen.getByRole('list', { name: 'How the score is made' })
|
||||
expect(within(parts).getByText('Similar to your starting artists').closest('li')).toHaveTextContent('60%')
|
||||
expect(within(parts).getByText('Genres close to your taste').closest('li')).toHaveTextContent('No genre data')
|
||||
expect(within(parts).getByText('Genres close to your taste').closest('li')).not.toHaveTextContent('0%')
|
||||
expect(within(parts).getByText('What you play').closest('li')).toHaveTextContent('50%')
|
||||
expect(within(parts).getByText('Not played yet').closest('li')).toHaveTextContent('100%')
|
||||
})
|
||||
|
||||
test('a known genre part is shown in percent', async () => {
|
||||
renderCard(make())
|
||||
await userEvent.click(screen.getByText('Why this?'))
|
||||
expect(screen.getByText('Genres close to your taste').closest('li')).toHaveTextContent('45%')
|
||||
})
|
||||
|
||||
test('adding to the wishlist puts the artist there', async () => {
|
||||
const { calls } = mockFetch({ 'PUT /api/v1/wishlist/7': none })
|
||||
renderCard(make())
|
||||
const button = screen.getByRole('button', { name: 'Wishlist' })
|
||||
expect(button).toHaveAttribute('aria-pressed', 'false')
|
||||
await userEvent.click(button)
|
||||
expect(calls.map((c) => c.key)).toContain('PUT /api/v1/wishlist/7')
|
||||
})
|
||||
|
||||
test('a wishlisted artist is shown as pressed, and a click takes it off', async () => {
|
||||
const { calls } = mockFetch({ 'DELETE /api/v1/wishlist/7': none })
|
||||
renderCard(make({ wishlisted: true }))
|
||||
const button = screen.getByRole('button', { name: 'Wishlist' })
|
||||
expect(button).toHaveAttribute('aria-pressed', 'true')
|
||||
await userEvent.click(button)
|
||||
expect(calls.map((c) => c.key)).toContain('DELETE /api/v1/wishlist/7')
|
||||
})
|
||||
|
||||
test('"Not interested" dismisses the artist', async () => {
|
||||
const { calls } = mockFetch({ 'POST /api/v1/discover/7/dismiss': none })
|
||||
renderCard(make())
|
||||
await userEvent.click(screen.getByRole('button', { name: 'Not interested' }))
|
||||
expect(calls.map((c) => c.key)).toContain('POST /api/v1/discover/7/dismiss')
|
||||
})
|
||||
|
||||
test('an action that fails says so in words', async () => {
|
||||
mockFetch({ 'PUT /api/v1/wishlist/7': () => apiError(404, 'artist_not_found') })
|
||||
renderCard(make())
|
||||
await userEvent.click(screen.getByRole('button', { name: 'Wishlist' }))
|
||||
expect(await screen.findByRole('alert')).toBeInTheDocument()
|
||||
})
|
||||
77
web/src/pages/discover/SuggestionCard.tsx
Normal file
77
web/src/pages/discover/SuggestionCard.tsx
Normal file
|
|
@ -0,0 +1,77 @@
|
|||
import { useTranslation } from 'react-i18next'
|
||||
import { Link } from 'react-router-dom'
|
||||
import { ApiError } from '@/api/client'
|
||||
import { useArtistMarks, type Suggestion } from '@/api/discover'
|
||||
import ArtistPhoto from '@/components/ArtistPhoto'
|
||||
import { Button } from '@/components/ui/button'
|
||||
import { errorMessageKey } from '@/i18n'
|
||||
|
||||
const percent = (value: number) => `${Math.round(value * 100)}%`
|
||||
|
||||
/** One part of the score, in words, with its share. A part that could not be worked out says so, not "0%". */
|
||||
function Part({ label, value, missing }: { label: string; value: number | null; missing: string }) {
|
||||
return (
|
||||
<li className="flex justify-between gap-4">
|
||||
<span>{label}</span>
|
||||
<span className="font-semibold">{value === null ? missing : percent(value)}</span>
|
||||
</li>
|
||||
)
|
||||
}
|
||||
|
||||
/** An artist Discover suggests, with how sure it is and why, and what you can do about it. */
|
||||
export default function SuggestionCard({ suggestion }: { suggestion: Suggestion }) {
|
||||
const { t } = useTranslation()
|
||||
const marks = useArtistMarks()
|
||||
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 items-center gap-4">
|
||||
<ArtistPhoto artistId={artist.id} name={artist.name} size={56} />
|
||||
<div className="flex min-w-0 flex-1 flex-col">
|
||||
<Link to={`/artists/${artist.id}`} className="truncate text-lg font-bold hover:underline">
|
||||
{artist.name}
|
||||
</Link>
|
||||
<div className="flex flex-wrap items-center gap-x-3 text-sm text-muted-foreground">
|
||||
<span>{t('discover.score', { percent: Math.round(suggestion.score * 100) })}</span>
|
||||
{suggestion.in_library && <span>{t('discover.inLibrary')}</span>}
|
||||
</div>
|
||||
{suggestion.because.length > 0 && (
|
||||
<p className="truncate text-sm text-muted-foreground">
|
||||
{t('discover.because', { names: suggestion.because.map((b) => b.artist.name).join(', ') })}
|
||||
</p>
|
||||
)}
|
||||
</div>
|
||||
<div className="flex shrink-0 flex-wrap justify-end gap-2">
|
||||
<Button
|
||||
variant={suggestion.wishlisted ? 'default' : 'outline'}
|
||||
aria-pressed={suggestion.wishlisted}
|
||||
onClick={() => (suggestion.wishlisted ? marks.removeFromWishlist : marks.addToWishlist).mutate(artist.id)}
|
||||
>
|
||||
{t('discover.wishlist')}
|
||||
</Button>
|
||||
<Button variant="outline" onClick={() => marks.dismiss.mutate(artist.id)}>
|
||||
{t('discover.notInterested')}
|
||||
</Button>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<details className="text-sm">
|
||||
<summary className="cursor-pointer font-semibold text-primary">{t('discover.why')}</summary>
|
||||
<ul aria-label={t('discover.parts.label')} className="mt-2 flex max-w-sm flex-col gap-1">
|
||||
<Part label={t('discover.parts.a')} value={parts.a} missing="" />
|
||||
<Part label={t('discover.parts.g')} value={parts.g} missing={t('discover.parts.noGenre')} />
|
||||
<Part label={t('discover.parts.p')} value={parts.p} missing="" />
|
||||
<Part label={t('discover.parts.d')} value={parts.d} missing="" />
|
||||
</ul>
|
||||
</details>
|
||||
|
||||
{failure && (
|
||||
<p role="alert" className="text-sm text-destructive">
|
||||
{t(errorMessageKey(failure instanceof ApiError ? failure.code : 'unknown'))}
|
||||
</p>
|
||||
)}
|
||||
</li>
|
||||
)
|
||||
}
|
||||
Loading…
Reference in a new issue