'use client'; import { useEffect, useState } from 'react'; import { FileText } from 'lucide-react'; import { treatmentsApi } from '@/lib/api/treatments'; import type { TreatmentAttachmentMeta } from '@/types/treatment'; interface CaseLatestAttachmentPreviewProps { attachment: TreatmentAttachmentMeta; className?: string; } export function CaseLatestAttachmentPreview({ attachment, className = 'aspect-square w-full max-w-[11rem]', }: CaseLatestAttachmentPreviewProps) { const [url, setUrl] = useState(null); const [failed, setFailed] = useState(false); useEffect(() => { let cancelled = false; let objectUrl: string | null = null; void (async () => { try { const blob = await treatmentsApi.getAttachmentFileBlob(attachment.id); if (cancelled) return; objectUrl = URL.createObjectURL(blob); setUrl(objectUrl); setFailed(false); } catch { if (!cancelled) setFailed(true); } })(); return () => { cancelled = true; if (objectUrl) URL.revokeObjectURL(objectUrl); }; }, [attachment.id]); const isImage = attachment.mimeType.startsWith('image/'); const isPdf = attachment.mimeType === 'application/pdf'; return (
{url && isImage ? ( {attachment.fileName} ) : url && isPdf ? (