|
| 1 | +import { useState, useRef, useCallback, useMemo } from 'react' |
| 2 | +import type { TopologyNode, TopologyLink } from '@/api/types' |
| 3 | + |
| 4 | +interface NetworkGraphProps { |
| 5 | + nodes: TopologyNode[] |
| 6 | + links: TopologyLink[] |
| 7 | + width?: number |
| 8 | + height?: number |
| 9 | + onNodeClick?: (node: TopologyNode) => void |
| 10 | + onLinkClick?: (link: TopologyLink) => void |
| 11 | +} |
| 12 | + |
| 13 | +interface TooltipState { |
| 14 | + visible: boolean |
| 15 | + x: number |
| 16 | + y: number |
| 17 | + content: string |
| 18 | +} |
| 19 | + |
| 20 | +// Color scale for utilization (0-100%) |
| 21 | +function getUtilizationColor(utilization: number): string { |
| 22 | + if (utilization <= 0) return '#94a3b8' // gray-400 |
| 23 | + if (utilization < 25) return '#22c55e' // green-500 |
| 24 | + if (utilization < 50) return '#84cc16' // lime-500 |
| 25 | + if (utilization < 75) return '#eab308' // yellow-500 |
| 26 | + if (utilization < 90) return '#f97316' // orange-500 |
| 27 | + return '#ef4444' // red-500 |
| 28 | +} |
| 29 | + |
| 30 | +export function NetworkGraph({ |
| 31 | + nodes, |
| 32 | + links, |
| 33 | + width = 800, |
| 34 | + height = 600, |
| 35 | + onNodeClick, |
| 36 | + onLinkClick, |
| 37 | +}: NetworkGraphProps) { |
| 38 | + const svgRef = useRef<SVGSVGElement>(null) |
| 39 | + const [tooltip, setTooltip] = useState<TooltipState>({ |
| 40 | + visible: false, |
| 41 | + x: 0, |
| 42 | + y: 0, |
| 43 | + content: '', |
| 44 | + }) |
| 45 | + const [transform, setTransform] = useState({ x: 0, y: 0, scale: 1 }) |
| 46 | + const [isPanning, setIsPanning] = useState(false) |
| 47 | + const [panStart, setPanStart] = useState({ x: 0, y: 0 }) |
| 48 | + |
| 49 | + // Create a map for quick node lookup |
| 50 | + const nodeMap = useMemo(() => { |
| 51 | + const map = new Map<string, TopologyNode>() |
| 52 | + nodes.forEach((node) => map.set(node.id, node)) |
| 53 | + return map |
| 54 | + }, [nodes]) |
| 55 | + |
| 56 | + // Calculate bounds and center the graph |
| 57 | + const viewBox = useMemo(() => { |
| 58 | + if (nodes.length === 0) return { minX: 0, minY: 0, width: 800, height: 600 } |
| 59 | + |
| 60 | + const xs = nodes.map((n) => n.x) |
| 61 | + const ys = nodes.map((n) => n.y) |
| 62 | + const minX = Math.min(...xs) - 50 |
| 63 | + const maxX = Math.max(...xs) + 50 |
| 64 | + const minY = Math.min(...ys) - 50 |
| 65 | + const maxY = Math.max(...ys) + 50 |
| 66 | + |
| 67 | + return { |
| 68 | + minX, |
| 69 | + minY, |
| 70 | + width: maxX - minX, |
| 71 | + height: maxY - minY, |
| 72 | + } |
| 73 | + }, [nodes]) |
| 74 | + |
| 75 | + const handleMouseDown = useCallback( |
| 76 | + (e: React.MouseEvent) => { |
| 77 | + if (e.button === 0) { |
| 78 | + setIsPanning(true) |
| 79 | + setPanStart({ x: e.clientX - transform.x, y: e.clientY - transform.y }) |
| 80 | + } |
| 81 | + }, |
| 82 | + [transform] |
| 83 | + ) |
| 84 | + |
| 85 | + const handleMouseMove = useCallback( |
| 86 | + (e: React.MouseEvent) => { |
| 87 | + if (isPanning) { |
| 88 | + setTransform((prev) => ({ |
| 89 | + ...prev, |
| 90 | + x: e.clientX - panStart.x, |
| 91 | + y: e.clientY - panStart.y, |
| 92 | + })) |
| 93 | + } |
| 94 | + }, |
| 95 | + [isPanning, panStart] |
| 96 | + ) |
| 97 | + |
| 98 | + const handleMouseUp = useCallback(() => { |
| 99 | + setIsPanning(false) |
| 100 | + }, []) |
| 101 | + |
| 102 | + const handleWheel = useCallback((e: React.WheelEvent) => { |
| 103 | + e.preventDefault() |
| 104 | + const delta = e.deltaY > 0 ? 0.9 : 1.1 |
| 105 | + setTransform((prev) => ({ |
| 106 | + ...prev, |
| 107 | + scale: Math.min(Math.max(prev.scale * delta, 0.1), 5), |
| 108 | + })) |
| 109 | + }, []) |
| 110 | + |
| 111 | + const showTooltip = useCallback((e: React.MouseEvent, content: string) => { |
| 112 | + const rect = svgRef.current?.getBoundingClientRect() |
| 113 | + if (rect) { |
| 114 | + setTooltip({ |
| 115 | + visible: true, |
| 116 | + x: e.clientX - rect.left + 10, |
| 117 | + y: e.clientY - rect.top - 10, |
| 118 | + content, |
| 119 | + }) |
| 120 | + } |
| 121 | + }, []) |
| 122 | + |
| 123 | + const hideTooltip = useCallback(() => { |
| 124 | + setTooltip((prev) => ({ ...prev, visible: false })) |
| 125 | + }, []) |
| 126 | + |
| 127 | + const resetView = useCallback(() => { |
| 128 | + setTransform({ x: 0, y: 0, scale: 1 }) |
| 129 | + }, []) |
| 130 | + |
| 131 | + return ( |
| 132 | + <div className="relative"> |
| 133 | + {/* Controls */} |
| 134 | + <div className="absolute right-2 top-2 z-10 flex gap-1"> |
| 135 | + <button |
| 136 | + onClick={() => setTransform((p) => ({ ...p, scale: p.scale * 1.2 }))} |
| 137 | + className="rounded bg-white px-2 py-1 text-sm shadow hover:bg-gray-100 dark:bg-gray-700 dark:hover:bg-gray-600" |
| 138 | + title="Zoom in" |
| 139 | + > |
| 140 | + + |
| 141 | + </button> |
| 142 | + <button |
| 143 | + onClick={() => setTransform((p) => ({ ...p, scale: p.scale * 0.8 }))} |
| 144 | + className="rounded bg-white px-2 py-1 text-sm shadow hover:bg-gray-100 dark:bg-gray-700 dark:hover:bg-gray-600" |
| 145 | + title="Zoom out" |
| 146 | + > |
| 147 | + - |
| 148 | + </button> |
| 149 | + <button |
| 150 | + onClick={resetView} |
| 151 | + className="rounded bg-white px-2 py-1 text-sm shadow hover:bg-gray-100 dark:bg-gray-700 dark:hover:bg-gray-600" |
| 152 | + title="Reset view" |
| 153 | + > |
| 154 | + Reset |
| 155 | + </button> |
| 156 | + </div> |
| 157 | + |
| 158 | + {/* Tooltip */} |
| 159 | + {tooltip.visible && ( |
| 160 | + <div |
| 161 | + className="pointer-events-none absolute z-20 rounded bg-gray-900 px-2 py-1 text-xs text-white shadow-lg dark:bg-gray-700" |
| 162 | + style={{ left: tooltip.x, top: tooltip.y }} |
| 163 | + > |
| 164 | + {tooltip.content} |
| 165 | + </div> |
| 166 | + )} |
| 167 | + |
| 168 | + {/* SVG Canvas */} |
| 169 | + <svg |
| 170 | + ref={svgRef} |
| 171 | + width={width} |
| 172 | + height={height} |
| 173 | + viewBox={`${viewBox.minX} ${viewBox.minY} ${viewBox.width} ${viewBox.height}`} |
| 174 | + className="cursor-grab rounded-lg border border-gray-200 bg-white active:cursor-grabbing dark:border-gray-700 dark:bg-gray-800" |
| 175 | + onMouseDown={handleMouseDown} |
| 176 | + onMouseMove={handleMouseMove} |
| 177 | + onMouseUp={handleMouseUp} |
| 178 | + onMouseLeave={handleMouseUp} |
| 179 | + onWheel={handleWheel} |
| 180 | + > |
| 181 | + <g |
| 182 | + transform={`translate(${transform.x / transform.scale}, ${transform.y / transform.scale}) scale(${transform.scale})`} |
| 183 | + > |
| 184 | + {/* Links */} |
| 185 | + {links.map((link) => { |
| 186 | + const source = nodeMap.get(link.source) |
| 187 | + const target = nodeMap.get(link.target) |
| 188 | + if (!source || !target) return null |
| 189 | + |
| 190 | + return ( |
| 191 | + <line |
| 192 | + key={link.id} |
| 193 | + x1={source.x} |
| 194 | + y1={source.y} |
| 195 | + x2={target.x} |
| 196 | + y2={target.y} |
| 197 | + stroke={getUtilizationColor(link.utilization)} |
| 198 | + strokeWidth={3} |
| 199 | + className="cursor-pointer transition-all hover:stroke-[5]" |
| 200 | + onMouseEnter={(e) => |
| 201 | + showTooltip( |
| 202 | + e, |
| 203 | + `${link.source} - ${link.target}\nLength: ${link.length_km.toFixed(0)} km\nUtilization: ${link.utilization.toFixed(1)}%` |
| 204 | + ) |
| 205 | + } |
| 206 | + onMouseLeave={hideTooltip} |
| 207 | + onClick={() => onLinkClick?.(link)} |
| 208 | + /> |
| 209 | + ) |
| 210 | + })} |
| 211 | + |
| 212 | + {/* Nodes */} |
| 213 | + {nodes.map((node) => ( |
| 214 | + <g |
| 215 | + key={node.id} |
| 216 | + transform={`translate(${node.x}, ${node.y})`} |
| 217 | + className="cursor-pointer" |
| 218 | + onMouseEnter={(e) => showTooltip(e, `Node ${node.id}\n${node.label}`)} |
| 219 | + onMouseLeave={hideTooltip} |
| 220 | + onClick={() => onNodeClick?.(node)} |
| 221 | + > |
| 222 | + <circle |
| 223 | + r={12} |
| 224 | + fill="#3b82f6" |
| 225 | + stroke="#1d4ed8" |
| 226 | + strokeWidth={2} |
| 227 | + className="transition-all hover:fill-blue-400" |
| 228 | + /> |
| 229 | + <text |
| 230 | + y={25} |
| 231 | + textAnchor="middle" |
| 232 | + className="fill-gray-700 text-xs font-medium dark:fill-gray-300" |
| 233 | + style={{ fontSize: '10px' }} |
| 234 | + > |
| 235 | + {node.id} |
| 236 | + </text> |
| 237 | + </g> |
| 238 | + ))} |
| 239 | + </g> |
| 240 | + </svg> |
| 241 | + </div> |
| 242 | + ) |
| 243 | +} |
0 commit comments