File size: 27,524 Bytes
3ad88a4
c9c783c
3ad88a4
c9c783c
 
 
 
 
 
 
 
 
3ad88a4
 
c9c783c
 
5fcd5c7
3ad88a4
183f2e6
 
 
 
 
 
 
 
 
 
 
 
 
 
5fcd5c7
3ad88a4
5fcd5c7
3ad88a4
 
c9c783c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3ad88a4
 
c9c783c
 
3ad88a4
c9c783c
3ad88a4
5661eaf
 
 
 
3ad88a4
c9c783c
3ad88a4
 
 
 
 
c9c783c
3ad88a4
 
 
 
c9c783c
 
3ad88a4
 
 
 
c9c783c
 
 
 
3ad88a4
 
 
c9c783c
3ad88a4
 
 
 
 
 
 
c9c783c
3ad88a4
 
 
 
 
c9c783c
 
3ad88a4
 
 
c9c783c
 
 
 
3ad88a4
c9c783c
 
 
 
 
 
 
 
 
 
 
3ad88a4
 
c9c783c
 
3ad88a4
 
 
 
 
c9c783c
 
 
3ad88a4
c9c783c
 
 
 
 
3ad88a4
 
 
c9c783c
3ad88a4
 
c9c783c
3ad88a4
 
c9c783c
 
 
 
 
 
 
 
 
 
 
3ad88a4
 
c9c783c
183f2e6
c9c783c
 
183f2e6
c9c783c
 
 
 
 
 
 
 
 
 
183f2e6
8009c2d
183f2e6
ec82092
c9c783c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3ad88a4
 
 
c9c783c
3ad88a4
 
 
 
 
c9c783c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
34ceb36
 
 
c9c783c
 
34ceb36
 
 
 
 
988ef7f
 
34ceb36
 
 
 
 
3ad88a4
c9c783c
 
 
 
 
 
a6aaf1e
ba23b72
 
 
 
 
 
 
 
 
 
 
 
c9c783c
 
 
 
3ad88a4
9662976
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c9c783c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3ad88a4
 
 
 
 
 
 
 
 
 
c9c783c
 
 
 
 
 
 
3ad88a4
c9c783c
3ad88a4
 
 
c9c783c
3ad88a4
 
 
 
c9c783c
 
 
 
 
 
 
3ad88a4
 
 
 
 
 
 
 
c9c783c
 
 
 
 
 
 
 
 
 
 
 
 
3ad88a4
c9c783c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3ad88a4
c9c783c
 
 
 
 
 
 
 
 
5fcd5c7
c9c783c
 
5fcd5c7
c9c783c
34ceb36
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c9c783c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
34ceb36
c9c783c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5fcd5c7
c9c783c
 
 
 
 
5fcd5c7
c9c783c
 
 
 
 
 
 
 
 
 
 
 
183f2e6
 
 
 
 
 
 
 
 
 
 
 
 
 
c9c783c
3ad88a4
c9c783c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3ad88a4
 
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
/**
 * GraphDiagram.jsx β€” Custom SVG canvas renderer for Architecture and Class diagrams.
 *
 * Replaced React Flow with the same hand-crafted SVG approach as ExploreView:
 *   - No port handles, no selection rings, no third-party CSS artifacts
 *   - Full visual control β€” same warm sketchbook palette throughout
 *   - Pan/zoom via CSS transform (scroll = zoom, drag = pan)
 *   - Bezier arrows with clickable hit areas for edge detail
 *
 * Layout: BFS topological columns β€” nodes with no incoming edges go left,
 * their dependents go right. Columns that exceed MAX_PER_COL are split
 * into sub-columns to prevent tall stacks.
 */

import { useEffect, useMemo, useCallback, useState, useRef } from "react";

// ── Type colours β€” blueprint palette, consistent with ExploreView ────────────
const TYPE_STYLE = {
  // OOP hierarchy β€” blue family (semantically related, slight variation is intentional)
  class:     { border: "#5B8FF9", glow: "rgba(91,143,249,0.38)",  dot: "#7DABFF" }, // blue
  abstract:  { border: "#818CF8", glow: "rgba(129,140,248,0.32)", dot: "#A5B4FC" }, // violet-blue
  mixin:     { border: "#A78BFA", glow: "rgba(167,139,250,0.32)", dot: "#C4B5FD" }, // violet
  // Modules / packages β€” amber (clearly warm vs cool)
  module:    { border: "#FBBF24", glow: "rgba(251,191,36,0.32)",  dot: "#FCD34D" }, // amber
  // Services / transforms β€” emerald
  service:   { border: "#34D399", glow: "rgba(52,211,153,0.32)",  dot: "#6EE7B7" }, // emerald
  transform: { border: "#2DD4BF", glow: "rgba(45,212,191,0.32)",  dot: "#5EEAD4" }, // teal
  // Data / I/O β€” rose/orange
  database:  { border: "#FB923C", glow: "rgba(251,146,60,0.32)",  dot: "#FDBA74" }, // orange
  input:     { border: "#F472B6", glow: "rgba(244,114,182,0.32)", dot: "#F9A8D4" }, // rose
  output:    { border: "#34D399", glow: "rgba(52,211,153,0.32)",  dot: "#6EE7B7" }, // emerald
  // External deps β€” neutral steel
  external:  { border: "#4E5E80", glow: "rgba(78,94,128,0.28)",   dot: "#8896B8" }, // steel
};
const FALLBACK_STYLE = { border: "#4E5E80", glow: "rgba(78,94,128,0.28)", dot: "#8896B8" };
function styleFor(type) { return TYPE_STYLE[type] || FALLBACK_STYLE; }

// ── Card geometry ─────────────────────────────────────────────────────────────
const CARD_W   = 220;   // canvas pixels β€” matches ExploreView width
const CARD_H   = 240;   // layout height per card (type+name+file+desc+items+ask β‰ˆ 230px + buffer)
const COL_GAP  = 100;   // horizontal space between columns
const ROW_GAP  = 48;    // vertical gap between cards in the same column

// ── Bezier arrow: right-edge of source β†’ left-edge of target ─────────────────
// Identical formula to ExploreView β€” consistent arrow shape across all views.
function bezierPath(from, to) {
  const x1 = from.x + CARD_W;
  const y1 = from.y + CARD_H / 2;
  const x2 = to.x;
  const y2 = to.y + CARD_H / 2;
  // Tension scales with horizontal distance so short and long arrows both look
  // natural β€” short: tight S-curve, long: gentle arc.
  const tension = Math.max((x2 - x1) * 0.55, 60);
  return `M ${x1} ${y1} C ${x1 + tension} ${y1}, ${x2 - tension} ${y2}, ${x2} ${y2}`;
}

// ── Layout: BFS topological columns ──────────────────────────────────────────
// Returns { nodes: [{id, x, y, data}], edges: [{source, target, label}] }
function computeLayout(rawNodes, rawEdges) {
  if (!rawNodes?.length) return { nodes: [], edges: [], posMap: {} };

  // 6 nodes per column: a fan-out graph (many nodes β†’ same root) with 8-11 children
  // was splitting into two sub-columns, forcing the root to draw long arrows that
  // crossed the first sub-column. 6 keeps most fan-outs in one column.
  const MAX_PER_COL = 6;

  // BFS to assign column depth to each node
  const inDegree = {};
  const outEdges = {};
  rawNodes.forEach(n => { inDegree[n.id] = 0; outEdges[n.id] = []; });
  rawEdges.forEach(e => {
    if (inDegree[e.target] !== undefined) inDegree[e.target]++;
    if (outEdges[e.source])               outEdges[e.source].push(e.target);
  });

  const depth = {};
  rawNodes.forEach(n => { depth[n.id] = 0; });
  const queue   = rawNodes.filter(n => inDegree[n.id] === 0).map(n => n.id);
  const tmpIn   = { ...inDegree };
  const visited = new Set(queue);
  let head = 0;
  while (head < queue.length) {
    const id = queue[head++];
    for (const nb of (outEdges[id] || [])) {
      depth[nb] = Math.max(depth[nb], depth[id] + 1);
      tmpIn[nb]--;
      if (tmpIn[nb] === 0 && !visited.has(nb)) { visited.add(nb); queue.push(nb); }
    }
  }

  // Group nodes by column
  const cols = {};
  rawNodes.forEach(n => {
    const c = depth[n.id];
    if (!cols[c]) cols[c] = [];
    cols[c].push(n);
  });

  // Split over-tall columns into sub-columns
  const colKeys = Object.keys(cols).map(Number).sort((a, b) => a - b);
  const expandedCols = {};
  let ci = 0;
  colKeys.forEach(col => {
    const nodes = cols[col];
    for (let s = 0; s < nodes.length; s += MAX_PER_COL) {
      expandedCols[ci++] = nodes.slice(s, s + MAX_PER_COL);
    }
  });

  // Assign pixel positions β€” center each column vertically around the tallest column.
  // We must compute maxColH first so every column is centered against the same baseline,
  // which guarantees all y values are non-negative (no card above the canvas top edge).
  // This mirrors ExploreView's layout algorithm exactly.
  const posMap = {};
  const finalColKeys = Object.keys(expandedCols).map(Number).sort((a, b) => a - b);
  const maxColH = Math.max(
    ...finalColKeys.map(col => {
      const n = expandedCols[col].length;
      return n * CARD_H + (n - 1) * ROW_GAP;
    })
  );
  finalColKeys.forEach((col) => {
    const colNodes = expandedCols[col];
    const colH   = colNodes.length * CARD_H + (colNodes.length - 1) * ROW_GAP;
    const startY = (maxColH - colH) / 2 + 48;  // always β‰₯ 48px β€” no negative coords
    colNodes.forEach((n, ri) => {
      posMap[n.id] = {
        x: col * (CARD_W + COL_GAP) + 48,
        y: startY + ri * (CARD_H + ROW_GAP),
      };
    });
  });

  const nodes = rawNodes.map(n => ({
    id:   n.id,
    x:    posMap[n.id]?.x ?? 0,
    y:    posMap[n.id]?.y ?? 0,
    data: {
      label:       n.label,
      type:        n.type,
      file:        n.file,
      description: n.description,
      items:       n.items || n.methods || [],
    },
  }));

  const edges = rawEdges.map(e => ({
    source: e.source,
    target: e.target,
    label:  e.label || "",
  }));

  return { nodes, edges, posMap };
}

// ── File icon β€” same SVG used in ExploreView's ec-file pill ──────────────────
function FileIcon() {
  return (
    <svg width="10" height="10" viewBox="0 0 16 16" fill="currentColor"
      style={{ opacity: 0.5, flexShrink: 0 }}>
      <path d="M2 1.75C2 .784 2.784 0 3.75 0h6.586c.464 0 .909.184 1.237.513l2.914 2.914c.329.328.513.773.513 1.237v9.586A1.75 1.75 0 0 1 13.25 16h-9.5A1.75 1.75 0 0 1 2 14.25Zm1.75-.25a.25.25 0 0 0-.25.25v12.5c0 .138.112.25.25.25h9.5a.25.25 0 0 0 .25-.25V6h-2.75A1.75 1.75 0 0 1 8.75 4.25V1.5Zm6.75.56v2.19c0 .138.112.25.25.25h2.19Z"/>
    </svg>
  );
}

// ── DiagramCard β€” ec-card positioned absolutely on the SVG canvas ─────────────
function DiagramCard({ node, pos, hoveredId, connectedIds, onSelect, onHover, onAsk, onDragStart, wasDragged }) {
  const dim       = hoveredId && hoveredId !== node.id && !connectedIds.has(node.id);
  const highlight = hoveredId === node.id || (hoveredId && connectedIds.has(node.id));
  const s = styleFor(node.data.type);

  return (
    <div
      className={`ec-card${dim ? " ec-dimmed" : ""}`}
      style={{
        position: "absolute",
        left:  pos.x,
        top:   pos.y,
        width: CARD_W,
        cursor: "grab",
        borderColor: highlight ? s.dot : undefined,
        boxShadow: highlight
          ? `0 0 0 2px ${s.dot}, 0 0 20px ${s.glow.replace(/[\d.]+\)$/, '0.60)')}, 0 20px 60px ${s.glow.replace(/[\d.]+\)$/, '0.45)')}`
          : undefined,
      }}
      onMouseDown={(e) => onDragStart?.(e, node)}
      onClick={() => { if (!wasDragged?.current) onSelect(node); }}
      onMouseEnter={() => onHover(node.id)}
      onMouseLeave={() => onHover(null)}
    >
      {/* Top row: type tag */}
      <div className="ec-card-top" style={{ marginBottom: 6 }}>
        <span className="ec-type-tag" style={{ color: s.dot, borderColor: `${s.dot}44` }}>
          {node.data.type}
        </span>
      </div>

      {/* Name */}
      <div className="ec-name">{node.data.label}</div>

      {/* File path */}
      {node.data.file && (
        <div className="ec-file" style={{ marginTop: 3 }}>
          <FileIcon />
          {node.data.file}
        </div>
      )}

      {/* Description β€” truncated; full text shown in NodeDetailPanel */}
      {node.data.description && (
        <div className="ec-subtitle" style={{ marginTop: 4 }}>
          {node.data.description.length > 90
            ? node.data.description.slice(0, 88) + "…"
            : node.data.description}
        </div>
      )}

      {/* Method pills */}
      {node.data.items?.length > 0 && (
        <div style={{ display: "flex", flexWrap: "wrap", gap: 3, marginTop: 6 }}>
          {node.data.items.slice(0, 4).map((item, i) => (
            <code key={i} className="ec-item">{item}</code>
          ))}
        </div>
      )}

      {/* Ask button */}
      {onAsk && (
        <button className="ec-ask" onClick={e => { e.stopPropagation(); onAsk(node.data); }}>
          Ask about this β†’
        </button>
      )}
    </div>
  );
}

// ── Main component ────────────────────────────────────────────────────────────
export default function GraphDiagram({ data, onNodeSelect, onEdgeSelect, onAskAbout, repo, panelOpen }) {
  const { nodes: layoutNodes, edges: layoutEdges, posMap } = useMemo(
    () => computeLayout(data?.nodes || [], data?.edges || []),
    [data]
  );

  const [hoveredId, setHoveredId] = useState(null);
  const [xform, setXform]         = useState({ x: 0, y: 0, scale: 0.85 });

  // nodePos overrides the static layout position for dragged nodes.
  // Key = node id, value = { x, y } in canvas coordinates.
  const [nodePos, setNodePos] = useState({});

  // canvas pan state
  const dragging  = useRef(false);
  const drag0     = useRef({});
  // Mirror of xform.scale in a ref so document-level handlers (which have
  // empty deps and can't close over xform) can always read the current scale.
  const scaleRef  = useRef(xform.scale);
  useEffect(() => { scaleRef.current = xform.scale; }, [xform.scale]);

  // per-node drag state β€” set on card mousedown, cleared on mouseup
  // { id, startPos: {x,y}, startMouse: {x,y} }
  const dragNode    = useRef(null);
  // Set to true when the node actually moves during a drag gesture.
  // Read by DiagramCard's onClick to suppress the click-to-select that
  // browsers fire automatically after every mousedown+mouseup pair.
  const wasDragged  = useRef(false);

  const wrapRef  = useRef(null);

  // Center the diagram in the viewport whenever layout changes.
  // We read the wrapper's actual rendered dimensions (not CSS values) via
  // getBoundingClientRect so the offset is always pixel-perfect.
  useEffect(() => {
    setNodePos({});
    if (!wrapRef.current || !layoutNodes.length) {
      setXform({ x: 0, y: 0, scale: 0.85 });
      return;
    }
    const { width, height } = wrapRef.current.getBoundingClientRect();
    // Smaller initial scale on mobile so the diagram fits without needing to zoom out
    const scale = width < 768 ? 0.5 : 0.85;
    // Offset so the scaled canvas sits in the centre of the wrapper
    const cx = (width  - canvasW * scale) / 2;
    const cy = (height - canvasH * scale) / 2;
    setXform({ x: cx, y: Math.max(cy, 24), scale });
  }, [layoutNodes]); // layoutNodes recomputes whenever data changes

  // Non-passive wheel zoom β€” passive: false required to call preventDefault()
  useEffect(() => {
    const el = wrapRef.current;
    if (!el) return;
    function onWheel(e) {
      e.preventDefault();
      const f = Math.exp(-e.deltaY * 0.001);
      const rect = el.getBoundingClientRect();
      // Mouse position relative to the wrapper element
      const mx = e.clientX - rect.left;
      const my = e.clientY - rect.top;
      setXform(t => {
        const newScale = Math.min(Math.max(t.scale * f, 0.2), 2.5);
        // Pin the canvas point under the cursor: the canvas coord under the mouse is
        // (mx - tx) / scale. After scaling, translate so that same coord maps back
        // to mx: newTx = mx - canvasX * newScale = mx - (mx - tx) * (newScale / scale)
        const ratio = newScale / t.scale;
        return { x: mx - (mx - t.x) * ratio, y: my - (my - t.y) * ratio, scale: newScale };
      });
    }
    el.addEventListener("wheel", onWheel, { passive: false });
    return () => el.removeEventListener("wheel", onWheel);
  }, []);

  // Touch pan (1 finger) + pinch-to-zoom (2 fingers)
  useEffect(() => {
    const el = wrapRef.current;
    if (!el) return;
    let lastTouch = null;   // { x, y } β€” single-finger pan anchor
    let lastPinch = null;   // { dist, mid } β€” two-finger pinch state

    function pinchDist(t) { return Math.hypot(t[1].clientX - t[0].clientX, t[1].clientY - t[0].clientY); }
    function pinchMid(t, rect) {
      return { x: (t[0].clientX + t[1].clientX) / 2 - rect.left,
               y: (t[0].clientY + t[1].clientY) / 2 - rect.top };
    }
    function onTouchStart(e) {
      if (e.touches.length === 1) {
        lastTouch = { x: e.touches[0].clientX, y: e.touches[0].clientY };
        lastPinch = null;
      } else if (e.touches.length === 2) {
        const t = [...e.touches];
        lastPinch = { dist: pinchDist(t), mid: pinchMid(t, el.getBoundingClientRect()) };
        lastTouch = null;
      }
    }
    function onTouchMove(e) {
      e.preventDefault();
      if (e.touches.length === 1 && lastTouch) {
        const dx = e.touches[0].clientX - lastTouch.x;
        const dy = e.touches[0].clientY - lastTouch.y;
        lastTouch = { x: e.touches[0].clientX, y: e.touches[0].clientY };
        setXform(t => ({ ...t, x: t.x + dx, y: t.y + dy }));
      } else if (e.touches.length === 2 && lastPinch) {
        const t   = [...e.touches];
        const rect = el.getBoundingClientRect();
        const d   = pinchDist(t);
        const mid = pinchMid(t, rect);
        const f   = d / lastPinch.dist;
        lastPinch = { dist: d, mid };
        setXform(s => {
          const newScale = Math.min(Math.max(s.scale * f, 0.2), 2.5);
          const ratio = newScale / s.scale;
          return { x: mid.x - (mid.x - s.x) * ratio, y: mid.y - (mid.y - s.y) * ratio, scale: newScale };
        });
      }
    }
    function onTouchEnd() { lastTouch = null; lastPinch = null; }

    el.addEventListener("touchstart", onTouchStart, { passive: true });
    el.addEventListener("touchmove",  onTouchMove,  { passive: false });
    el.addEventListener("touchend",   onTouchEnd);
    return () => {
      el.removeEventListener("touchstart", onTouchStart);
      el.removeEventListener("touchmove",  onTouchMove);
      el.removeEventListener("touchend",   onTouchEnd);
    };
  }, []);

  // Document-level mousemove/mouseup so drag works even when the cursor
  // glides over child cards or outside the wrapper boundary mid-gesture.
  useEffect(() => {
    function onDocMove(e) {
      if (dragNode.current) {
        const dx = (e.clientX - dragNode.current.startMouse.x) / scaleRef.current;
        const dy = (e.clientY - dragNode.current.startMouse.y) / scaleRef.current;
        // Only count as a drag if the node moved more than 4px β€” prevents
        // suppressing clicks caused by tiny hand tremor on mousedown.
        if (Math.abs(dx) > 4 || Math.abs(dy) > 4) wasDragged.current = true;
        // Capture id and new position NOW β€” before passing the callback to
        // setNodePos. React may call the callback asynchronously, by which
        // point onDocUp could have already set dragNode.current = null,
        // causing a "Cannot read property of null" crash.
        const id   = dragNode.current.id;
        const newX = dragNode.current.startPos.x + dx;
        const newY = dragNode.current.startPos.y + dy;
        setNodePos(prev => ({ ...prev, [id]: { x: newX, y: newY } }));
        return;
      }
      if (!dragging.current) return;
      setXform(t => ({
        ...t,
        x: drag0.current.tx + (e.clientX - drag0.current.mx),
        y: drag0.current.ty + (e.clientY - drag0.current.my),
      }));
    }
    function onDocUp() {
      dragNode.current = null;
      dragging.current = false;
      if (wrapRef.current) wrapRef.current.style.cursor = "grab";
      // wasDragged is reset AFTER the click event fires (click fires synchronously
      // after mouseup in the same task). setTimeout(0) defers the reset past it.
      setTimeout(() => { wasDragged.current = false; }, 0);
    }
    document.addEventListener("mousemove", onDocMove);
    document.addEventListener("mouseup",   onDocUp);
    return () => {
      document.removeEventListener("mousemove", onDocMove);
      document.removeEventListener("mouseup",   onDocUp);
    };
  }, []); // empty deps β€” only refs + stable setters used inside

  // Returns the live (possibly dragged) position for a node id.
  // Falls back to posMap (static layout) when the node hasn't been moved.
  const getPosFor = useCallback((id) => nodePos[id] ?? posMap[id], [nodePos, posMap]);

  // Canvas pan β€” fires only when no node drag is in progress
  function onMouseDown(e) {
    if (e.button !== 0) return;
    dragging.current = true;
    drag0.current = { mx: e.clientX, my: e.clientY, tx: xform.x, ty: xform.y };
    e.currentTarget.style.cursor = "grabbing";
  }

  // Called from DiagramCard β€” stops propagation so the wrapper's onMouseDown
  // (canvas pan) does NOT fire when dragging a node.
  function onNodeDragStart(e, node) {
    if (e.button !== 0) return;
    e.stopPropagation();
    const current = nodePos[node.id] ?? { x: node.x, y: node.y };
    dragNode.current = {
      id:         node.id,
      startPos:   current,
      startMouse: { x: e.clientX, y: e.clientY },
    };
  }


  // Connected IDs for hover dimming
  const connectedIds = useMemo(() => {
    if (!hoveredId) return new Set();
    const ids = new Set();
    layoutEdges.forEach(e => {
      if (e.source === hoveredId) ids.add(e.target);
      if (e.target === hoveredId) ids.add(e.source);
    });
    return ids;
  }, [hoveredId, layoutEdges]);

  const handleNodeSelect = useCallback((node) => {
    onNodeSelect?.({
      kind:        "node",
      id:          node.id,
      label:       node.data.label,
      type:        node.data.type,
      file:        node.data.file,
      description: node.data.description,
      items:       node.data.items || [],
    });
  }, [onNodeSelect]);

  const handleEdgeClick = useCallback((edge) => {
    const srcNode = layoutNodes.find(n => n.id === edge.source);
    const tgtNode = layoutNodes.find(n => n.id === edge.target);
    const srcLabel = srcNode?.data?.label || edge.source;
    const tgtLabel = tgtNode?.data?.label || edge.target;
    onEdgeSelect?.({
      kind:         "edge",
      label:        `${srcLabel} β†’ ${tgtLabel}`,
      type:         "edge",
      file:         null,
      description:  edge.label ? `Relationship: "${edge.label}"` : "",
      items:        [],
      autoQuestion: `In ${repo}, why does "${srcLabel}" ${edge.label || "depend on"} "${tgtLabel}"? What specifically does it use from it, and what would break if this dependency were removed?`,
    });
  }, [onEdgeSelect, layoutNodes, repo]);

  const handleNodeAsk = useCallback((nodeData) => {
    onAskAbout?.(`Explain "${nodeData.label}" in ${repo} in detail β€” what does it do, what are its key responsibilities, and what other parts of the codebase depend on it?`);
  }, [onAskAbout, repo]);

  // Canvas bounding box
  const allX    = layoutNodes.map(n => n.x + CARD_W + 60);
  const allY    = layoutNodes.map(n => n.y + CARD_H + 80);
  const canvasW = Math.max(...allX, 700);
  const canvasH = Math.max(...allY, 500);

  // Legend: unique types actually present in this diagram
  const presentTypes = useMemo(() => {
    const seen = new Set();
    layoutNodes.forEach(n => { if (n.data?.type) seen.add(n.data.type); });
    return [...seen].sort();
  }, [layoutNodes]);

  return (
    <div className="ec-container">

      {/* ── Canvas ── */}
      <div
        ref={wrapRef}
        className="ec-canvas-wrapper"
        onMouseDown={onMouseDown}
      >
        <div
          className="ec-canvas"
          style={{
            width:           canvasW,
            height:          canvasH,
            transform:       `translate(${xform.x}px, ${xform.y}px) scale(${xform.scale})`,
            transformOrigin: "0 0",
          }}
        >
          {/* ── SVG arrow layer ── */}
          <svg
            width={canvasW}
            height={canvasH}
            style={{ position: "absolute", inset: 0, pointerEvents: "none", overflow: "visible" }}
            aria-hidden="true"
          >
            <defs>
              <marker id="gd-arrow"    markerWidth="7" markerHeight="5" refX="7" refY="2.5" orient="auto">
                <polygon points="0 0, 7 2.5, 0 5" fill="rgba(91,143,249,0.35)" />
              </marker>
              <marker id="gd-arrow-hi" markerWidth="7" markerHeight="5" refX="7" refY="2.5" orient="auto">
                <polygon points="0 0, 7 2.5, 0 5" fill="#7DABFF" />
              </marker>
              {/* Per-edge directional gradients β€” bright at source, fades toward target.
                  gradientUnits="userSpaceOnUse" maps x1/y1/x2/y2 to canvas coordinates
                  so the gradient always aligns with the actual arrow path. */}
              {layoutEdges.map((edge, i) => {
                const from = getPosFor(edge.source);
                const to   = getPosFor(edge.target);
                if (!from || !to) return null;
                return (
                  <linearGradient key={`grad-${i}`} id={`edge-grad-${i}`}
                    gradientUnits="userSpaceOnUse"
                    x1={from.x + CARD_W} y1={from.y + CARD_H / 2}
                    x2={to.x}            y2={to.y   + CARD_H / 2}>
                    <stop offset="0%"   stopColor="#7DABFF" stopOpacity="0.55" />
                    <stop offset="100%" stopColor="#5B8FF9" stopOpacity="0.08" />
                  </linearGradient>
                );
              })}
            </defs>

            {layoutEdges.map((edge, i) => {
              const from = getPosFor(edge.source);
              const to   = getPosFor(edge.target);
              if (!from || !to) return null;

              // Highlight an edge when either endpoint is the hovered node OR a direct
              // neighbour. connectedIds only contains neighbours, so we also check
              // hoveredId directly β€” otherwise edges FROM the hovered node aren't lit.
              const touchesHovered = edge.source === hoveredId || edge.target === hoveredId;
              const isHi  = hoveredId && (touchesHovered || (connectedIds.has(edge.source) && connectedIds.has(edge.target)));
              const isDim = hoveredId && !isHi;
              const d     = bezierPath(from, to);

              // Midpoint for optional edge label
              const mx = (from.x + CARD_W + to.x) / 2;
              const my = (from.y + to.y + CARD_H) / 2;

              return (
                <g key={i}>
                  {/* Wide transparent stroke = clickable hit area.
                      pointerEvents: "stroke" means only the stroke path area fires events,
                      not the bounding box β€” so cards behind don't get shadowed. */}
                  <path
                    d={d}
                    fill="none"
                    stroke="transparent"
                    strokeWidth={14}
                    style={{ cursor: "pointer", pointerEvents: "stroke" }}
                    onClick={() => handleEdgeClick(edge)}
                  />
                  {/* Visible arrow.
                      "inherits" = solid line (UML: solid with hollow arrow).
                      "uses"     = dashed line (UML: dependency/composition). */}
                  <path
                    d={d}
                    fill="none"
                    stroke={isHi ? "#7DABFF" : `url(#edge-grad-${i})`}
                    strokeWidth={isHi ? 2 : 1.5}
                    strokeDasharray={edge.label === "uses" ? "5 3" : undefined}
                    markerEnd={isHi ? "url(#gd-arrow-hi)" : "url(#gd-arrow)"}
                    style={{
                      opacity:    isDim ? 0.1 : 1,
                      transition: "opacity 0.15s, stroke 0.15s, stroke-width 0.15s",
                      pointerEvents: "none",
                    }}
                  />
                  {/* Edge label */}
                  {edge.label && (
                    <text
                      x={mx} y={my}
                      textAnchor="middle"
                      fontSize="10"
                      fontFamily="JetBrains Mono, monospace"
                      fill="#A8C5FF"
                      style={{
                        opacity:    isDim ? 0.08 : 0.75,
                        transition: "opacity 0.15s",
                        pointerEvents: "none",
                        paintOrder: "stroke",
                        stroke: "#09090E",
                        strokeWidth: 3,
                      }}
                    >
                      {edge.label}
                    </text>
                  )}
                </g>
              );
            })}
          </svg>

          {/* ── Diagram node cards ── */}
          {layoutNodes.map(node => (
            <DiagramCard
              key={node.id}
              node={node}
              pos={getPosFor(node.id) ?? { x: node.x, y: node.y }}
              hoveredId={hoveredId}
              connectedIds={connectedIds}
              onSelect={handleNodeSelect}
              onHover={setHoveredId}
              onAsk={onAskAbout ? handleNodeAsk : null}
              onDragStart={onNodeDragStart}
              wasDragged={wasDragged}
            />
          ))}
        </div>
      </div>

      {/* ── Legend bar β€” same structure as ExploreView's ec-legend ── */}
      {!panelOpen && (
        <div className="ec-legend">
          {presentTypes.map(type => {
            const s = styleFor(type);
            return (
              <span key={type} className="ec-legend-item">
                <span className="ec-legend-dot" style={{ background: s.dot }} />
                {type}
              </span>
            );
          })}
          <span className="ec-legend-hint">
            {layoutNodes.length} nodes Β· {layoutEdges.length} edges Β· scroll to zoom Β· drag to pan Β· click to explore
          </span>
        </div>
      )}
    </div>
  );
}