'use client'; import { useEffect, useState } from 'react'; import { FileText } from 'lucide-react'; import type { LabCaseAttachmentMeta } from '@/types/cases'; interface LabCaseAttachmentPreviewProps { caseId: string; attachment: LabCaseAttachmentMeta; loadBlob: (caseId: string, attachmentId: string) => Promise; className?: string; } export function LabCaseAttachmentPreview({ caseId, attachment, loadBlob, className = 'aspect-square w-full max-w-[11rem]', }: LabCaseAttachmentPreviewProps) { 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 loadBlob(caseId, 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); }; }, [caseId, attachment.id, loadBlob]); const isImage = attachment.mimeType.startsWith('image/'); const isPdf = attachment.mimeType === 'application/pdf'; return (
{url && isImage ? ( {attachment.fileName} ) : url && isPdf ? (