From 2519d4606109d491230d56c43fbe20017ca35964 Mon Sep 17 00:00:00 2001 From: LordMathis Date: Fri, 23 May 2025 23:21:58 +0200 Subject: [PATCH] Refactor Node component to destructure props and improve onNodeClick handling --- app/src/components/files/FileTree.tsx | 45 ++++++++++++++------------- 1 file changed, 24 insertions(+), 21 deletions(-) diff --git a/app/src/components/files/FileTree.tsx b/app/src/components/files/FileTree.tsx index ee02761..0c7e141 100644 --- a/app/src/components/files/FileTree.tsx +++ b/app/src/components/files/FileTree.tsx @@ -41,21 +41,24 @@ const FileIcon = ({ node }: { node: NodeApi }) => { }; // Define a Node component that matches what React-Arborist expects -function Node(props: { +function Node({ + node, + style, + dragHandle, + onNodeClick, + ...rest +}: { node: NodeApi; style: React.CSSProperties; - dragHandle: React.Ref; -}) { - const { node, style, dragHandle } = props; - + dragHandle?: React.Ref; + onNodeClick?: (node: NodeApi) => void; + // Accept any extra props from Arborist, but do not use an index signature +} & Record) { const handleClick = () => { if (node.isInternal) { node.toggle(); - } else { - const treeProps = node.tree.props; - if (typeof treeProps.onNodeClick === 'function') { - treeProps.onNodeClick(node); - } + } else if (typeof onNodeClick === 'function') { + onNodeClick(node); } }; @@ -73,6 +76,7 @@ function Node(props: { overflow: 'hidden', }} onClick={handleClick} + {...rest} > = ({ return true; }); + // Handler for node click + const onNodeClick = (node: NodeApi) => { + const fileNode = node.data; + if (!node.isInternal) { + void handleFileSelect(fileNode.path); + } + }; + return (
= ({ indent={24} rowHeight={28} onActivate={(node) => { - const fileNode = node.data as FileNode; + const fileNode = node.data; if (!node.isInternal) { void handleFileSelect(fileNode.path); } }} - {...({ - // Use a spread with type assertion to add onNodeClick - onNodeClick: (node: NodeApi) => { - const fileNode = node.data; - if (!node.isInternal) { - void handleFileSelect(fileNode.path); - } - }, - } as any)} > - {Node} + {(props) => } )}