import React, { useState, useRef, useEffect } from 'react';
import {
Folder, FileCode, Plus, Settings, Search, Trash2,
ArrowLeft, Save, Play, ChevronRight, X, Check, Globe
} from 'lucide-react';
const THEMES = {
dark: { id: 'dark', name: 'Midnight Dark', bg: 'bg-[#0d1117]', card: 'bg-[#161b22]', text: 'text-slate-300', border: 'border-[#30363d]', accent: 'bg-blue-600', editor: 'bg-[#0d1117]', line: 'text-slate-600' },
light: { id: 'light', name: 'Clean Light', bg: 'bg-slate-50', card: 'bg-white', text: 'text-slate-800', border: 'border-slate-200', accent: 'bg-blue-500', editor: 'bg-white', line: 'text-slate-400' },
hacker: { id: 'hacker', name: 'Matrix Green', bg: 'bg-black', card: 'bg-neutral-900', text: 'text-green-500', border: 'border-green-900', accent: 'bg-green-700', editor: 'bg-black', line: 'text-green-900' }
};
export default function WebIDE() {
const [theme, setTheme] = useState(THEMES.dark);
const [files, setFiles] = useState([]);
const [currentDir, setCurrentDir] = useState(null);
const [activeFile, setActiveFile] = useState(null);
const [view, setView] = useState('home'); // home, editor, preview
const [editorContent, setEditorContent] = useState('');
const [savedContent, setSavedContent] = useState('');
const [searchCode, setSearchCode] = useState('');
const [modal, setModal] = useState(null);
const [input, setInput] = useState('');
const [path, setPath] = useState([{ id: null, name: 'Home' }]);
const textareaRef = useRef(null);
// --- File Logic ---
const currentItems = files.filter(f => f.parentId === currentDir);
const handleCreate = () => {
if (!input) return;
const newItem = {
id: Date.now(),
name: input,
type: modal === 'folder' ? 'folder' : 'file',
parentId: currentDir,
content: modal === 'file' ? '\n
Hello!
' : null
};
setFiles([...files, newItem]);
setModal(null);
setInput('');
if (newItem.type === 'file') openFile(newItem);
};
const openFile = (file) => {
setActiveFile(file);
setEditorContent(file.content);
setSavedContent(file.content);
setView('editor');
};
const deleteItem = (id) => {
if (window.confirm("คุณแน่ใจหรือไม่ที่จะลบรายการนี้?")) {
setFiles(files.filter(f => f.id !== id && f.parentId !== id));
}
};
const saveFile = () => {
setFiles(files.map(f => f.id === activeFile.id ? {...f, content: editorContent} : f));
setSavedContent(editorContent);
};
const handleSearchCode = () => {
const index = editorContent.indexOf(searchCode);
if (index !== -1 && textareaRef.current) {
textareaRef.current.focus();
textareaRef.current.setSelectionRange(index, index + searchCode.length);
} else {
alert("ไม่พบคำที่ค้นหา");
}
};
// --- Render Helpers ---
const isDirty = editorContent !== savedContent;
return (
{/* Header */}
{/* Main Content */}
{view === 'home' && (
Workspace
{currentDir && (
)}
{currentItems.length === 0 ? (
ยังไม่มีข้อมูลในโฟลเดอร์นี้
) : (
{currentItems.map(item => (
{
if (item.type === 'folder') {
setCurrentDir(item.id);
setPath([...path, {id: item.id, name: item.name}]);
} else {
openFile(item);
}
}} className="flex flex-col items-center gap-2">
{item.type === 'folder' ? : }
{item.name}
))}
)}
)}
{view === 'editor' && (
{editorContent.split('\n').map((_, i) =>
{i+1}
)}
)}
{view === 'preview' && (
Simulated Browser: {activeFile?.name}
)}
{/* Modal */}
{modal && (
)}
);
}