Skip to content

Commit a58cc6a

Browse files
Merge pull request #14 from oslabs-beta/LK/visualizingParsedData
Lk/visualizing parsed data
2 parents 1b86337 + a75fc24 commit a58cc6a

File tree

5 files changed

+129
-128
lines changed

5 files changed

+129
-128
lines changed

extension.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ function activate(context) {
2222
const tree = new Parser(fileArray[0].path);
2323
tree.parse();
2424
const data = tree.getTree();
25-
// console.log('Data sent back: ', data);
25+
console.log('Data sent back: \n', data);
2626
createPanel(context, data);
2727
});
2828
context.subscriptions.push(disposable, result);

src/panel.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,12 +34,14 @@ function createPanel(context, data) {
3434
switch (msg.type) {
3535
case 'onData':
3636
if (!msg.value) break;
37-
// context.workspaceState.update('reactLabyrinth', msg.value);
37+
context.workspaceState = context.workspaceState || {};
38+
context.workspaceState.update('reactLabyrinth', msg.value);
39+
// console.log('msg.value from panel.js: ', msg.value);
3840
panel.webview.postMessage(
3941
{
4042
type: 'parsed-data',
4143
value: msg.value, // tree object
42-
// settings: await vscode.workspace.getConfiguration('reactLabyrinth')
44+
settings: await vscode.workspace.getConfiguration('reactLabyrinth')
4345
});
4446
break;
4547

src/webview/Flow.jsx

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import React, { useCallback, useEffect } from "react";
1+
import React, { useCallback, useEffect, useState } from "react";
22
import ReactFlow, {
33
addEdge,
44
MiniMap,
@@ -9,30 +9,35 @@ import ReactFlow, {
99
} from "reactflow";
1010
import "reactflow/dist/style.css"
1111

12-
import {
13-
nodes as initialNodes,
14-
edges as initialEdges
15-
} from "./initial";
12+
import FlowBuilder from './flowBuilder.js';
1613

1714
const onInit = (reactFlowInstance) =>
1815
console.log("flow loaded:", reactFlowInstance);
1916

2017
const OverviewFlow = () => {
18+
const initialNodes = [];
19+
const initialEdges = [];
20+
2121
const [nodes, setNodes, onNodesChange] = useNodesState(initialNodes);
2222
const [edges, setEdges, onEdgesChange] = useEdgesState(initialEdges);
23+
2324
const onConnect = useCallback(
24-
(params) => setEdges((eds) => addEdge(params, eds)),
25-
[setEdges]
25+
(params) => setEdges((eds) => addEdge({ ...params, type: ConnectionLineType.Bezier, animated: true }, eds)),
26+
[]
2627
);
2728

2829
useEffect(() => {
2930
window.addEventListener('message', (e) => {
3031
const msg = e.data; // object containing type prop and value prop
3132
switch (msg.type) {
3233
case 'parsed-data': {
33-
// this is where we would set state of msg for tree data, root file, and other info
34-
console.log('msg.value: ', msg.value);
35-
console.log('testing from flow.jsx');
34+
const results = new FlowBuilder(msg.value);
35+
results.build(msg.settings)
36+
console.log('results: ', results);
37+
console.log('results.initialNodes: ', results.initialNodes);
38+
console.log('results.initialEdges: ', results.initialEdges);
39+
setNodes(results.initialNodes);
40+
setEdges(results.initialEdges);
3641
break;
3742
}
3843
}

src/webview/flowBuilder.js

Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
import React from 'react';
2+
// will create a build func and then call the helper funcs to return an object
3+
// make a new instance of this class in flow, call the build method, and pass this as state
4+
class FlowBuilder {
5+
constructor(data) {
6+
this.parsedData = [data];
7+
this.id = 0;
8+
this.initialNodes = [];
9+
this.viewData;
10+
this.edgeId = 0;
11+
this.initialEdges = [];
12+
}
13+
14+
buildNodesArray(parsedData) {
15+
if (!parsedData) return;
16+
17+
parsedData.forEach((item) => {
18+
const node = {
19+
id: (++this.id).toString(),
20+
data: {
21+
label: (
22+
<div key={this.id}>
23+
<ul>
24+
<li>{item.fileName}</li>
25+
<li>Client Component: {item.isClientComponent.toString()}</li>
26+
</ul>
27+
</div>
28+
)
29+
},
30+
type: item.depth === 0 ? 'input' : '',
31+
position: { x: 0, y: 0 },
32+
style: {
33+
backgroundColor: "var(--vscode-dropdown-background)",
34+
borderRadius: "15px",
35+
width: '265px',
36+
boxShadow: '0px 4px 4px rgba(0, 0, 0, 0.25)',
37+
border: 'none',
38+
padding: '10px 10px 3px 10px'
39+
},
40+
};
41+
this.initialNodes.push(node);
42+
if (item.children) {
43+
this.buildNodesArray(item.children);
44+
}
45+
});
46+
// console.log('initialNodes', this.initialNodes);
47+
};
48+
49+
buildEdgesArray(parsedData, parentID) {
50+
if (!parsedData) return;
51+
52+
parsedData.forEach((item) => {
53+
const nodeID = ++this.edgeId;
54+
if (parentID) {
55+
const edge = {
56+
id: `e${parentID}-${nodeID}`,
57+
source: parentID.toString(),
58+
target: nodeID.toString(),
59+
type: 'bezier',
60+
animated: false,
61+
};
62+
this.initialEdges.push(edge);
63+
}
64+
if (item.children) {
65+
this.buildEdgesArray(item.children, nodeID);
66+
}
67+
});
68+
// console.log('initialEdges', this.initialEdges);
69+
}
70+
71+
build(settings) {
72+
const treeParsed = JSON.parse(JSON.stringify(this.parsedData[0]));
73+
console.log('settings: ', settings);
74+
const traverse = (node) => {
75+
let validChildren = [];
76+
77+
for (let i = 0; i < node.children.length; i++) {
78+
if (
79+
node.children[i].thirdParty &&
80+
settings.thirdParty &&
81+
!node.children[i].reactRouter
82+
) {
83+
validChildren.push(node.children[i]);
84+
} else if (node.children[i].reactRouter && settings.reactRouter) {
85+
validChildren.push(node.children[i]);
86+
} else if (
87+
!node.children[i].thirdParty &&
88+
!node.children[i].reactRouter
89+
) {
90+
validChildren.push(node.children[i]);
91+
}
92+
}
93+
94+
// Update children with only valid nodes, and recurse through each node
95+
node.children = validChildren;
96+
node.children.forEach((child) => {
97+
traverse(child);
98+
});
99+
}
100+
traverse(treeParsed);
101+
// Update the vewData state
102+
this.viewData = ([treeParsed]);
103+
console.log('viewData:', this.viewData);
104+
this.buildNodesArray(this.viewData);
105+
this.buildEdgesArray(this.viewData);
106+
}
107+
}
108+
109+
export default FlowBuilder;

src/webview/initial.js

Lines changed: 0 additions & 115 deletions
This file was deleted.

0 commit comments

Comments
 (0)