A like or unlike that failed restored the whole liked list from before it started, undoing any like made while it was in flight. Now only its own id goes back to how it was. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
117 lines
3.6 KiB
TypeScript
117 lines
3.6 KiB
TypeScript
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<EntityKind, 'tracks' | 'albums' | 'artists'> = {
|
|
track: 'tracks',
|
|
album: 'albums',
|
|
artist: 'artists'
|
|
};
|
|
|
|
const SET_KEY_BY_KIND: Record<EntityKind, keyof LikedIdsResponse> = {
|
|
track: 'track_ids',
|
|
album: 'album_ids',
|
|
artist: 'artist_ids'
|
|
};
|
|
|
|
export function createLikedIdsQuery() {
|
|
return createQuery({
|
|
queryKey: qk.likedIds(),
|
|
queryFn: () => api.get<LikedIdsResponse>('/api/likes/ids'),
|
|
staleTime: 60_000
|
|
});
|
|
}
|
|
|
|
export function createLikedTracksInfiniteQuery() {
|
|
return createInfiniteQuery({
|
|
queryKey: qk.likedTracks(),
|
|
queryFn: ({ pageParam = 0 }) =>
|
|
api.get<Page<TrackRef>>(`/api/likes/tracks?limit=${ARTIST_PAGE_SIZE}&offset=${pageParam}`),
|
|
initialPageParam: 0,
|
|
getNextPageParam: pageGetNextPageParam<TrackRef>()
|
|
});
|
|
}
|
|
|
|
export function createLikedAlbumsInfiniteQuery() {
|
|
return createInfiniteQuery({
|
|
queryKey: qk.likedAlbums(),
|
|
queryFn: ({ pageParam = 0 }) =>
|
|
api.get<Page<AlbumRef>>(`/api/likes/albums?limit=${ARTIST_PAGE_SIZE}&offset=${pageParam}`),
|
|
initialPageParam: 0,
|
|
getNextPageParam: pageGetNextPageParam<AlbumRef>()
|
|
});
|
|
}
|
|
|
|
export function createLikedArtistsInfiniteQuery() {
|
|
return createInfiniteQuery({
|
|
queryKey: qk.likedArtists(),
|
|
queryFn: ({ pageParam = 0 }) =>
|
|
api.get<Page<ArtistRef>>(`/api/likes/artists?limit=${ARTIST_PAGE_SIZE}&offset=${pageParam}`),
|
|
initialPageParam: 0,
|
|
getNextPageParam: pageGetNextPageParam<ArtistRef>()
|
|
});
|
|
}
|
|
|
|
// 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<LikedIdsResponse>(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<LikedIdsResponse>(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<void> {
|
|
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<void> {
|
|
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;
|
|
}
|
|
}
|