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(); }