Skip to content

Commit 174852a

Browse files
authored
Merge pull request #6 from longcipher/fix/frontend
dynamic import for Sigma and graphology
2 parents c4e51f1 + a48db83 commit 174852a

File tree

3 files changed

+71
-53
lines changed

3 files changed

+71
-53
lines changed

frontend/biome.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,8 @@
105105
},
106106
"javascript": {
107107
"formatter": {
108-
"quoteStyle": "double"
108+
"quoteStyle": "double",
109+
"lineEnding": "lf"
109110
}
110111
}
111112
}

frontend/src/app/page.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -77,10 +77,10 @@ export default function Home() {
7777

7878
return (
7979
<div className="flex flex-col h-screen gap-4 p-4">
80-
<nav className="flex gap-4">
80+
{/* <nav className="flex gap-4">
8181
<a className="rounded transition-colors flex items-center justify-center bg-cpurple gap-2 hover:bg-[#AB47BC] dark:hover:bg-[#ccc] h-10 px-8 py-4">
8282
<i className="iconfont icon-pause" />
83-
{/* <i className="iconfont icon-playfill"></i> */}
83+
<i className="iconfont icon-playfill"></i>
8484
</a>
8585
<div className="flex gap-1">
8686
<a className="rounded transition-colors flex items-center justify-center bg-cblue gap-2 hover:bg-[#42A5F5] dark:hover:bg-[#ccc] h-10 px-8 py-4">
@@ -90,7 +90,7 @@ export default function Home() {
9090
<i className="iconfont icon-you" />
9191
</a>
9292
</div>
93-
</nav>
93+
</nav> */}
9494
<main className="flex-1 flex flex-col rounded border-2 border-orange-500">
9595
<div className="bg-blue-100 font-600 px-4 py-2 text-lg">
9696
Block: {blockNumber}

frontend/src/components/Graph.js

Lines changed: 66 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -1,53 +1,70 @@
1-
import { Graph } from "graphology";
1+
import dynamic from "next/dynamic";
22
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+
);
3865
};
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+
);
5269

5370
export default GraphComponent;

0 commit comments

Comments
 (0)