import React, { useState, useEffect } from 'react'; export function FileEditor({ projectName }) { const [files, setFiles] = useState([]); const [selectedFile, setSelectedFile] = useState(null); const [fileContent, setFileContent] = useState(''); const [expandedDirs, setExpandedDirs] = useState(new Set()); const [loading, setLoading] = useState(false); useEffect(() => { if (projectName) { loadProjectTree(); } }, [projectName]); const loadProjectTree = async () => { try { const response = await fetch(`http://localhost:8002/api/files/tree?project=${projectName}`); const data = await response.json(); setFiles(data.tree || []); } catch (err) { console.error('Error loading file tree:', err); } }; const loadFile = async (filePath) => { setLoading(true); try { const response = await fetch( `http://localhost:8002/api/files/read?project=${projectName}&path=${encodeURIComponent(filePath)}` ); const data = await response.json(); setFileContent(data.content || ''); setSelectedFile(filePath); } catch (err) { console.error('Error loading file:', err); setFileContent(`Error loading file: ${err.message}`); } finally { setLoading(false); } }; const saveFile = async () => { if (!selectedFile) return; try { await fetch(`http://localhost:8002/api/files/write`, { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ project: projectName, path: selectedFile, content: fileContent }) }); alert('File saved!'); } catch (err) { alert('Error saving file: ' + err.message); } }; const toggleDir = (path) => { const newExpanded = new Set(expandedDirs); if (newExpanded.has(path)) { newExpanded.delete(path); } else { newExpanded.add(path); } setExpandedDirs(newExpanded); }; const FileTreeNode = ({ node, depth = 0 }) => (
{ if (node.type === 'dir') { toggleDir(node.path); } else { loadFile(node.path); } }} style={{ padding: '6px 8px', cursor: 'pointer', background: selectedFile === node.path ? 'rgba(59, 130, 246, 0.2)' : 'transparent', borderLeft: selectedFile === node.path ? '2px solid #3b82f6' : 'none', fontSize: '12px', userSelect: 'none', display: 'flex', alignItems: 'center', gap: '6px' }} > {node.type === 'dir' ? ( expandedDirs.has(node.path) ? '▼' : '▶' ) : ( '📄' )} {node.name}
{node.type === 'dir' && expandedDirs.has(node.path) && node.children && (
{node.children.map((child, i) => ( ))}
)}
); return (
{/* File Tree */}

{projectName}

{files.map((node, i) => ( ))}
{/* Editor */}
{selectedFile ? ( <>
{selectedFile}