|
| 1 | +import React, { useState } from 'react'; |
| 2 | +import { FolderOpenOutlined, FolderOutlined, FileOutlined, FileTextOutlined } from '@ant-design/icons'; |
| 3 | +import clsx from 'clsx'; |
| 4 | + |
| 5 | +interface FileTreeProps { |
| 6 | + children: React.ReactNode; |
| 7 | + className?: string; |
| 8 | +} |
| 9 | + |
| 10 | +interface ItemProps { |
| 11 | + name: string; |
| 12 | + comment?: string; |
| 13 | + children?: React.ReactNode; |
| 14 | + defaultOpen?: boolean; |
| 15 | +} |
| 16 | + |
| 17 | +const FileTreeContext = React.createContext<{ level: number }>({ level: 0 }); |
| 18 | + |
| 19 | +export function FileTree({ children, className }: FileTreeProps) { |
| 20 | + return ( |
| 21 | + <div className={clsx("border border-border rounded-lg bg-surface-1/30 p-4 font-mono text-sm overflow-x-auto", className)}> |
| 22 | + <FileTreeContext.Provider value={{ level: 0 }}> |
| 23 | + <div className="flex flex-col gap-1"> |
| 24 | + {children} |
| 25 | + </div> |
| 26 | + </FileTreeContext.Provider> |
| 27 | + </div> |
| 28 | + ); |
| 29 | +} |
| 30 | + |
| 31 | +function Folder({ name, comment, children, defaultOpen = true }: ItemProps) { |
| 32 | + const { level } = React.useContext(FileTreeContext); |
| 33 | + const [isOpen, setIsOpen] = useState(defaultOpen); |
| 34 | + |
| 35 | + return ( |
| 36 | + <div className="flex flex-col"> |
| 37 | + <div |
| 38 | + className="flex items-center gap-2 py-1 hover:bg-surface-2/50 rounded cursor-pointer select-none" |
| 39 | + style={{ paddingLeft: `${level * 1.5}rem` }} |
| 40 | + onClick={() => setIsOpen(!isOpen)} |
| 41 | + > |
| 42 | + <span className="text-text-secondary"> |
| 43 | + {isOpen ? <FolderOpenOutlined /> : <FolderOutlined />} |
| 44 | + </span> |
| 45 | + <span className="font-medium text-text-primary">{name}</span> |
| 46 | + {comment && <span className="text-text-muted text-xs ml-2">// {comment}</span>} |
| 47 | + </div> |
| 48 | + {isOpen && children && ( |
| 49 | + <FileTreeContext.Provider value={{ level: level + 1 }}> |
| 50 | + <div className="flex flex-col gap-1 border-l border-border/30 ml-[calc(0.5rem+3px)]"> |
| 51 | + {/* Adjust margin to align with the folder icon center roughly, or just use padding */} |
| 52 | + {/* Actually, the level padding in children handles indentation. |
| 53 | + But to draw lines, we might need more complex CSS. |
| 54 | + For now, let's stick to simple indentation. */} |
| 55 | + {children} |
| 56 | + </div> |
| 57 | + </FileTreeContext.Provider> |
| 58 | + )} |
| 59 | + </div> |
| 60 | + ); |
| 61 | +} |
| 62 | + |
| 63 | +function File({ name, comment }: ItemProps) { |
| 64 | + const { level } = React.useContext(FileTreeContext); |
| 65 | + |
| 66 | + return ( |
| 67 | + <div |
| 68 | + className="flex items-center gap-2 py-1 hover:bg-surface-2/50 rounded select-text" |
| 69 | + style={{ paddingLeft: `${level * 1.5}rem` }} |
| 70 | + > |
| 71 | + <span className="text-text-secondary"> |
| 72 | + <FileTextOutlined /> |
| 73 | + </span> |
| 74 | + <span className="text-text-primary">{name}</span> |
| 75 | + {comment && <span className="text-text-muted text-xs ml-2">// {comment}</span>} |
| 76 | + </div> |
| 77 | + ); |
| 78 | +} |
| 79 | + |
| 80 | +FileTree.Folder = Folder; |
| 81 | +FileTree.File = File; |
| 82 | + |
| 83 | +export default FileTree; |
0 commit comments