import { useState, useEffect } from 'react'; import { Button } from '@/components/ui/button'; import { Textarea } from '@/components/ui/textarea'; import { CardContent, CardHeader, CardTitle } from '@/components/ui/card'; import { Save, Loader2 } from 'lucide-react'; import { cn } from '@/lib/utils'; interface FileEditorProps { filePath: string; content: string; onSave: (content: string) => void; isLoading: boolean; } export function FileEditor({ filePath, content, onSave, isLoading }: FileEditorProps) { const [editedContent, setEditedContent] = useState(content); const [isSaving, setIsSaving] = useState(false); const [hasChanges, setHasChanges] = useState(false); useEffect(() => { setEditedContent(content); setHasChanges(false); }, [content, filePath]); useEffect(() => { setHasChanges(editedContent !== content); }, [editedContent, content]); const handleSave = async () => { setIsSaving(true); try { await onSave(editedContent); setHasChanges(false); } catch (error) { console.error('Failed to save file:', error); } finally { setIsSaving(false); } }; const getLanguageFromPath = (path: string): string => { const extension = path.split('.').pop()?.toLowerCase(); const languageMap: Record = { js: 'javascript', jsx: 'javascript', ts: 'typescript', tsx: 'typescript', html: 'html', css: 'css', json: 'json', md: 'markdown', py: 'python', java: 'java', cpp: 'cpp', c: 'c', sh: 'bash', yml: 'yaml', yaml: 'yaml', }; return languageMap[extension || ''] || 'text'; }; const getMonospaceFont = () => { return 'font-mono text-sm'; }; return (
{filePath}
{hasChanges && ( Unsaved changes )}
Language: {getLanguageFromPath(filePath)}
{isLoading ? (
) : (