Files
siftlode/frontend/src/lib/plexWatch.ts
T
peter 785080f3da fix(plex): a Surprise card whose watched toggle failed is put back
Deep-review round 3 of bug hunt 2026-09 — the sibling of the collection page's fix (d349bc90).
Discover's toggle flips the Surprise draw too, but its resync refetches only the rails (refetching the
draw would reshuffle it under the viewer), so a refused toggle stayed flipped there. toggleWatchState
takes an optional `revert`, called on a failure only; the queryKeys comment that claimed the watched
toggle invalidates the Surprise key now says what the code does.
2026-09-11 18:05:30 +02:00

27 lines
1.1 KiB
TypeScript

import type { TFunction } from "i18next";
import { api, type PlexCard } from "./api";
import { notifyActionError } from "./actionErrors";
// Persist a watch-state toggle from any Plex surface: flip via the caller's `optimistic` cache
// updater, call the API, surface a failure (the click used to look dead on 403/404/429), then
// `resync` (invalidate) — which corrects the optimistic flip to truth either way. Shared by the
// browse grid and the person page, each of which owns a different cache shape.
//
// `revert` is for a cache `resync` must NOT refetch — a random draw (Discover's Surprise rail), which
// a refetch would reshuffle under the viewer. It is called on a failure only, to put the card back.
export async function toggleWatchState(
card: PlexCard,
t: TFunction,
handlers: { optimistic: (next: string) => void; resync: () => void; revert?: () => void },
) {
const next = card.status === "watched" ? "new" : "watched";
handlers.optimistic(next);
try {
await api.plexSetState(card.id, next);
} catch (e) {
handlers.revert?.();
notifyActionError(e, t("plex.watchedFailed"));
}
handlers.resync();
}