import type { QueryClient } from '@tanstack/svelte-query'; import { createQuery, createInfiniteQuery } from '@tanstack/svelte-query'; import { api } from './client'; import { qk, ARTIST_PAGE_SIZE } from './queries'; import { pageGetNextPageParam } from './paging'; import type { ArtistRef, AlbumRef, TrackRef, Page, LikedIdsResponse } from './types'; export type EntityKind = 'track' | 'album' | 'artist'; const PATH_BY_KIND: Record = { track: 'tracks', album: 'albums', artist: 'artists' }; const SET_KEY_BY_KIND: Record = { track: 'track_ids', album: 'album_ids', artist: 'artist_ids' }; export function createLikedIdsQuery() { return createQuery({ queryKey: qk.likedIds(), queryFn: () => api.get('/api/likes/ids'), staleTime: 60_000 }); } export function createLikedTracksInfiniteQuery() { return createInfiniteQuery({ queryKey: qk.likedTracks(), queryFn: ({ pageParam = 0 }) => api.get>(`/api/likes/tracks?limit=${ARTIST_PAGE_SIZE}&offset=${pageParam}`), initialPageParam: 0, getNextPageParam: pageGetNextPageParam() }); } export function createLikedAlbumsInfiniteQuery() { return createInfiniteQuery({ queryKey: qk.likedAlbums(), queryFn: ({ pageParam = 0 }) => api.get>(`/api/likes/albums?limit=${ARTIST_PAGE_SIZE}&offset=${pageParam}`), initialPageParam: 0, getNextPageParam: pageGetNextPageParam() }); } export function createLikedArtistsInfiniteQuery() { return createInfiniteQuery({ queryKey: qk.likedArtists(), queryFn: ({ pageParam = 0 }) => api.get>(`/api/likes/artists?limit=${ARTIST_PAGE_SIZE}&offset=${pageParam}`), initialPageParam: 0, getNextPageParam: pageGetNextPageParam() }); } // setLiked marks one id liked or not in the cached liked sets, leaving every // other entry as it is. Nothing is cached before the first fetch, and then // there is nothing to change. function setLiked(client: QueryClient, kind: EntityKind, id: string, liked: boolean): void { const setKey = SET_KEY_BY_KIND[kind]; client.setQueryData(qk.likedIds(), (prev) => { if (!prev || prev[setKey].includes(id) === liked) return prev; return { ...prev, [setKey]: liked ? [...prev[setKey], id] : prev[setKey].filter((x) => x !== id) }; }); } // wasLiked is the cached state of one id, or undefined before the first fetch. function wasLiked(client: QueryClient, kind: EntityKind, id: string): boolean | undefined { return client.getQueryData(qk.likedIds())?.[SET_KEY_BY_KIND[kind]].includes(id); } // likeEntity shows the like straight away, then tells the server. If the // server refuses, only this id goes back to how it was: a like or unlike made // while the request was out keeps its own state. unlikeEntity is the same. export async function likeEntity( client: QueryClient, kind: EntityKind, id: string ): Promise { const before = wasLiked(client, kind, id); setLiked(client, kind, id, true); try { await api.post(`/api/likes/${PATH_BY_KIND[kind]}/${id}`, undefined); } catch (err) { if (before !== undefined) setLiked(client, kind, id, before); throw err; } } export async function unlikeEntity( client: QueryClient, kind: EntityKind, id: string ): Promise { const before = wasLiked(client, kind, id); setLiked(client, kind, id, false); try { await api.del(`/api/likes/${PATH_BY_KIND[kind]}/${id}`); } catch (err) { if (before !== undefined) setLiked(client, kind, id, before); throw err; } }