-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathNodeDetailPanel.tsx
More file actions
320 lines (297 loc) · 14.6 KB
/
Copy pathNodeDetailPanel.tsx
File metadata and controls
320 lines (297 loc) · 14.6 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
'use client';
import { useEffect, useMemo, useState } from 'react';
import { ExternalLink, Network, PanelRightClose, PanelRightOpen } from 'lucide-react';
import { api } from '@/lib/api-client';
import { FILE_TYPES, FileType } from './CustomNode';
interface GraphNodeShape {
id: string;
data: {
label: string;
description?: string;
filePath: string;
fileType?: FileType;
entity?: 'file' | 'module';
moduleKey?: string;
linesOfCode?: number;
exports?: string[];
memberCount?: number;
topFiles?: string[];
dominantTypes?: string[];
internalEdgeCount?: number;
externalEdgeCount?: number;
internalDensity?: number;
metrics?: {
in_degree: number;
out_degree: number;
degree: number;
centrality: number;
};
};
}
interface NodeDetailPanelProps {
repoId: string;
node: GraphNodeShape | null;
incomingEdges: { source: string; label?: string }[];
outgoingEdges: { target: string; label?: string }[];
onNodeClick: (nodeId: string) => void;
onDrillDownModule?: (moduleKey: string) => void;
open: boolean;
onToggleOpen: () => void;
}
export function NodeDetailPanel({
repoId,
node,
incomingEdges,
outgoingEdges,
onNodeClick,
onDrillDownModule,
open,
onToggleOpen,
}: NodeDetailPanelProps) {
const [sourceCode, setSourceCode] = useState('');
const [sourceLoading, setSourceLoading] = useState(false);
const [sourceError, setSourceError] = useState<string | null>(null);
const [expandedSource, setExpandedSource] = useState(false);
const nodeId = node?.id || null;
const filePath = node?.data.filePath || '';
const isModule = node?.data.entity === 'module';
const moduleKey = node?.data.moduleKey || '';
const fileType = node?.data.fileType || 'default';
const config = FILE_TYPES[fileType];
const Icon = config.icon;
useEffect(() => {
let cancelled = false;
async function loadSource() {
if (!nodeId || !filePath || isModule) {
setSourceCode('');
setSourceError(null);
return;
}
setSourceLoading(true);
setSourceError(null);
setExpandedSource(false);
try {
const res = await api.getRepoFileContent(repoId, filePath);
if (!cancelled) {
setSourceCode(res.content || '');
}
} catch {
if (!cancelled) {
setSourceError('Unable to load source preview.');
setSourceCode('');
}
} finally {
if (!cancelled) {
setSourceLoading(false);
}
}
}
loadSource();
return () => {
cancelled = true;
};
}, [filePath, isModule, nodeId, repoId]);
const previewCode = useMemo(() => {
if (!sourceCode) return '';
if (expandedSource) return sourceCode;
return sourceCode.split('\n').slice(0, 80).join('\n');
}, [sourceCode, expandedSource]);
return (
<aside
className={`
relative transition-all duration-200
${open
? 'w-full border-l border-zinc-800/80 bg-zinc-950/90 backdrop-blur-md md:w-[360px] xl:w-[390px]'
: 'w-12 border-l border-zinc-800/60 bg-zinc-950/70 backdrop-blur-sm'}
`}
>
<button
type="button"
onClick={onToggleOpen}
className="absolute left-2 top-2 z-20 inline-flex h-8 w-8 items-center justify-center rounded-md border border-zinc-800 bg-zinc-900/80 text-zinc-300 transition-colors hover:bg-zinc-800"
title={open ? 'Collapse inspector' : 'Expand inspector'}
>
{open ? <PanelRightClose size={14} /> : <PanelRightOpen size={14} />}
</button>
{!open ? null : !node ? (
<div className="flex h-full items-center justify-center px-6 text-center">
<div>
<Network className="mx-auto mb-3 text-zinc-600" size={24} />
<p className="text-sm text-zinc-400">Select a node to inspect dependencies and source preview.</p>
</div>
</div>
) : (
<div className="flex h-full flex-col overflow-hidden">
<div className="border-b border-zinc-800/80 px-4 pb-4 pt-12">
<div className="mb-2 flex items-start justify-between gap-2">
<div className="min-w-0">
<h3 className="truncate text-base font-semibold text-zinc-100">{node.data.label}</h3>
<p className="truncate text-xs text-zinc-400">{node.data.filePath}</p>
</div>
<span
className="inline-flex items-center gap-1 rounded-md border px-1.5 py-0.5 text-[10px] font-medium"
style={{ borderColor: `${config.color}66`, color: config.color, backgroundColor: `${config.color}1A` }}
>
<Icon size={10} />
{config.label}
</span>
</div>
<p className="text-xs leading-relaxed text-zinc-400">{node.data.description || 'No description available.'}</p>
</div>
<div className="flex-1 space-y-5 overflow-y-auto px-4 py-4">
<div className="grid grid-cols-2 gap-2 text-[11px]">
<StatCard label={isModule ? 'Files' : 'LOC'} value={String(isModule ? (node.data.memberCount || 0) : (node.data.linesOfCode || 0))} />
<StatCard label="Degree" value={String(node.data.metrics?.degree || 0)} />
<StatCard label="In" value={String(node.data.metrics?.in_degree || 0)} />
<StatCard label="Out" value={String(node.data.metrics?.out_degree || 0)} />
{isModule && (
<>
<StatCard label="Internal" value={String(node.data.internalEdgeCount || 0)} />
<StatCard label="External" value={String(node.data.externalEdgeCount || 0)} />
<StatCard label="Int. Density" value={String((node.data.internalDensity || 0).toFixed(3))} />
</>
)}
</div>
{isModule && (
<section className="space-y-2">
<div className="flex items-center justify-between">
<h4 className="text-xs font-medium uppercase tracking-wider text-zinc-500">Module Scope</h4>
{!!moduleKey && onDrillDownModule && (
<button
type="button"
onClick={() => onDrillDownModule(moduleKey)}
className="rounded-md border border-emerald-700/70 bg-emerald-500/10 px-2 py-0.5 text-[10px] text-emerald-300 hover:bg-emerald-500/20"
>
Open File Graph
</button>
)}
</div>
{node.data.moduleKey && (
<p className="truncate rounded-md border border-zinc-800 bg-zinc-900/70 px-2 py-1 text-[11px] text-zinc-300">
{node.data.moduleKey}
</p>
)}
{!!node.data.dominantTypes?.length && (
<div className="flex flex-wrap gap-1.5">
{node.data.dominantTypes.map((type) => (
<span key={type} className="rounded-md border border-zinc-700 bg-zinc-900 px-2 py-0.5 text-[10px] text-zinc-300">
{type}
</span>
))}
</div>
)}
</section>
)}
{isModule && !!node.data.topFiles?.length && (
<section>
<h4 className="mb-2 text-xs font-medium uppercase tracking-wider text-zinc-500">Top Files</h4>
<div className="space-y-1.5">
{node.data.topFiles.slice(0, 8).map((item) => (
<div
key={item}
className="truncate rounded-md border border-zinc-800 bg-zinc-900/70 px-2.5 py-1.5 text-[11px] text-zinc-300"
>
{item}
</div>
))}
</div>
</section>
)}
{!!node.data.exports?.length && (
<section>
<h4 className="mb-2 text-xs font-medium uppercase tracking-wider text-zinc-500">Exports</h4>
<div className="flex flex-wrap gap-1.5">
{node.data.exports.slice(0, 12).map((item) => (
<span key={item} className="rounded-md border border-zinc-700 bg-zinc-900 px-2 py-0.5 text-[10px] text-zinc-300">
{item}
</span>
))}
</div>
</section>
)}
<RelationList
title="Imported By"
items={incomingEdges.map((edge) => ({ id: edge.source, label: edge.label }))}
onNodeClick={onNodeClick}
/>
<RelationList
title="Imports From"
items={outgoingEdges.map((edge) => ({ id: edge.target, label: edge.label }))}
onNodeClick={onNodeClick}
/>
{!isModule && (
<section>
<div className="mb-2 flex items-center justify-between">
<h4 className="text-xs font-medium uppercase tracking-wider text-zinc-500">Source Preview</h4>
<button
type="button"
onClick={() => setExpandedSource((prev) => !prev)}
disabled={!sourceCode || sourceLoading}
className="rounded-md border border-zinc-700 px-2 py-0.5 text-[10px] text-zinc-300 transition-colors hover:bg-zinc-800 disabled:opacity-40"
>
{expandedSource ? 'Show Less' : 'View Source Code'}
</button>
</div>
<div className="overflow-hidden rounded-lg border border-zinc-800 bg-zinc-950">
{sourceLoading ? (
<div className="p-3 text-xs text-zinc-500">Loading source...</div>
) : sourceError ? (
<div className="p-3 text-xs text-rose-400">{sourceError}</div>
) : previewCode ? (
<pre className="max-h-[300px] overflow-auto p-3 text-[11px] leading-relaxed text-zinc-300">
{previewCode}
</pre>
) : (
<div className="p-3 text-xs text-zinc-500">No source preview available.</div>
)}
</div>
</section>
)}
</div>
</div>
)}
</aside>
);
}
function StatCard({ label, value }: { label: string; value: string }) {
return (
<div className="rounded-lg border border-zinc-800 bg-zinc-900/70 px-2 py-2 text-center">
<div className="text-sm font-semibold text-zinc-200">{value}</div>
<div className="text-[10px] uppercase tracking-wider text-zinc-500">{label}</div>
</div>
);
}
function RelationList({
title,
items,
onNodeClick,
}: {
title: string;
items: Array<{ id: string; label?: string }>;
onNodeClick: (nodeId: string) => void;
}) {
if (items.length === 0) {
return null;
}
return (
<section>
<h4 className="mb-2 text-xs font-medium uppercase tracking-wider text-zinc-500">{title}</h4>
<div className="space-y-1.5">
{items.slice(0, 12).map((item) => (
<button
key={`${title}-${item.id}`}
type="button"
onClick={() => onNodeClick(item.id)}
className="flex w-full items-center justify-between rounded-md border border-zinc-800 bg-zinc-900/70 px-2.5 py-1.5 text-left text-[11px] text-zinc-300 transition-colors hover:border-zinc-700 hover:bg-zinc-800"
>
<span className="min-w-0 truncate">{item.id.split('/').pop()}</span>
<span className="ml-2 inline-flex items-center gap-1 text-[10px] text-zinc-500">
{item.label || 'imports'}
<ExternalLink size={10} />
</span>
</button>
))}
</div>
</section>
);
}