import type { ObjectDocumentRow } from "@/types/analytics"; function safeUrl(url: string): string { try { const parsed = new URL(url); if (parsed.protocol === "https:" || parsed.protocol === "http:") { return url; } } catch { // fall through } return "#"; } function fmtSize(bytes: number | null): string { if (bytes === null) return ""; if (bytes < 1024) return `${bytes} Б`; if (bytes < 1024 * 1024) return `${(bytes / 1024).toFixed(0)} КБ`; return `${(bytes / (1024 * 1024)).toFixed(1)} МБ`; } const DOC_TYPE_ORDER: Record = { декларация: 1, разрешение: 2, проектная: 3, отчётность: 4, прочее: 5, }; export function ObjectDocumentsList({ docs }: { docs: ObjectDocumentRow[] }) { if (docs.length === 0) { return (

Документы отсутствуют.

); } // Group by doc_type const byType = new Map(); for (const d of docs) { const group = byType.get(d.doc_type) ?? []; group.push(d); byType.set(d.doc_type, group); } const sortedTypes = [...byType.keys()].sort((a, b) => { const oa = DOC_TYPE_ORDER[a.toLowerCase()] ?? 99; const ob = DOC_TYPE_ORDER[b.toLowerCase()] ?? 99; return oa - ob || a.localeCompare(b, "ru"); }); return (
{sortedTypes.map((type) => { const group = byType.get(type)!; return (
{type}
{group.map((doc, i) => (
📄 {doc.doc_num ? `${doc.doc_num}` : `Документ ${i + 1}`} {doc.posted_at ? ( {doc.posted_at.slice(0, 10)} ) : null} {doc.size_bytes ? ( {fmtSize(doc.size_bytes)} ) : null}
))}
); })}
); }