sonda/web/src/api/smart.ts
tgiachi a2938f0f96 feat(web): smart playlists
- A Smart playlists page with the ones I made and a way to make a new one.
- A page for a smart playlist: its rules in words, its songs, Play, Shuffle again (for the random order), Save as playlist (keeps what is on screen), Edit and Delete.
- An editor for a new one and for one that exists, with a live preview of the songs the rules give: genres (chips, with the library's genres offered), file format and bitrate, when it was added, listening, taste, length, order and limit. What the editor has no field for (artists, albums, skipped lately, another format) is kept when a playlist is changed.
- Texts in English and Italian, and tests for the list, the page, the editor and the preview.
2026-11-13 09:46:03 +01:00

104 lines
3.6 KiB
TypeScript

import { keepPreviousData, useMutation, useQuery, useQueryClient, type QueryClient } from '@tanstack/react-query'
import { api } from './client'
import { usePagedList, type ListOptions, type Track } from './catalog'
import type { Playlist } from './playlists'
import { refresh as refreshPlaylists } from './playlists'
import type { components } from './schema'
export type SmartPlaylist = components['schemas']['SmartPlaylist']
export type SmartRules = components['schemas']['SmartRules']
export type SmartSort = components['schemas']['SmartSort']
export const SORTS: readonly SmartSort[] = ['random', 'most_played', 'least_played', 'recently_added', 'title']
export const DEFAULT_LIMIT = 50
export const MAX_LIMIT = 500
/** What makes a smart playlist, or the preview of one. */
export interface SmartInput {
rules: SmartRules
sort: SmartSort
limit: number
}
/** Your smart playlists, the one changed last first. */
export function useSmartPlaylists(opts: ListOptions = {}) {
return usePagedList<SmartPlaylist>('/smart-playlists', {}, opts)
}
export function useSmartPlaylist(id: number | null) {
return useQuery({
queryKey: ['smart', id],
queryFn: () => api.get<SmartPlaylist>(`/smart-playlists/${id}`),
enabled: id !== null,
})
}
/** The songs of a smart playlist now. The songs on screen stay until the next ones arrive. */
export function useSmartTracks(id: number, variant: number) {
return useQuery({
queryKey: ['smart-tracks', id, variant],
queryFn: () => api.get<{ variant: number; tracks: Track[] }>(`/smart-playlists/${id}/tracks`, variant > 0 ? { variant } : undefined),
placeholderData: keepPreviousData,
})
}
/** The songs that rules give, without keeping them. It asks again when the rules change. */
export function useSmartPreview(input: SmartInput) {
return useQuery({
queryKey: ['smart-preview', input],
queryFn: () => api.post<{ variant: number; tracks: Track[] }>('/smart-playlists/preview', input),
placeholderData: keepPreviousData,
})
}
/** The genres of the library with their songs, the biggest first. */
export function useGenres() {
return useQuery({
queryKey: ['genres'],
queryFn: () => api.get<{ items: { name: string; tracks: number }[] }>('/genres').then((r) => r.items),
staleTime: 5 * 60_000,
})
}
function refresh(client: QueryClient) {
return client.invalidateQueries({
predicate: (query) => {
const key = query.queryKey[0]
return typeof key === 'string' && (key.startsWith('/smart-playlists') || key === 'smart' || key === 'smart-tracks')
},
})
}
export function useCreateSmart() {
const client = useQueryClient()
return useMutation({
mutationFn: (body: SmartInput & { name: string; description: string }) => api.post<SmartPlaylist>('/smart-playlists', body),
onSuccess: () => refresh(client),
})
}
export function useUpdateSmart() {
const client = useQueryClient()
return useMutation({
mutationFn: ({ id, ...body }: Partial<SmartInput> & { id: number; name?: string; description?: string }) =>
api.patch<SmartPlaylist>(`/smart-playlists/${id}`, body),
onSuccess: () => refresh(client),
})
}
export function useDeleteSmart() {
const client = useQueryClient()
return useMutation({
mutationFn: (id: number) => api.del(`/smart-playlists/${id}`),
onSuccess: () => refresh(client),
})
}
/** Keeps the songs on screen as a normal playlist of mine. */
export function useSaveSmart() {
const client = useQueryClient()
return useMutation({
mutationFn: ({ id, ...body }: { id: number; name: string; variant: number }) => api.post<Playlist>(`/smart-playlists/${id}/playlist`, body),
onSuccess: () => refreshPlaylists(client),
})
}