forked from Vero-protocol/vero-guardian-dashboard
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathContractCallGraph.tsx
More file actions
413 lines (371 loc) · 14.1 KB
/
Copy pathContractCallGraph.tsx
File metadata and controls
413 lines (371 loc) · 14.1 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
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
'use client';
import {
useCallback,
useEffect,
useMemo,
useRef,
useState,
type ReactElement,
} from 'react';
import { GitGraph, Maximize2, RotateCcw, ZoomIn, ZoomOut } from 'lucide-react';
import { useTranslation } from 'react-i18next';
import {
buildCytoscapeElements,
buildStylesheet,
computeInDegree,
DEFAULT_CALL_GRAPH,
findLeaves,
findRoots,
NODE_COLORS,
type CallGraph,
type CallGraphNode,
} from './contractCallGraph';
// ---------------------------------------------------------------------------
// Legend
// ---------------------------------------------------------------------------
const LEGEND_KINDS: CallGraphNode['kind'][] = ['entry', 'internal', 'external', 'event'];
const KIND_LABEL_KEYS: Record<CallGraphNode['kind'], string> = {
entry: 'contractCallGraph.kindEntry',
internal: 'contractCallGraph.kindInternal',
external: 'contractCallGraph.kindExternal',
event: 'contractCallGraph.kindEvent',
};
function Legend(): ReactElement {
const { t } = useTranslation();
return (
<div className="flex flex-wrap gap-3">
{LEGEND_KINDS.map((kind) => (
<div key={kind} className="flex items-center gap-1.5 text-xs text-slate-600 dark:text-slate-300">
<span
className="w-3 h-3 rounded-sm shrink-0"
style={{ backgroundColor: NODE_COLORS[kind].bg }}
aria-hidden="true"
/>
{t(KIND_LABEL_KEYS[kind])}
</div>
))}
<div className="flex items-center gap-1.5 text-xs text-slate-600 dark:text-slate-300">
<span className="w-5 h-0 border-t-2 border-dashed border-slate-400 shrink-0" aria-hidden="true" />
{t('contractCallGraph.edgeConditional')}
</div>
</div>
);
}
// ---------------------------------------------------------------------------
// Node detail panel
// ---------------------------------------------------------------------------
interface NodeDetail {
id: string;
label: string;
kind: CallGraphNode['kind'];
gasCost: number;
inDegree: number;
callers: string[];
callees: string[];
}
function NodeDetailPanel({ detail, onClose }: { detail: NodeDetail; onClose: () => void }): ReactElement {
const { t } = useTranslation();
return (
<div
role="region"
aria-label={t('contractCallGraph.detailAriaLabel')}
className="absolute bottom-3 left-3 right-3 sm:right-auto sm:w-72 bg-white dark:bg-slate-900 border border-slate-200 dark:border-slate-700 rounded-xl shadow-xl p-4 z-10 text-xs space-y-3"
>
<div className="flex items-start justify-between gap-2">
<div>
<p className="font-mono font-semibold text-slate-900 dark:text-white text-sm">{detail.label}</p>
<span
className="inline-block mt-1 rounded-full px-2 py-0.5 text-white font-semibold"
style={{ backgroundColor: NODE_COLORS[detail.kind].bg }}
>
{t(KIND_LABEL_KEYS[detail.kind])}
</span>
</div>
<button
type="button"
onClick={onClose}
aria-label={t('contractCallGraph.closeDetail')}
className="shrink-0 text-slate-400 hover:text-slate-600 dark:hover:text-slate-200 focus:outline-none focus:ring-2 focus:ring-indigo-500 rounded"
>
✕
</button>
</div>
{detail.gasCost > 0 && (
<div className="flex justify-between">
<span className="text-slate-500 dark:text-slate-400">{t('contractCallGraph.gasCost')}</span>
<span className="font-mono font-medium text-slate-800 dark:text-slate-200">
{new Intl.NumberFormat('en-US', { notation: 'compact', maximumFractionDigits: 1 }).format(detail.gasCost)}
</span>
</div>
)}
<div className="flex justify-between">
<span className="text-slate-500 dark:text-slate-400">{t('contractCallGraph.inDegree')}</span>
<span className="font-mono font-medium text-slate-800 dark:text-slate-200">{detail.inDegree}</span>
</div>
{detail.callers.length > 0 && (
<div>
<p className="text-slate-500 dark:text-slate-400 mb-1">{t('contractCallGraph.callers')}</p>
<ul className="flex flex-wrap gap-1">
{detail.callers.map((c) => (
<li key={c} className="font-mono px-1.5 py-0.5 bg-slate-100 dark:bg-slate-800 rounded text-slate-700 dark:text-slate-300">{c}</li>
))}
</ul>
</div>
)}
{detail.callees.length > 0 && (
<div>
<p className="text-slate-500 dark:text-slate-400 mb-1">{t('contractCallGraph.callees')}</p>
<ul className="flex flex-wrap gap-1">
{detail.callees.map((c) => (
<li key={c} className="font-mono px-1.5 py-0.5 bg-slate-100 dark:bg-slate-800 rounded text-slate-700 dark:text-slate-300">{c}</li>
))}
</ul>
</div>
)}
</div>
);
}
// ---------------------------------------------------------------------------
// Main component
// ---------------------------------------------------------------------------
export interface ContractCallGraphProps {
/** The call graph to visualize. Defaults to the Vero contract demo graph. */
graph?: CallGraph;
/** Canvas height in pixels. Defaults to 480. */
height?: number;
}
/**
* ContractCallGraph — issue #53
*
* Renders a directed call graph of Vero smart contract functions using
* Cytoscape.js. Cytoscape is imported dynamically to keep the SSR bundle
* clean (it accesses `window` internally).
*
* All graph data lives in local state — no network calls.
*/
export default function ContractCallGraph({
graph = DEFAULT_CALL_GRAPH,
height = 480,
}: ContractCallGraphProps): ReactElement {
const { t } = useTranslation();
const containerRef = useRef<HTMLDivElement>(null);
// cy instance stored in a ref — never triggers a re-render
const cyRef = useRef<cytoscape.Core | null>(null);
const [selectedNode, setSelectedNode] = useState<NodeDetail | null>(null);
const [isReady, setIsReady] = useState(false);
const elements = useMemo(() => buildCytoscapeElements(graph), [graph]);
const stylesheet = useMemo(() => buildStylesheet(), []);
const inDegrees = useMemo(() => computeInDegree(graph), [graph]);
// Build node lookup maps for the detail panel
const nodeMap = useMemo(
() => new Map(graph.nodes.map((n) => [n.id, n])),
[graph],
);
const buildDetail = useCallback(
(id: string): NodeDetail | null => {
const node = nodeMap.get(id);
if (!node) return null;
return {
id,
label: node.label,
kind: node.kind,
gasCost: node.gasCost ?? 0,
inDegree: inDegrees[id] ?? 0,
callers: graph.edges.filter((e) => e.target === id).map((e) => e.source),
callees: graph.edges.filter((e) => e.source === id).map((e) => e.target),
};
},
[nodeMap, inDegrees, graph.edges],
);
// Initialise Cytoscape lazily (dynamic import avoids SSR issues)
useEffect(() => {
if (!containerRef.current) return;
let destroyed = false;
import('cytoscape').then(({ default: cytoscape }) => {
if (destroyed || !containerRef.current) return;
const cy = cytoscape({
container: containerRef.current,
elements,
style: stylesheet as any,
layout: {
name: 'breadthfirst',
directed: true,
padding: 24,
spacingFactor: 1.4,
} as cytoscape.BreadthFirstLayoutOptions,
userZoomingEnabled: true,
userPanningEnabled: true,
boxSelectionEnabled: false,
minZoom: 0.3,
maxZoom: 3,
});
cyRef.current = cy;
setIsReady(true);
// Node tap — show detail and highlight neighbourhood
cy.on('tap', 'node', (evt) => {
const nodeId = evt.target.id() as string;
const detail = buildDetail(nodeId);
setSelectedNode(detail);
// Dim all, then highlight the tapped node + its neighbourhood
cy.elements().addClass('dimmed').removeClass('highlighted');
const neighbourhood = evt.target.closedNeighborhood();
neighbourhood.removeClass('dimmed').addClass('highlighted');
});
// Tap on background — reset
cy.on('tap', (evt) => {
if (evt.target === cy) {
cy.elements().removeClass('dimmed highlighted');
setSelectedNode(null);
}
});
});
return () => {
destroyed = true;
cyRef.current?.destroy();
cyRef.current = null;
setIsReady(false);
};
// Intentionally only run on mount / graph change
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [graph]);
// Toolbar actions
const handleZoomIn = useCallback(() => {
cyRef.current?.zoom(cyRef.current.zoom() * 1.25);
cyRef.current?.center();
}, []);
const handleZoomOut = useCallback(() => {
cyRef.current?.zoom(cyRef.current.zoom() * 0.8);
cyRef.current?.center();
}, []);
const handleFit = useCallback(() => {
cyRef.current?.fit(undefined, 24);
}, []);
const handleReset = useCallback(() => {
cyRef.current?.fit(undefined, 24);
cyRef.current?.elements().removeClass('dimmed highlighted');
setSelectedNode(null);
}, []);
// Graph stats for the summary strip
const roots = useMemo(() => findRoots(graph), [graph]);
const leaves = useMemo(() => findLeaves(graph), [graph]);
if (graph.nodes.length === 0) {
return (
<section
aria-labelledby="contract-call-graph-title"
className="bg-white dark:bg-slate-900/50 border border-slate-200 dark:border-slate-800 rounded-2xl p-6 shadow-lg"
>
<h3
id="contract-call-graph-title"
className="text-lg font-semibold text-slate-900 dark:text-white flex items-center gap-2 mb-2"
>
<GitGraph className="w-5 h-5 text-indigo-500" aria-hidden="true" />
{t('contractCallGraph.heading')}
</h3>
<p className="text-sm text-slate-500 dark:text-slate-400 py-8 text-center">
{t('contractCallGraph.empty')}
</p>
</section>
);
}
return (
<section
aria-labelledby="contract-call-graph-title"
className="bg-white dark:bg-slate-900/50 border border-slate-200 dark:border-slate-800 rounded-2xl p-6 shadow-lg"
>
{/* Header */}
<div className="flex flex-wrap items-start justify-between gap-4 mb-4">
<div>
<h3
id="contract-call-graph-title"
className="text-lg font-semibold text-slate-900 dark:text-white flex items-center gap-2"
>
<GitGraph className="w-5 h-5 text-indigo-500" aria-hidden="true" />
{t('contractCallGraph.heading')}
</h3>
<p className="text-sm text-slate-500 dark:text-slate-400 mt-0.5">
{t('contractCallGraph.subheading')}
</p>
</div>
{/* Toolbar */}
<div className="flex items-center gap-1" role="toolbar" aria-label={t('contractCallGraph.toolbarAriaLabel')}>
{[
{ icon: ZoomIn, label: t('contractCallGraph.zoomIn'), action: handleZoomIn },
{ icon: ZoomOut, label: t('contractCallGraph.zoomOut'), action: handleZoomOut },
{ icon: Maximize2, label: t('contractCallGraph.fit'), action: handleFit },
{ icon: RotateCcw, label: t('contractCallGraph.reset'), action: handleReset },
].map(({ icon: Icon, label, action }) => (
<button
key={label}
type="button"
onClick={action}
aria-label={label}
disabled={!isReady}
className="p-2 rounded-lg text-slate-500 hover:bg-slate-100 dark:hover:bg-slate-800 disabled:opacity-40 disabled:cursor-not-allowed transition-colors focus:outline-none focus:ring-2 focus:ring-indigo-500"
>
<Icon className="w-4 h-4" aria-hidden="true" />
</button>
))}
</div>
</div>
{/* Legend */}
<div className="mb-3">
<Legend />
</div>
{/* Stats strip */}
<div className="flex flex-wrap gap-4 mb-4 text-xs text-slate-500 dark:text-slate-400">
<span>
<span className="font-semibold text-slate-700 dark:text-slate-300">{graph.nodes.length}</span>{' '}
{t('contractCallGraph.statNodes')}
</span>
<span>
<span className="font-semibold text-slate-700 dark:text-slate-300">{graph.edges.length}</span>{' '}
{t('contractCallGraph.statEdges')}
</span>
<span>
<span className="font-semibold text-slate-700 dark:text-slate-300">{roots.length}</span>{' '}
{t('contractCallGraph.statEntryPoints')}
</span>
<span>
<span className="font-semibold text-slate-700 dark:text-slate-300">{leaves.length}</span>{' '}
{t('contractCallGraph.statLeaves')}
</span>
</div>
{/* Graph canvas */}
<div className="relative rounded-xl overflow-hidden border border-slate-200 dark:border-slate-700 bg-slate-50 dark:bg-slate-950">
{/* Loading overlay */}
{!isReady && (
<div
className="absolute inset-0 flex items-center justify-center bg-slate-50 dark:bg-slate-950 z-10"
aria-live="polite"
>
<p className="text-sm text-slate-400 animate-pulse">{t('contractCallGraph.loading')}</p>
</div>
)}
{/* Hint */}
{isReady && (
<p className="absolute top-2 right-3 text-xs text-slate-400 dark:text-slate-500 select-none z-10 pointer-events-none">
{t('contractCallGraph.tapHint')}
</p>
)}
{/* Cytoscape mount point */}
<div
ref={containerRef}
data-testid="cytoscape-container"
style={{ height }}
aria-label={t('contractCallGraph.canvasAriaLabel')}
aria-description={t('contractCallGraph.canvasDescription')}
/>
{/* Node detail overlay */}
{selectedNode && (
<NodeDetailPanel
detail={selectedNode}
onClose={() => {
cyRef.current?.elements().removeClass('dimmed highlighted');
setSelectedNode(null);
}}
/>
)}
</div>
</section>
);
}