File size: 28,963 Bytes
41ad1b0
 
 
 
 
 
 
05251fc
33a4fd2
41ad1b0
 
05251fc
41ad1b0
 
 
 
d242052
 
 
05251fc
c34a74f
 
41ad1b0
c34a74f
 
41ad1b0
 
 
 
 
 
 
 
 
 
 
 
 
 
d242052
41ad1b0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d242052
 
 
 
 
 
41ad1b0
 
 
 
 
 
 
 
 
 
 
 
33a4fd2
41ad1b0
d242052
33a4fd2
05251fc
 
41ad1b0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
05251fc
41ad1b0
05251fc
41ad1b0
 
 
 
 
 
 
 
 
05251fc
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
41ad1b0
 
 
 
 
 
 
 
05251fc
41ad1b0
 
 
05251fc
 
 
 
 
 
 
41ad1b0
 
 
 
05251fc
41ad1b0
 
 
 
 
 
 
 
 
05251fc
 
 
 
41ad1b0
 
 
 
 
 
 
 
 
05251fc
41ad1b0
 
 
 
 
 
05251fc
 
 
 
 
41ad1b0
 
 
 
05251fc
 
 
 
 
 
41ad1b0
05251fc
 
41ad1b0
 
 
 
05251fc
 
 
41ad1b0
 
05251fc
41ad1b0
 
05251fc
 
 
41ad1b0
05251fc
 
 
 
33a4fd2
41ad1b0
 
05251fc
41ad1b0
05251fc
d242052
05251fc
 
 
 
 
 
 
 
d242052
41ad1b0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
33a4fd2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
41ad1b0
 
 
 
 
 
 
33a4fd2
05251fc
33a4fd2
 
41ad1b0
 
05251fc
33a4fd2
41ad1b0
 
05251fc
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
41ad1b0
c34a74f
 
 
d242052
 
 
41ad1b0
 
05251fc
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c34a74f
 
05251fc
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c34a74f
05251fc
41ad1b0
 
 
d242052
 
 
 
41ad1b0
 
 
c34a74f
41ad1b0
 
 
 
 
 
 
 
 
 
33a4fd2
 
 
 
 
 
 
41ad1b0
 
 
 
 
 
c34a74f
 
 
05251fc
c34a74f
05251fc
 
41ad1b0
 
 
 
 
05251fc
c34a74f
 
05251fc
 
 
c34a74f
 
41ad1b0
 
 
 
 
05251fc
c34a74f
05251fc
 
c34a74f
 
05251fc
 
c34a74f
 
 
41ad1b0
 
 
d242052
05251fc
33a4fd2
 
 
 
 
 
41ad1b0
 
05251fc
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
41ad1b0
 
 
 
d242052
05251fc
41ad1b0
 
 
05251fc
41ad1b0
 
 
 
d242052
 
05251fc
 
 
 
 
 
 
 
 
 
 
 
d242052
 
 
 
 
 
 
 
 
 
41ad1b0
 
 
 
 
 
 
33a4fd2
 
 
 
d242052
 
 
 
 
33a4fd2
 
 
c34a74f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
41ad1b0
05251fc
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
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
import React, {
  useState,
  useEffect,
  useRef,
  useCallback,
  useMemo,
} from 'react';
import { Info, RefreshCw, GitBranch, Network, Layers, RotateCw, X } from 'lucide-react';
import { ReactFlowProvider, useReactFlow } from 'reactflow';

import { apiUrl, extractErrorMessage } from '../../../lib/api';
import { normalizeGraphPath, resolveGraphNode } from '../../../lib/graphPathUtils';
import { GraphCanvas } from './GraphCanvas';
import { GraphToolbar } from './GraphToolbar';
import { SearchBar } from './SearchBar';
import { NodeDetailsPanel } from './NodeDetailsPanel';
import { GraphFilterBar } from './GraphFilterBar';
import { GraphBreadcrumbNav } from './GraphBreadcrumbNav';
import { ArchitectureDiagramModal } from './ArchitectureDiagramModal';
import { GraphWorkspaceProvider, useGraphWorkspace } from './workspaceStore';
import { computeGraphStats } from './graphStats';
import { CATEGORY_COLORS, CATEGORY_LABELS } from './types';
import type { GraphNode, GraphEdge, GraphMode, GraphResponse } from './types';
import { EmptyState } from '../../ui/EmptyState';
import { Button } from '../../ui/Button';

// ---------------------------------------------------------------------------
// Helpers
// ---------------------------------------------------------------------------

/** Build the fetch URL for each graph mode. */
function buildUrl(
  owner: string,
  repo: string,
  mode: GraphMode,
  focusNode: string | null,
  searchQuery: string,
  traceDir: 'forward' | 'backward' | 'both',
): string {
  const base = `/api/v1/graph/${owner}/${repo}`;
  switch (mode) {
    case 'neighbors':
      return apiUrl(`${base}/neighbors/${focusNode}`);
    case 'trace_fwd':
      return apiUrl(`${base}/trace/${focusNode}?direction=forward&depth=6`);
    case 'trace_bwd':
      return apiUrl(`${base}/trace/${focusNode}?direction=backward&depth=6`);
    case 'search':
      return apiUrl(`${base}/search?q=${encodeURIComponent(searchQuery)}`);
    case 'full':
    default:
      return searchQuery.trim()
        ? apiUrl(`${base}/full?q=${encodeURIComponent(searchQuery)}`)
        : apiUrl(`${base}/full`);
  }
}

// ---------------------------------------------------------------------------
// Component
// ---------------------------------------------------------------------------

interface InteractiveDependencyGraphProps {
  repoName: string;
  /**
   * Externally requested focus target (e.g. "View in graph" from the reading
   * path). The `token` lets the same path be re-requested; bumping it re-runs
   * the focus even when the path is unchanged.
   */
  focusRequest?: { path: string; token: number } | null;
}

/**
 * PH2-001 Interactive Dependency Graph orchestrator.
 *
 * Owns all state: mode, focusNode, searchQuery, selectedNode, graph data.
 * Delegates rendering to four sub-components:
 *   GraphToolbar    β€” mode buttons, fit/reset, status
 *   SearchBar       β€” debounced search input
 *   GraphCanvas     β€” React Flow canvas + Dagre layout
 *   NodeDetailsPanel β€” right-side drawer
 */
const InteractiveDependencyGraphInner: React.FC<
  InteractiveDependencyGraphProps
> = ({ repoName, focusRequest }) => {
  const { zoomIn, zoomOut, setViewport, getViewport, setCenter, fitView, getNodes } = useReactFlow();
  const { selectNode } = useGraphWorkspace();

  // ── Repo split ──────────────────────────────────────────────────────────
  const [owner, repo] = useMemo(() => {
    const parts = repoName.split('/');
    return [parts[0] ?? '', parts[1] ?? ''];
  }, [repoName]);

  // ── Graph data state ────────────────────────────────────────────────────
  const [apiNodes, setApiNodes] = useState<GraphNode[]>([]);
  const [apiEdges, setApiEdges] = useState<GraphEdge[]>([]);
  const [matchCount, setMatchCount] = useState<number | null>(null);

  // ── Interaction state ───────────────────────────────────────────────────
  const [mode, setMode] = useState<GraphMode>('full');
  const [focusNode, setFocusNode] = useState<string | null>(null);
  const [selectedNode, setSelectedNode] = useState<GraphNode | null>(null);
  const [nonGraphFileTarget, setNonGraphFileTarget] = useState<string | null>(null);
  const [searchQuery, setSearchQuery] = useState('');
  const [traceDir, setTraceDir] = useState<'forward' | 'backward' | 'both'>('both');

  // ── Request state ───────────────────────────────────────────────────────
  const [loading, setLoading] = useState(false);
  const [error, setError] = useState<string | null>(null);

  // ── Refs ─────────────────────────────────────────────────────────────────
  const fitViewRef = useRef<(() => void) | null>(null);
  const abortRef = useRef<AbortController | null>(null);
  const searchDebounceRef = useRef<ReturnType<typeof setTimeout> | null>(null);
  /** Holds a pending target file to resolve as soon as graph nodes become available. */
  const pendingTargetRef = useRef<string | null>(focusRequest?.path ?? null);

  // ── Centering and Selection Helpers ─────────────────────────────────────
  const centerOnNode = useCallback(
    (nodeId: string) => {
      let attempts = 0;
      const tryCenter = () => {
        attempts++;
        const nodes = getNodes();
        const target = nodes.find((n) => n.id === nodeId);
        if (target && target.position) {
          const x = target.position.x + (target.width ?? 200) / 2;
          const y = target.position.y + (target.height ?? 40) / 2;
          setCenter(x, y, { zoom: 1.15, duration: 400 });
        } else if (attempts < 10) {
          setTimeout(tryCenter, 60);
        } else {
          fitView({ padding: 0.15, duration: 300, minZoom: 0.15, maxZoom: 1.5 });
        }
      };
      setTimeout(tryCenter, 50);
    },
    [getNodes, setCenter, fitView],
  );

  const applyTargetSelection = useCallback(
    (targetPath: string, nodes: GraphNode[]) => {
      if (!targetPath || !nodes || nodes.length === 0) return;
      const decodedTarget = decodeURIComponent(targetPath);
      const match = resolveGraphNode(decodedTarget, nodes, repoName);
      if (match) {
        setNonGraphFileTarget(null);
        setSelectedNode(match);
        setFocusNode(match.id);
        selectNode(match.id);
        centerOnNode(match.id);
        pendingTargetRef.current = null;

        // Keep URL in sync with durable file parameter without reload
        if (typeof window !== 'undefined') {
          const url = new URL(window.location.href);
          url.searchParams.set('tab', 'graph');
          url.searchParams.set('file', match.id);
          url.searchParams.delete('focus');
          window.history.replaceState({}, '', url.toString());
          window.dispatchEvent(new CustomEvent('aria-workspace-file-select', { detail: { path: match.id } }));
        }
      } else {
        // Node not in graph (e.g. documentation or non-indexed file)
        // Keep the graph fully rendered, set non-graph notice, and fit viewport
        setNonGraphFileTarget(decodedTarget);
        pendingTargetRef.current = null;
        setTimeout(() => fitView({ padding: 0.15, duration: 300, minZoom: 0.15, maxZoom: 1.5 }), 100);
      }
    },
    [repoName, selectNode, centerOnNode, fitView],
  );

  // ── Core fetch ───────────────────────────────────────────────────────────
  const fetchGraph = useCallback(
    async (
      fetchMode: GraphMode,
      focusId: string | null,
      query: string,
      dir: 'forward' | 'backward' | 'both',
      isOptionalEnrichment = false,
    ) => {
      if (!owner || !repo) return;

      if (!isOptionalEnrichment) {
        abortRef.current?.abort();
        abortRef.current = new AbortController();
        setLoading(true);
        setError(null);
        setMatchCount(null);
      }

      const url = buildUrl(owner, repo, fetchMode, focusId, query, dir);

      try {
        const res = await fetch(url, { signal: isOptionalEnrichment ? undefined : abortRef.current?.signal });

        if (!res.ok) {
          let detail = `HTTP ${res.status}`;
          try {
            const body = await res.json();
            detail = extractErrorMessage(body);
          } catch {
            /* ignore */
          }
          if (isOptionalEnrichment) {
            // CRITICAL: Optional enrichment failure must NOT clear existing nodes!
            return;
          }
          setError(detail);
          setApiNodes([]);
          setApiEdges([]);
          return;
        }

        const data: GraphResponse = await res.json();

        if (data.error) {
          if (isOptionalEnrichment) return;
          setError(data.error);
          setApiNodes([]);
          setApiEdges([]);
          return;
        }

        const receivedNodes = data.nodes ?? [];
        const receivedEdges = data.edges ?? [];

        setApiNodes(receivedNodes);
        setApiEdges(receivedEdges);
        if (data.matched_count !== undefined) {
          setMatchCount(data.matched_count);
        }

        // Asynchronously resolve pending focus target against loaded nodes
        if (pendingTargetRef.current) {
          applyTargetSelection(pendingTargetRef.current, receivedNodes);
        } else {
          setTimeout(() => fitView({ padding: 0.15, duration: 300, minZoom: 0.15, maxZoom: 1.5 }), 80);
        }
      } catch (err: any) {
        if (err.name === 'AbortError') return;
        if (isOptionalEnrichment) return;
        setError(extractErrorMessage(err));
        setApiNodes([]);
        setApiEdges([]);
      } finally {
        if (!isOptionalEnrichment) {
          setLoading(false);
        }
      }
    },
    [owner, repo, applyTargetSelection, fitView],
  );

  const lastHandledTokenRef = useRef<number | null>(focusRequest?.token ?? null);

  // ── Initial load: always fetch full repository graph first ────────────────
  useEffect(() => {
    if (focusRequest?.path) {
      pendingTargetRef.current = focusRequest.path;
      lastHandledTokenRef.current = focusRequest.token;
    }
    setMode('full');
    fetchGraph('full', null, '', 'both');
    return () => abortRef.current?.abort();
  }, [repoName]);

  // ── External focus request handling ───────────────────────────────────────
  useEffect(() => {
    if (!focusRequest?.path || focusRequest.token === lastHandledTokenRef.current) return;
    lastHandledTokenRef.current = focusRequest.token;
    pendingTargetRef.current = focusRequest.path;

    if (apiNodes.length > 0) {
      applyTargetSelection(focusRequest.path, apiNodes);
    }
  }, [focusRequest?.token, focusRequest?.path, apiNodes, applyTargetSelection]);

  // ── Search debounce ──────────────────────────────────────────────────────
  const handleSearchChange = useCallback(
    (value: string) => {
      setSearchQuery(value);
      if (searchDebounceRef.current) clearTimeout(searchDebounceRef.current);
      searchDebounceRef.current = setTimeout(() => {
        if (value.trim()) {
          setMode('search');
          setFocusNode(null);
          setSelectedNode(null);
          fetchGraph('search', null, value, 'both');
        } else {
          setMode('full');
          fetchGraph('full', null, '', 'both');
        }
      }, 300);
    },
    [fetchGraph],
  );

  const handleSearchClear = useCallback(() => {
    setSearchQuery('');
    setMatchCount(null);
    setMode('full');
    fetchGraph('full', null, '', 'both');
  }, [fetchGraph]);

  // ── Toolbar actions ──────────────────────────────────────────────────────
  const handleExpand = useCallback(() => {
    if (!focusNode) return;
    setMode('neighbors');
    setTraceDir('both');
    fetchGraph('neighbors', focusNode, '', 'both');
  }, [focusNode, fetchGraph]);

  const handleTraceForward = useCallback(
    (nodeId?: string) => {
      const id = nodeId ?? focusNode;
      if (!id) return;
      setFocusNode(id);
      setMode('trace_fwd');
      setTraceDir('forward');
      fetchGraph('trace_fwd', id, '', 'forward');
    },
    [focusNode, fetchGraph],
  );

  const handleTraceBackward = useCallback(
    (nodeId?: string) => {
      const id = nodeId ?? focusNode;
      if (!id) return;
      setFocusNode(id);
      setMode('trace_bwd');
      setTraceDir('backward');
      fetchGraph('trace_bwd', id, '', 'backward');
    },
    [focusNode, fetchGraph],
  );

  const handleTraceBoth = useCallback(
    (nodeId?: string) => {
      const id = nodeId ?? focusNode;
      if (!id) return;
      setFocusNode(id);
      // Reuse trace_fwd mode key β€” backend gets direction=both
      setMode('trace_fwd');
      setTraceDir('both');
      fetchGraph('trace_fwd', id, '', 'both');
    },
    [focusNode, fetchGraph],
  );

  const handlePanUp = useCallback(() => {
    const { x, y, zoom } = getViewport();
    setViewport({ x, y: y + 150, zoom }, { duration: 200 });
  }, [getViewport, setViewport]);

  const handlePanDown = useCallback(() => {
    const { x, y, zoom } = getViewport();
    setViewport({ x, y: y - 150, zoom }, { duration: 200 });
  }, [getViewport, setViewport]);

  const handlePanLeft = useCallback(() => {
    const { x, y, zoom } = getViewport();
    setViewport({ x: x + 150, y, zoom }, { duration: 200 });
  }, [getViewport, setViewport]);

  const handlePanRight = useCallback(() => {
    const { x, y, zoom } = getViewport();
    setViewport({ x: x - 150, y, zoom }, { duration: 200 });
  }, [getViewport, setViewport]);

  const handleZoomIn = useCallback(() => {
    zoomIn({ duration: 200 });
  }, [zoomIn]);

  const handleZoomOut = useCallback(() => {
    zoomOut({ duration: 200 });
  }, [zoomOut]);

  const handleCenterGraph = useCallback(() => {
    const nodes = getNodes();
    if (nodes.length > 0) {
      let minX = Infinity;
      let maxX = -Infinity;
      let minY = Infinity;
      let maxY = -Infinity;
      nodes.forEach((node) => {
        const x = node.position.x;
        const y = node.position.y;
        const w = node.width ?? 200;
        const h = node.height ?? 40;
        if (x < minX) minX = x;
        if (x + w > maxX) maxX = x + w;
        if (y < minY) minY = y;
        if (y + h > maxY) maxY = y + h;
      });
      const centerX = minX + (maxX - minX) / 2;
      const centerY = minY + (maxY - minY) / 2;
      const { zoom } = getViewport();
      setCenter(centerX, centerY, { zoom, duration: 200 });
    }
  }, [getNodes, getViewport, setCenter]);

  const handleReset = useCallback(() => {
    setMode('full');
    setFocusNode(null);
    setSelectedNode(null);
    setSearchQuery('');
    setMatchCount(null);
    fetchGraph('full', null, '', 'both');
    setTimeout(() => {
      fitView({ padding: 0.15, duration: 200, minZoom: 0.15, maxZoom: 1.5 });
    }, 100);
  }, [fetchGraph, fitView]);

  const handleFitView = useCallback(() => {
    fitView({ padding: 0.15, duration: 200, minZoom: 0.15, maxZoom: 1.5 });
  }, [fitView]);

  // ── Node selection ────────────────────────────────────────────────────────
  const handleNodeSelect = useCallback(
    (node: GraphNode | null) => {
      setNonGraphFileTarget(null);
      setSelectedNode(node);
      if (node) {
        setFocusNode(node.id);
        selectNode(node.id);
        if (typeof window !== 'undefined') {
          const url = new URL(window.location.href);
          url.searchParams.set('tab', 'graph');
          url.searchParams.set('file', node.id);
          url.searchParams.delete('focus');
          window.history.replaceState({}, '', url.toString());
          window.dispatchEvent(new CustomEvent('aria-workspace-file-select', { detail: { path: node.id } }));
        }
      } else {
        setFocusNode(null);
        selectNode(null);
        if (typeof window !== 'undefined') {
          const url = new URL(window.location.href);
          url.searchParams.delete('file');
          url.searchParams.delete('focus');
          window.history.replaceState({}, '', url.toString());
          window.dispatchEvent(new CustomEvent('aria-workspace-file-select', { detail: { path: null } }));
        }
      }
    },
    [selectNode],
  );

  // Compute lightweight stats client-side over current view
  const stats = useMemo(() => computeGraphStats(apiNodes, apiEdges), [apiNodes, apiEdges]);

  // Diagram Modal state
  const [diagramModalNodeId, setDiagramModalNodeId] = useState<string | null>(null);

  // ── Render ────────────────────────────────────────────────────────────────
  return (
    <div className="card overflow-hidden flex flex-col h-[700px] relative border border-border/70 bg-[#030303]">
      {/* Editorial Technical Header */}
      <div className="px-4 py-2.5 border-b border-border/80 bg-zinc-950/90 flex items-center justify-between gap-4 z-10 flex-wrap font-mono">
        <div className="flex items-center gap-3">
          <div className="flex items-center gap-2">
            <span className="text-[10px] font-bold text-indigo-400 tracking-wider uppercase">FILE GRAPH</span>
            <span className="text-zinc-600 text-[10px]">/</span>
            <span className="text-[10px] text-zinc-400 uppercase tracking-wider">REPOSITORY TOPOLOGY</span>
          </div>
          <div className="hidden sm:flex items-center gap-1.5 text-[10px] text-zinc-400">
            <span className="text-zinc-200 font-bold">{apiNodes.length.toLocaleString()}</span> NODES
            <span className="text-zinc-600">Β·</span>
            <span className="text-zinc-200 font-bold">{apiEdges.length.toLocaleString()}</span> EDGES
            {stats.components > 0 && (
              <>
                <span className="text-zinc-600">Β·</span>
                <span className="text-zinc-200 font-bold">{stats.components}</span> COMPONENTS
              </>
            )}
            <span className="text-zinc-600">Β·</span>
            <span className="text-zinc-400">DIRECTED</span>
          </div>
        </div>

        <div className="flex items-center gap-3 ml-auto">
          <SearchBar
            value={searchQuery}
            matchCount={matchCount}
            onChange={handleSearchChange}
            onClear={handleSearchClear}
          />

          {/* Compact Legend */}
          <div className="hidden xl:flex items-center gap-3 text-[9px] text-zinc-400 uppercase font-mono">
            <span className="flex items-center gap-1">
              <span className="h-1.5 w-1.5 rounded-full bg-emerald-400" /> Entry
            </span>
            <span className="flex items-center gap-1">
              <span className="h-1.5 w-1.5 rounded-full bg-blue-400" /> Module
            </span>
            <span className="flex items-center gap-1">
              <span className="h-1.5 w-1.5 rounded-full bg-amber-400" /> Coupled
            </span>
            <span className="flex items-center gap-1">
              <span className="h-1.5 w-1.5 rounded-full bg-indigo-400" /> Focus
            </span>
          </div>
        </div>
      </div>

      {/* Filter Bar & Breadcrumb Navigation */}
      <GraphFilterBar />
      <GraphBreadcrumbNav />

      {/* ── Toolbar ─────────────────────────────────────────────────── */}
      <GraphToolbar
        mode={mode}
        traceDir={traceDir}
        focusNode={focusNode}
        loading={loading}
        nodeCount={apiNodes.length}
        edgeCount={apiEdges.length}
        onFitView={handleFitView}
        onReset={handleReset}
        onTraceForward={() => handleTraceForward()}
        onTraceBackward={() => handleTraceBackward()}
        onTraceBoth={() => handleTraceBoth()}
        onNeighbors={handleExpand}
        onPanUp={handlePanUp}
        onPanDown={handlePanDown}
        onPanLeft={handlePanLeft}
        onPanRight={handlePanRight}
        onZoomIn={handleZoomIn}
        onZoomOut={handleZoomOut}
        onCenterGraph={handleCenterGraph}
      />

      {/* ── Main canvas area ─────────────────────────────────────────── */}
      <div className="flex-grow relative flex overflow-hidden">
        {/* Loading overlay */}
        {loading && (
          <div
            role="status"
            aria-live="polite"
            className="absolute inset-0 bg-canvas/80 backdrop-blur-sm flex flex-col items-center justify-center gap-2 z-30 font-mono text-xs text-text-muted"
          >
            <RefreshCw className="h-5 w-5 animate-spin text-indigo-400" aria-hidden="true" />
            <span className="tracking-wide">BUILDING REPOSITORY TOPOLOGY…</span>
          </div>
        )}

        {/* Error overlay */}
        {!loading && error && (
          <div className="absolute inset-0 bg-canvas/85 backdrop-blur-sm flex items-center justify-center z-30 p-6 font-mono">
            <EmptyState
              tone="danger"
              icon={<Info className="h-6 w-6 text-red-400" aria-hidden="true" />}
              title="TOPOLOGY UNAVAILABLE"
              description={error || 'Unable to load repository dependency graph.'}
              action={<Button variant="ghost" onClick={handleReset}>Retry</Button>}
            />
          </div>
        )}

        {/* Empty state */}
        {!loading && !error && apiNodes.length === 0 && (
          <div className="absolute inset-0 flex items-center justify-center z-30 p-6 font-mono">
            <EmptyState
              icon={<Network className="h-6 w-6 text-zinc-500" aria-hidden="true" />}
              title={searchQuery ? 'NO MATCHING NODES' : 'GRAPH IS EMPTY'}
              description={
                searchQuery
                  ? 'Try a different keyword or clear search to reveal the full topology.'
                  : 'Run repository analysis to index module relationships.'
              }
              action={searchQuery ? <Button variant="ghost" onClick={handleSearchClear}>Clear search</Button> : undefined}
            />
          </div>
        )}

        {/* React Flow canvas */}
        <div className="flex-grow h-full bg-[#030303]">
          <GraphCanvas
            apiNodes={apiNodes}
            apiEdges={apiEdges}
            onNodeSelect={handleNodeSelect}
            fitViewRef={fitViewRef}
          />
        </div>

        {/* Compact instruction hint when no node is selected */}
        {!selectedNode && !nonGraphFileTarget && apiNodes.length > 0 && (
          <div className="absolute top-3 right-3 hidden lg:flex items-center gap-2 px-3 py-1.5 bg-zinc-950/85 border border-zinc-800/80 rounded-md text-[10px] font-mono text-zinc-400 shadow-sm backdrop-blur-sm pointer-events-none select-none">
            <span className="h-1.5 w-1.5 rounded-full bg-indigo-500/80" />
            <span>Select any node to open Architecture Inspector</span>
          </div>
        )}

        {/* Non-graph file drawer notice */}
        {nonGraphFileTarget && !selectedNode && (
          <div
            role="dialog"
            aria-label={`Notice: ${nonGraphFileTarget} not in graph`}
            className="fixed inset-x-0 bottom-0 max-h-[80vh] md:absolute md:right-0 md:top-0 md:bottom-0 md:max-h-none md:w-[380px] bg-zinc-950 border-t md:border-t-0 md:border-l border-zinc-800 flex flex-col z-20 shadow-2xl font-mono animate-in fade-in slide-in-from-right-2 duration-200"
          >
            <div className="flex items-start justify-between px-4 pt-4 pb-3 border-b border-zinc-800/80 shrink-0">
              <div>
                <span className="text-[9px] font-bold text-amber-400 uppercase tracking-wider block">
                  Topology Notice
                </span>
                <h3 className="text-xs font-semibold text-zinc-100 truncate block mt-0.5" title={nonGraphFileTarget}>
                  {nonGraphFileTarget.split('/').pop() || nonGraphFileTarget}
                </h3>
                <span className="text-[9px] text-zinc-500 truncate block">{nonGraphFileTarget}</span>
              </div>
              <button
                type="button"
                onClick={() => setNonGraphFileTarget(null)}
                className="text-zinc-400 hover:text-zinc-100 p-1 rounded"
                title="Dismiss notice"
              >
                <X className="h-4 w-4" />
              </button>
            </div>
            <div className="p-4 space-y-3 text-xs text-zinc-300">
              <div className="p-3 bg-amber-950/20 border border-amber-500/30 rounded-lg space-y-1.5">
                <span className="text-[10px] font-bold text-amber-400 uppercase tracking-wider block">
                  File Not Represented in Topology
                </span>
                <p className="text-[11px] text-zinc-300 leading-relaxed">
                  This file exists in the repository but does not import or export code modules in the structural AST dependency graph (e.g. documentation, static assets, or configs).
                </p>
              </div>
              <p className="text-[10px] text-zinc-400 leading-relaxed">
                The full repository dependency graph remains rendered and interactive. Click any graph node to inspect its architectural properties.
              </p>
            </div>
          </div>
        )}

        {/* Node details panel */}
        {selectedNode && (
          <NodeDetailsPanel
            node={selectedNode}
            repoName={repoName}
            onClose={() => handleNodeSelect(null)}
            onExpand={(id) => {
              setFocusNode(id);
              setMode('neighbors');
              fetchGraph('neighbors', id, '', 'both', true);
            }}
            onTraceForward={handleTraceForward}
            onTraceBackward={handleTraceBackward}
            onTraceBoth={handleTraceBoth}
            onOpenDiagramModal={(id) => setDiagramModalNodeId(id)}
            onSelectNode={(id) => {
              const match = resolveGraphNode(id, apiNodes, repoName) || {
                id,
                label: id.split('/').pop() || id,
                category: 'regular',
                degree: 1,
                centrality: 0.1,
                language: 'typescript',
                highlighted: false,
                is_focus: true,
              };
              handleNodeSelect(match);
              centerOnNode(match.id);
            }}
          />
        )}

        {/* Diagram exporter modal */}
        {diagramModalNodeId && (
          <ArchitectureDiagramModal
            repoName={repoName}
            nodeId={diagramModalNodeId}
            onClose={() => setDiagramModalNodeId(null)}
          />
        )}
      </div>
    </div>
  );
};

export const InteractiveDependencyGraph: React.FC<
  InteractiveDependencyGraphProps
> = (props) => {
  return (
    <GraphWorkspaceProvider>
      <ReactFlowProvider>
        <InteractiveDependencyGraphInner {...props} />
      </ReactFlowProvider>
    </GraphWorkspaceProvider>
  );
};

interface StatPillProps {
  icon: React.ReactNode;
  label: string;
  value: number | string;
  tone?: 'neutral' | 'danger';
}
const StatPill: React.FC<StatPillProps> = ({ icon, label, value, tone = 'neutral' }) => (
  <span
    className={[
      'flex items-center gap-1 px-2 py-0.5 rounded border',
      tone === 'danger'
        ? 'border-danger/30 bg-danger/10 text-danger'
        : 'border-border bg-canvas text-text',
    ].join(' ')}
    title={`${label}: ${value}`}
  >
    <span className="text-text-muted" aria-hidden="true">{icon}</span>
    <span className="text-text-muted">{label}</span>
    <span className="font-bold">{value}</span>
  </span>
);

export default InteractiveDependencyGraph;
// Interactive dependency graph entry point