import { Card, CardContent, CardHeader } from '@/components/ui/card'; import { Badge } from '@/components/ui/badge'; import { CheckCircle, XCircle, Eye } from 'lucide-react'; import { cn } from '@/lib/utils'; import { FileContents } from '@/components/ai/FileContents'; interface FileViewerProps { path: string; result?: string; isError?: boolean; className?: string; } export function FileViewer({ path, result, isError = false, className }: FileViewerProps) { // Determine if it's a directory or file based on the result const isDirectory = result?.includes('├──') || result?.includes('└──') || result?.includes('│'); // 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, isDir: boolean) => { if (isDir) return '📁'; 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, isDirectory || false); const fileExtension = getFileExtension(path); // Parse directory structure for better display const parseDirectoryStructure = (content: string) => { if (!isDirectory) return content; const lines = content.split('\n').filter(line => line.trim()); return lines.map((line) => { // Extract the file/folder name from tree structure const match = line.match(/[├└]──\s*(.+)$/); if (match) { const name = match[1]; const isFolder = !name.includes('.'); const icon = isFolder ? '📁' : getFileIcon(name, false); return { line, name, icon, isFolder }; } return { line, name: line.trim(), icon: '📄', isFolder: false }; }); }; const directoryItems = isDirectory ? parseDirectoryStructure(result || '') : []; const directoryItemsArray = Array.isArray(directoryItems) ? directoryItems : []; return (
{fileIcon}
{path}
{isDirectory ? 'Directory listing' : `${fileExtension ? fileExtension.toUpperCase() + ' file' : 'File'} contents`}
{result ? ( isError ? ( Error ) : ( Viewed ) ) : ( Loading )}
{result && ( {isError ? (
{result}
) : isDirectory ? (
Directory Structure
{directoryItemsArray.map((item, index) => (
{item.line.replace(/[├└]──\s*(.+)$/, '')} {item.icon} {item.name}
))}
) : ( {fileExtension.toUpperCase()} )} /> )} {/* File stats */} {!isError && result && (
{isDirectory ? ( {directoryItemsArray.length} item{directoryItemsArray.length !== 1 ? 's' : ''} ) : ( <> {result.split('\n').length} line{result.split('\n').length !== 1 ? 's' : ''} {result.length} character{result.length !== 1 ? 's' : ''} )}
)}
)}
); }