chatbot-template/components/chat/preview-attachment.tsx

57 lines
1.6 KiB
TypeScript
Raw Normal View History

import Image from "next/image";
import type { Attachment } from "@/lib/types";
import { Spinner } from "../ui/spinner";
import { CrossSmallIcon } from "./icons";
2024-10-11 18:00:22 +05:30
export const PreviewAttachment = ({
attachment,
isUploading = false,
2025-08-28 14:15:36 +01:00
onRemove,
2024-10-11 18:00:22 +05:30
}: {
attachment: Attachment;
isUploading?: boolean;
2025-08-28 14:15:36 +01:00
onRemove?: () => void;
2024-10-11 18:00:22 +05:30
}) => {
const { name, url, contentType } = attachment;
return (
2025-09-10 16:59:48 +01:00
<div
className="group relative h-24 w-24 shrink-0 overflow-hidden rounded-xl border border-border/40 bg-muted"
data-testid="input-attachment-preview"
2025-09-10 16:59:48 +01:00
>
{contentType?.startsWith("image") ? (
2025-09-08 21:02:06 +01:00
<Image
alt={name ?? "attachment"}
className="size-full object-cover"
height={96}
src={url}
width={96}
2025-08-28 14:15:36 +01:00
/>
) : (
2025-09-10 16:59:48 +01:00
<div className="flex size-full items-center justify-center text-muted-foreground text-xs">
2025-08-28 14:15:36 +01:00
File
</div>
)}
2024-10-11 18:00:22 +05:30
2025-08-28 14:15:36 +01:00
{isUploading && (
<div
className="absolute inset-0 flex items-center justify-center rounded-xl bg-black/40 backdrop-blur-sm"
data-testid="input-attachment-loader"
>
<Spinner className="size-5" />
2025-08-28 14:15:36 +01:00
</div>
)}
{onRemove && !isUploading && (
<button
className="absolute top-1.5 right-1.5 flex size-5 items-center justify-center rounded-full bg-black/60 text-white opacity-0 backdrop-blur-sm transition-opacity hover:bg-black/80 group-hover:opacity-100"
2025-08-28 14:15:36 +01:00
onClick={onRemove}
type="button"
2025-08-28 14:15:36 +01:00
>
<CrossSmallIcon size={10} />
</button>
2025-08-28 14:15:36 +01:00
)}
</div>
2024-10-11 18:00:22 +05:30
);
};