Skip to content

Commit e517bd6

Browse files
committed
Got everything converted to TS, except node parameter on line 70. Still working on getting a solution
1 parent 164d30c commit e517bd6

File tree

1 file changed

+19
-19
lines changed

1 file changed

+19
-19
lines changed

src/webview/Flow.tsx

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,9 @@ import ReactFlow, {
77
Background,
88
useNodesState,
99
useEdgesState,
10-
ReactFlowInstance
10+
ReactFlowInstance,
11+
Node,
12+
Edge
1113
} from "reactflow";
1214
import "reactflow/dist/style.css";
1315
import { ConnectionLineType } from "../types/connection";
@@ -16,14 +18,15 @@ import * as d3 from 'd3';
1618
import { Tree } from "../types/tree";
1719
import { hierarchyData } from "../types/hierarchyData";
1820
import { getNonce } from "../getNonce";
21+
import { FlowNode } from "typescript";
1922

2023
const onInit = (reactFlowInstance: ReactFlowInstance) =>
2124
console.log("flow loaded:", reactFlowInstance);
2225

2326
const OverviewFlow = () => {
2427
const reactFlowWrapper = useRef<HTMLDivElement>(null);
25-
const initialNodes = [];
26-
const initialEdges = [];
28+
const initialNodes : Node[] = [];
29+
const initialEdges : Edge[] = [];
2730

2831
const [nodes, setNodes, onNodesChange] = useNodesState([]);
2932
const [edges, setEdges, onEdgesChange] = useEdgesState([]);
@@ -35,19 +38,17 @@ const OverviewFlow = () => {
3538

3639

3740
useEffect(() => {
38-
window.addEventListener('message', (e) => {
41+
window.addEventListener('message', (e: MessageEvent) => {
3942
// object containing type prop and value prop
40-
const msg = e.data;
43+
const msg : MessageEvent = e;
44+
console.log(e)
4145

42-
switch (msg.type) {
46+
switch (msg.data.type) {
4347
case 'parsed-data': {
44-
let data : object | undefined = msg.value;
45-
console.log('data', data)
48+
let data : Tree | undefined = msg.data.value;
4649
mappedData(data)
47-
console.log('nodes', initialNodes)
4850
setEdges(initialEdges);
4951
setNodes(initialNodes)
50-
console.log('edges: ', edges);
5152
break;
5253
}
5354
}
@@ -56,25 +57,24 @@ const OverviewFlow = () => {
5657

5758

5859
// Function that creates Tree Structure
59-
function mappedData (data) {
60+
function mappedData (data: Tree) : void {
6061

6162
// Create a holder for the heirarchical data (msg.value), data comes in an object of all the Trees
62-
const root : any = d3.hierarchy(data)
63-
console.log('root', root)
63+
const root : d3.HierarchyNode<Tree> = d3.hierarchy(data)
64+
6465

6566
//create tree layout and give nodes their positions
66-
const treeLayout = d3.tree().size([800, 500])
67+
const treeLayout : d3.TreeLayout<unknown> = d3.tree().size([800, 500])
6768
treeLayout(root);
68-
6969
// Iterate through each Tree and create a node
7070
root.each((node: any) : void => {
71-
71+
console.log('Node', node)
7272
// Create a Node from the current Root and add it to our nodes array
7373
initialNodes.push({
7474
id: node.data.id,
75+
position: { x: node.x ? node.x : 0, y: node.y ? node.y : 0 },
7576
type: 'default',
7677
data: { label: node.data.name },
77-
position: { x: node.x ? node.x : 0, y: node.y ? node.y : 0 },
7878
style: {
7979
borderRadius: '6px',
8080
borderWidth: '2px',
@@ -88,7 +88,7 @@ const OverviewFlow = () => {
8888

8989
// If the current node has a parent, create an edge to show relationship
9090
if (node.data.parent) {
91-
const newEdge = {
91+
const newEdge : Edge = {
9292
id: `${getNonce()}`,
9393
source: node.data.parent,
9494
target: node.data.id,
@@ -98,7 +98,7 @@ const OverviewFlow = () => {
9898

9999

100100
// Check if the edge already exists before adding
101-
const edgeExists = initialEdges.some(
101+
const edgeExists : boolean = initialEdges.some(
102102
edge => edge.source === newEdge.source && edge.target === newEdge.target
103103
);
104104

0 commit comments

Comments
 (0)