"use client"
import { AlertCircleIcon, FileTextIcon, Loader2Icon } from "lucide-react"
import { cn } from "@/lib/utils"
export type MessageAttachmentStatus = "ready" | "uploading" | "error"
export type MessageAttachmentChipProps = {
type: "image" | "document" | "file"
name: string
src?: string
status?: MessageAttachmentStatus
errorMessage?: string
onOpen?: () => void
className?: string
}
export function MessageAttachmentChip({
type,
name,
src,
status = "ready",
errorMessage,
onOpen,
className,
}: MessageAttachmentChipProps) {
const isImage = type === "image" && !!src
const isUploading = status === "uploading"
const isError = status === "error"
const typeLabel =
type === "image" ? "Image" : type === "document" ? "Document" : "File"
return (
<button
type="button"
data-slot="message-attachment-chip"
title={isError ? errorMessage || `${name} failed` : name}
aria-label={`${typeLabel} attachment${
isError ? ", upload failed" : isUploading ? ", uploading" : ""
}: ${name}`}
disabled={!onOpen && !isImage}
onClick={onOpen}
className={cn(
"bg-muted relative size-14 overflow-hidden rounded-[calc(1.5rem-8px)] outline-none transition-transform after:pointer-events-none after:absolute after:inset-0 after:rounded-[inherit] after:ring-1 after:ring-border after:ring-inset after:transition-colors focus-visible:ring-1 focus-visible:ring-ring/50 active:scale-[0.96] motion-reduce:transition-none",
isError && "after:ring-destructive/60",
onOpen || isImage ? "cursor-pointer" : "cursor-default",
className
)}
>
{isImage ? (
// eslint-disable-next-line @next/next/no-img-element
<img src={src} alt="" className="size-full object-cover" />
) : (
<span className="text-muted-foreground flex size-full items-center justify-center">
<FileTextIcon className="size-5" />
</span>
)}
{isUploading ? (
<span
aria-hidden
className="bg-background/60 absolute inset-0 flex items-center justify-center backdrop-blur-[2px]"
>
<Loader2Icon className="text-muted-foreground size-4 animate-spin" />
</span>
) : null}
{isError ? (
<span
aria-hidden
className="bg-background/70 absolute inset-0 flex items-center justify-center"
>
<AlertCircleIcon className="text-destructive size-4" />
</span>
) : null}
</button>
)
}
export function MessageAttachmentRow({
children,
align = "end",
className,
}: {
children: React.ReactNode
align?: "start" | "end"
className?: string
}) {
return (
<div
data-slot="message-attachment-row"
className={cn(
"flex w-full flex-row gap-2",
align === "end" ? "justify-end" : "justify-start",
className
)}
>
{children}
</div>
)
}