"use client"; import { useEffect, useRef, useState, useTransition } from "react"; import { useRouter } from "next/navigation"; import { Tag, ChevronDown, Eye, EyeOff, Gem, Star, Package, MinusCircle } from "lucide-react"; import { useSelection } from "@/components/select/SelectionProvider"; import { bulkSetMark, bulkSetWatched, bulkSetOwned } from "@/app/actions/bulk"; import { dispatchQueueRemove } from "@/components/queue/watchQueueEvents"; import { cn } from "@/lib/utils"; type Action = "watched" | "unwatched" | "vip" | "favorite" | "owned" | "unmark"; export function MarkActionPopover() { const sel = useSelection(); const count = sel.ids.size; const enabled = count > 0; const [open, setOpen] = useState(false); const wrapRef = useRef(null); const [pending, start] = useTransition(); const router = useRouter(); useEffect(() => { if (!open) return; const onDoc = (e: MouseEvent) => { if (!wrapRef.current?.contains(e.target as Node)) setOpen(false); }; document.addEventListener("mousedown", onDoc); return () => document.removeEventListener("mousedown", onDoc); }, [open]); useEffect(() => { if (!open) return; const onKey = (e: KeyboardEvent) => { if (e.key === "Escape") setOpen(false); }; window.addEventListener("keydown", onKey); return () => window.removeEventListener("keydown", onKey); }, [open]); // If selection drains while menu is open, close it. useEffect(() => { if (!enabled && open) setOpen(false); }, [enabled, open]); function pick(action: Action) { setOpen(false); const ids = Array.from(sel.ids); if (ids.length === 0) return; start(async () => { if (action === "watched") { await bulkSetWatched(ids, true); dispatchQueueRemove(ids); } else if (action === "unwatched") await bulkSetWatched(ids, false); else if (action === "vip") await bulkSetMark(ids, "vip"); else if (action === "favorite") await bulkSetMark(ids, "favorite"); else if (action === "owned") await bulkSetOwned(ids, true); else if (action === "unmark") await bulkSetMark(ids, "unmarked"); router.refresh(); }); } return (
{open && enabled && (
e.stopPropagation()} > pick("watched")} /> pick("unwatched")} /> pick("vip")} /> pick("favorite")} /> pick("owned")} /> pick("unmark")} />
)}
); } function Divider() { return
; } function Row({ icon: Icon, label, onClick, colorClass, iconStyle, }: { icon: React.ComponentType<{ className?: string; style?: React.CSSProperties }>; label: string; onClick: () => void; colorClass?: string; iconStyle?: React.CSSProperties; }) { return ( ); }