import type { HTMLAttributes, ComponentType } from 'react'; import React, { useState, useMemo, useContext } from 'react'; import classNames from 'classnames'; import type { FileIconSlot } from '@teambit/code'; import flatten from 'lodash.flatten'; import type { WidgetProps } from '@teambit/ui-foundation.ui.tree.tree-node'; import { TreeNode as Node } from '@teambit/ui-foundation.ui.tree.tree-node'; import { DrawerUI } from '@teambit/ui-foundation.ui.tree.drawer'; import { FileTree, useFileTreeContext } from '@teambit/ui-foundation.ui.tree.file-tree'; import { FolderTreeNode } from '@teambit/ui-foundation.ui.tree.folder-tree-node'; import type { FileIconMatch } from '@teambit/code.ui.utils.get-file-icon'; import { getFileIcon } from '@teambit/code.ui.utils.get-file-icon'; import type { TreeNode, TreeNodeProps } from '@teambit/design.ui.tree'; import { TreeContext } from '@teambit/base-ui.graph.tree.tree-context'; import styles from './code-compare-tree.module.scss'; export type CodeCompareTreeProps = { currentFile?: string; fileIconSlot?: FileIconSlot; fileTree: string[]; drawerName: string; widgets?: ComponentType>[]; getHref?: (node: TreeNode) => string; onTreeNodeSelected?: (id: string, event?: React.MouseEvent) => void; open?: boolean; } & HTMLAttributes; function CompareFileTreeNode(props: TreeNodeProps) { const { node } = props; const { id } = node; const fileTreeContext = useFileTreeContext(); const { selected, onSelect } = useContext(TreeContext); const href = fileTreeContext?.getHref?.(node); const widgets = fileTreeContext?.widgets; const icon = fileTreeContext?.getIcon?.(node); const isActive = id === selected; if (!node?.children) { return ( onSelect(node.id, e))} /> ); } return ; } function getIcon(fileIconMatchers: FileIconMatch[]) { return function Icon({ id }: TreeNode) { return getFileIcon(fileIconMatchers, id); }; } export function CodeCompareTree({ currentFile, fileIconSlot, className, fileTree, drawerName, widgets, getHref, open, onTreeNodeSelected, }: CodeCompareTreeProps) { const fileIconMatchers: FileIconMatch[] = useMemo(() => flatten(fileIconSlot?.values()), [fileIconSlot]); const defaultDrawer = () => { return ['FILES']; }; const [openDrawerList, onToggleDrawer] = useState(defaultDrawer); const handleDrawerToggle = (id: string) => { const isDrawerOpen = openDrawerList.includes(id); if (isDrawerOpen) { onToggleDrawer((list) => list.filter((drawer) => drawer !== id)); return; } onToggleDrawer((list) => list.concat(id)); }; if (!open) return null; return (
handleDrawerToggle('FILES')} name={drawerName} contentClass={styles.componentCompareCodeDrawerContent} className={classNames(styles.componentCompareCodeTabDrawer)} drawerNameClass={styles.componentCompareDrawerName} >
); }