Skip to content

Commit 2cecae0

Browse files
committed
can now see all root components in display tab
1 parent aff1979 commit 2cecae0

File tree

4 files changed

+81
-19
lines changed

4 files changed

+81
-19
lines changed

app/src/components/StateManagement/DisplayTab/DataTable.tsx

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,19 @@ const StyledTableRow = styled(TableRow)(({ theme }) => ({
3131
}));
3232

3333
export default function DataTable(props) {
34-
const { currComponentState, setCurrComponentState, parentProps, setParentProps, clickedComp } = props;
34+
const { currComponentState, setCurrComponentState, parentProps, setParentProps, clickedComp, data } = props;
35+
const [state, dispatch] = useContext(StateContext);
36+
37+
//determine if the current component is a root component
38+
let isRoot = false;
39+
40+
//iterate through the data array and see if clickedComp is in state.rootComponents
41+
for (let i = 0; i < data.length; i++) {
42+
if (data[i]['name'] === clickedComp) {
43+
//check to see if clickedComp is in rootComponents
44+
if (state.rootComponents.includes(data[i]['id'])) isRoot = true;
45+
}
46+
}
3547

3648
return (
3749
<>
@@ -43,8 +55,8 @@ export default function DataTable(props) {
4355
>
4456

4557
{/* this table will contain passed down stateProps data from parent component */}
46-
{/* we are checking if the clicked component is App-- if it is App, it doesn't have any parents so don't need this table*/}
47-
{(clickedComp !== 'App' &&
58+
{/* we are checking if the clicked component is a root component-- if it is, it doesn't have any parents so don't need this table*/}
59+
{(!isRoot &&
4860
<>
4961
<TableHead>
5062
<TableRow>

app/src/components/StateManagement/DisplayTab/DisplayContainer.tsx

Lines changed: 29 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import React, {useState, useContext} from 'react';
1+
import React, {useState, useContext, useEffect} from 'react';
22
import Tree from './Tree';
33
import Divider from '@mui/material/Divider';
44
import Grid from '@mui/material/Grid';
@@ -12,22 +12,42 @@ function DisplayContainer({data, props}) { // "data" is referring to components
1212
const [currComponentState, setCurrComponentState] = useState([]);
1313
const [parentProps, setParentProps] = useState([]);
1414
const [state, dispatch] = useContext(StateContext);
15+
16+
let root = '';
1517

16-
17-
// check projectType. If it's 'Classic React', title should read 'App'. Otherwise it's Next.js or Gatsby which uses 'index'
18-
let root;
19-
if (state.projectType === "Classic React") {
18+
// check the canvasFocus
19+
// if canvasFocus is a root component, use that root component as "root"
20+
if (state.rootComponents.includes(state.canvasFocus.componentId)) {
21+
//find the name of the canvasFocus component in data
22+
//set root equal to that name
23+
// iterate through data (which is an array of objects)
24+
for (let i = 0; i < data.length; i++) {
25+
// find object with id that is equal to state.canvasFocus.componentId
26+
// then set root equal to the name of that component
27+
if (data[i]["id"] === state.canvasFocus.componentId) root = data[i]["name"];
28+
}
29+
} else if (state.projectType === "Classic React") {
30+
// else default to the main root component (aka app or index depending on react vs next/gatsby)
2031
root = 'App';
21-
} else {
32+
} else {
2233
root = 'index';
23-
}
34+
}
35+
2436
const [clickedComp, setClickedComp] = useState(root);
2537

2638
return (
2739
<div style={{display: 'flex'}}>
28-
{<Tree data = {data} currComponentState={currComponentState} setCurrComponentState ={setCurrComponentState} parentProps={parentProps} setParentProps={setParentProps} setClickedComp={setClickedComp} />}
40+
{<Tree data = {data} currComponentState={currComponentState} setCurrComponentState ={setCurrComponentState} parentProps={parentProps} setParentProps={setParentProps} setClickedComp={setClickedComp}/>}
2941
<Divider orientation="vertical" variant="middle" flexItem />
3042
<Grid item>
43+
<Typography
44+
style={{ color: 'black' }}
45+
variant="subtitle2"
46+
gutterBottom={true}
47+
align="center"
48+
>
49+
Click on a component in the graph to see its state!
50+
</Typography>
3151
<Typography
3252
style={{ color: 'black' }}
3353
variant="h6"
@@ -36,7 +56,7 @@ function DisplayContainer({data, props}) { // "data" is referring to components
3656
>
3757
Total State for {clickedComp}
3858
</Typography>
39-
<DataTable currComponentState={currComponentState} setCurrComponentState ={setCurrComponentState} parentProps={parentProps} setParentProps={setParentProps} props={props} clickedComp={clickedComp} />
59+
<DataTable currComponentState={currComponentState} setCurrComponentState ={setCurrComponentState} parentProps={parentProps} setParentProps={setParentProps} props={props} clickedComp={clickedComp} data = {data} />
4060
</Grid>
4161
</div>
4262
);

app/src/components/StateManagement/DisplayTab/Tree.tsx

Lines changed: 30 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import React, { useRef, useEffect, useContext } from 'react';
1+
import React, { useRef, useEffect, useContext, useState } from 'react';
22
import { select, hierarchy, tree, linkHorizontal} from 'd3';
33
import cloneDeep from 'lodash/cloneDeep';
44
import useResizeObserver from './useResizeObserver';
@@ -10,7 +10,7 @@ function usePrevious(value) {
1010
return ref.current;
1111
}
1212

13-
function Tree({ data, currComponentState, setCurrComponentState, parentProps, setParentProps, setClickedComp }) {
13+
function Tree({ data, currComponentState, setCurrComponentState, parentProps, setParentProps, setClickedComp}) {
1414
const [state, dispatch] = useContext(StateContext);
1515
const svgRef = useRef();
1616
const wrapperRef = useRef();
@@ -41,7 +41,6 @@ function Tree({ data, currComponentState, setCurrComponentState, parentProps, se
4141
// create a deep clone of data to avoid mutating the actual children array in removing separators
4242
const dataDeepClone = cloneDeep(data);
4343

44-
// LEGACYPD to look at when trying to figure out how to convert tab to work for NextJS
4544
if(state.projectType === 'Next.js') {
4645
dataDeepClone.forEach(element => {
4746
element.children = sanitize(element.children).filter(element => !Array.isArray(element));
@@ -57,7 +56,7 @@ function Tree({ data, currComponentState, setCurrComponentState, parentProps, se
5756

5857
// remove separators and update components to current versions
5958
dataDeepClone.forEach(component => removeHTMLElements(component.children));
60-
59+
6160
// will be called initially and on every data change
6261
useEffect(() => {
6362
const svg = select(svgRef.current);
@@ -66,7 +65,31 @@ function Tree({ data, currComponentState, setCurrComponentState, parentProps, se
6665
// (dimensions are null for the first render)
6766
const { width, height } = dimensions || wrapperRef.current.getBoundingClientRect();
6867
// transform hierarchical data
69-
const root = hierarchy(dataDeepClone[0]);
68+
69+
let root;
70+
let rootName;
71+
72+
if (state.rootComponents.includes(state.canvasFocus.componentId)) {
73+
// find out if canvasFocus is a root component
74+
// if yes, set root to be that canvasFocus component
75+
// find that component inside dataDeepClone
76+
for (let i = 0; i < dataDeepClone.length; i++) {
77+
if (dataDeepClone[i]['id'] === state.canvasFocus.componentId) {
78+
// reassign root to be dataDeepClone at that element
79+
80+
root = hierarchy(dataDeepClone[i]);
81+
rootName = dataDeepClone[i]["name"];
82+
83+
}
84+
}
85+
} else {
86+
// if no, set root to be dataDeepClone[0]
87+
root = hierarchy(dataDeepClone[0]);
88+
rootName = dataDeepClone[0]["name"];
89+
}
90+
91+
setClickedComp(rootName);
92+
7093
const treeLayout = tree().size([height, width - 125]);
7194
// Returns a new link generator with horizontal display.
7295
// To visualize links in a tree diagram rooted on the left edge of the display
@@ -76,7 +99,7 @@ function Tree({ data, currComponentState, setCurrComponentState, parentProps, se
7699

77100
// insert our data into the tree layout
78101
treeLayout(root);
79-
102+
80103
svg
81104
.selectAll('.node')
82105
.data(root.descendants())
@@ -99,7 +122,7 @@ function Tree({ data, currComponentState, setCurrComponentState, parentProps, se
99122
let passedInProps;
100123
let componentStateProps;
101124

102-
// iterate through data array to find stateProps for parent and clicked element
125+
// iterate through data array to find stateProps and passedInProps
103126
for(let i = 0; i < data.length; i++) {
104127
if(data[i]["name"] === nameOfClicked) {
105128
componentStateProps = data[i]["stateProps"];

app/src/components/right/ComponentPanel.tsx

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,13 @@ const ComponentPanel = ({isThemeLight}): JSX.Element => {
6464
const checkIfRoot = (inputName: String): boolean => {
6565
let rootDupe = false;
6666
// checks to see if inputted comp name is equal to root component name. Want to prevent that
67+
68+
//carly console logs
69+
const rootComponents = state.rootComponents;
70+
const allComponents = state.components;
71+
console.log({rootComponents});
72+
console.log({allComponents});
73+
6774
if (inputName.toLowerCase() === 'index'|| inputName.toLowerCase() === 'app') {
6875
rootDupe = true;
6976
}

0 commit comments

Comments
 (0)