-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathreact-vue-debugger.tsx
More file actions
314 lines (297 loc) · 11.3 KB
/
react-vue-debugger.tsx
File metadata and controls
314 lines (297 loc) · 11.3 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
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
import React, { useState, useEffect } from 'react';
import { Search, Zap, Layers, RefreshCw, ChevronRight, ChevronDown, AlertCircle, CheckCircle } from 'lucide-react';
const ComponentDebugger = () => {
const [framework, setFramework] = useState('react');
const [components, setComponents] = useState([]);
const [selectedComponent, setSelectedComponent] = useState(null);
const [expandedNodes, setExpandedNodes] = useState(new Set());
const [searchTerm, setSearchTerm] = useState('');
const [isScanning, setIsScanning] = useState(false);
// Simulate component tree data
const mockReactComponents = [
{
id: 'app-1',
name: 'App',
type: 'Component',
props: { theme: 'dark', version: '1.0' },
state: { isLoading: false, user: { name: 'John', role: 'admin' } },
renderCount: 3,
renderTime: '2.4ms',
children: [
{
id: 'header-1',
name: 'Header',
type: 'Component',
props: { title: 'Dashboard' },
state: { isMenuOpen: false },
renderCount: 2,
renderTime: '0.8ms',
children: []
},
{
id: 'sidebar-1',
name: 'Sidebar',
type: 'Component',
props: { collapsed: false },
state: { activeItem: 'home' },
renderCount: 5,
renderTime: '1.2ms',
children: [
{
id: 'nav-1',
name: 'Navigation',
type: 'Component',
props: { items: ['Home', 'Profile', 'Settings'] },
state: {},
renderCount: 5,
renderTime: '0.5ms',
children: []
}
]
},
{
id: 'content-1',
name: 'Content',
type: 'Component',
props: { fullWidth: true },
state: { data: [1, 2, 3, 4, 5] },
renderCount: 8,
renderTime: '3.1ms',
children: []
}
]
}
];
const mockVueComponents = [
{
id: 'app-v1',
name: 'App',
type: 'Component',
props: { mode: 'production' },
state: { count: 42, items: ['a', 'b', 'c'] },
renderCount: 4,
renderTime: '2.1ms',
children: [
{
id: 'layout-v1',
name: 'MainLayout',
type: 'Component',
props: { fixed: true },
state: { scrollY: 0 },
renderCount: 4,
renderTime: '1.5ms',
children: []
}
]
}
];
useEffect(() => {
setComponents(framework === 'react' ? mockReactComponents : mockVueComponents);
setExpandedNodes(new Set(['app-1', 'app-v1']));
}, [framework]);
const scanComponents = () => {
setIsScanning(true);
setTimeout(() => {
setComponents(framework === 'react' ? mockReactComponents : mockVueComponents);
setIsScanning(false);
}, 800);
};
const toggleExpand = (id) => {
const newExpanded = new Set(expandedNodes);
if (newExpanded.has(id)) {
newExpanded.delete(id);
} else {
newExpanded.add(id);
}
setExpandedNodes(newExpanded);
};
const renderComponentTree = (component, depth = 0) => {
const hasChildren = component.children && component.children.length > 0;
const isExpanded = expandedNodes.has(component.id);
const isSelected = selectedComponent?.id === component.id;
if (searchTerm && !component.name.toLowerCase().includes(searchTerm.toLowerCase())) {
return null;
}
return (
<div key={component.id} style={{ marginLeft: depth * 20 }}>
<div
onClick={() => setSelectedComponent(component)}
className={`flex items-center gap-2 px-3 py-2 rounded cursor-pointer transition-all ${
isSelected ? 'bg-blue-600' : 'hover:bg-gray-800'
}`}
>
{hasChildren && (
<button onClick={(e) => { e.stopPropagation(); toggleExpand(component.id); }}>
{isExpanded ? <ChevronDown size={16} /> : <ChevronRight size={16} />}
</button>
)}
{!hasChildren && <div style={{ width: 16 }} />}
<Layers size={16} className="text-blue-400" />
<span className="font-mono text-sm"><{component.name} /></span>
<span className="text-xs text-gray-500 ml-auto">{component.renderTime}</span>
</div>
{isExpanded && hasChildren && (
<div>
{component.children.map(child => renderComponentTree(child, depth + 1))}
</div>
)}
</div>
);
};
const renderValue = (value, key) => {
if (typeof value === 'object' && value !== null) {
return (
<div className="ml-4 border-l-2 border-gray-700 pl-3">
{Object.entries(value).map(([k, v]) => (
<div key={k} className="mb-1">
<span className="text-purple-400">{k}:</span> {renderValue(v, k)}
</div>
))}
</div>
);
}
return <span className="text-green-400">{JSON.stringify(value)}</span>;
};
return (
<div className="bg-black text-white h-screen flex flex-col" style={{ fontFamily: 'system-ui, -apple-system, sans-serif' }}>
{/* Header */}
<div className="bg-gray-900 border-b border-gray-800 px-4 py-3">
<div className="flex items-center justify-between">
<div className="flex items-center gap-3">
<Zap className="text-blue-500" size={24} />
<h1 className="text-xl font-bold">Component Debugger</h1>
</div>
<div className="flex gap-2">
<button
onClick={() => setFramework('react')}
className={`px-4 py-2 rounded transition-all ${
framework === 'react' ? 'bg-blue-600' : 'bg-gray-800 hover:bg-gray-700'
}`}
>
React
</button>
<button
onClick={() => setFramework('vue')}
className={`px-4 py-2 rounded transition-all ${
framework === 'vue' ? 'bg-green-600' : 'bg-gray-800 hover:bg-gray-700'
}`}
>
Vue
</button>
<button
onClick={scanComponents}
disabled={isScanning}
className="px-4 py-2 bg-gray-800 hover:bg-gray-700 rounded transition-all flex items-center gap-2"
>
<RefreshCw size={16} className={isScanning ? 'animate-spin' : ''} />
Scan
</button>
</div>
</div>
</div>
{/* Main Content */}
<div className="flex flex-1 overflow-hidden">
{/* Component Tree */}
<div className="w-1/2 border-r border-gray-800 flex flex-col">
<div className="p-3 border-b border-gray-800">
<div className="relative">
<Search className="absolute left-3 top-2.5 text-gray-500" size={18} />
<input
type="text"
placeholder="Search components..."
value={searchTerm}
onChange={(e) => setSearchTerm(e.target.value)}
className="w-full bg-gray-900 border border-gray-700 rounded px-10 py-2 text-sm focus:outline-none focus:border-blue-500"
/>
</div>
</div>
<div className="flex-1 overflow-auto p-2">
{components.map(component => renderComponentTree(component))}
</div>
</div>
{/* Details Panel */}
<div className="w-1/2 flex flex-col overflow-auto">
{selectedComponent ? (
<div className="p-4">
<div className="mb-6">
<h2 className="text-2xl font-bold mb-2 text-blue-400"><{selectedComponent.name} /></h2>
<div className="flex items-center gap-4 text-sm text-gray-400">
<span className="flex items-center gap-1">
<CheckCircle size={14} />
Renders: {selectedComponent.renderCount}
</span>
<span>Time: {selectedComponent.renderTime}</span>
</div>
</div>
{/* Props Section */}
<div className="mb-6 bg-gray-900 rounded-lg p-4 border border-gray-800">
<h3 className="text-lg font-semibold mb-3 text-yellow-400">Props</h3>
<div className="font-mono text-sm">
{Object.keys(selectedComponent.props).length > 0 ? (
Object.entries(selectedComponent.props).map(([key, value]) => (
<div key={key} className="mb-2">
<span className="text-purple-400">{key}:</span> {renderValue(value, key)}
</div>
))
) : (
<span className="text-gray-500">No props</span>
)}
</div>
</div>
{/* State Section */}
<div className="mb-6 bg-gray-900 rounded-lg p-4 border border-gray-800">
<h3 className="text-lg font-semibold mb-3 text-green-400">State</h3>
<div className="font-mono text-sm">
{Object.keys(selectedComponent.state).length > 0 ? (
Object.entries(selectedComponent.state).map(([key, value]) => (
<div key={key} className="mb-2">
<span className="text-purple-400">{key}:</span> {renderValue(value, key)}
</div>
))
) : (
<span className="text-gray-500">No state</span>
)}
</div>
</div>
{/* Performance Metrics */}
<div className="bg-gray-900 rounded-lg p-4 border border-gray-800">
<h3 className="text-lg font-semibold mb-3 text-orange-400">Performance</h3>
<div className="space-y-2 text-sm">
<div className="flex justify-between">
<span className="text-gray-400">Render Time:</span>
<span className="font-mono">{selectedComponent.renderTime}</span>
</div>
<div className="flex justify-between">
<span className="text-gray-400">Total Renders:</span>
<span className="font-mono">{selectedComponent.renderCount}</span>
</div>
<div className="flex items-center gap-2 mt-3">
{selectedComponent.renderCount > 5 ? (
<>
<AlertCircle size={16} className="text-yellow-500" />
<span className="text-yellow-500 text-xs">High render count detected</span>
</>
) : (
<>
<CheckCircle size={16} className="text-green-500" />
<span className="text-green-500 text-xs">Performance looks good</span>
</>
)}
</div>
</div>
</div>
</div>
) : (
<div className="flex-1 flex items-center justify-center text-gray-500">
<div className="text-center">
<Layers size={48} className="mx-auto mb-4 opacity-50" />
<p>Select a component to view details</p>
</div>
</div>
)}
</div>
</div>
</div>
);
};
export default ComponentDebugger;