Skip to content

Commit 961769a

Browse files
committed
UPDATE:
1. changes in real time
1 parent 0be7252 commit 961769a

File tree

1 file changed

+17
-8
lines changed

1 file changed

+17
-8
lines changed

qubitverse/visualizer/src/components/HilbertSpaceResult.jsx

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,12 @@ import { Network } from "vis-network/standalone";
44
function HilbertSpaceResult({ nodes, edges, measuredValue }) {
55
const containerRef = useRef(null);
66
const networkRef = useRef(null);
7-
const [network, setNetwork] = useState(null);
8-
const [graphData, setGraphData] = useState({ nodes, edges });
7+
// Create a ref to store the nodes dataset
8+
const nodesRef = useRef(nodes);
9+
10+
useEffect(() => {
11+
nodesRef.current = nodes;
12+
}, [nodes]);
913

1014
useEffect(() => {
1115
if (containerRef.current && !networkRef.current) {
@@ -33,27 +37,28 @@ function HilbertSpaceResult({ nodes, edges, measuredValue }) {
3337
improvedLayout: true,
3438
hierarchical: {
3539
enabled: false,
36-
direction: "LR", // "UD" = Top to Bottom, "LR" = Left to Right
40+
direction: "UD",
3741
sortMethod: "directed",
3842
},
43+
3944
},
4045
};
4146

42-
networkRef.current = new Network(containerRef.current, graphData, options);
47+
networkRef.current = new Network(containerRef.current, { nodes, edges }, options);
4348

4449
// When a node is clicked, toggle its expanded state.
4550
networkRef.current.on("click", (params) => {
4651
if (params.nodes.length > 0) {
4752
const nodeId = params.nodes[0];
48-
const node = nodes.get(nodeId);
53+
const node = nodesRef.current.get(nodeId);
4954
if (node) {
5055
if (!node.expanded) {
5156
const tableText = node.values
5257
.map((row) => `${row.qubit}: ${row.value}`)
5358
.join("\n");
54-
nodes.update({ id: nodeId, label: tableText, expanded: true });
59+
nodesRef.current.update({ id: nodeId, label: tableText, expanded: true });
5560
} else {
56-
nodes.update({
61+
nodesRef.current.update({
5762
id: nodeId,
5863
label: node.originalLabel,
5964
expanded: false,
@@ -62,8 +67,12 @@ function HilbertSpaceResult({ nodes, edges, measuredValue }) {
6267
}
6368
}
6469
});
70+
}
71+
}, [nodes, edges]);
6572

66-
setNetwork(networkRef.current);
73+
useEffect(() => {
74+
if (networkRef.current) {
75+
networkRef.current.setData({ nodes, edges });
6776
}
6877
}, [nodes, edges]);
6978

0 commit comments

Comments
 (0)