import { useState, type ReactNode } from "react"; import { useTranslation } from "react-i18next"; import { Download } from "lucide-react"; import { cn } from "@/lib/utils"; import type { UIMediaAttachment } from "@/lib/types"; import { ImageThumbnail } from "@/components/ImageGallery"; import { ImageLightbox } from "@/components/ImageLightbox"; import { useRegisterInlineImage } from "@/components/InlineImageContext"; import { AttachmentCard, AttachmentTypeFace, attachmentCardActionClassName } from "@/components/AttachmentCard"; interface AttachmentTileProps { attachment: UIMediaAttachment; className?: string; inline?: boolean; variant?: "default" | "compact"; pending?: boolean; } export function AttachmentTile({ attachment, className, inline = false, variant = "default", pending = false }: AttachmentTileProps) { const { t } = useTranslation(); const videoUrl = attachment.url; const [failedUrl, setFailedUrl] = useState(undefined); const failed = !!videoUrl && failedUrl === videoUrl; const [imageOpen, setImageOpen] = useState(false); const hasUrl = typeof attachment.url === "string" && attachment.url.length > 0; const label = attachmentLabel(attachment, t); useRegisterInlineImage(inline && attachment.kind === "image" && hasUrl && !failed ? attachment.url : undefined); if (attachment.kind === "image" && hasUrl && !failed) { return ( setImageOpen(true)} onError={() => setFailedUrl(attachment.url)} /> {}} onOpenChange={setImageOpen} /> ); } if (attachment.kind === "video" && hasUrl && !failed) { return ( ); } const filePath = attachment.name || attachment.url?.split(/[?#]/, 1)[0] || ""; const name = attachment.name || label; const available = hasUrl && !failed; const status = available ? undefined : pending ? t("message.delivery.sending") : t("message.attachmentUnavailable"); const download = available ? : undefined; return {status ? {status} : null} {attachment.kind === "video" && failed && !pending ? ( ) : null} ; } 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-control", "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" }); }