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.
64 lines
2.4 KiB
TypeScript
64 lines
2.4 KiB
TypeScript
import { useInfiniteQuery, useQuery } from '@tanstack/react-query'
|
|
import type { components } from './schema'
|
|
import { api } from './client'
|
|
|
|
export type Artist = components['schemas']['Artist']
|
|
export type Album = components['schemas']['Album']
|
|
export type Track = components['schemas']['Track']
|
|
|
|
export interface Page<T> {
|
|
items: T[]
|
|
total: number
|
|
limit: number
|
|
offset: number
|
|
}
|
|
|
|
const PAGE_SIZE = 50
|
|
|
|
type Params = Record<string, string | number | undefined>
|
|
|
|
export interface ListOptions {
|
|
/** How many items one request asks for (default 50). */
|
|
pageSize?: number
|
|
/** False keeps the list from asking for anything yet (default true). */
|
|
enabled?: boolean
|
|
}
|
|
|
|
/** A list the server pages by `limit` and `offset`, read one page after the other. */
|
|
export function usePagedList<T>(path: string, params: Params, opts: ListOptions = {}) {
|
|
const pageSize = opts.pageSize ?? PAGE_SIZE
|
|
return useInfiniteQuery({
|
|
queryKey: [path, params, pageSize],
|
|
enabled: opts.enabled ?? true,
|
|
initialPageParam: 0,
|
|
queryFn: ({ pageParam }) => api.get<Page<T>>(path, { ...params, limit: pageSize, offset: pageParam }),
|
|
getNextPageParam: (last) => {
|
|
const next = last.offset + last.items.length
|
|
return last.items.length > 0 && next < last.total ? next : undefined
|
|
},
|
|
select: (data) => ({ items: data.pages.flatMap((p) => p.items), total: data.pages[0]?.total ?? 0 }),
|
|
})
|
|
}
|
|
|
|
export function useArtistList(q?: string, list: ListOptions = {}) {
|
|
return usePagedList<Artist>('/artists', { q: q || undefined }, 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 = {}) {
|
|
const { q, artistId, albumId, ...list } = opts
|
|
return usePagedList<Track>('/tracks', { q: q || undefined, artist_id: artistId, album_id: albumId }, list)
|
|
}
|
|
|
|
export function useArtist(id: number) {
|
|
return useQuery({ queryKey: ['artist', id], queryFn: () => api.get<Artist>(`/artists/${id}`) })
|
|
}
|
|
|
|
export function useAlbum(id: number) {
|
|
return useQuery({ queryKey: ['album', id], queryFn: () => api.get<Album>(`/albums/${id}`) })
|
|
}
|