| 48 | }; |
| 49 | |
| 50 | export const FileTree = ({ |
| 51 | expanded: controlledExpanded, |
| 52 | defaultExpanded = new Set(), |
| 53 | selectedPath, |
| 54 | onSelect, |
| 55 | onExpandedChange, |
| 56 | className, |
| 57 | children, |
| 58 | ...props |
| 59 | }: FileTreeProps) => { |
| 60 | const [internalExpanded, setInternalExpanded] = useState(defaultExpanded); |
| 61 | const expandedPaths = controlledExpanded ?? internalExpanded; |
| 62 | |
| 63 | const togglePath = useCallback( |
| 64 | (path: string) => { |
| 65 | const newExpanded = new Set(expandedPaths); |
| 66 | if (newExpanded.has(path)) { |
| 67 | newExpanded.delete(path); |
| 68 | } else { |
| 69 | newExpanded.add(path); |
| 70 | } |
| 71 | setInternalExpanded(newExpanded); |
| 72 | onExpandedChange?.(newExpanded); |
| 73 | }, |
| 74 | [expandedPaths, onExpandedChange] |
| 75 | ); |
| 76 | |
| 77 | const contextValue = useMemo( |
| 78 | () => ({ expandedPaths, onSelect, selectedPath, togglePath }), |
| 79 | [expandedPaths, onSelect, selectedPath, togglePath] |
| 80 | ); |
| 81 | |
| 82 | return ( |
| 83 | <FileTreeContext.Provider value={contextValue}> |
| 84 | <div |
| 85 | className={cn( |
| 86 | "rounded-lg border bg-background font-mono text-sm", |
| 87 | className |
| 88 | )} |
| 89 | role="tree" |
| 90 | {...props} |
| 91 | > |
| 92 | <div className="p-2">{children}</div> |
| 93 | </div> |
| 94 | </FileTreeContext.Provider> |
| 95 | ); |
| 96 | }; |
| 97 | |
| 98 | interface FileTreeFolderContextType { |
| 99 | path: string; |