2026-10-20 14:14:09 -07:00
import { useCallback , useEffect , useRef , useState } from "preact/hooks" ;
2026-10-14 17:25:52 -07:00
import { Copy , Download , FileText , Image , LoaderCircle } from "lucide-preact" ;
import { attachmentSize , type AgentAttachment } from "@/lib/agent-attachments" ;
2026-10-20 14:14:09 -07:00
type Props = { file : AgentAttachment ; inline? : boolean ; download ( file : AgentAttachment , signal? : AbortSignal ) : Promise < Blob > };
2026-10-14 17:25:52 -07:00
/** Authenticated bytes only; no native credential, remote href, or public file serving. */
2026-10-20 14:14:09 -07:00
export function AgentAttachmentCard ({ file , download , inline = false } : Props ) {
2026-10-14 17:25:52 -07:00
const [ preview , setPreview ] = useState < string >();
const [ error , setError ] = useState < string >();
const [ busy , setBusy ] = useState ( false );
const [ copied , setCopied ] = useState ( false );
const controller = useRef < AbortController >();
2026-10-20 14:14:09 -07:00
const transferring = useRef ( false );
2026-10-14 17:25:52 -07:00
useEffect (() => () => { if ( preview ) URL . revokeObjectURL ( preview ); }, [ preview ]);
2026-10-20 14:14:09 -07:00
const retrieve = useCallback ( async ( show : boolean ) => {
if ( transferring . current ) return ;
transferring . current = true ;
2026-10-14 17:25:52 -07:00
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." ); }
2026-10-20 14:14:09 -07:00
finally { if ( controller . current === request ) { transferring . current = false ; if ( ! request . signal . aborted ) setBusy ( false ); } }
}, [ download , file . id , file . sha256 , file . mimeType , file . kind , file . sizeBytes , file . filename ]);
useEffect (() => {
setPreview ( undefined ); setError ( undefined ); setBusy ( false ); transferring . current = false ;
if ( inline && file . kind === "image" ) void retrieve ( true );
return () => { controller . current ? . abort (); };
}, [ inline , retrieve ]);
2026-10-14 17:25:52 -07:00
return < div className = "agent-attachment" >
< div className = "agent-attachment__title" >< FileText size = { 15 } />< span >{ file . filename }</ span >< small >{ attachmentSize ( file . sizeBytes )}</ small ></ div >
2026-10-20 14:14:09 -07:00
{ ! inline && file . workspacePath ? < div className = "agent-attachment__path" >< code title = { file . workspacePath }>{ file . workspacePath }</ code >< button type = "button" aria-label = { `Copy path for ${ file . filename } ` } title = { copied ? "Copied" : "Copy workspace path" } onClick = {() => { void navigator . clipboard . writeText ( file . workspacePath ! ). then (() => setCopied ( true )). catch (() => setError ( "Could not copy the path." )); }}>< Copy size = { 13 } /></ button ></ div > : null }
{ inline ? < div className = "agent-attachment__image-frame" aria-busy = { busy }>
{ preview ? < img className = "agent-attachment__preview" src = { preview } alt = { file . filename } onError = {() => { setPreview ( undefined ); setError ( "Image could not be displayed." ); }} />
: < span role = "status" >{ error ? "Image unavailable" : "Loading image…" }</ span >}
</ div > : preview ? < img className = "agent-attachment__preview" src = { preview } alt = { file . filename } /> : null }
2026-10-14 17:25:52 -07:00
< div className = "agent-attachment__actions" >
2026-10-20 14:14:09 -07:00
{ file . kind === "image" && ( ! inline || error ) ? < button type = "button" disabled = { busy } onClick = {() => preview ? setPreview ( undefined ) : void retrieve ( true )}>< Image size = { 13 } />{ inline ? "Retry image" : preview ? "Hide preview" : "Preview" }</ button > : null }
2026-10-14 17:25:52 -07:00
< button type = "button" disabled = { busy } onClick = {() => void retrieve ( false )}>{ busy ? < LoaderCircle size = { 13 } className = "animate-spin" /> : < Download size = { 13 } />} Download </ button >
</ div >
{ error ? < span className = "agent-attachment__error" role = "alert" >{ error } Retry using the buttons above .</ span > : null }
</ div >;
}