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'; import { useGitStatus } from '@/hooks/useGitStatus'; interface FileEditorProps { filePath: string; content: string; onSave: (content: string) => void; isLoading: boolean; projectId?: string; } export function FileEditor({ filePath, content, onSave, isLoading, projectId }: FileEditorProps) { const [editedContent, setEditedContent] = useState(content); const [isSaving, setIsSaving] = useState(false); const [hasChanges, setHasChanges] = useState(false); const { data: gitStatus } = useGitStatus(projectId || null); 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'; }; // Helper function to get git status for the current file const getFileGitStatus = (filePath: string) => { if (!gitStatus?.changedFiles) return null; const fileChange = gitStatus.changedFiles.find(change => change.filepath === filePath); return fileChange?.status || null; }; // Helper function to get styling classes based on git status const getGitStatusClasses = (status: string | null) => { switch (status) { case 'added': case 'untracked': return 'text-green-600 dark:text-green-400'; case 'modified': case 'staged': return 'text-yellow-600 dark:text-yellow-400'; default: return ''; } }; // Get git status for the current file const currentFileGitStatus = getFileGitStatus(filePath); const gitStatusClasses = getGitStatusClasses(currentFileGitStatus); return (
{filePath}
{hasChanges && ( Unsaved changes )}
Language: {getLanguageFromPath(filePath)}
{isLoading ? (
) : (