From 6d03b1b6619e24e75d04e4168372cdd57364cfd0 Mon Sep 17 00:00:00 2001 From: tgiachi Date: Thu, 12 Nov 2026 14:10:07 +0100 Subject: [PATCH] feat(web): the wishlist and the not-interested lists Paged rows with remove, not interested and interested again. An artist links to its page only when it is in the library, since one kept alive only by a mark answers 404. The wishlist can hide the artists you already have. --- web/src/i18n/locales/en.json | 7 ++ web/src/i18n/locales/it.json | 7 ++ web/src/pages/discover/MarkedList.test.tsx | 126 +++++++++++++++++++++ web/src/pages/discover/MarkedList.tsx | 81 +++++++++++++ 4 files changed, 221 insertions(+) create mode 100644 web/src/pages/discover/MarkedList.test.tsx create mode 100644 web/src/pages/discover/MarkedList.tsx diff --git a/web/src/i18n/locales/en.json b/web/src/i18n/locales/en.json index b2b1e6b..7a884b2 100644 --- a/web/src/i18n/locales/en.json +++ b/web/src/i18n/locales/en.json @@ -74,6 +74,13 @@ "partial": "Last.fm could not be reached for some artists, so this list may be shorter.", "busy": "Discover is busy with another request. Try again in a moment.", "retryIn": "Try again in {{seconds}} s", + "marked": { + "hideOwned": "Hide the ones I already have", + "remove": "Remove", + "interestedAgain": "Interested again", + "emptyWishlist": "Nothing on your wishlist yet. Add artists from the suggestions.", + "emptyDismissed": "You have not dismissed any artist." + }, "seeds": { "label": "Find an artist to start from", "list": "Starting artists", diff --git a/web/src/i18n/locales/it.json b/web/src/i18n/locales/it.json index a6c2c8a..890e280 100644 --- a/web/src/i18n/locales/it.json +++ b/web/src/i18n/locales/it.json @@ -74,6 +74,13 @@ "partial": "Last.fm non era raggiungibile per alcuni artisti, quindi la lista può essere più corta.", "busy": "Discover è occupato con un'altra richiesta. Riprova tra poco.", "retryIn": "Riprova tra {{seconds}} s", + "marked": { + "hideOwned": "Nascondi quelli che ho già", + "remove": "Togli", + "interestedAgain": "Mi interessa di nuovo", + "emptyWishlist": "Ancora niente nella wishlist. Aggiungi artisti dai suggerimenti.", + "emptyDismissed": "Non hai scartato nessun artista." + }, "seeds": { "label": "Cerca un artista da cui partire", "list": "Artisti di partenza", diff --git a/web/src/pages/discover/MarkedList.test.tsx b/web/src/pages/discover/MarkedList.test.tsx new file mode 100644 index 0000000..9aac4e2 --- /dev/null +++ b/web/src/pages/discover/MarkedList.test.tsx @@ -0,0 +1,126 @@ +import { QueryClient, QueryClientProvider } from '@tanstack/react-query' +import { render, screen, waitFor, 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 { sessionStore } from '@/auth/session' +import { initI18n } from '@/i18n' +import { apiError, mockFetch, type MockRoutes } from '@/test/mockFetch' +import MarkedList from './MarkedList' + +const none = () => new Response(null, { status: 204 }) + +const marked = (id: number, name: string, in_library = false) => ({ + artist: { id, name }, + in_library, + marked_at: '2026-11-12T10:00:00Z', +}) + +const page = (items: object[], total = items.length, offset = 0) => ({ items, total, limit: 50, offset }) + +function renderList(kind: 'wishlist' | 'dismissed') { + const client = new QueryClient({ defaultOptions: { queries: { retry: false }, mutations: { retry: false } } }) + return render( + + + + + , + ) +} + +function wishlist(over: MockRoutes = {}): MockRoutes { + return { 'GET /api/v1/wishlist': () => page([marked(1, 'Portishead'), marked(2, 'Björk', true)]), ...over } +} + +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('an artist you have links to its page, one you only wish for is just a name', async () => { + mockFetch(wishlist()) + renderList('wishlist') + expect(await screen.findByRole('link', { name: 'Björk' })).toHaveAttribute('href', '/artists/2') + expect(screen.getByText('Portishead')).toBeInTheDocument() + expect(screen.queryByRole('link', { name: 'Portishead' })).not.toBeInTheDocument() + expect(screen.getByText('In your library')).toBeInTheDocument() +}) + +test('"Hide the ones I already have" leaves only the wishes', async () => { + mockFetch(wishlist()) + renderList('wishlist') + await screen.findByText('Portishead') + await userEvent.click(screen.getByRole('checkbox', { name: 'Hide the ones I already have' })) + expect(screen.queryByText('Björk')).not.toBeInTheDocument() + expect(screen.getByText('Portishead')).toBeInTheDocument() +}) + +test('removing a wish asks the server and the artist is gone when the list is read again', async () => { + let items = [marked(1, 'Portishead'), marked(2, 'Björk', true)] + const { calls } = mockFetch({ + 'GET /api/v1/wishlist': () => page(items), + 'DELETE /api/v1/wishlist/1': () => { + items = items.filter((m) => m.artist.id !== 1) + return none() + }, + }) + renderList('wishlist') + const row = (await screen.findByText('Portishead')).closest('li') as HTMLElement + await userEvent.click(within(row).getByRole('button', { name: 'Remove' })) + await waitFor(() => expect(screen.queryByText('Portishead')).not.toBeInTheDocument()) + expect(calls.map((c) => c.key)).toContain('DELETE /api/v1/wishlist/1') +}) + +test('"Not interested" on a wish dismisses the artist', async () => { + const { calls } = mockFetch(wishlist({ 'POST /api/v1/discover/1/dismiss': none })) + renderList('wishlist') + const row = (await screen.findByText('Portishead')).closest('li') as HTMLElement + await userEvent.click(within(row).getByRole('button', { name: 'Not interested' })) + expect(calls.map((c) => c.key)).toContain('POST /api/v1/discover/1/dismiss') +}) + +test('an empty wishlist says how to fill it', async () => { + mockFetch({ 'GET /api/v1/wishlist': () => page([]) }) + renderList('wishlist') + expect(await screen.findByText('Nothing on your wishlist yet. Add artists from the suggestions.')).toBeInTheDocument() +}) + +test('the dismissed artists can be wanted again', async () => { + const { calls } = mockFetch({ + 'GET /api/v1/discover/dismissed': () => page([marked(5, 'Oasis')]), + 'DELETE /api/v1/discover/5/dismiss': none, + }) + renderList('dismissed') + const row = (await screen.findByText('Oasis')).closest('li') as HTMLElement + await userEvent.click(within(row).getByRole('button', { name: 'Interested again' })) + expect(calls.map((c) => c.key)).toContain('DELETE /api/v1/discover/5/dismiss') +}) + +test('an empty dismissed list says so', async () => { + mockFetch({ 'GET /api/v1/discover/dismissed': () => page([]) }) + renderList('dismissed') + expect(await screen.findByText('You have not dismissed any artist.')).toBeInTheDocument() +}) + +test('a long list is read a page at a time', async () => { + const { calls } = mockFetch({ + 'GET /api/v1/wishlist': ({ url }) => { + const offset = Number(url.searchParams.get('offset')) + return offset === 0 ? page([marked(1, 'First')], 2, 0) : page([marked(2, 'Second')], 2, offset) + }, + }) + renderList('wishlist') + await screen.findByText('First') + await userEvent.click(screen.getByRole('button', { name: 'Show more' })) + expect(await screen.findByText('Second')).toBeInTheDocument() + expect(calls).toHaveLength(2) +}) + +test('a failure is said in words with a way to try again', async () => { + mockFetch({ 'GET /api/v1/wishlist': () => apiError(500, 'internal') }) + renderList('wishlist') + const alert = await screen.findByRole('alert') + expect(within(alert).getByRole('button', { name: 'Try again' })).toBeInTheDocument() +}) diff --git a/web/src/pages/discover/MarkedList.tsx b/web/src/pages/discover/MarkedList.tsx new file mode 100644 index 0000000..c458b5a --- /dev/null +++ b/web/src/pages/discover/MarkedList.tsx @@ -0,0 +1,81 @@ +import { useState } from 'react' +import { useTranslation } from 'react-i18next' +import { Link } from 'react-router-dom' +import { ApiError } from '@/api/client' +import { useArtistMarks, useDismissed, useWishlist, type MarkedArtist } from '@/api/discover' +import LoadMore from '@/components/LoadMore' +import { EmptyMessage, ErrorMessage, Loading } from '@/components/Status' +import { Button } from '@/components/ui/button' +import { errorMessageKey } from '@/i18n' + +/** The wishlist, or the artists you are not interested in: one row each, with what you can do about it. */ +export default function MarkedList({ kind }: { kind: 'wishlist' | 'dismissed' }) { + const { t } = useTranslation() + const wished = useWishlist({ enabled: kind === 'wishlist' }) + const dismissed = useDismissed({ enabled: kind === 'dismissed' }) + const query = kind === 'wishlist' ? wished : dismissed + const marks = useArtistMarks() + const [hideOwned, setHideOwned] = useState(false) + + const all = query.data?.items ?? [] + const items = hideOwned ? all.filter((m) => !m.in_library) : all + const failure = marks.removeFromWishlist.error ?? marks.dismiss.error ?? marks.undismiss.error + + if (query.isError) return void query.refetch()} /> + if (query.isPending) return + + return ( +
+ {kind === 'wishlist' && all.length > 0 && ( + + )} + + {all.length === 0 ? ( + {t(kind === 'wishlist' ? 'discover.marked.emptyWishlist' : 'discover.marked.emptyDismissed')} + ) : ( +
    + {items.map((m: MarkedArtist) => ( +
  • +
    + {m.in_library ? ( + + {m.artist.name} + + ) : ( + {m.artist.name} + )} + {m.in_library && {t('discover.inLibrary')}} +
    +
    + {kind === 'wishlist' ? ( + <> + + + + ) : ( + + )} +
    +
  • + ))} +
+ )} + + {failure ? ( +

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

+ ) : null} + +
+ ) +}