'use client'; import { useMemo } from 'react'; import { Modal } from '@/components/ui'; interface DocItem { name: string; ref?: string; articles?: string; content?: string; } interface Props { isOpen: boolean; onClose: () => void; doc: DocItem | null; } /** Простой рендер markdown-подобных заголовков и параграфов для оглавления */ function getHeadings(content: string): { id: string; text: string; level: number }[] { const headings: { id: string; text: string; level: number }[] = []; const lines = content.split('\n'); lines.forEach((line, i) => { const m = line.match(/^(#{1,3})\s+(.+)$/); if (m) headings.push({ id: `h-${i}`, text: m[2], level: m[1].length }); }); return headings; } function renderContent(content: string) { const lines = content.split('\n'); return lines.map((line, i) => { if (line.startsWith('### ')) return
{line}
; }); } export default function HelpDocumentModal({ isOpen, onClose, doc }: Props) { const content = doc?.content || (doc ? `${doc.name}\n\n${doc.ref || ''}\n\n${doc.articles || ''}` : ''); const headings = useMemo(() => getHeadings(content), [content]); const handleDownload = () => { if (!doc) return; const text = [doc.name, doc.ref || '', doc.articles || '', '', content].filter(Boolean).join('\n'); const blob = new Blob([text], { type: 'text/plain;charset=utf-8' }); const url = URL.createObjectURL(blob); const a = document.createElement('a'); a.href = url; a.download = `${doc.name.replace(/\s+/g, '_')}.txt`; a.click(); URL.revokeObjectURL(url); }; if (!doc) return null; return ({doc.ref}
} {doc.articles &&{doc.articles}
} {renderContent(content)}