import { Prism as SyntaxHighlighter } from 'react-syntax-highlighter'; import { oneDark, oneLight } from 'react-syntax-highlighter/dist/esm/styles/prism'; import { useTheme } from '@/hooks/useTheme'; import { cn } from '@/lib/utils'; interface FileContentsProps { content: string; filePath: string; className?: string; showLineNumbers?: boolean; maxHeight?: string; showHeader?: boolean; headerContent?: React.ReactNode; } export function FileContents({ content, filePath, className, showLineNumbers, maxHeight = 'max-h-80', showHeader = true, headerContent }: FileContentsProps) { const { theme } = useTheme(); // Get file extension for syntax highlighting hints const getFileExtension = (path: string) => { const parts = path.split('.'); return parts.length > 1 ? parts[parts.length - 1].toLowerCase() : ''; }; // Map file extensions to syntax highlighter languages const getLanguageFromExtension = (ext: string): string => { const languageMap: Record = { 'ts': 'typescript', 'tsx': 'tsx', 'js': 'javascript', 'jsx': 'jsx', 'json': 'json', 'md': 'markdown', 'css': 'css', 'scss': 'scss', 'sass': 'sass', 'less': 'less', 'html': 'html', 'xml': 'xml', 'svg': 'xml', 'yml': 'yaml', 'yaml': 'yaml', 'toml': 'toml', 'py': 'python', 'rb': 'ruby', 'php': 'php', 'java': 'java', 'c': 'c', 'cpp': 'cpp', 'cs': 'csharp', 'go': 'go', 'rs': 'rust', 'sh': 'bash', 'bash': 'bash', 'zsh': 'bash', 'fish': 'bash', 'sql': 'sql', 'dockerfile': 'dockerfile', 'gitignore': 'gitignore', 'env': 'bash', 'ini': 'ini', 'conf': 'nginx', 'nginx': 'nginx', 'apache': 'apache', 'htaccess': 'apache', 'vim': 'vim', 'lua': 'lua', 'r': 'r', 'matlab': 'matlab', 'swift': 'swift', 'kotlin': 'kotlin', 'dart': 'dart', 'elm': 'elm', 'haskell': 'haskell', 'clojure': 'clojure', 'scala': 'scala', 'perl': 'perl', 'powershell': 'powershell', 'makefile': 'makefile', 'cmake': 'cmake', 'gradle': 'gradle', 'properties': 'properties', 'diff': 'diff', 'patch': 'diff', 'log': 'log' }; return languageMap[ext] || 'text'; }; // Check if content should be syntax highlighted const shouldHighlight = (ext: string, text: string): boolean => { // Don't highlight very large files (over 50KB) for performance if (text.length > 50000) return false; // Don't highlight binary-looking content (check for null bytes and other binary indicators) if (text.includes('\0') || text.includes('\uFFFD')) return false; // Always highlight known code file extensions const codeExtensions = ['ts', 'tsx', 'js', 'jsx', 'json', 'css', 'scss', 'html', 'xml', 'py', 'rb', 'php', 'java', 'c', 'cpp', 'cs', 'go', 'rs', 'sh', 'bash', 'sql', 'yml', 'yaml', 'toml', 'md']; return codeExtensions.includes(ext); }; const fileExtension = getFileExtension(filePath); const shouldUseHighlighting = shouldHighlight(fileExtension, content); // Determine if we should show line numbers const shouldShowLineNumbers = showLineNumbers ?? (content.split('\n').length > 10); return (
{showHeader && (
File Contents {headerContent || (fileExtension && ( {fileExtension.toUpperCase()} ))}
)}
{shouldUseHighlighting ? ( {content} ) : (
              {content}
            
)}
); }