import { useState } from 'react'; import { ChevronRight, ChevronDown, File, Folder, FolderOpen } from 'lucide-react'; import type { FileNode } from '@glmpilot/shared'; import { useEditorStore } from '@/stores/editorStore'; import { useFileStore } from '@/stores/fileStore'; import { getLanguageFromPath } from '@glmpilot/shared'; import { cn } from '@/lib/utils'; const FILE_ICONS: Record = { html: '🌐', css: '🎨', scss: '🎨', javascript: '⚡', typescript: '💠', javascriptreact: '⚛️', typescriptreact: '⚛️', json: '📋', markdown: '📝', svg: '🖼️', }; interface FileTreeItemProps { node: FileNode; depth: number; } function FileTreeItem({ node, depth }: FileTreeItemProps) { const [expanded, setExpanded] = useState(depth < 2); const addFile = useEditorStore((s) => s.addFile); const activeFilePath = useEditorStore((s) => s.activeFilePath); const files = useFileStore((s) => s.files); const handleClick = () => { if (node.type === 'directory') { setExpanded(!expanded); } else { const language = getLanguageFromPath(node.path); const content = files[node.path] || ''; addFile(node.path, content, language); } }; const isActive = node.path === activeFilePath; const lang = node.type === 'file' ? getLanguageFromPath(node.path) : ''; const icon = FILE_ICONS[lang] || ''; return (
{node.type === 'directory' && expanded && node.children && (
{node.children .sort((a, b) => { if (a.type !== b.type) return a.type === 'directory' ? -1 : 1; return a.name.localeCompare(b.name); }) .map((child) => ( ))}
)}
); } export default function FileTree() { const fileTree = useFileStore((s) => s.fileTree); return (
{fileTree .sort((a, b) => { if (a.type !== b.type) return a.type === 'directory' ? -1 : 1; return a.name.localeCompare(b.name); }) .map((node) => ( ))}
); }