import { Card, CardContent, CardHeader } from '@/components/ui/card'; import { Badge } from '@/components/ui/badge'; import { CheckCircle, XCircle, Edit3 } from 'lucide-react'; import { cn } from '@/lib/utils'; import { FileContents } from '@/components/ai/FileContents'; interface FileWriterProps { path: string; fileText: string; result?: string; isError?: boolean; className?: string; } export function FileWriter({ path, fileText, result, isError = false, className }: FileWriterProps) { // Get file extension for syntax highlighting hints const getFileExtension = (filePath: string) => { const parts = filePath.split('.'); return parts.length > 1 ? parts[parts.length - 1].toLowerCase() : ''; }; const getFileIcon = (filePath: string) => { const ext = getFileExtension(filePath); switch (ext) { case 'ts': case 'tsx': return '🔷'; case 'js': case 'jsx': return '🟨'; case 'json': return '📋'; case 'md': return '📝'; case 'css': return '🎨'; case 'html': return '🌐'; case 'svg': return '🖼️'; case 'png': case 'jpg': case 'jpeg': case 'gif': return '🖼️'; case 'yml': case 'yaml': return '⚙️'; case 'toml': return '⚙️'; case 'lock': return '🔒'; default: return '📄'; } }; const fileIcon = getFileIcon(path); const fileExtension = getFileExtension(path); const isNewFile = path.includes('new') || result?.includes('created') || result?.includes('Created'); // Calculate file stats const lineCount = fileText.split('\n').length; const charCount = fileText.length; const wordCount = fileText.split(/\s+/).filter(word => word.length > 0).length; // Preview first few lines for collapsed view const previewLines = fileText.split('\n').slice(0, 5); const hasMoreLines = lineCount > 5; const previewContent = previewLines.join('\n') + (hasMoreLines ? `\n... (${lineCount - 5} more lines)` : ''); return (
{fileIcon}
{path}
{isNewFile ? 'Created new file' : 'Updated file contents'}
{result !== undefined ? ( isError ? ( Failed ) : ( {isNewFile ? 'Created' : 'Updated'} ) ) : ( Writing )}
{/* Error display */} {isError && result && (
{result}
)} {/* File stats */}
{lineCount} line{lineCount !== 1 ? 's' : ''} {wordCount} word{wordCount !== 1 ? 's' : ''} {charCount} character{charCount !== 1 ? 's' : ''} {fileExtension && ( {fileExtension.toUpperCase()} )}
{/* File preview */}
▶ Preview file contents {fileExtension.toUpperCase()} )} />
{/* Success message */} {!isError && result && (
✓ File {isNewFile ? 'created' : 'updated'} successfully
)}
); }