"use client"; import { useEffect, useState, useTransition } from "react"; import { useRouter } from "next/navigation"; import { ListVideo, Eye, Trash2, X, Loader2 } from "lucide-react"; import { useWatchQueue } from "./WatchQueueProvider"; import { fetchQueueCovers } from "@/app/actions/queue"; import { ImageCard, type CardImage } from "@/components/grid/ImageCard"; import { bulkSetWatched } from "@/app/actions/bulk"; export function QueueView() { const router = useRouter(); const queue = useWatchQueue(); const [covers, setCovers] = useState([]); const [loading, setLoading] = useState(true); const [pending, start] = useTransition(); // Re-fetch when the queue id list changes. Cheap — single query keyed by IN(...). useEffect(() => { let live = true; setLoading(true); fetchQueueCovers(queue.ids).then((c) => { if (!live) return; setCovers(c); setLoading(false); }); return () => { live = false; }; }, [queue.ids]); // If a server-side delete or watched flip means a queued id no longer // resolves to a cover, prune it locally so the count stays honest. useEffect(() => { if (loading) return; const present = new Set(covers.map((c) => c.id)); const stale = queue.ids.filter((id) => !present.has(id)); if (stale.length > 0) queue.removeMany(stale); }, [loading, covers, queue]); function markAllWatched() { if (covers.length === 0) return; if (!confirm(`Mark all ${covers.length} covers as watched and clear the queue?`)) return; const ids = covers.map((c) => c.id); start(async () => { await bulkSetWatched(ids, true); queue.removeMany(ids); router.refresh(); }); } function clearQueue() { if (queue.ids.length === 0) return; if (!confirm("Clear the watch queue? Covers themselves are not affected.")) return; queue.clear(); } return ( <>

Watch queue

{queue.ids.length === 0 ? "Empty — right-click any cover and choose “Add to queue”." : `${covers.length} cover${covers.length === 1 ? "" : "s"} queued. Marking watched removes them automatically.`}

{queue.ids.length > 0 && (
)}
{loading ? (
Loading queue…
) : covers.length === 0 ? (

{queue.ids.length > 0 ? "Queue items couldn't be resolved (covers may have been deleted)." : "Queue is empty."}

) : (
{covers.map((c) => (
))}
)} ); }