import { appApiPath } from "@agent-native/core/client/api-path"; import type { ComposeAttachment } from "@shared/types"; import { IconAlertCircle, IconPaperclip, IconPhoto, IconX, } from "@tabler/icons-react"; import { useState } from "react"; import { formatFileSize } from "@/lib/upload"; import { cn } from "@/lib/utils"; function attachmentUrl(att: ComposeAttachment): string { return att.url.startsWith("/api/") ? appApiPath(att.url) : att.url; } function canPreview(att: ComposeAttachment): boolean { return /^image\/(?:png|jpe?g|gif|webp)$/i.test(att.mimeType); } export function AttachmentStrip({ attachments, onRemove, }: { attachments: ComposeAttachment[]; onRemove: (attachmentId: string) => void; }) { const [failedPreviews, setFailedPreviews] = useState>( () => new Set(), ); if (attachments.length === 0) return null; return (
{attachments.map((att) => { const previewable = canPreview(att); const previewFailed = failedPreviews.has(att.id); return (
{previewable && !previewFailed ? ( setFailedPreviews((prev) => { const next = new Set(prev); next.add(att.id); return next; }) } /> ) : previewFailed ? ( ) : previewable ? ( ) : ( )}
{att.originalName}
{previewFailed ? "Preview unavailable" : formatFileSize(att.size)}
); })}
); }