Spaces:
Running
Running
File size: 19,013 Bytes
c2ea5ed cc3c5e9 c2ea5ed |
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 |
import React, {
useRef,
useEffect,
useState,
useCallback,
useMemo,
} from "react";
import { Button } from "@/components/ui/button";
import { Input } from "@/components/ui/input";
import { Card, CardContent, CardHeader, CardTitle } from "@/components/ui/card";
import { Badge } from "@/components/ui/badge";
import { Separator } from "@/components/ui/separator";
import {
ArrowLeft,
Search,
ZoomIn,
ZoomOut,
RotateCcw,
Download,
} from "lucide-react";
import {
UniversalGraphData,
UniversalNode,
UniversalLink,
GraphVisualizationConfig,
GraphSelectionCallbacks,
GraphInteractionState,
GraphStats,
} from "@/types/graph-visualization";
import { CytoscapeGraphCore } from "@/lib/cytoscape-graph-core";
interface BaseGraphVisualizerProps {
data: UniversalGraphData;
config: GraphVisualizationConfig;
onBack?: () => void;
title?: string;
subtitle?: string;
className?: string;
children?: React.ReactNode;
}
export const BaseGraphVisualizer: React.FC<BaseGraphVisualizerProps> = ({
data,
config,
onBack,
title = "Graph Visualization",
subtitle,
className = "",
children,
}) => {
const containerRef = useRef<HTMLDivElement>(null);
const cytoscapeRef = useRef<CytoscapeGraphCore | null>(null);
// State management
const [loading, setLoading] = useState(true);
const [error, setError] = useState<string | null>(null);
const [interactionState, setInteractionState] =
useState<GraphInteractionState>({
selectedElement: null,
selectedElementType: null,
hoveredElement: null,
searchTerm: "",
highlightedElements: new Set(),
});
// Graph statistics
const [stats, setStats] = useState<GraphStats>({
nodeCount: 0,
linkCount: 0,
nodeTypes: {},
linkTypes: {},
averageDegree: 0,
maxDegree: 0,
connectedComponents: 1,
});
// Calculate graph statistics
const calculateStats = useCallback(
(graphData: UniversalGraphData): GraphStats => {
const nodeTypes: Record<string, number> = {};
const linkTypes: Record<string, number> = {};
const nodeDegrees: Record<string, number> = {};
// Count node types
graphData.nodes.forEach((node) => {
const type = node.type || "Unknown";
nodeTypes[type] = (nodeTypes[type] || 0) + 1;
nodeDegrees[node.id] = 0;
});
// Count link types and node degrees
graphData.links.forEach((link) => {
const type = link.type || "Unknown";
linkTypes[type] = (linkTypes[type] || 0) + 1;
const sourceId =
typeof link.source === "string" ? link.source : link.source.id;
const targetId =
typeof link.target === "string" ? link.target : link.target.id;
nodeDegrees[sourceId] = (nodeDegrees[sourceId] || 0) + 1;
nodeDegrees[targetId] = (nodeDegrees[targetId] || 0) + 1;
});
const degrees = Object.values(nodeDegrees);
const averageDegree =
degrees.length > 0
? degrees.reduce((a, b) => a + b, 0) / degrees.length
: 0;
const maxDegree = degrees.length > 0 ? Math.max(...degrees) : 0;
return {
nodeCount: graphData.nodes.length,
linkCount: graphData.links.length,
nodeTypes,
linkTypes,
averageDegree: Math.round(averageDegree * 100) / 100,
maxDegree,
connectedComponents: 1, // Simplified for now
};
},
[]
);
// Selection callbacks - wrapped in useMemo to prevent recreation on every render
const selectionCallbacks = useMemo(
(): GraphSelectionCallbacks => ({
onNodeSelect: (node: UniversalNode) => {
setInteractionState((prev) => ({
...prev,
selectedElement: node,
selectedElementType: "node",
}));
},
onLinkSelect: (link: UniversalLink) => {
setInteractionState((prev) => ({
...prev,
selectedElement: link,
selectedElementType: "link",
}));
},
onClearSelection: () => {
setInteractionState((prev) => ({
...prev,
selectedElement: null,
selectedElementType: null,
}));
},
}),
[]
);
// Initialize Cytoscape visualization
useEffect(() => {
console.log("BaseGraphVisualizer: useEffect triggered", {
containerRef: !!containerRef.current,
dataNodes: data.nodes.length,
dataLinks: data.links.length,
});
const initializeVisualization = async () => {
try {
if (!containerRef.current) {
console.log(
"BaseGraphVisualizer: Container ref not ready during initialization"
);
return;
}
// Wait for layout to settle
await new Promise((resolve) => setTimeout(resolve, 200));
const container = containerRef.current;
let width = container.clientWidth || config.width;
let height = container.clientHeight || config.height;
// If container has zero dimensions, try to get parent dimensions
if (width === 0 || height === 0) {
const parent = container.parentElement;
if (parent) {
width = parent.clientWidth || config.width;
height = parent.clientHeight || config.height;
console.log("BaseGraphVisualizer: Using parent dimensions", {
parentWidth: width,
parentHeight: height,
});
}
}
const finalConfig = {
...config,
width: Math.max(width, 350),
height: Math.max(height, 350),
};
console.log(
"BaseGraphVisualizer: Initializing with config",
finalConfig
);
cytoscapeRef.current = new CytoscapeGraphCore(
containerRef.current,
finalConfig,
selectionCallbacks
);
// Load data
console.log("BaseGraphVisualizer: Loading data", {
nodes: data.nodes.length,
links: data.links.length,
});
cytoscapeRef.current.updateGraph(data, true);
// Calculate and set statistics
setStats(calculateStats(data));
console.log(
"BaseGraphVisualizer: Initialization complete, setting loading to false"
);
setLoading(false);
} catch (err) {
console.error("BaseGraphVisualizer: Error initializing:", err);
setError(
`Failed to initialize visualization: ${
err instanceof Error ? err.message : String(err)
}`
);
setLoading(false);
}
};
// Function to check if refs are ready and initialize
const checkAndInitialize = () => {
if (!containerRef.current) {
console.log("BaseGraphVisualizer: Missing container ref, retrying...");
return false;
}
return true;
};
// If refs not ready immediately, set up a retry mechanism
if (!checkAndInitialize()) {
let retryCount = 0;
const maxRetries = 20;
const retryInterval = setInterval(() => {
retryCount++;
console.log(
`BaseGraphVisualizer: Retry attempt ${retryCount}/${maxRetries}`
);
if (checkAndInitialize()) {
clearInterval(retryInterval);
initializeVisualization();
} else if (retryCount >= maxRetries) {
clearInterval(retryInterval);
console.error("BaseGraphVisualizer: Max retries reached, giving up");
setError(
"Failed to initialize visualization: DOM elements not ready"
);
setLoading(false);
}
}, 200);
return () => {
clearInterval(retryInterval);
if (cytoscapeRef.current) {
cytoscapeRef.current.destroy();
cytoscapeRef.current = null;
}
};
}
// If refs are ready, initialize immediately
const timeoutId = setTimeout(initializeVisualization, 100);
// Cleanup
return () => {
clearTimeout(timeoutId);
if (cytoscapeRef.current) {
cytoscapeRef.current.destroy();
cytoscapeRef.current = null;
}
};
}, [data, config, calculateStats, selectionCallbacks]);
// Handle search
const handleSearch = useCallback(
(searchTerm: string) => {
setInteractionState((prev) => ({
...prev,
searchTerm,
highlightedElements: new Set(
data.nodes
.filter(
(node) =>
node.name?.toLowerCase().includes(searchTerm.toLowerCase()) ||
node.label?.toLowerCase().includes(searchTerm.toLowerCase()) ||
node.id.toLowerCase().includes(searchTerm.toLowerCase())
)
.map((node) => node.id)
),
}));
},
[data.nodes]
);
// Control handlers
const handleZoomIn = () => cytoscapeRef.current?.zoomIn();
const handleZoomOut = () => cytoscapeRef.current?.zoomOut();
const handleResetZoom = () => cytoscapeRef.current?.resetZoom();
const handleDownload = () => {
if (!cytoscapeRef.current) return;
// For Cytoscape, we'll export as PNG instead of SVG
const cy = cytoscapeRef.current.getCytoscape();
if (!cy) return;
const pngData = cy.png({ scale: 2, full: true });
const downloadLink = document.createElement("a");
downloadLink.href = pngData;
downloadLink.download = `graph-${Date.now()}.png`;
document.body.appendChild(downloadLink);
downloadLink.click();
document.body.removeChild(downloadLink);
};
// Handle window resize
useEffect(() => {
const handleResize = () => {
if (cytoscapeRef.current && containerRef.current) {
const container = containerRef.current;
const width = container.clientWidth;
const height = container.clientHeight;
cytoscapeRef.current.resize(width, height);
}
};
window.addEventListener("resize", handleResize);
return () => window.removeEventListener("resize", handleResize);
}, []);
if (loading) {
return (
<div className="flex items-center justify-center h-96">
<div className="text-center">
<div className="animate-spin rounded-full h-8 w-8 border-b-2 border-blue-600 mx-auto mb-4"></div>
<p className="text-muted-foreground">Loading visualization...</p>
</div>
</div>
);
}
if (error) {
return (
<div className="flex items-center justify-center h-96">
<div className="text-center">
<div className="text-red-500 mb-4">⚠️</div>
<p className="text-red-600 font-medium">Visualization Error</p>
<p className="text-muted-foreground text-sm mt-2">{error}</p>
</div>
</div>
);
}
return (
<div className={`flex flex-col h-full ${className}`}>
{/* Header */}
<div className="flex items-center justify-between p-4 border-b">
<div className="flex items-center space-x-4">
{onBack && (
<Button variant="ghost" size="sm" onClick={onBack}>
<ArrowLeft className="h-4 w-4 mr-2" />
Back
</Button>
)}
<div>
<h1 className="text-xl font-semibold">{title}</h1>
{subtitle && (
<p className="text-sm text-muted-foreground">{subtitle}</p>
)}
</div>
</div>
{/* Toolbar */}
{config.showToolbar && (
<div className="flex items-center space-x-2">
{config.enableSearch && (
<div className="relative">
<Search className="absolute left-3 top-1/2 transform -translate-y-1/2 h-4 w-4 text-muted-foreground" />
<Input
placeholder="Search nodes..."
value={interactionState.searchTerm}
onChange={(e) => handleSearch(e.target.value)}
className="pl-9 w-48"
/>
</div>
)}
<Button variant="outline" size="sm" onClick={handleZoomIn}>
<ZoomIn className="h-4 w-4" />
</Button>
<Button variant="outline" size="sm" onClick={handleZoomOut}>
<ZoomOut className="h-4 w-4" />
</Button>
<Button variant="outline" size="sm" onClick={handleResetZoom}>
<RotateCcw className="h-4 w-4" />
</Button>
<Button variant="outline" size="sm" onClick={handleDownload}>
<Download className="h-4 w-4" />
</Button>
</div>
)}
</div>
{/* Main content */}
<div className="flex flex-1 overflow-hidden">
{/* Graph container */}
<div className="flex-1 relative">
<div
ref={containerRef}
className="w-full h-full"
style={{ minHeight: "400px" }}
>
{/* Cytoscape container - no SVG needed */}
</div>
</div>
{/* Sidebar */}
{config.showSidebar && (
<div className="w-96 border-l bg-gray-50 overflow-y-auto">
<div className="p-4 space-y-4">
{/* Statistics */}
{config.showStats && (
<Card>
<CardHeader>
<CardTitle className="text-sm">Graph Overview</CardTitle>
</CardHeader>
<CardContent className="space-y-2">
<div className="flex justify-between">
<span className="text-sm text-muted-foreground">
Nodes:
</span>
<Badge variant="secondary">{stats.nodeCount}</Badge>
</div>
<div className="flex justify-between">
<span className="text-sm text-muted-foreground">
Links:
</span>
<Badge variant="secondary">{stats.linkCount}</Badge>
</div>
<div className="flex justify-between">
<span className="text-sm text-muted-foreground">
Avg Degree:
</span>
<Badge variant="outline">{stats.averageDegree}</Badge>
</div>
</CardContent>
</Card>
)}
{/* Node Types */}
{Object.keys(stats.nodeTypes).length > 0 && (
<Card>
<CardHeader>
<CardTitle className="text-sm">Node Types</CardTitle>
</CardHeader>
<CardContent>
<div className="space-y-2">
{Object.entries(stats.nodeTypes).map(([type, count]) => (
<div
key={type}
className="flex justify-between items-center"
>
<span className="text-sm">{type}</span>
<Badge variant="outline">{count}</Badge>
</div>
))}
</div>
</CardContent>
</Card>
)}
{/* Selected Element Info */}
{interactionState.selectedElement && (
<Card>
<CardHeader>
<CardTitle className="text-sm">
{interactionState.selectedElementType === "node"
? "Node"
: "Link"}{" "}
Details
</CardTitle>
</CardHeader>
<CardContent>
<div className="space-y-2">
<div>
<span className="text-sm font-medium">ID:</span>
<p className="text-sm text-muted-foreground break-all">
{interactionState.selectedElement.id}
</p>
</div>
{/* Show name only for nodes or links that have name/label */}
{interactionState.selectedElementType === "node" &&
(interactionState.selectedElement as UniversalNode)
.name && (
<div>
<span className="text-sm font-medium">Name:</span>
<p className="text-sm text-muted-foreground">
{
(
interactionState.selectedElement as UniversalNode
).name
}
</p>
</div>
)}
{interactionState.selectedElement.type && (
<div>
<span className="text-sm font-medium">Type:</span>
<Badge variant="secondary" className="ml-2">
{interactionState.selectedElement.type}
</Badge>
</div>
)}
{/* Link-specific info */}
{interactionState.selectedElementType === "link" && (
<>
<Separator />
<div>
<span className="text-sm font-medium">Source:</span>
<p className="text-sm text-muted-foreground">
{(() => {
const link =
interactionState.selectedElement as UniversalLink;
return typeof link.source === "string"
? link.source
: link.source.id;
})()}
</p>
</div>
<div>
<span className="text-sm font-medium">Target:</span>
<p className="text-sm text-muted-foreground">
{(() => {
const link =
interactionState.selectedElement as UniversalLink;
return typeof link.target === "string"
? link.target
: link.target.id;
})()}
</p>
</div>
</>
)}
</div>
</CardContent>
</Card>
)}
{/* Custom children content */}
{children}
</div>
</div>
)}
</div>
</div>
);
};
|