From 71b0b39a0772dd23de141e35a46e782f22eb925d Mon Sep 17 00:00:00 2001 From: tgiachi Date: Thu, 12 Nov 2026 14:06:49 +0100 Subject: [PATCH] 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. --- web/src/i18n/locales/en.json | 14 +++ web/src/i18n/locales/it.json | 14 +++ .../pages/discover/SuggestionCard.test.tsx | 110 ++++++++++++++++++ web/src/pages/discover/SuggestionCard.tsx | 77 ++++++++++++ 4 files changed, 215 insertions(+) create mode 100644 web/src/pages/discover/SuggestionCard.test.tsx create mode 100644 web/src/pages/discover/SuggestionCard.tsx diff --git a/web/src/i18n/locales/en.json b/web/src/i18n/locales/en.json index a57db21..1674534 100644 --- a/web/src/i18n/locales/en.json +++ b/web/src/i18n/locales/en.json @@ -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.", diff --git a/web/src/i18n/locales/it.json b/web/src/i18n/locales/it.json index 27281ef..9b31471 100644 --- a/web/src/i18n/locales/it.json +++ b/web/src/i18n/locales/it.json @@ -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.", diff --git a/web/src/pages/discover/SuggestionCard.test.tsx b/web/src/pages/discover/SuggestionCard.test.tsx new file mode 100644 index 0000000..314d8b2 --- /dev/null +++ b/web/src/pages/discover/SuggestionCard.test.tsx @@ -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 { + 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( + + + + + , + ) +} + +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() +}) diff --git a/web/src/pages/discover/SuggestionCard.tsx b/web/src/pages/discover/SuggestionCard.tsx new file mode 100644 index 0000000..0e2b893 --- /dev/null +++ b/web/src/pages/discover/SuggestionCard.tsx @@ -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 ( +
  • + {label} + {value === null ? missing : percent(value)} +
  • + ) +} + +/** 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 ( +
  • +
    + +
    + + {artist.name} + +
    + {t('discover.score', { percent: Math.round(suggestion.score * 100) })} + {suggestion.in_library && {t('discover.inLibrary')}} +
    + {suggestion.because.length > 0 && ( +

    + {t('discover.because', { names: suggestion.because.map((b) => b.artist.name).join(', ') })} +

    + )} +
    +
    + + +
    +
    + +
    + {t('discover.why')} +
      + + + + +
    +
    + + {failure && ( +

    + {t(errorMessageKey(failure instanceof ApiError ? failure.code : 'unknown'))} +

    + )} +
  • + ) +}