|
1 | | -import { Graph } from "graphology"; |
| 1 | +import dynamic from "next/dynamic"; |
2 | 2 | import React, { useEffect, useRef } from "react"; |
3 | | -import Sigma from "sigma"; |
4 | | - |
5 | | -const GraphComponent = ({ graphData }) => { |
6 | | - const containerRef = useRef(null); |
7 | | - const sigmaInstanceRef = useRef(null); |
8 | | - |
9 | | - useEffect(() => { |
10 | | - if (containerRef.current) { |
11 | | - const graph = new Graph(); |
12 | | - for (const node of graphData.nodes) { |
13 | | - graph.addNode(node.id, { ...node }); |
14 | | - } |
15 | | - for (const edge of graphData.edges) { |
16 | | - graph.addEdge(edge.source, edge.target, { ...edge }); |
17 | | - } |
18 | | - sigmaInstanceRef.current = new Sigma(graph, containerRef.current); |
19 | | - } |
20 | | - |
21 | | - return () => { |
22 | | - sigmaInstanceRef.current?.kill(); |
23 | | - }; |
24 | | - }, [graphData]); |
25 | | - |
26 | | - useEffect(() => { |
27 | | - const container = containerRef.current; |
28 | | - |
29 | | - if (container) { |
30 | | - const resizeObserver = new ResizeObserver(() => { |
31 | | - sigmaInstanceRef.current?.refresh(); |
32 | | - }); |
33 | | - |
34 | | - resizeObserver.observe(container); |
35 | | - |
36 | | - return () => { |
37 | | - resizeObserver.disconnect(); |
| 3 | + |
| 4 | +// Dynamically import Sigma, ensuring it only loads on the client side |
| 5 | +const GraphComponent = dynamic( |
| 6 | + () => |
| 7 | + import("sigma").then((Sigma) => { |
| 8 | + return ({ graphData }) => { |
| 9 | + const containerRef = useRef(null); |
| 10 | + const sigmaInstanceRef = useRef(null); |
| 11 | + |
| 12 | + useEffect(() => { |
| 13 | + if (containerRef.current) { |
| 14 | + const { Graph } = require("graphology"); // Dynamically import graphology on the client side |
| 15 | + const graph = new Graph(); |
| 16 | + |
| 17 | + // Add nodes and edges to the graph |
| 18 | + for (const node of graphData.nodes) { |
| 19 | + graph.addNode(node.id, { ...node }); |
| 20 | + } |
| 21 | + for (const edge of graphData.edges) { |
| 22 | + graph.addEdge(edge.source, edge.target, { ...edge }); |
| 23 | + } |
| 24 | + |
| 25 | + // Initialize Sigma instance |
| 26 | + sigmaInstanceRef.current = new Sigma.default( |
| 27 | + graph, |
| 28 | + containerRef.current, |
| 29 | + ); |
| 30 | + } |
| 31 | + |
| 32 | + // Clean up the Sigma instance on component unmount |
| 33 | + return () => { |
| 34 | + sigmaInstanceRef.current?.kill(); |
| 35 | + }; |
| 36 | + }, [graphData]); |
| 37 | + |
| 38 | + useEffect(() => { |
| 39 | + const container = containerRef.current; |
| 40 | + |
| 41 | + if (container) { |
| 42 | + const resizeObserver = new ResizeObserver(() => { |
| 43 | + sigmaInstanceRef.current?.refresh(); |
| 44 | + }); |
| 45 | + |
| 46 | + // Observe container resizing to refresh the Sigma instance |
| 47 | + resizeObserver.observe(container); |
| 48 | + |
| 49 | + // Disconnect the ResizeObserver on cleanup |
| 50 | + return () => { |
| 51 | + resizeObserver.disconnect(); |
| 52 | + }; |
| 53 | + } |
| 54 | + }, []); |
| 55 | + |
| 56 | + return ( |
| 57 | + <div |
| 58 | + ref={containerRef} |
| 59 | + style={{ |
| 60 | + width: "100%", |
| 61 | + height: "100%", |
| 62 | + }} |
| 63 | + /> |
| 64 | + ); |
38 | 65 | }; |
39 | | - } |
40 | | - }, []); |
41 | | - |
42 | | - return ( |
43 | | - <div |
44 | | - ref={containerRef} |
45 | | - style={{ |
46 | | - width: "100%", |
47 | | - height: "100%", |
48 | | - }} |
49 | | - /> |
50 | | - ); |
51 | | -}; |
| 66 | + }), |
| 67 | + { ssr: false }, // Disable SSR |
| 68 | +); |
52 | 69 |
|
53 | 70 | export default GraphComponent; |
0 commit comments