import { createContext, useCallback, useContext, useEffect, useRef, useState, type ReactNode } from "react"; import { AlarmClock, Archive, ArrowRightLeft, Ban, Clock, MoreHorizontal, ExternalLink, RotateCcw, UserRound } from "lucide-react"; import type { AttentionItem } from "@paperclipai/shared"; import { Link } from "@/lib/router"; import { attentionCanWithdraw, attentionCloseModes, attentionForOtherText, attentionHiddenState, attentionHiddenText, attentionOtherAudienceUserId, attentionStaleChipText, attentionTransferTargets, attentionUserLabel, planStaleWithdraw, } from "@/lib/attention-close"; import { Badge } from "./ui/badge"; import { Button } from "./ui/button"; import { Dialog, DialogContent, DialogDescription, DialogFooter, DialogHeader, DialogTitle } from "./ui/dialog"; import { DropdownMenu, DropdownMenuContent, DropdownMenuItem, DropdownMenuSeparator, DropdownMenuSub, DropdownMenuSubContent, DropdownMenuSubTrigger, DropdownMenuTrigger, } from "./ui/dropdown-menu"; import { IssueGroupHeader } from "./IssueGroupHeader"; import { Textarea } from "./ui/textarea"; const HOUR_MS = 60 * 60 * 1000; const DAY_MS = 24 * HOUR_MS; /** Tomorrow at 9am local time. */ function tomorrowMorningIso(): string { const d = new Date(); d.setDate(d.getDate() + 1); d.setHours(9, 0, 0, 0); return d.toISOString(); } /** Snooze presets, resolved to a future ISO timestamp at click time. */ const SNOOZE_PRESETS: ReadonlyArray<{ label: string; resolve: () => string }> = [ { label: "1 hour", resolve: () => new Date(Date.now() + HOUR_MS).toISOString() }, { label: "4 hours", resolve: () => new Date(Date.now() + 4 * HOUR_MS).toISOString() }, { label: "Tomorrow morning", resolve: tomorrowMorningIso }, { label: "Next week", resolve: () => new Date(Date.now() + 7 * DAY_MS).toISOString() }, ]; export interface AttentionCloseMenuProps { item: AttentionItem; currentUserId: string | null | undefined; userLabelMap?: ReadonlyMap | null; onWithdraw?: (item: AttentionItem, reason: string) => void; onArchive?: (item: AttentionItem) => void; onSnooze?: (item: AttentionItem, until: string) => void; onTransfer?: (item: AttentionItem, userId: string) => void; /** * Restore a card you archived or snoozed (the Archived view, PAP-749 plan * §3). With it, a hidden card offers Restore instead of Archive, and a * snoozed card offers "Change snooze time". */ onRestore?: (item: AttentionItem) => void; busy?: boolean; className?: string; } /** * One Close… menu on every card (PAP-749 plan §2, Rule 6). Each verb has one * effect: Withdraw request closes it for everyone, Archive and Snooze hide it * for you only, Restore shows it again, Transfer sends it to a different * person. The menu shows only the verbs the server listed in `closeModes`; * the server checks each one again. */ export function AttentionCloseMenu({ item, currentUserId, userLabelMap, onWithdraw, onArchive, onSnooze, onTransfer, onRestore, busy = false, className, }: AttentionCloseMenuProps) { const [withdrawOpen, setWithdrawOpen] = useState(false); const [transferTo, setTransferTo] = useState<{ userId: string; label: string } | null>(null); const otherUserId = attentionOtherAudienceUserId(item, currentUserId); const otherName = otherUserId ? attentionUserLabel(otherUserId, userLabelMap) : null; const hidden = onRestore ? attentionHiddenState(item) : null; const canWithdraw = !!onWithdraw && attentionCanWithdraw(item); const canRestore = hidden !== null; const canArchive = !hidden && !!onArchive && attentionCloseModes(item).includes("archive"); const canSnooze = !!onSnooze && attentionCloseModes(item).includes("snooze"); const targets = onTransfer ? attentionTransferTargets(item, currentUserId, userLabelMap) : []; const href = item.subject.href; if (!canWithdraw && !canRestore && !canArchive && !canSnooze && targets.length === 0 && !href) return null; const transfer = (target: { userId: string; label: string }) => { // Transfer changes who the card is for, so a card for a different person asks first. if (otherName) setTransferTo(target); else onTransfer?.(item, target.userId); }; return ( <> {canWithdraw && ( setWithdrawOpen(true)} data-attention-close-mode="withdraw"> Withdraw request… )} {canRestore && ( onRestore?.(item)} data-attention-close-mode="restore"> {hidden?.kind === "snoozed" ? "Restore now" : "Restore"} )} {canArchive && ( onArchive?.(item)} data-attention-close-mode="archive"> Archive for me )} {canSnooze && ( onSnooze?.(item, until)} label={hidden?.kind === "snoozed" ? "Change snooze time" : undefined} /> )} {targets.length > 0 && ( Transfer to… {targets.map((target) => ( transfer(target)}> @{target.label} ))} )} {href && ( <> {(canWithdraw || canRestore || canArchive || canSnooze || targets.length > 0) && } Open source )} {canWithdraw && ( { setWithdrawOpen(false); onWithdraw?.(item, reason); }} /> )} {transferTo && otherName && ( { if (!open) setTransferTo(null); }}> event.stopPropagation()}> {attentionForOtherText(otherName, "continue")} The card leaves the list of @{otherName} and goes to @{transferTo.label}. The history records the transfer. )} ); } /** * Withdraw closes the request for everyone and is never an approval. It cannot * be undone, so it always confirms; a card for a different person also says * "This is for @x. Withdraw anyway?" (PAP-749 plan §2, special conditions). */ function WithdrawRequestDialog({ open, onOpenChange, title, otherName, onConfirm, }: { open: boolean; onOpenChange: (open: boolean) => void; title: string | null; otherName: string | null; onConfirm: (reason: string) => void; }) { const [reason, setReason] = useState(""); return ( { if (!next) setReason(""); onOpenChange(next); }}> event.stopPropagation()} data-attention-withdraw-dialog> {otherName ? attentionForOtherText(otherName, "withdraw") : "Withdraw this request?"} {title ? <>{title}{" — "} : null} This closes the request for everyone and tells the agent it is out of date. It is not an approval. You cannot undo it.