import React, { useRef, useEffect, useLayoutEffect, useState, useMemo, useCallback } from 'react'; import ForceGraph3D from 'react-force-graph-3d'; import * as THREE from 'three'; import SpriteText from 'three-spritetext'; import { Plus, Minus, Maximize, Rotate3d, Tags } from 'lucide-react'; import { ROOT_ID, TIER_COLORS, TIER_LABELS, SIGNAL_COLOR, idOf, nodeRadius, escapeHtml } from '../lib/graph'; const SPHERE = new THREE.SphereGeometry(1, 24, 18); const LABEL_COLOR = '#e7ebf3'; const AUTO_ROTATE_SPEED = 0.6; // Soft radial sprite shared by every node's halo, so nodes glow without post-processing let glowTexture = null; const getGlowTexture = () => { if (glowTexture) return glowTexture; const size = 128; const canvas = document.createElement('canvas'); canvas.width = size; canvas.height = size; const ctx = canvas.getContext('2d'); const gradient = ctx.createRadialGradient(size / 2, size / 2, 0, size / 2, size / 2, size / 2); gradient.addColorStop(0, 'rgba(255,255,255,1)'); gradient.addColorStop(0.18, 'rgba(255,255,255,0.55)'); gradient.addColorStop(0.5, 'rgba(255,255,255,0.1)'); gradient.addColorStop(1, 'rgba(255,255,255,0)'); ctx.fillStyle = gradient; ctx.fillRect(0, 0, size, size); glowTexture = new THREE.CanvasTexture(canvas); return glowTexture; }; const truncate = (text, max) => (text.length > max ? `${text.slice(0, max - 1)}…` : text); /** * Applies the current highlight / selection / hover state to a node's meshes. * Objects are built once and mutated here, which is far cheaper than rebuilding them. */ function styleNode(obj, node, v) { const ud = obj.userData; const lit = v.lit.has(node.id); const selected = node.id === v.selectedId; const hovered = node.id === v.hoverId; const near = v.focus ? v.focus.has(node.id) : false; const active = lit || selected || near || hovered; const faded = v.dimming && !active; const color = lit ? SIGNAL_COLOR : selected ? '#ffffff' : ud.color; const scale = lit || selected ? 1.3 : hovered ? 1.2 : 1; ud.core.material.color.set(color); ud.core.material.opacity = faded ? 0.14 : 1; ud.core.scale.setScalar(ud.r * scale); ud.halo.material.color.set(color); ud.halo.material.opacity = faded ? 0.03 : lit || selected ? 0.9 : 0.35; ud.halo.scale.setScalar(ud.r * (lit || selected ? 6 : 4)); const important = ud.tier === 'root' || ud.tier === 'hub'; ud.label.visible = active || (v.labelMode === 'all' ? !faded : important && !v.dimming); const labelColor = lit ? SIGNAL_COLOR : LABEL_COLOR; if (ud.label.color !== labelColor) ud.label.color = labelColor; ud.label.position.y = ud.r * scale + ud.label.textHeight * 0.8 + 1.5; } const GraphView = ({ data, analysis, documentName, highlightedNodes = [], selectedId, onSelect }) => { const fgRef = useRef(); const containerRef = useRef(); const rotateTimerRef = useRef(); const [dimensions, setDimensions] = useState({ width: 0, height: 0 }); const [hoverId, setHoverId] = useState(null); const [autoRotate, setAutoRotate] = useState(true); const [labelMode, setLabelMode] = useState(() => (data.nodes.length > 70 ? 'smart' : 'all')); const autoRotateRef = useRef(autoRotate); const visual = useMemo(() => { const lit = new Set(highlightedNodes); const focus = selectedId ? new Set([selectedId, ...(analysis.neighbors.get(selectedId) || []).map((n) => n.id)]) : null; return { lit, focus, selectedId, labelMode, dimming: lit.size > 0 || !!focus }; }, [highlightedNodes, selectedId, analysis, labelMode]); // Read by the node builder, so freshly created objects start in the right state const visualRef = useRef({ ...visual, hoverId }); useLayoutEffect(() => { visualRef.current = { ...visual, hoverId }; autoRotateRef.current = autoRotate; }); const nodeThreeObject = useCallback((node) => { const tier = analysis.tier.get(node.id) || 'detail'; const color = TIER_COLORS[tier]; const r = nodeRadius(tier, analysis.degree.get(node.id) || 0); const core = new THREE.Mesh(SPHERE, new THREE.MeshBasicMaterial({ color, transparent: true })); const halo = new THREE.Sprite(new THREE.SpriteMaterial({ map: getGlowTexture(), color, transparent: true, blending: THREE.AdditiveBlending, depthWrite: false, })); const text = node.id === ROOT_ID && documentName ? truncate(documentName, 32) : truncate(String(node.id), 40); const label = new SpriteText(text); label.fontFace = 'Inter, system-ui, sans-serif'; label.fontWeight = tier === 'hub' || tier === 'root' ? '600' : '500'; label.textHeight = tier === 'root' ? 8 : tier === 'hub' ? 6.5 : tier === 'concept' ? 5 : 4.2; label.color = LABEL_COLOR; label.backgroundColor = 'rgba(5,7,13,0.6)'; label.padding = [3, 1.5]; label.borderRadius = 2.5; label.material.depthWrite = false; const group = new THREE.Group(); group.add(halo, core, label); group.userData = { core, halo, label, color, r, tier }; styleNode(group, node, visualRef.current); return group; }, [analysis, documentName]); // Re-style existing objects whenever highlight, selection, hover or label mode changes useEffect(() => { const current = { ...visual, hoverId }; data.nodes.forEach((node) => { if (node.__threeObj?.userData?.core) styleNode(node.__threeObj, node, current); }); }, [visual, hoverId, data]); const linkColor = useCallback((link) => { const s = idOf(link.source); const t = idOf(link.target); const rootEdge = s === ROOT_ID || t === ROOT_ID; if (visual.lit.has(s) && visual.lit.has(t)) return 'rgba(74,222,128,0.95)'; if (visual.selectedId && (s === visual.selectedId || t === visual.selectedId)) return 'rgba(255,255,255,0.75)'; if (!rootEdge && (visual.lit.has(s) || visual.lit.has(t))) return 'rgba(74,222,128,0.35)'; if (visual.dimming) return rootEdge ? 'rgba(148,163,184,0.03)' : 'rgba(148,163,184,0.06)'; return rootEdge ? 'rgba(148,163,184,0.12)' : 'rgba(148,163,255,0.5)'; }, [visual]); const linkWidth = useCallback((link) => { const s = idOf(link.source); const t = idOf(link.target); if (visual.lit.has(s) && visual.lit.has(t)) return 1.8; if (visual.selectedId && (s === visual.selectedId || t === visual.selectedId)) return 1.2; return 0.5; }, [visual]); const linkParticles = useCallback((link) => { const s = idOf(link.source); const t = idOf(link.target); if (visual.lit.has(s) && visual.lit.has(t)) return 3; if (visual.selectedId && (s === visual.selectedId || t === visual.selectedId)) return 2; return 0; }, [visual]); const linkParticleColor = useCallback((link) => (visual.lit.has(idOf(link.source)) ? SIGNAL_COLOR : '#ffffff'), [visual]); const linkLabel = useCallback((link) => { const s = idOf(link.source); const t = idOf(link.target); if (s === ROOT_ID || t === ROOT_ID) return ''; return `