import { api, type PlexCard } from "./api"; import { notifyActionError } from "./actionErrors"; import { msg } from "./notifications"; // 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 called on a failure only, to put the card back where `resync` does not, or not at once: // a component's local copy of the rows (a Plex playlist), which an identical refetch never re-seeds // (react-query keeps the same `data`), or a cache whose refetch lands later than the click's answer. export async function toggleWatchState( card: PlexCard, 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, msg("plex.watchedFailed")); } handlers.resync(); }