import { useEffect, useState, type ReactNode } from "react"; import type { FeedbackDataSharingPreference, FeedbackVoteValue, } from "@paperclipai/shared"; import { cn, formatShortDate } from "../lib/utils"; import { timeAgo } from "../lib/timeAgo"; import { useCopyAction, useCopyToast } from "../lib/use-copy-action"; import { Button } from "@/components/ui/button"; import { Tooltip, TooltipContent, TooltipTrigger } from "@/components/ui/tooltip"; import { Popover, PopoverContent, PopoverTrigger } from "@/components/ui/popover"; import { Textarea } from "@/components/ui/textarea"; import { Dialog, DialogContent, DialogDescription, DialogFooter, DialogHeader, DialogTitle, } from "@/components/ui/dialog"; import { DropdownMenu, DropdownMenuContent, DropdownMenuItem, DropdownMenuTrigger, } from "@/components/ui/dropdown-menu"; import { Check, Copy, MoreHorizontal, ThumbsDown, ThumbsUp, X } from "lucide-react"; const WEEK_MS = 7 * 24 * 60 * 60 * 1000; /** * Short relative timestamp for an agent bubble — "2h ago" within a week, then * an absolute short date. Mirrors the task thread's `commentDateLabel` so both * surfaces read identically. */ export function agentBubbleDateLabel(date: Date | string | undefined): string { if (!date) return ""; const then = new Date(date).getTime(); if (Date.now() - then < WEEK_MS) return timeAgo(date); return formatShortDate(date); } /** * Copy-to-clipboard icon button shared by every agent-bubble footer — the * conference-room {@link AgentBubbleActionRow} and the redesigned task thread's * {@link TaskChatBubbleActions} (PAP-413). Single-sourced so both footers keep * identical sizing, radius, and the "copied ✓" feedback affordance rather than * re-declaring the same button markup on each surface. */ export function BubbleCopyButton({ copyText }: { copyText: string }) { const { copied, failed, copy } = useCopyAction(2000); const label = failed ? "Couldn’t copy message" : "Copy message"; return ( ); } /** * Shared agent-bubble action row — copy · 👍 · 👎 · timestamp · ⋯ menu. * * Rendered below every agent bubble so the task thread (`IssueChatThread`) and * the conference room (`BoardChat`) speak the same bubble language (PAP-95 / * PAP-105). Each surface supplies its own copy text, timestamp label, optional * feedback-vote wiring, and any extra overflow-menu items (e.g. stop-run / * view-run on the task side). */ export function AgentBubbleActionRow({ copyText, dateLabel, dateTitle, anchorHref, feedback, menuItems, className, }: { copyText: string; /** Short relative label shown inline (e.g. "2h ago"). */ dateLabel?: string; /** Full datetime shown in the hover tooltip. */ dateTitle?: string; /** Anchor href for the timestamp link (deep-link to the comment). */ anchorHref?: string; /** When provided, renders the 👍/👎 feedback buttons wired to this vote API. */ feedback?: { activeVote: FeedbackVoteValue | null; sharingPreference: FeedbackDataSharingPreference; termsUrl: string | null; onVote: ( vote: FeedbackVoteValue, options?: { allowSharing?: boolean; reason?: string }, ) => Promise; } | null; /** Extra DropdownMenuItem nodes appended after the default "Copy message". */ menuItems?: ReactNode; className?: string; }) { // The menu closes on click, so its copy confirmation has to outlive it. const copyWithToast = useCopyToast(); return (
{feedback ? ( ) : null} {dateLabel ? ( {dateLabel} {dateTitle} ) : null} { void copyWithToast(copyText, "Message copied"); }} > Copy message {menuItems}
); } /** * 👍/👎 feedback buttons with downvote-reason popover and a sharing-preference * prompt. Self-contained (no IssueChatCtx) so it is reused by both the task * thread and the conference room via {@link AgentBubbleActionRow}. */ export function IssueChatFeedbackButtons({ activeVote, sharingPreference = "prompt", termsUrl, onVote, }: { activeVote: FeedbackVoteValue | null; sharingPreference: FeedbackDataSharingPreference; termsUrl: string | null; onVote: (vote: FeedbackVoteValue, options?: { allowSharing?: boolean; reason?: string }) => Promise; }) { const [isSaving, setIsSaving] = useState(false); const [optimisticVote, setOptimisticVote] = useState(null); const [reasonOpen, setReasonOpen] = useState(false); const [downvoteReason, setDownvoteReason] = useState(""); const [pendingSharingDialog, setPendingSharingDialog] = useState<{ vote: FeedbackVoteValue; reason?: string; } | null>(null); const visibleVote = optimisticVote ?? activeVote ?? null; useEffect(() => { if (optimisticVote && activeVote === optimisticVote) setOptimisticVote(null); }, [activeVote, optimisticVote]); async function doVote( vote: FeedbackVoteValue, options?: { allowSharing?: boolean; reason?: string }, ) { setIsSaving(true); try { await onVote(vote, options); } catch { setOptimisticVote(null); } finally { setIsSaving(false); } } function handleVote(vote: FeedbackVoteValue, reason?: string) { setOptimisticVote(vote); if (sharingPreference === "prompt") { setPendingSharingDialog({ vote, ...(reason ? { reason } : {}) }); return; } const allowSharing = sharingPreference === "allowed"; void doVote(vote, { ...(allowSharing ? { allowSharing: true } : {}), ...(reason ? { reason } : {}), }); } function handleThumbsUp() { handleVote("up"); } function handleThumbsDown() { setOptimisticVote("down"); setReasonOpen(true); // Submit the initial down vote right away handleVote("down"); } function handleSubmitReason() { if (!downvoteReason.trim()) return; // Re-submit with reason attached if (sharingPreference === "prompt") { setPendingSharingDialog({ vote: "down", reason: downvoteReason }); } else { const allowSharing = sharingPreference === "allowed"; void doVote("down", { ...(allowSharing ? { allowSharing: true } : {}), reason: downvoteReason, }); } setReasonOpen(false); setDownvoteReason(""); } return ( <>
What could have been better?