feat: Recently added on Home, with a newest-first order for albums

I can now see on Home what I added to the library lately.

- GET /albums takes sort=recent: the albums added last first, by the date of the newest file of the album that is not missing, then the newest album. The default stays by title and any other value is invalid_input.
- The albums shelf on Home is Recently added and asks for that order; See all leads to /albums?sort=recent, which the albums page understands.
- English and Italian.
This commit is contained in:
tgiachi 2026-11-14 10:29:08 +01:00
commit 246cdc63d3
8 changed files with 129 additions and 17 deletions

View file

@ -44,9 +44,10 @@ export function useArtistList(q?: string, list: ListOptions = {}) {
return usePagedList<Artist>('/artists', { q: q || undefined }, list)
}
export function useAlbumList(opts: { q?: string; artistId?: number } & ListOptions = {}) {
const { q, artistId, ...list } = opts
return usePagedList<Album>('/albums', { q: q || undefined, artist_id: artistId }, list)
/** `sort: 'recent'` asks for the albums added last first; without it they come by title. */
export function useAlbumList(opts: { q?: string; artistId?: number; sort?: 'recent' } & ListOptions = {}) {
const { q, artistId, sort, ...list } = opts
return usePagedList<Album>('/albums', { q: q || undefined, artist_id: artistId, sort }, list)
}
export function useTrackList(opts: { q?: string; artistId?: number; albumId?: number } & ListOptions = {}) {

View file

@ -1704,7 +1704,9 @@ export interface paths {
};
/**
* List albums
* @description Albums with a playable track, sorted by title. `artist_id` matches the albums an artist
* @description Albums with a playable track, sorted by title, or with `sort=recent` the ones added last first (by the date of
* the newest file of the album that is not missing, which is the best the index knows, then the album that came
* last). `artist_id` matches the albums an artist
* owns plus those where it is credited on a playable track, the same set as its `album_count`.
*/
get: operations["listAlbums"];
@ -6856,6 +6858,8 @@ export interface operations {
listAlbums: {
parameters: {
query?: {
/** @description `title` (the default) or `recent`. Anything else is `invalid_input`. */
sort?: "title" | "recent";
/**
* @description Case- and accent-insensitive substring of the name or title: `bjork`, `BJORK` and `BJÖRK` all
* find `Björk`, and `strasse` finds `Straße`. At most 200 characters.

View file

@ -671,6 +671,7 @@
"greeting_afternoon": "Good afternoon",
"greeting_evening": "Good evening",
"recent": "Recently played",
"recentlyAdded": "Recently added",
"nothingPlayed": "Play something and it will show up here.",
"emptyLibrary": "Your library is empty. An admin can add a folder of music and scan it.",
"trackGone": "That song is no longer in your library."

View file

@ -671,6 +671,7 @@
"greeting_afternoon": "Buon pomeriggio",
"greeting_evening": "Buonasera",
"recent": "Ascoltati di recente",
"recentlyAdded": "Aggiunti di recente",
"nothingPlayed": "Ascolta qualcosa e comparirà qui.",
"emptyLibrary": "La libreria è vuota. Un admin può aggiungere una cartella di musica e scansionarla.",
"trackGone": "Quel brano non è più nella tua libreria."

View file

@ -10,12 +10,13 @@ export default function AlbumsPage() {
const { t } = useTranslation()
const [params] = useSearchParams()
const q = params.get('q') ?? ''
const query = useAlbumList({ q })
const sort = params.get('sort') === 'recent' ? 'recent' : undefined
const query = useAlbumList({ q, sort })
const items = query.data?.items ?? []
return (
<div className="flex flex-col gap-6">
<PageTitle>{t('nav.albums')}</PageTitle>
<PageTitle>{t(sort ? 'home.recentlyAdded' : 'nav.albums')}</PageTitle>
{query.isError ? (
<ErrorMessage error={query.error} onRetry={() => query.refetch()} />
) : query.isPending ? (

View file

@ -126,10 +126,20 @@ test('with no history it says to play something', async () => {
expect(await screen.findByText('Play something and it will show up here.')).toBeInTheDocument()
})
test('the albums shelf is the albums added last, asked of the server newest first', async () => {
const { calls } = mockFetch(routes())
renderHome()
await screen.findByRole('region', { name: 'Recently added' })
expect(screen.queryByRole('region', { name: 'Albums' })).not.toBeInTheDocument()
const asked = calls.filter((c) => c.key === 'GET /api/v1/albums').at(-1)!
expect(asked.url.searchParams.get('sort')).toBe('recent')
expect(asked.url.searchParams.get('limit')).toBe('10')
})
test('albums and artists have their own shelves with links to their pages', async () => {
mockFetch(routes())
renderHome()
const albums = await screen.findByRole('region', { name: 'Albums' })
const albums = await screen.findByRole('region', { name: 'Recently added' })
expect(await within(albums).findByRole('link', { name: /OK Computer/ })).toHaveAttribute('href', '/albums/9')
const artists = await screen.findByRole('region', { name: 'Artists' })
expect(await within(artists).findByRole('link', { name: /Radiohead/ })).toHaveAttribute('href', '/artists/5')
@ -138,8 +148,8 @@ test('albums and artists have their own shelves with links to their pages', asyn
test('a shelf with more than ten items links to the whole list', async () => {
mockFetch(routes({ 'GET /api/v1/albums': () => page([album], 40) }))
renderHome()
const albums = await screen.findByRole('region', { name: 'Albums' })
expect(await within(albums).findByRole('link', { name: 'See all' })).toHaveAttribute('href', '/albums')
const albums = await screen.findByRole('region', { name: 'Recently added' })
expect(await within(albums).findByRole('link', { name: 'See all' })).toHaveAttribute('href', '/albums?sort=recent')
})
test('an empty library says so instead of showing empty shelves', async () => {
@ -150,7 +160,7 @@ test('an empty library says so instead of showing empty shelves', async () => {
}))
renderHome()
expect(await screen.findByText(/Your library is empty/)).toBeInTheDocument()
expect(screen.queryByRole('region', { name: 'Albums' })).not.toBeInTheDocument()
expect(screen.queryByRole('region', { name: 'Recently added' })).not.toBeInTheDocument()
})
test('when the history cannot be read the shelves of albums and artists still show', async () => {

View file

@ -27,7 +27,7 @@ export default function HomePage() {
const queryClient = useQueryClient()
const playTracks = usePlayer((s) => s.playTracks)
const history = useRecentListens()
const albums = useAlbumList({ pageSize: SHELF })
const albums = useAlbumList({ pageSize: SHELF, sort: 'recent' })
const artists = useArtistList(undefined, { pageSize: SHELF })
const [playError, setPlayError] = useState<string | null>(null)
// Read once per visit of the page: the greeting does not need to change while it is open.
@ -95,7 +95,7 @@ export default function HomePage() {
<ErrorMessage error={albums.error} onRetry={() => albums.refetch()} />
) : (
albumItems.length > 0 && (
<Section title={t('nav.albums')}>
<Section title={t('home.recentlyAdded')}>
<ul className={CARD_GRID}>
{albumItems.map((album) => (
<li key={album.id}>
@ -104,7 +104,7 @@ export default function HomePage() {
))}
</ul>
{(albums.data?.total ?? 0) > albumItems.length && (
<Link to="/albums" className="inline-flex min-h-11 items-center self-start font-semibold text-primary hover:underline">
<Link to="/albums?sort=recent" className="inline-flex min-h-11 items-center self-start font-semibold text-primary hover:underline">
{t('common.seeAll')}
</Link>
)}

View file

@ -456,6 +456,22 @@ test('the albums list shows each album with its artist and links to it', async (
expect(screen.getByRole('link', { name: /Kid A/ })).toHaveAttribute('href', '/albums/10')
})
test('the albums list can come newest first, when the address says so', async () => {
const { calls } = mockFetch({ 'GET /api/v1/albums': () => page([album]) })
renderPage(<AlbumsPage />, '/albums?sort=recent', '/albums')
await screen.findByRole('link', { name: /OK Computer/ })
expect(screen.getByRole('heading', { level: 1, name: 'Recently added' })).toBeInTheDocument()
expect(calls.at(-1)!.url.searchParams.get('sort')).toBe('recent')
})
test('the albums list is by title unless the address asks otherwise', async () => {
const { calls } = mockFetch({ 'GET /api/v1/albums': () => page([album]) })
renderPage(<AlbumsPage />, '/albums?sort=nonsense', '/albums')
await screen.findByRole('link', { name: /OK Computer/ })
expect(calls.at(-1)!.url.searchParams.has('sort')).toBe(false)
expect(screen.getByRole('heading', { level: 1, name: 'Albums' })).toBeInTheDocument()
})
test('the songs list plays from the song that was clicked', async () => {
mockFetch({ 'GET /api/v1/tracks': () => page([track(1), track(2), track(3)]) })
renderPage(<SongsPage />, '/songs')