feat: add 3D toggle and adjust theme

This commit is contained in:
2025-02-20 17:26:27 +01:00
parent 5405c3e12f
commit 13bb965b28
3 changed files with 171 additions and 13 deletions

View File

@@ -1,6 +1,7 @@
import { useEffect, useRef, useState } from "react";
import { ChangeEvent, useEffect, useRef, useState } from "react";
import { apiAuth } from "./api";
import { GraphCanvas, GraphCanvasRef, GraphEdge, GraphNode, useSelection } from "reagraph";
import { GraphCanvas, GraphCanvasRef, GraphEdge, GraphNode, recommendLayout, useSelection } from "reagraph";
import { customTheme } from "./NetworkTheme";
interface NetworkData {
nodes: GraphNode[],
@@ -10,6 +11,7 @@ interface NetworkData {
export const GraphComponent = () => {
const [data, setData] = useState({ nodes: [], edges: [] } as NetworkData);
const [loading, setLoading] = useState(true);
const [threed, setThreed] = useState(false);
async function loadData() {
setLoading(true);
@@ -26,20 +28,46 @@ export const GraphComponent = () => {
ref: graphRef,
nodes: data.nodes,
edges: data.edges,
pathSelectionType: 'out'
pathSelectionType: 'out',
});
function handleThreed() {
setThreed(!threed)
graphRef.current?.fitNodesInView();
graphRef.current?.centerGraph();
graphRef.current?.resetControls();
}
return (
<GraphCanvas
draggable
ref={graphRef}
nodes={data.nodes}
edges={data.edges}
selections={selections}
actives={actives}
onCanvasClick={onCanvasClick}
onNodeClick={onNodeClick}
/>
<div style={{ position: 'absolute', top: 0, bottom: 0, left: 0, right: 0 }}>
<div className="controls" >
<div className="control" onClick={handleThreed}>
<span>2D</span>
<div className="switch">
<input type="checkbox" checked={threed} />
<span className="slider round"></span>
</div>
<span>3D</span>
</div>
</div>
<GraphCanvas
draggable
cameraMode={threed ? "rotate" : "pan"}
layoutType={threed ? "forceDirected3d" : "forceDirected2d"}
layoutOverrides={{
linkDistance: 200
}}
labelType="auto"
ref={graphRef}
theme={customTheme}
nodes={data.nodes}
edges={data.edges}
selections={selections}
actives={actives}
onCanvasClick={onCanvasClick}
onNodeClick={onNodeClick}
/>
</div>
);
}