import { keepPreviousData, useMutation, useQuery, useQueryClient, type QueryClient } from '@tanstack/react-query' import { api } from './client' import { usePagedList, type ListOptions, type Track } from './catalog' import { useLang } from './naming' 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('/smart-playlists', {}, opts) } export function useSmartPlaylist(id: number | null) { return useQuery({ queryKey: ['smart', id], queryFn: () => api.get(`/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 albums that stand for a smart playlist, for its picture: up to four that have a cover, in the order of its songs. * It is asked apart from the songs, once the list is on screen, and kept for a few minutes. */ export function useSmartCovers(id: number, variant = 0) { return useQuery({ queryKey: ['smart-covers', id, variant], queryFn: () => api.get<{ album_ids: number[] }>(`/smart-playlists/${id}/covers`, variant > 0 ? { variant } : undefined).then((r) => r.album_ids), staleTime: 5 * 60_000, }) } /** The same for a suggested smart playlist. */ export function useSuggestedCovers(key: string, variant = 0) { return useQuery({ queryKey: ['smart-suggested-covers', key, variant], queryFn: () => api.get<{ album_ids: number[] }>(`/smart-playlists/suggested/${encodeURIComponent(key)}/covers`, variant > 0 ? { variant } : undefined).then((r) => r.album_ids), staleTime: 5 * 60_000, }) } /** 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, }) } /** A smart playlist that Sonda makes for everybody: a key (its name comes from the texts of the page) and its rules. */ export interface SuggestedSmart extends SmartInput { key: string } /** The smart playlists that are there without making them. */ export function useSuggested() { return useQuery({ queryKey: ['smart-suggested'], queryFn: () => api.get<{ items: SuggestedSmart[] }>('/smart-playlists/suggested').then((r) => r.items), staleTime: 10 * 60_000, }) } /** The songs of a suggested one now. The songs on screen stay until the next ones arrive. */ export function useSuggestedTracks(key: string, variant: number) { return useQuery({ queryKey: ['smart-suggested-tracks', key, variant], queryFn: () => api.get<{ variant: number; tracks: Track[] }>(`/smart-playlists/suggested/${encodeURIComponent(key)}/tracks`, variant > 0 ? { variant } : undefined), placeholderData: keepPreviousData, }) } /** Keeps the songs of a suggested one on screen as a normal playlist of mine. */ export function useSaveSuggested() { const client = useQueryClient() const lang = useLang() return useMutation({ mutationFn: ({ key, ...body }: { key: string; name: string; variant: number }) => api.post(`/smart-playlists/suggested/${encodeURIComponent(key)}/playlist`, { ...body, auto_name: true, lang }), onSuccess: () => refreshPlaylists(client), }) } /** 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' || key === 'smart-covers') }, }) } export function useCreateSmart() { const client = useQueryClient() const lang = useLang() // A blank name is for Claude to fill in, when it is on. return useMutation({ mutationFn: (body: SmartInput & { name: string; description: string }) => api.post('/smart-playlists', { ...body, lang }), onSuccess: () => refresh(client), }) } export function useUpdateSmart() { const client = useQueryClient() return useMutation({ mutationFn: ({ id, ...body }: Partial & { id: number; name?: string; description?: string }) => api.patch(`/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() const lang = useLang() return useMutation({ mutationFn: ({ id, ...body }: { id: number; name: string; variant: number }) => api.post(`/smart-playlists/${id}/playlist`, { ...body, auto_name: true, lang }), onSuccess: () => refreshPlaylists(client), }) }