fix(web): a failed like reverts only itself

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>
This commit is contained in:
2026-11-25 22:02:00 -05:00
co-authored by Claude Opus 5.5
parent 785b15ab5f
commit 987c2a68fa
2 changed files with 50 additions and 20 deletions
+22
View File
@@ -69,4 +69,26 @@ describe('likes mutations', () => {
| { track_ids: string[] } | undefined;
expect(cached?.track_ids).not.toContain('t2');
});
test('a failed like reverts only itself, not a like made while it was out', async () => {
let fail!: (e: Error) => void;
(api.post as ReturnType<typeof vi.fn>)
.mockImplementationOnce(() => new Promise((_, reject) => { fail = reject; }))
.mockResolvedValueOnce(null);
const first = likeEntity(client, 'track', 'tA');
await likeEntity(client, 'track', 'tB');
fail(new Error('boom'));
await expect(first).rejects.toThrow('boom');
const cached = client.getQueryData(qk.likedIds()) as { track_ids: string[] } | undefined;
expect(cached?.track_ids).toContain('tB');
expect(cached?.track_ids).not.toContain('tA');
expect(cached?.track_ids).toContain('t1');
});
test('a failed unlike puts back only its own like', async () => {
(api.del as ReturnType<typeof vi.fn>).mockRejectedValueOnce(new Error('boom'));
await expect(unlikeEntity(client, 'track', 't1')).rejects.toThrow('boom');
const cached = client.getQueryData(qk.likedIds()) as { track_ids: string[] } | undefined;
expect(cached?.track_ids).toEqual(['t1']);
});
});
+28 -20
View File
@@ -63,25 +63,39 @@ export function createLikedArtistsInfiniteQuery() {
});
}
// likeEntity does an optimistic insert into the likedIds cache, fires the
// network call, and rolls back on error. Same shape for unlikeEntity.
// 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 setKey = SET_KEY_BY_KIND[kind];
const previous = client.getQueryData<LikedIdsResponse>(qk.likedIds());
if (previous) {
client.setQueryData<LikedIdsResponse>(qk.likedIds(), {
...previous,
[setKey]: previous[setKey].includes(id) ? previous[setKey] : [...previous[setKey], id]
});
}
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 (previous) client.setQueryData(qk.likedIds(), previous);
if (before !== undefined) setLiked(client, kind, id, before);
throw err;
}
}
@@ -91,18 +105,12 @@ export async function unlikeEntity(
kind: EntityKind,
id: string
): Promise<void> {
const setKey = SET_KEY_BY_KIND[kind];
const previous = client.getQueryData<LikedIdsResponse>(qk.likedIds());
if (previous) {
client.setQueryData<LikedIdsResponse>(qk.likedIds(), {
...previous,
[setKey]: previous[setKey].filter((x) => x !== id)
});
}
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 (previous) client.setQueryData(qk.likedIds(), previous);
if (before !== undefined) setLiked(client, kind, id, before);
throw err;
}
}