import { Icon } from '@teambit/evangelist.elements.icon'; import classNames from 'classnames'; import React, { useState, useEffect, useRef } from 'react'; import AnimateHeight from 'react-animate-height'; import type { TreeNodeProps } from '@teambit/design.ui.tree'; import { indentClass, indentStyle, TreeLayer, useTree } from '@teambit/design.ui.tree'; import type { PayloadType } from '../payload-type'; import { getName } from '../utils/get-name'; import styles from './scope-tree-node.module.scss'; export type ScopeTreeNodeProps = {} & TreeNodeProps; export function ScopeTreeNode({ node, depth }: ScopeTreeNodeProps) { const { isCollapsed, activePath } = useTree(); const isActive = activePath?.startsWith(node.id); const initialOpen = isActive || !isCollapsed; const [open, toggle] = useState(initialOpen); const displayName = getName(node.id.replace(/\/$/, '')); const firstRun = useRef(true); useEffect(() => { const { current } = firstRun; if (current) return; if (isActive === true) toggle(true); }, [isActive]); useEffect(() => { const { current } = firstRun; if (current) return; toggle(!isCollapsed); }, [isCollapsed]); useEffect(() => { firstRun.current = false; }, []); const highlighted = !open && isActive; return (
{node.id && (
toggle(!open)} onKeyDown={(e) => { if (e.key === 'Enter') toggle(!open); }} role="button" tabIndex={0} >
{displayName}
)}
{node.children && }
); }