import { useEffect, useRef, useState } from "preact/hooks"; import { Copy, Download, FileText, Image, LoaderCircle } from "lucide-preact"; import { attachmentSize, type AgentAttachment } from "@/lib/agent-attachments"; type Props = { file: AgentAttachment; download(file: AgentAttachment, signal?: AbortSignal): Promise }; /** Authenticated bytes only; no native credential, remote href, or public file serving. */ export function AgentAttachmentCard({ file, download }: Props) { const [preview, setPreview] = useState(); const [error, setError] = useState(); const [busy, setBusy] = useState(false); const [copied, setCopied] = useState(false); const controller = useRef(); useEffect(() => () => { controller.current?.abort(); }, []); useEffect(() => () => { if (preview) URL.revokeObjectURL(preview); }, [preview]); const retrieve = async (show: boolean) => { if (busy) return; const request = new AbortController(); controller.current = request; setBusy(true); setError(undefined); try { const blob = await download(file, request.signal); if (request.signal.aborted) return; const url = URL.createObjectURL(blob); if (show && file.kind === "image" && ["image/png", "image/jpeg", "image/gif", "image/webp"].includes(blob.type)) setPreview(url); else { const anchor = document.createElement("a"); anchor.href = url; anchor.download = file.filename; anchor.click(); setTimeout(() => URL.revokeObjectURL(url), 1000); } } catch (failure) { if (!request.signal.aborted) setError(failure instanceof Error ? failure.message : "Download failed."); } finally { if (!request.signal.aborted) setBusy(false); } }; return
{file.filename}{attachmentSize(file.sizeBytes)}
{file.workspacePath ?
{file.workspacePath}
: null} {preview ? {file.filename} : null}
{file.kind === "image" ? : null}
{error ? {error} Retry using the buttons above. : null}
; }