Spaces:
Configuration error

File size: 4,557 Bytes
420bd16
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
import React, { useEffect, useRef, useState } from 'react';
import PropTypes from 'prop-types';
import mermaid from 'mermaid';

// Initialize mermaid with default config
try {
  mermaid.initialize({
    startOnLoad: true,
    theme: 'default',
    securityLevel: 'loose',
    mindmap: {
      padding: 20,
      useMaxWidth: true,
    },
  });
} catch (error) {
  console.error('Error initializing mermaid:', error);
}

export const formatToMermaid = (content) => {
  if (!content) return '';
  
  try {
    const lines = content.split('\n')
      .filter(line => line.trim() && line.trim() !== 'mindmap');
    
    let mermaidContent = 'mindmap\n';
    
    lines.forEach(line => {
      const indentMatch = line.match(/^[\t\s]*/);
      const level = indentMatch ? indentMatch[0].length / 2 : 0;
      const indent = '  '.repeat(level);
      
      // Convert node types to Mermaid syntax
      let processedLine = line.trim();
      if (processedLine.includes('{"') && processedLine.includes('"}')) {
        processedLine = processedLine.replace(/\{"(.*?)"\}/, '[$1]((diamond))');
      } else if (processedLine.includes('["') && processedLine.includes('"]')) {
        processedLine = processedLine.replace(/\["(.*?)"\]/, '[$1]((square))');
      } else if (processedLine.includes('<"') && processedLine.includes('">')) {
        processedLine = processedLine.replace(/<"(.*?)">/, '[$1]((circle))');
      } else if (processedLine.includes('|"') && processedLine.includes('"|')) {
        processedLine = processedLine.replace(/\|"(.*?)"\|/, '[$1]((hexagon))');
      } else if (processedLine.includes('(("') && processedLine.includes('"))')) {
        processedLine = processedLine.replace(/\(\("(.*?)"\)\)/, '[$1]((stadium))');
      } else if (processedLine.includes('("') && processedLine.includes('")')) {
        processedLine = processedLine.replace(/\("(.*?)"\)/, '[$1]((rounded))');
      }
      
      // Handle icons
      if (processedLine.includes('::icon')) {
        processedLine = processedLine.replace(/::icon\((.*?)\)/, '::$1');
      }
      
      mermaidContent += `${indent}${processedLine}\n`;
    });
    
    return mermaidContent;
  } catch (error) {
    console.error('Error formatting mermaid content:', error);
    return ''; // Return empty string on error
  }
};

export function MermaidMindMap({ content }) {
  const mermaidRef = useRef(null);
  const [error, setError] = useState(null);
  const [isLoading, setIsLoading] = useState(true);

  useEffect(() => {
    let mounted = true;
    let renderTimeout;

    const renderMermaid = async () => {
      if (!mermaidRef.current) return;

      try {
        setIsLoading(true);
        setError(null);

        const mermaidContent = formatToMermaid(content);
        if (!mermaidContent) {
          throw new Error('Invalid mind map content');
        }

        // Clear previous content
        mermaidRef.current.innerHTML = mermaidContent;

        // Add a small delay to ensure DOM is updated
        renderTimeout = setTimeout(async () => {
          try {
            await mermaid.contentLoaded();
            if (mounted) {
              setIsLoading(false);
            }
          } catch (err) {
            if (mounted) {
              console.error('Error rendering mermaid:', err);
              setError('Failed to render mind map');
              setIsLoading(false);
            }
          }
        }, 100);
      } catch (err) {
        if (mounted) {
          console.error('Error processing mermaid content:', err);
          setError('Failed to process mind map content');
          setIsLoading(false);
        }
      }
    };

    renderMermaid();

    return () => {
      mounted = false;
      if (renderTimeout) {
        clearTimeout(renderTimeout);
      }
      if (mermaidRef.current) {
        mermaidRef.current.innerHTML = '';
      }
    };
  }, [content]);

  if (error) {
    return (
      <div className="mermaid-error">
        <div className="error-icon">⚠️</div>
        <div className="error-message">{error}</div>
      </div>
    );
  }

  return (
    <div className="mermaid-mindmap">
      {isLoading && (
        <div className="mermaid-loading">
          <div className="loading-spinner" />
          <div className="loading-text">Rendering mind map...</div>
        </div>
      )}
      <div ref={mermaidRef} className="mermaid">
        {formatToMermaid(content)}
      </div>
    </div>
  );
}

MermaidMindMap.propTypes = {
  content: PropTypes.string.isRequired,
};

export default MermaidMindMap;