| 113 | }; |
| 114 | |
| 115 | export const FileTreeFolder = ({ |
| 116 | path, |
| 117 | name, |
| 118 | className, |
| 119 | children, |
| 120 | ...props |
| 121 | }: FileTreeFolderProps) => { |
| 122 | const { expandedPaths, togglePath, selectedPath, onSelect } = |
| 123 | useContext(FileTreeContext); |
| 124 | const isExpanded = expandedPaths.has(path); |
| 125 | const isSelected = selectedPath === path; |
| 126 | |
| 127 | const handleOpenChange = useCallback(() => { |
| 128 | togglePath(path); |
| 129 | }, [togglePath, path]); |
| 130 | |
| 131 | const handleSelect = useCallback(() => { |
| 132 | onSelect?.(path); |
| 133 | }, [onSelect, path]); |
| 134 | |
| 135 | const folderContextValue = useMemo( |
| 136 | () => ({ isExpanded, name, path }), |
| 137 | [isExpanded, name, path] |
| 138 | ); |
| 139 | |
| 140 | return ( |
| 141 | <FileTreeFolderContext.Provider value={folderContextValue}> |
| 142 | <Collapsible onOpenChange={handleOpenChange} open={isExpanded}> |
| 143 | <div |
| 144 | className={cn("", className)} |
| 145 | role="treeitem" |
| 146 | tabIndex={0} |
| 147 | {...props} |
| 148 | > |
| 149 | <CollapsibleTrigger asChild> |
| 150 | <button |
| 151 | className={cn( |
| 152 | "flex w-full items-center gap-1 rounded px-2 py-1 text-left transition-colors hover:bg-muted/50", |
| 153 | isSelected && "bg-muted" |
| 154 | )} |
| 155 | onClick={handleSelect} |
| 156 | type="button" |
| 157 | > |
| 158 | <ChevronRightIcon |
| 159 | className={cn( |
| 160 | "size-4 shrink-0 text-muted-foreground transition-transform", |
| 161 | isExpanded && "rotate-90" |
| 162 | )} |
| 163 | /> |
| 164 | <FileTreeIcon> |
| 165 | {isExpanded ? ( |
| 166 | <FolderOpenIcon className="size-4 text-blue-500" /> |
| 167 | ) : ( |
| 168 | <FolderIcon className="size-4 text-blue-500" /> |
| 169 | )} |
| 170 | </FileTreeIcon> |
| 171 | <FileTreeName>{name}</FileTreeName> |
| 172 | </button> |