Skip to content

Commit 489eb3d

Browse files
committed
2 parents 36ac84b + e6617bf commit 489eb3d

File tree

1 file changed

+16
-8
lines changed

1 file changed

+16
-8
lines changed

app/src/tree/TreeChart.tsx

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
import React, { useRef, useEffect, useContext } from "react";
2-
import { select, hierarchy, tree, linkHorizontal } from "d3";
1+
import React, { useRef, useEffect, useContext } from 'react';
2+
import { select, hierarchy, tree, linkHorizontal } from 'd3';
33
import cloneDeep from 'lodash/cloneDeep';
4-
import useResizeObserver from "./useResizeObserver";
4+
import useResizeObserver from './useResizeObserver';
55
import StateContext from '../context/context';
66

77
function usePrevious(value) {
@@ -12,7 +12,7 @@ function usePrevious(value) {
1212
return ref.current;
1313
}
1414

15-
function TreeChart({ data }) {
15+
function TreeChart({ data }) { // data is components from state - passed in from BottomTabs
1616
const [state, dispatch] = useContext(StateContext);
1717
const canvasId = state.canvasFocus.componentId;
1818

@@ -32,17 +32,25 @@ function TreeChart({ data }) {
3232
// loop over array
3333
for (let i = 0; i < arr.length; i++) {
3434
// if element is separator, remove it
35-
if (arr[i].name === 'separator') arr.splice(i, 1);
35+
if (arr[i].name === 'separator') {
36+
arr.splice(i, 1);
37+
i -= 1;
38+
}
3639
// if element has a children array and that array has length, recursive call
37-
if (arr[i].name === 'div' && arr[i].children.length) removeSeparators(arr[i].children);
40+
else if ((arr[i].name === 'div' || arr[i].type === 'Component') && arr[i].children.length) {
41+
// if element is a component, replace it with deep clone of latest version (to update with new HTML elements)
42+
if (arr[i].type === 'Component') arr[i] = cloneDeep(data.find(component => component.name === arr[i].name));
43+
removeSeparators(arr[i].children);
44+
}
3845
}
3946
// return mutated array
4047
return arr;
4148
};
4249

4350
// create a deep clone of data to avoid mutating the actual children array in removing separators
4451
const dataDeepClone = cloneDeep(data);
45-
dataDeepClone.forEach((component) => {
52+
// remove separators and update components to current versions
53+
dataDeepClone.forEach(component => {
4654
removeSeparators(component.children);
4755
});
4856

@@ -53,7 +61,7 @@ function TreeChart({ data }) {
5361
// but use getBoundingClientRect on initial render
5462
// (dimensions are null for the first render)
5563
const { width, height } =
56-
dimensions || wrapperRef.current.getBoundingClientRect();
64+
dimensions || wrapperRef.current.getBoundingClientRect();
5765
// transform hierarchical data
5866
const root = hierarchy(dataDeepClone[canvasId - 1]); // pass in clone here instead of data
5967
const treeLayout = tree().size([height, width - 125]);

0 commit comments

Comments
 (0)