'use client'; import { useCallback, useEffect, useState } from 'react'; import { useTranslations } from 'next-intl'; import { Download, FileText } from 'lucide-react'; import { DialogCloseButton } from '@/components/ui/shared/DialogCloseButton'; import { ResponsiveDialogOverlay, ResponsiveDialogPanel, } from '@/components/ui/shared/ResponsiveDialog'; import { Button } from '@/components/ui/shared/Button'; import type { LabCaseAttachmentMeta } from '@/types/cases'; interface LabCaseAttachmentsDialogProps { open: boolean; onClose: () => void; caseId: string; attachments: LabCaseAttachmentMeta[]; loadBlob: (caseId: string, attachmentId: string) => Promise; } function downloadBlob(blob: Blob, fileName: string) { const url = URL.createObjectURL(blob); const anchor = document.createElement('a'); anchor.href = url; anchor.download = fileName; anchor.click(); URL.revokeObjectURL(url); } function formatFileSize(bytes: number): string { if (bytes < 1024) return `${bytes} B`; const kb = bytes / 1024; if (kb < 1024) return `${kb.toFixed(1)} KB`; return `${(kb / 1024).toFixed(1)} MB`; } function AttachmentPreviewTile({ caseId, attachment, loadBlob, onDownload, }: { caseId: string; attachment: LabCaseAttachmentMeta; loadBlob: (caseId: string, attachmentId: string) => Promise; onDownload: (blob: Blob, fileName: string) => void; }) { const t = useTranslations('cases'); const [url, setUrl] = useState(null); const [blob, setBlob] = useState(null); const [failed, setFailed] = useState(false); const [downloading, setDownloading] = useState(false); useEffect(() => { let cancelled = false; let objectUrl: string | null = null; void (async () => { try { const loaded = await loadBlob(caseId, attachment.id); if (cancelled) return; objectUrl = URL.createObjectURL(loaded); setBlob(loaded); 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 ? (