import { useState, type ReactNode } from "react"; import { FileIcon, ImageIcon, PlaySquare } from "lucide-react"; import { useTranslation } from "react-i18next"; import { cn } from "@/lib/utils"; import type { UIMediaAttachment } from "@/lib/types"; interface AttachmentTileProps { attachment: UIMediaAttachment; className?: string; inline?: boolean; variant?: "default" | "compact"; } export function AttachmentTile({ attachment, className, inline = false, variant = "default" }: AttachmentTileProps) { const { t } = useTranslation(); const [failed, setFailed] = useState(false); const hasUrl = typeof attachment.url === "string" && attachment.url.length > 0; const label = attachmentLabel(attachment, t); if (attachment.kind === "image" && hasUrl && !failed) { return ( {attachment.name setFailed(true)} className={cn( "block h-auto max-w-full bg-background object-contain", variant === "compact" ? "max-h-40" : "max-h-[34rem]", )} /> ); } if (attachment.kind === "video" && hasUrl) { return ( ); } const Icon = attachment.kind === "video" ? PlaySquare : attachment.kind === "image" ? ImageIcon : FileIcon; const body = ( <> {attachment.name ?? label} ); if (hasUrl && !failed) { return ( {body} ); } return (
{body} {t("message.attachmentUnavailable", { defaultValue: "Attachment unavailable" })}
); } function AttachmentFrame({ attachment, children, className, inline = false, variant = "default", }: { attachment: UIMediaAttachment; children: ReactNode; className?: string; inline?: boolean; variant?: "default" | "compact"; }) { const frameClassName = cn( "not-prose my-3 block w-fit max-w-full overflow-hidden rounded-[14px]", "border border-border/60 bg-muted/40", attachment.kind === "image" && "bg-background/85", attachment.kind === "video" ? "w-[min(100%,32rem)]" : "", variant === "compact" && "my-1 rounded-xl shadow-none", variant === "compact" && attachment.kind === "video" && "w-[min(100%,20rem)]", className, ); const bodyClassName = "block max-w-full"; const body = inline ? ( {children} ) : (
{children}
); return inline ? ( {body} ) : (
{body}
); } function attachmentLabel(attachment: UIMediaAttachment, t: ReturnType["t"]): string { if (attachment.kind === "video") { return t("message.videoAttachment", { defaultValue: "Video attachment" }); } if (attachment.kind === "image") { return t("message.imageAttachment", { defaultValue: "Image attachment" }); } return t("message.fileAttachment", { defaultValue: "File attachment" }); }