-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathComponentTree.tsx
More file actions
206 lines (180 loc) · 7.44 KB
/
ComponentTree.tsx
File metadata and controls
206 lines (180 loc) · 7.44 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
import React, { useCallback, useMemo, useState } from 'react';
import { useDesigner } from '../context/DesignerContext';
import { ScrollArea } from '@object-ui/components';
import { Button } from '@object-ui/components';
import { cn } from '@object-ui/components';
import {
ChevronRight,
ChevronDown,
Layers,
Eye,
EyeOff,
GripVertical
} from 'lucide-react';
import type { SchemaNode } from '@object-ui/core';
// Maximum depth to check when searching for selected descendants
// Prevents stack overflow with extremely deeply nested components
const MAX_TREE_DEPTH = 100;
interface ComponentTreeProps {
className?: string;
}
interface TreeNodeProps {
node: SchemaNode;
level: number;
isSelected: boolean;
selectedNodeId: string | null;
onSelect: (id: string) => void;
}
const TreeNode: React.FC<TreeNodeProps> = React.memo(({ node, level, isSelected, selectedNodeId, onSelect }) => {
const [isExpanded, setIsExpanded] = useState(true);
const [isVisible, setIsVisible] = useState(true);
const hasChildren = useMemo(() => {
if (!node.body) return false;
if (Array.isArray(node.body)) return node.body.length > 0;
return typeof node.body === 'object';
}, [node.body]);
const children = useMemo(() => {
if (!node.body) return [];
if (Array.isArray(node.body)) return node.body;
return [node.body as SchemaNode];
}, [node.body]);
// Check if selectedNodeId exists anywhere in this node's subtree
// Uses recursive approach with depth limiting to prevent stack overflow
const hasSelectedDescendant = useMemo(() => {
if (!selectedNodeId) return false;
const checkNode = (n: SchemaNode, depth = 0): boolean => {
// Prevent stack overflow with max depth check
if (depth > MAX_TREE_DEPTH) return false;
if (n.id === selectedNodeId) return true;
if (!n.body) return false;
const childNodes = Array.isArray(n.body) ? n.body : [n.body as SchemaNode];
return childNodes.some(child => checkNode(child, depth + 1));
};
return children.some(child => checkNode(child));
}, [children, selectedNodeId]);
const handleClick = useCallback(() => {
onSelect(node.id || '');
}, [node.id, onSelect]);
const toggleExpand = useCallback((e: React.MouseEvent) => {
e.stopPropagation();
setIsExpanded(prev => !prev);
}, []);
const toggleVisibility = useCallback((e: React.MouseEvent) => {
e.stopPropagation();
setIsVisible(prev => !prev);
}, []);
return (
<div>
<div
onClick={handleClick}
className={cn(
"flex items-center gap-1 py-1.5 px-2 hover:bg-blue-50 cursor-pointer transition-colors group text-sm border-l-2",
isSelected
? "bg-blue-100 border-blue-600 text-blue-900"
: hasSelectedDescendant
? "border-blue-300 text-gray-700"
: "border-transparent text-gray-700"
)}
style={{ paddingLeft: `${level * 16 + 8}px` }}
>
{/* Expand/Collapse Button */}
<button
onClick={toggleExpand}
className={cn(
"w-4 h-4 flex items-center justify-center text-gray-400 hover:text-gray-700 transition-transform",
!hasChildren && "opacity-0 pointer-events-none"
)}
>
{isExpanded ? (
<ChevronDown size={14} />
) : (
<ChevronRight size={14} />
)}
</button>
{/* Drag Handle */}
<div className="opacity-0 group-hover:opacity-100 transition-opacity">
<GripVertical size={12} className="text-gray-400" />
</div>
{/* Node Type/Label */}
<span className="flex-1 truncate font-medium">
{node.type}
</span>
{/* Node ID Badge */}
{node.id && (
<span className="text-[9px] text-gray-400 font-mono bg-gray-100 px-1.5 py-0.5 rounded opacity-0 group-hover:opacity-100 transition-opacity">
{node.id.split('-')[0]}
</span>
)}
{/* Visibility Toggle */}
<button
onClick={toggleVisibility}
className="opacity-0 group-hover:opacity-100 transition-opacity text-gray-400 hover:text-gray-700"
title={isVisible ? "Hide component" : "Show component"}
>
{isVisible ? <Eye size={14} /> : <EyeOff size={14} />}
</button>
</div>
{/* Children */}
{hasChildren && isExpanded && (
<div className={cn(!isVisible && "opacity-50")}>
{children.map((child, index) => (
<TreeNode
key={child.id || `child-${index}`}
node={child}
level={level + 1}
isSelected={child.id === selectedNodeId}
selectedNodeId={selectedNodeId}
onSelect={onSelect}
/>
))}
</div>
)}
</div>
);
});
TreeNode.displayName = 'TreeNode';
export const ComponentTree: React.FC<ComponentTreeProps> = React.memo(({ className }) => {
const { schema, selectedNodeId, setSelectedNodeId } = useDesigner();
const handleSelect = useCallback((id: string) => {
setSelectedNodeId(id);
}, [setSelectedNodeId]);
return (
<div className={cn("flex flex-col h-full bg-white", className)}>
<ScrollArea className="flex-1">
<div className="p-2">
{schema && (
<TreeNode
node={schema}
level={0}
isSelected={selectedNodeId === schema.id}
selectedNodeId={selectedNodeId}
onSelect={handleSelect}
/>
)}
</div>
</ScrollArea>
{/* Tree Actions */}
<div className="px-4 py-2 border-t shrink-0 bg-gray-50">
<div className="flex gap-1">
<Button
variant="ghost"
size="sm"
className="h-7 text-xs flex-1"
title="Expand all nodes"
>
Expand All
</Button>
<Button
variant="ghost"
size="sm"
className="h-7 text-xs flex-1"
title="Collapse all nodes"
>
Collapse All
</Button>
</div>
</div>
</div>
);
});
ComponentTree.displayName = 'ComponentTree';