Awakened_RiShre / src /components /CodeWorkspace.tsx
usrrootkali's picture
Upload 34 files
ffe328b verified
Raw History Blame Contribute Delete
8.33 kB
import React, { useState } from 'react';
import { Project } from '../types';
import { downloadSingleFile } from '../utils/exportZip';
import {
FileCode,
FileText,
Plus,
Trash2,
Download,
Copy,
Check,
FolderTree,
} from 'lucide-react';
interface CodeWorkspaceProps {
project: Project;
onUpdateFileContent: (fileId: string, newContent: string) => void;
onAddFile: (name: string, content?: string) => void;
onDeleteFile: (fileId: string) => void;
}
export const CodeWorkspace: React.FC<CodeWorkspaceProps> = ({
project,
onUpdateFileContent,
onAddFile,
onDeleteFile,
}) => {
const [activeFileId, setActiveFileId] = useState<string>(
project.activeFileId || project.files[0]?.id || ''
);
const [copied, setCopied] = useState(false);
const [newFileName, setNewFileName] = useState('');
const [isAddingFile, setIsAddingFile] = useState(false);
const activeFile = project.files.find((f) => f.id === activeFileId) || project.files[0];
const handleCopy = () => {
if (!activeFile) return;
navigator.clipboard.writeText(activeFile.content);
setCopied(true);
setTimeout(() => setCopied(false), 2000);
};
const handleCreateFile = (e: React.FormEvent) => {
e.preventDefault();
if (!newFileName.trim()) return;
onAddFile(newFileName.trim(), '// New file created in RiShre\n');
setNewFileName('');
setIsAddingFile(false);
};
const lineCount = activeFile?.content ? activeFile.content.split('\n').length : 0;
const lineNumbers = Array.from({ length: Math.max(lineCount, 1) }, (_, i) => i + 1);
return (
<div className="flex h-full bg-[#070312] overflow-hidden select-none">
{/* File Explorer Sidebar */}
<div className="w-56 bg-[#0a0517] border-r border-purple-500/20 flex flex-col shrink-0">
<div className="h-10 px-3 border-b border-purple-500/20 flex items-center justify-between text-xs text-purple-200 font-bold">
<div className="flex items-center gap-1.5 font-mono">
<FolderTree className="w-3.5 h-3.5 text-fuchsia-400" />
<span>PROJECT FILES</span>
</div>
<button
onClick={() => setIsAddingFile(true)}
title="Create New File"
className="p-1 rounded-lg hover:bg-[#180a33] text-purple-300 hover:text-white transition-colors cursor-pointer"
>
<Plus className="w-3.5 h-3.5" />
</button>
</div>
{/* Add File Inline Input */}
{isAddingFile && (
<form onSubmit={handleCreateFile} className="p-2 border-b border-purple-500/20 bg-[#120826]/70">
<input
type="text"
autoFocus
placeholder="e.g. script.js"
value={newFileName}
onChange={(e) => setNewFileName(e.target.value)}
className="w-full bg-[#080314] border border-purple-500/50 rounded-lg px-2.5 py-1 text-xs text-white focus:outline-none"
/>
<div className="flex justify-end gap-1 mt-1.5">
<button
type="button"
onClick={() => setIsAddingFile(false)}
className="px-2 py-0.5 rounded text-[10px] text-purple-300/70 hover:text-white"
>
Cancel
</button>
<button
type="submit"
className="px-2.5 py-0.5 rounded text-[10px] bg-gradient-to-r from-purple-600 to-fuchsia-600 text-white font-bold"
>
Create
</button>
</div>
</form>
)}
{/* File Tree List */}
<div className="flex-1 overflow-y-auto p-2 space-y-0.5">
{project.files.map((file) => {
const isActive = file.id === activeFile?.id;
return (
<div
key={file.id}
onClick={() => setActiveFileId(file.id)}
className={`group flex items-center justify-between px-2.5 py-1.5 rounded-xl text-xs font-mono transition-colors cursor-pointer ${
isActive
? 'bg-purple-600/20 text-purple-100 font-bold border border-purple-500/40 shadow-[0_0_12px_rgba(168,85,247,0.2)]'
: 'text-purple-300/70 hover:text-purple-100 hover:bg-[#130829]'
}`}
>
<div className="flex items-center gap-2 truncate">
<FileCode className={`w-3.5 h-3.5 shrink-0 ${isActive ? 'text-fuchsia-400' : 'text-purple-400/60'}`} />
<span className="truncate">{file.name}</span>
</div>
{project.files.length > 1 && (
<button
onClick={(e) => {
e.stopPropagation();
if (confirm(`Delete "${file.name}"?`)) {
onDeleteFile(file.id);
}
}}
title="Delete File"
className="opacity-0 group-hover:opacity-100 p-1 hover:text-rose-400 transition-opacity"
>
<Trash2 className="w-3 h-3" />
</button>
)}
</div>
);
})}
</div>
</div>
{/* Code Editor Area */}
<div className="flex-1 flex flex-col bg-[#05020c] overflow-hidden">
{activeFile ? (
<>
{/* Tab & Editor Actions Header */}
<div className="h-10 bg-[#0a0517] border-b border-purple-500/20 px-4 flex items-center justify-between shrink-0">
<div className="flex items-center gap-2.5">
<FileText className="w-3.5 h-3.5 text-fuchsia-400" />
<span className="text-xs font-mono font-bold text-white">{activeFile.name}</span>
<span className="text-[10px] font-mono px-2 py-0.5 rounded-full bg-purple-950/80 text-purple-300 border border-purple-500/30 uppercase">
{activeFile.language}
</span>
<span className="text-[11px] font-mono text-purple-400/60">{lineCount} lines</span>
</div>
<div className="flex items-center gap-1.5">
<button
onClick={handleCopy}
className="flex items-center gap-1 px-2.5 py-1 rounded-lg bg-[#14082b] hover:bg-[#1e0c3d] border border-purple-500/30 text-xs text-purple-200 transition-colors cursor-pointer"
>
{copied ? <Check className="w-3 h-3 text-emerald-400" /> : <Copy className="w-3 h-3 text-purple-300" />}
<span>{copied ? 'Copied' : 'Copy'}</span>
</button>
<button
onClick={() => downloadSingleFile(activeFile.name, activeFile.content)}
className="flex items-center gap-1 px-2.5 py-1 rounded-lg bg-[#14082b] hover:bg-[#1e0c3d] border border-purple-500/30 text-xs text-purple-200 transition-colors cursor-pointer"
>
<Download className="w-3 h-3 text-purple-300" />
<span>Download</span>
</button>
</div>
</div>
{/* Editor Body with Gutter */}
<div className="flex-1 flex overflow-hidden font-mono text-xs leading-relaxed">
<div className="w-12 py-3 bg-[#080314] text-purple-400/40 text-right pr-3 select-none border-r border-purple-500/15 overflow-hidden font-mono shrink-0">
{lineNumbers.map((num) => (
<div key={num} className="h-5">
{num}
</div>
))}
</div>
<textarea
value={activeFile.content}
onChange={(e) => onUpdateFileContent(activeFile.id, e.target.value)}
spellCheck={false}
className="flex-1 p-3 bg-transparent text-purple-100 resize-none focus:outline-none overflow-auto font-mono whitespace-pre leading-5"
style={{ tabSize: 2 }}
/>
</div>
</>
) : (
<div className="flex-1 flex items-center justify-center text-purple-400/40 font-mono text-xs">
No active file selected.
</div>
)}
</div>
</div>
);
};