Skip to content

Commit 891812c

Browse files
Copilothuangyiirene
andcommitted
Extract MAX_TREE_DEPTH constant and fix comment accuracy
Co-authored-by: huangyiirene <7665279+huangyiirene@users.noreply.github.com>
1 parent 088c2d7 commit 891812c

1 file changed

Lines changed: 7 additions & 3 deletions

File tree

packages/designer/src/components/ComponentTree.tsx

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,10 @@ import {
1313
} from 'lucide-react';
1414
import type { SchemaNode } from '@object-ui/core';
1515

16+
// Maximum depth to check when searching for selected descendants
17+
// Prevents stack overflow with extremely deeply nested components
18+
const MAX_TREE_DEPTH = 100;
19+
1620
interface ComponentTreeProps {
1721
className?: string;
1822
}
@@ -42,13 +46,13 @@ const TreeNode: React.FC<TreeNodeProps> = React.memo(({ node, level, isSelected,
4246
}, [node.body]);
4347

4448
// Check if selectedNodeId exists anywhere in this node's subtree
45-
// Uses iterative approach to avoid stack overflow with deeply nested components
49+
// Uses recursive approach with depth limiting to prevent stack overflow
4650
const hasSelectedDescendant = useMemo(() => {
4751
if (!selectedNodeId) return false;
4852

4953
const checkNode = (n: SchemaNode, depth = 0): boolean => {
50-
// Prevent infinite recursion with max depth check (100 levels should be more than enough)
51-
if (depth > 100) return false;
54+
// Prevent stack overflow with max depth check
55+
if (depth > MAX_TREE_DEPTH) return false;
5256

5357
if (n.id === selectedNodeId) return true;
5458
if (!n.body) return false;

0 commit comments

Comments
 (0)