Skip to content

Commit 5a3f338

Browse files
Merge pull request #26 from oslabs-beta/FL-JA/RL-39/Flow-to-TS
Fl ja/rl 39/flow to ts
2 parents b0d453e + 017c393 commit 5a3f338

File tree

2 files changed

+43
-26
lines changed

2 files changed

+43
-26
lines changed

src/types/hierarchyData.ts

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,17 @@ import { Tree } from "./tree"
22

33

44
export interface hierarchyData {
5-
children?: hierarchyData[],
6-
data: Tree,
7-
depth: number,
8-
height: number,
9-
parent: hierarchyData | null
10-
}
5+
id: string,
6+
position: { x: number, y: number },
7+
type: string,
8+
data: { label: string },
9+
style: {
10+
borderRadius: string,
11+
borderWidth: string,
12+
borderColor: string,
13+
display: string,
14+
justifyContent: string,
15+
placeItems: string,
16+
backgroundColor: string,
17+
}
18+
}

src/webview/Flow.tsx

Lines changed: 29 additions & 20 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,33 @@ 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+
65+
// Dynamically adjust height and width of display depending on the amount of nodes
66+
const totalNodes : number = root.descendants().length;
67+
const width : number = Math.max(totalNodes * 100, 800);
68+
const height = Math.max(totalNodes * 20, 500)
69+
70+
71+
//create tree layout and give nodes their positions and
72+
const treeLayout : d3.TreeLayout<unknown> = d3.tree()
73+
.size([ width, height ])
74+
.separation((a: d3.HierarchyPointNode<Node>, b: d3.HierarchyPointNode<Node>) => (a.parent == b.parent ? 2 : 2.5))
6475

65-
//create tree layout and give nodes their positions
66-
const treeLayout = d3.tree().size([800, 500])
67-
treeLayout(root);
6876

77+
treeLayout(root);
6978
// Iterate through each Tree and create a node
7079
root.each((node: any) : void => {
71-
80+
console.log('Node', node)
7281
// Create a Node from the current Root and add it to our nodes array
7382
initialNodes.push({
7483
id: node.data.id,
84+
position: { x: node.x ? node.x : 0, y: node.y ? node.y : 0 },
7585
type: 'default',
7686
data: { label: node.data.name },
77-
position: { x: node.x ? node.x : 0, y: node.y ? node.y : 0 },
7887
style: {
7988
borderRadius: '6px',
8089
borderWidth: '2px',
@@ -88,7 +97,7 @@ const OverviewFlow = () => {
8897

8998
// If the current node has a parent, create an edge to show relationship
9099
if (node.data.parent) {
91-
const newEdge = {
100+
const newEdge : Edge = {
92101
id: `${getNonce()}`,
93102
source: node.data.parent,
94103
target: node.data.id,
@@ -98,7 +107,7 @@ const OverviewFlow = () => {
98107

99108

100109
// Check if the edge already exists before adding
101-
const edgeExists = initialEdges.some(
110+
const edgeExists : boolean = initialEdges.some(
102111
edge => edge.source === newEdge.source && edge.target === newEdge.target
103112
);
104113

0 commit comments

Comments
 (0)