From 44dbc727aa2a205495e0ab8b976ba86e5e852e79 Mon Sep 17 00:00:00 2001 From: tgiachi Date: Thu, 12 Nov 2026 14:17:05 +0100 Subject: [PATCH] fix(web): no photo lookups for artists you do not have, and stale suggestions after changes A suggestion for an artist outside the library no longer asks for its photo, since that makes the server look the name up at the providers for every card. Changing a wish or a dismissal marks the kept suggestions old without asking Discover at once. Visible focus on the switch, the tabs and the chip buttons. --- web/src/api/discover.test.tsx | 13 ++++++++++++ web/src/api/discover.ts | 7 ++++++- web/src/components/ArtistPhoto.tsx | 20 ++++++++++++++++--- web/src/components/ListeningSwitch.tsx | 2 +- web/src/pages/DiscoverPage.tsx | 2 +- web/src/pages/discover/SeedPicker.tsx | 2 +- .../pages/discover/SuggestionCard.test.tsx | 11 ++++++++++ web/src/pages/discover/SuggestionCard.tsx | 2 +- 8 files changed, 51 insertions(+), 8 deletions(-) diff --git a/web/src/api/discover.test.tsx b/web/src/api/discover.test.tsx index eccfd0f..d7ae582 100644 --- a/web/src/api/discover.test.tsx +++ b/web/src/api/discover.test.tsx @@ -156,3 +156,16 @@ test('being interested again lifts the dismissal', async () => { await act(() => result.current.undismiss.mutateAsync(7)) expect(calls.map((c) => c.key)).toEqual(['DELETE /api/v1/discover/7/dismiss']) }) + +test('being interested again makes the kept suggestions old, without asking Discover at once', async () => { + const { calls } = mockFetch({ 'DELETE /api/v1/discover/7/dismiss': () => new Response(null, { status: 204 }) }) + const { client, wrapper } = setup() + const key = ['discover', { seeds: [1], inLibrary: undefined }] + client.setQueryData(key, discover([suggestion(8)])) + const { result } = renderHook(() => useArtistMarks(), { wrapper }) + await act(() => result.current.undismiss.mutateAsync(7)) + // The artist is missing from what is kept, so the next visit must read it again; nobody is looking now, and + // the call is heavy, so nothing is asked yet. + expect(client.getQueryState(key)?.isInvalidated).toBe(true) + expect(calls.map((c) => c.key)).not.toContain('GET /api/v1/discover') +}) diff --git a/web/src/api/discover.ts b/web/src/api/discover.ts index d72375f..b54041d 100644 --- a/web/src/api/discover.ts +++ b/web/src/api/discover.ts @@ -94,7 +94,12 @@ export function useArtistMarks() { onError: (_e: unknown, _id: number, context?: { before: Snapshot }) => { for (const [key, data] of context?.before ?? []) client.setQueryData(key, data) }, - onSettled: () => refreshLists(client), + onSettled: () => { + // What is kept may now lack an artist (interested again) or be wrong if the call failed: mark it old so the + // next visit reads it again. Nothing is asked now: Discover is heavy and the screen already shows the change. + void client.invalidateQueries({ queryKey: [DISCOVER], refetchType: 'none' }) + return refreshLists(client) + }, } } diff --git a/web/src/components/ArtistPhoto.tsx b/web/src/components/ArtistPhoto.tsx index 3aa2639..1850d4f 100644 --- a/web/src/components/ArtistPhoto.tsx +++ b/web/src/components/ArtistPhoto.tsx @@ -6,8 +6,22 @@ function initial(name: string): string { return Array.from(name.trim())[0]?.toUpperCase() ?? '?' } -/** A round photo of an artist, or the first letter of the name when there is no free picture. */ -export default function ArtistPhoto({ artistId, name, size }: { artistId: number; name: string; size: number }) { +/** + * A round photo of an artist, or the first letter of the name when there is no free picture. With `photo` + * false it never asks for the picture: asking makes the server look the artist up at the providers, which a + * list of names nobody chose should not do. + */ +export default function ArtistPhoto({ + artistId, + name, + size, + photo = true, +}: { + artistId: number + name: string + size: number + photo?: boolean +}) { useAccessToken() // the URL carries the token, so it has to follow a renewal const src = mediaUrl(`/artists/${artistId}/image`) const [failed, setFailed] = useState(null) @@ -17,7 +31,7 @@ export default function ArtistPhoto({ artistId, name, size }: { artistId: number style={{ width: size, height: size }} className="relative flex shrink-0 items-center justify-center overflow-hidden rounded-md bg-primary text-primary-foreground" > - {failed !== src ? ( + {photo && failed !== src ? ( {name} (on ? change(false) : setAsking(true))} className={cn( - 'relative h-7 w-12 shrink-0 rounded-md border border-input transition-colors disabled:opacity-50', + 'relative h-7 w-12 shrink-0 rounded-md border border-input transition-colors focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-ring disabled:opacity-50', on ? 'bg-primary' : 'bg-secondary', )} > diff --git a/web/src/pages/DiscoverPage.tsx b/web/src/pages/DiscoverPage.tsx index 38024ad..8c256bc 100644 --- a/web/src/pages/DiscoverPage.tsx +++ b/web/src/pages/DiscoverPage.tsx @@ -56,7 +56,7 @@ export default function DiscoverPage() { tabIndex={current === tab.id ? 0 : -1} onClick={() => choose(tab.id)} className={cn( - 'min-h-11 rounded-lg px-4 font-semibold', + 'min-h-11 rounded-lg px-4 font-semibold focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-ring', current === tab.id ? 'bg-secondary text-foreground' : 'text-muted-foreground hover:text-foreground', )} > diff --git a/web/src/pages/discover/SeedPicker.tsx b/web/src/pages/discover/SeedPicker.tsx index 239ffc3..0362b12 100644 --- a/web/src/pages/discover/SeedPicker.tsx +++ b/web/src/pages/discover/SeedPicker.tsx @@ -62,7 +62,7 @@ export default function SeedPicker({ type="button" aria-label={t('discover.seeds.remove', { name: seed.name })} onClick={() => onRemove(seed.id)} - className="flex size-7 items-center justify-center rounded-sm text-muted-foreground hover:text-foreground" + className="flex size-7 items-center justify-center rounded-sm text-muted-foreground hover:text-foreground focus-visible:outline-2 focus-visible:outline-ring" > diff --git a/web/src/pages/discover/SuggestionCard.test.tsx b/web/src/pages/discover/SuggestionCard.test.tsx index 314d8b2..45b67af 100644 --- a/web/src/pages/discover/SuggestionCard.test.tsx +++ b/web/src/pages/discover/SuggestionCard.test.tsx @@ -50,6 +50,17 @@ test('it names the artist with a link, the score in percent, and what it comes f expect(screen.getByText('Because of Björk, Massive Attack')).toBeInTheDocument() }) +test('an artist you do not have gets no photo request: looking at a list must not ask a provider about each name', () => { + renderCard(make({ in_library: false })) + expect(screen.queryByRole('img')).not.toBeInTheDocument() + expect(screen.getByText('P', { selector: '[data-placeholder]' })).toBeInTheDocument() +}) + +test('an artist you have shows its photo', () => { + renderCard(make({ in_library: true })) + expect(screen.getByRole('img', { name: 'Portishead' })).toBeInTheDocument() +}) + test('an artist with no reason to give says nothing about one', () => { renderCard(make({ because: [] })) expect(screen.queryByText(/Because of/)).not.toBeInTheDocument() diff --git a/web/src/pages/discover/SuggestionCard.tsx b/web/src/pages/discover/SuggestionCard.tsx index 3568645..b550fba 100644 --- a/web/src/pages/discover/SuggestionCard.tsx +++ b/web/src/pages/discover/SuggestionCard.tsx @@ -28,7 +28,7 @@ export default function SuggestionCard({ suggestion }: { suggestion: Suggestion return (
  • - +
    {artist.name}