-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathMermaidViewer.jsx
More file actions
248 lines (219 loc) · 7.11 KB
/
MermaidViewer.jsx
File metadata and controls
248 lines (219 loc) · 7.11 KB
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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
import React, { useState, useEffect, useRef } from 'react';
import { Box, CircularProgress, IconButton, Tooltip } from '@mui/material';
import RefreshIcon from '@mui/icons-material/Refresh';
import ZoomInIcon from '@mui/icons-material/ZoomIn';
import ZoomOutIcon from '@mui/icons-material/ZoomOut';
import RestartAltIcon from '@mui/icons-material/RestartAlt';
import mermaid from 'mermaid';
import { useTranslation } from 'react-i18next';
import { apiFetch } from '../services/api';
export default function MermaidViewer({ filename, projectName, className = '' }) {
const { t } = useTranslation();
const [mermaidContent, setMermaidContent] = useState('');
const [loading, setLoading] = useState(true);
const [error, setError] = useState(null);
const [refreshKey, setRefreshKey] = useState(0);
const [zoom, setZoom] = useState(1);
const mermaidRef = useRef(null);
// Initialize mermaid
useEffect(() => {
mermaid.initialize({
startOnLoad: true,
theme: 'default',
securityLevel: 'loose',
fontFamily: 'Arial, sans-serif',
});
}, []);
// Function to fetch mermaid file content
const fetchMermaidContent = async () => {
try {
setLoading(true);
setError(null);
const response = await apiFetch(
`/api/workspace/${encodeURIComponent(projectName)}/files/${filename}?v=${refreshKey}`
);
if (!response.ok) {
throw new Error(`Failed to load file: ${response.statusText}`);
}
const text = await response.text();
setMermaidContent(text);
setLoading(false);
} catch (err) {
console.error('Error loading mermaid file:', err);
setError(err.message);
setLoading(false);
}
};
// Initial load
useEffect(() => {
fetchMermaidContent();
}, [filename, projectName, refreshKey]);
// Render mermaid diagram whenever content changes
useEffect(() => {
if (mermaidContent && mermaidRef.current) {
const renderMermaid = async () => {
try {
// Clear previous content
mermaidRef.current.innerHTML = '';
// Generate a unique ID for this diagram
const id = `mermaid-${Date.now()}-${Math.random().toString(36).substr(2, 9)}`;
// Render the diagram
const { svg } = await mermaid.render(id, mermaidContent);
// Insert the SVG
mermaidRef.current.innerHTML = svg;
} catch (err) {
console.error('Error rendering mermaid diagram:', err);
setError(`Failed to render diagram: ${err.message}`);
}
};
renderMermaid();
}
}, [mermaidContent]);
// Handler for manual reload
const handleReload = () => {
setRefreshKey(prev => prev + 1);
};
// Zoom handlers
const handleZoomIn = () => {
setZoom(prev => Math.min(prev + 0.25, 3));
};
const handleZoomOut = () => {
setZoom(prev => Math.max(prev - 0.25, 0.25));
};
const handleZoomReset = () => {
setZoom(1);
};
// Listen for file changes via claudeHook events
useEffect(() => {
const handleClaudeHook = (event) => {
// Check if this is a PostHook event for our file
if (event.type === 'claudeHook' && event.detail) {
const { hook, file } = event.detail;
console.log('[MermaidViewer] Received claudeHook:', { hook, file, currentFilename: filename });
if (hook === 'PostHook' && file) {
// Handle both absolute and relative paths
const normalizedFile = file.replace(/\\/g, '/');
const normalizedFilename = filename.replace(/\\/g, '/');
console.log('[MermaidViewer] Normalized paths:', { normalizedFile, normalizedFilename });
// Check if paths match (exact match or file ends with filename)
const exactMatch = normalizedFile === normalizedFilename;
const endsWithMatch = normalizedFile.endsWith('/' + normalizedFilename);
console.log('[MermaidViewer] Match check:', { exactMatch, endsWithMatch });
if (exactMatch || endsWithMatch) {
console.log('[MermaidViewer] ✓ Match found! Refreshing diagram for', filename);
// Increment refresh key to trigger reload
setRefreshKey(prev => prev + 1);
} else {
console.log('[MermaidViewer] ✗ No match for', filename);
}
}
}
};
window.addEventListener('claudeHook', handleClaudeHook);
return () => {
window.removeEventListener('claudeHook', handleClaudeHook);
};
}, [filename]);
if (loading) {
return (
<Box
className={className}
display="flex"
justifyContent="center"
alignItems="center"
height="100%"
>
<CircularProgress />
</Box>
);
}
if (error) {
return (
<Box
className={className}
p={2}
color="error.main"
>
{t('mermaidViewer.errorLoading')} {error}
</Box>
);
}
return (
<Box className={className} height="100%" width="100%" position="relative">
{/* Control buttons */}
<Box
sx={{
position: 'absolute',
top: 8,
right: 18,
zIndex: 1000,
display: 'flex',
gap: 0.5,
bgcolor: 'background.paper',
boxShadow: 1,
borderRadius: 1,
p: 0.5
}}
>
<Tooltip title={t('mermaidViewer.zoomOut')}>
<IconButton
onClick={handleZoomOut}
disabled={loading || zoom <= 0.25}
size="small"
sx={{ '&:hover': { bgcolor: 'action.hover' } }}
>
<ZoomOutIcon fontSize="small" />
</IconButton>
</Tooltip>
<Tooltip title={t('mermaidViewer.resetZoom', { percent: Math.round(zoom * 100) })}>
<IconButton
onClick={handleZoomReset}
disabled={loading || zoom === 1}
size="small"
sx={{ '&:hover': { bgcolor: 'action.hover' } }}
>
<RestartAltIcon fontSize="small" />
</IconButton>
</Tooltip>
<Tooltip title={t('mermaidViewer.zoomIn')}>
<IconButton
onClick={handleZoomIn}
disabled={loading || zoom >= 3}
size="small"
sx={{ '&:hover': { bgcolor: 'action.hover' } }}
>
<ZoomInIcon fontSize="small" />
</IconButton>
</Tooltip>
<Tooltip title={t('mermaidViewer.reloadFile')}>
<IconButton
onClick={handleReload}
disabled={loading}
size="small"
sx={{ '&:hover': { bgcolor: 'action.hover' } }}
>
<RefreshIcon fontSize="small" />
</IconButton>
</Tooltip>
</Box>
<Box
ref={mermaidRef}
sx={{
height: '100%',
overflow: 'auto',
p: 3,
display: 'flex',
justifyContent: 'center',
alignItems: 'center',
'& svg': {
maxWidth: '100%',
height: 'auto',
transform: `scale(${zoom})`,
transformOrigin: 'center center',
transition: 'transform 0.2s ease-in-out'
}
}}
/>
</Box>
);
}