import React, { useEffect, useState, useId, useCallback } from 'react' import ReactMarkdown from 'react-markdown' import remarkGfm from 'remark-gfm' import mermaid from 'mermaid' import { Maximize2, X, AlertCircle } from 'lucide-react' import { CopyButton } from '@/components/ui/copy-button' import { MarkdownLink } from '@/components/ui/markdown-link' import { useTheme } from '@/hooks/useTheme' import { markdownRehypePlugins } from '@/lib/markdownRehypePlugins' interface TextPartProps { text: string onFileClick?: (filePath: string) => void } interface MermaidBlockProps { code: string } function MermaidBlock({ code }: MermaidBlockProps) { const [svg, setSvg] = useState(null) const [error, setError] = useState(null) const [isExpanded, setIsExpanded] = useState(false) const theme = useTheme() const uniqueId = useId().replace(/:/g, '-') const renderAttempt = React.useRef(0) useEffect(() => { mermaid.initialize({ startOnLoad: false, theme: theme === 'dark' ? 'dark' : 'default', securityLevel: 'loose', fontFamily: 'inherit', flowchart: { htmlLabels: false, }, }) }, [theme]) const renderDiagram = useCallback(async () => { renderAttempt.current += 1 const currentAttempt = renderAttempt.current try { setError(null) const id = `mermaid-${uniqueId}-${currentAttempt}` const result = await mermaid.render(id, code.trim()) if (currentAttempt === renderAttempt.current && result?.svg) { setSvg(result.svg) } } catch (err: unknown) { if (currentAttempt === renderAttempt.current) { let message = 'Failed to render diagram' if (err instanceof Error) { message = err.message } else if (typeof err === 'string') { message = err } else if (err && typeof err === 'object' && 'message' in err) { message = String((err as { message: unknown }).message) } setError(message) setSvg(null) } } }, [code, uniqueId]) useEffect(() => { renderDiagram() }, [renderDiagram, theme]) if (error) { return (
Mermaid Error
{error}
            {code}
          
) } if (!svg) { return (
) } return ( <>
{isExpanded && (
setIsExpanded(false)} >
e.stopPropagation()} >
)} ) } interface CodeBlockProps { children?: React.ReactNode className?: string [key: string]: unknown } function CodeBlock({ children, className, ...props }: CodeBlockProps) { const extractTextContent = (node: React.ReactNode): string => { if (typeof node === 'string') return node if (typeof node === 'number') return node.toString() if (Array.isArray(node)) return node.map(extractTextContent).join('') if (React.isValidElement(node)) { const element = node as React.ReactElement> if (element.props.children) { return extractTextContent(element.props.children as React.ReactNode) } } return '' } const codeContent = extractTextContent(children) return (
        {children}
      
) } function isMermaidBlockComplete(text: string): boolean { const mermaidPattern = /```mermaid\s*([\s\S]*?)```/g let match while ((match = mermaidPattern.exec(text)) !== null) { if (match[1]) return true } return false } export function TextPart({ text, onFileClick }: TextPartProps) { const mermaidComplete = React.useMemo(() => { return text ? isMermaidBlockComplete(text) : false }, [text]) if (!text || text.trim() === '') { return null } return (
{ if (typeof n === 'string') return n if (typeof n === 'number') return n.toString() if (Array.isArray(n)) return n.map(extractText).join('') if (React.isValidElement(n)) { const el = n as React.ReactElement<{ children?: React.ReactNode }> if (el.props.children) { return extractText(el.props.children) } } return '' } const mermaidCode = extractText(children) return } if (isInline) { return ( {children} ) } return ( {children} ) }, pre({ children }) { return ( {children} ) }, p({ children }) { return

{children}

}, strong({ children }) { return {children} }, ul({ children }) { return
    {children}
}, ol({ children }) { return
    {children}
}, li({ children }) { return
  • {children}
  • }, table({ children }) { return (
    {children}
    ) }, a(props) { return }, }} > {text}
    ) }