Skip to content

Commit 18b7ca6

Browse files
committed
refactor: merge TreeItem and ChildrenTreeItem together
1 parent ac413f0 commit 18b7ca6

File tree

1 file changed

+20
-35
lines changed

1 file changed

+20
-35
lines changed

libs/react/src/components/context-tree.tsx

Lines changed: 20 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ import { IContextNode, InsertionPointWithElement } from '@mweb/core'
33
import React from 'react'
44
import { useCore } from '../contexts/core-context'
55

6+
// ToDo: move to React context to reuse semantic tree?
7+
68
export const ContextTree: FC<{
79
children: React.FC<{ context: IContextNode; insPoints: InsertionPointWithElement[] }>
810
}> = ({ children }) => {
@@ -13,10 +15,12 @@ export const ContextTree: FC<{
1315
return <TreeItem node={tree} component={children} />
1416
}
1517

16-
const ChildrenTreeItem: FC<{
18+
const TreeItem: FC<{
1719
node: IContextNode
1820
component: React.FC<{ context: IContextNode; insPoints: InsertionPointWithElement[] }>
1921
}> = ({ node, component: Component }) => {
22+
const [wrappedNode, setWrappedNode] = useState({ node })
23+
const [insPoints, setInsPoints] = useState([...node.insPoints])
2024
const [children, setChildren] = useState([...node.children])
2125

2226
// ToDo: refactor it. It stores unique key for each context node
@@ -25,39 +29,6 @@ const ChildrenTreeItem: FC<{
2529
)
2630
const contextKeyCounter = useRef(node.children.length - 1) // last index
2731

28-
useEffect(() => {
29-
const subscriptions = [
30-
node.on('childContextAdded', ({ child }) => {
31-
contextKeyRef.current.set(child, ++contextKeyCounter.current)
32-
setChildren((prev) => [...prev, child])
33-
}),
34-
node.on('childContextRemoved', ({ child }) => {
35-
setChildren((prev) => prev.filter((c) => c !== child))
36-
}),
37-
]
38-
39-
return () => {
40-
subscriptions.forEach((sub) => sub.remove())
41-
}
42-
}, [node])
43-
44-
return children.map((child) => (
45-
<TreeItem
46-
// key={`${child.namespace}/${child.contextType}/${child.id}`}
47-
key={contextKeyRef.current.get(child)}
48-
node={child}
49-
component={Component}
50-
/>
51-
))
52-
}
53-
54-
const TreeItem: FC<{
55-
node: IContextNode
56-
component: React.FC<{ context: IContextNode; insPoints: InsertionPointWithElement[] }>
57-
}> = ({ node, component: Component }) => {
58-
const [wrappedNode, setWrappedNode] = useState({ node })
59-
const [insPoints, setInsPoints] = useState([...node.insPoints])
60-
6132
useEffect(() => {
6233
// The Component re-renders when the current node changes only.
6334
// Changing the children and the parent node will not cause a re-render.
@@ -73,6 +44,13 @@ const TreeItem: FC<{
7344
setWrappedNode({ node })
7445
// ToDo: don't replace whole node
7546
}),
47+
node.on('childContextAdded', ({ child }) => {
48+
contextKeyRef.current.set(child, ++contextKeyCounter.current)
49+
setChildren((prev) => [...prev, child])
50+
}),
51+
node.on('childContextRemoved', ({ child }) => {
52+
setChildren((prev) => prev.filter((c) => c !== child))
53+
}),
7654
]
7755

7856
return () => {
@@ -85,7 +63,14 @@ const TreeItem: FC<{
8563
{wrappedNode.node.element ? (
8664
<Component context={wrappedNode.node} insPoints={insPoints} />
8765
) : null}
88-
<ChildrenTreeItem node={wrappedNode.node} component={Component} />
66+
{children.map((child) => (
67+
<TreeItem
68+
// key={`${child.namespace}/${child.contextType}/${child.id}`}
69+
key={contextKeyRef.current.get(child)}
70+
node={child}
71+
component={Component}
72+
/>
73+
))}
8974
</>
9075
)
9176
}

0 commit comments

Comments
 (0)