2025-07-03 02:26:34 -07:00
|
|
|
import type { Attachment } from '@/lib/types';
|
2025-08-28 14:15:36 +01:00
|
|
|
import { Loader } from './elements/loader';
|
2025-09-01 11:07:07 +01:00
|
|
|
import { CrossSmallIcon, } from './icons';
|
2025-08-28 14:15:36 +01:00
|
|
|
import { Button } from './ui/button';
|
2025-09-08 21:02:06 +01:00
|
|
|
import Image from 'next/image';
|
2024-10-11 18:00:22 +05:30
|
|
|
|
|
|
|
|
export const PreviewAttachment = ({
|
|
|
|
|
attachment,
|
|
|
|
|
isUploading = false,
|
2025-08-28 14:15:36 +01:00
|
|
|
onRemove,
|
|
|
|
|
onEdit,
|
2024-10-11 18:00:22 +05:30
|
|
|
}: {
|
|
|
|
|
attachment: Attachment;
|
|
|
|
|
isUploading?: boolean;
|
2025-08-28 14:15:36 +01:00
|
|
|
onRemove?: () => void;
|
|
|
|
|
onEdit?: () => void;
|
2024-10-11 18:00:22 +05:30
|
|
|
}) => {
|
|
|
|
|
const { name, url, contentType } = attachment;
|
|
|
|
|
|
|
|
|
|
return (
|
2025-09-08 21:02:06 +01:00
|
|
|
<div data-testid="input-attachment-preview" className="group relative size-16 rounded-lg overflow-hidden bg-muted border">
|
2025-08-28 14:15:36 +01:00
|
|
|
{contentType?.startsWith('image') ? (
|
2025-09-08 21:02:06 +01:00
|
|
|
<Image
|
2025-08-28 14:15:36 +01:00
|
|
|
src={url}
|
|
|
|
|
alt={name ?? 'An image attachment'}
|
2025-09-08 21:02:06 +01:00
|
|
|
className="size-full object-cover"
|
|
|
|
|
width={64}
|
|
|
|
|
height={64}
|
2025-08-28 14:15:36 +01:00
|
|
|
/>
|
|
|
|
|
) : (
|
2025-09-08 21:02:06 +01:00
|
|
|
<div className="size-full flex items-center justify-center text-xs text-muted-foreground">
|
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 bg-black/50">
|
|
|
|
|
<Loader size={16} />
|
|
|
|
|
</div>
|
|
|
|
|
)}
|
|
|
|
|
|
|
|
|
|
{onRemove && !isUploading && (
|
|
|
|
|
<Button
|
|
|
|
|
onClick={onRemove}
|
|
|
|
|
size="sm"
|
|
|
|
|
variant="destructive"
|
|
|
|
|
className="absolute top-0.5 right-0.5 opacity-0 group-hover:opacity-100 transition-opacity size-4 p-0 rounded-full"
|
|
|
|
|
>
|
|
|
|
|
<CrossSmallIcon size={8} />
|
|
|
|
|
</Button>
|
|
|
|
|
)}
|
|
|
|
|
|
2025-09-08 21:02:06 +01:00
|
|
|
<div className="absolute bottom-0 inset-x-0 bg-gradient-to-t from-black/80 to-transparent text-white text-[10px] px-1 py-0.5 truncate">
|
2025-08-28 14:15:36 +01:00
|
|
|
{name}
|
2024-10-11 18:00:22 +05:30
|
|
|
</div>
|
2024-10-24 16:35:51 -04:00
|
|
|
</div>
|
2024-10-11 18:00:22 +05:30
|
|
|
);
|
|
|
|
|
};
|