/* eslint-disable */ // @ts-nocheck import './TableTree.scss'; import React, { ReactNode, useLayoutEffect, useRef, useState } from 'react'; import { prepareData } from '../../utils/TableCell/TableCell'; import Icon from '../Icon'; import { checkEmpty } from '../../utils/checkEmpty/checkEmpty'; interface ColumnDataProps { name: string; accessor: string; width: string; isClickable?: boolean; minWidth?: string; cell?: (e: any) => JSX.Element | string | ReactNode; } interface ObjectProps { [key: string]: any; } interface TableTreeProps { withCheckBox: boolean; columnsData: Array; treeData: Array; onClick?: ( event: React.MouseEvent, data: any ) => void; } const TableTree = ({ columnsData, treeData, onClick = () => {}, }: TableTreeProps) => { const [expandedNodes, setExpandedNodes] = useState>( new Set() ); useLayoutEffect(() => { const defaultExpanded: Set = new Set(); // Recursive function to add nodes and their children to the expanded set const expandNodeRecursively = (node: ObjectProps) => { if (node.expanded) { // Add the node to the expanded set defaultExpanded.add(node); // If the node has children, recursively expand them as well if (node.children) { node.children.forEach((child: ObjectProps) => expandNodeRecursively(child) ); } } }; // Iterate over the treeData to check which nodes should be expanded treeData.forEach((node) => { expandNodeRecursively(node); }); // Set the expanded nodes state setExpandedNodes(defaultExpanded); }, [treeData]); // Function to calculate total children height const calculateTotalChildrenHeight = (node: any): number => { if (!node.children || node.children.length === 0) { return 1; } // Start with 1 for the current node and node itself is included in the height calculation before considering its children. let totalHeight = 1; if (expandedNodes.has(node)) { node.children.forEach((child: any) => { totalHeight += calculateTotalChildrenHeight(child); }); } return totalHeight; }; const TreeNode = ({ node, level, isLast }: any) => { const nodeRef = useRef(null); const [nodeHeight, setNodeHeight] = useState(0); const [totalChildrenHeight, setTotalChildrenHeight] = useState(0); const isExpanded = expandedNodes.has(node); useLayoutEffect(() => { if (nodeRef.current) { const observer = new ResizeObserver(() => { // Update nodeHeight when the size of the element changes const currentHeight = nodeRef.current?.offsetHeight || 0; setNodeHeight(currentHeight); // Calculate total children height const childrenHeight = calculateTotalChildrenHeight(node); setTotalChildrenHeight(childrenHeight); }); // Start observing the current node observer.observe(nodeRef.current); return () => { observer.disconnect(); }; } }, [isExpanded, node]); const handleToggleExpand = () => { setExpandedNodes((prev) => { const newExpandedNodes = new Set(prev); if (newExpandedNodes.has(node)) { newExpandedNodes.delete(node); // Collapse the node } else { newExpandedNodes.add(node); // Expand the node } return newExpandedNodes; }); }; const renderRowData = (columnsData: any) => { return columnsData.map((column: any) => { if (column.accessor === 'title') { return ( {node.folder && ( )}
onClick(event, node)} > {prepareData(node, column)}
); } else if (column.accessor) { return ( {prepareData(node, column)} ); } }); }; return ( <> {renderRowData(columnsData)} {/* Render children only if the node is expanded */} {isExpanded && !checkEmpty(node?.children) && renderTree(node.children, level + 1)} ); }; const renderTree = (nodes: any, level = 0) => { return nodes.map((node: any, index: number) => { const isLast = index === nodes.length - 1; return ; }); }; return (
{columnsData.map((column: any, index) => ( ))} {renderTree(treeData)}
{column.name}
); }; export default TableTree;