import React, { useEffect, useRef } from 'react'; import { Check, Copy } from 'lucide-react'; import { logger } from '../utils/secureLogger'; interface CodeHighlightProps { children: string; language?: string; className?: string; } export function CodeHighlight({ children, language = 'text', className = '' }: CodeHighlightProps) { const codeRef = useRef(null); const [copied, setCopied] = React.useState(false); useEffect(() => { if (codeRef.current && typeof window !== 'undefined') { // Simple syntax highlighting for common languages const code = codeRef.current; const text = code.textContent || ''; if (language === 'javascript' || language === 'js') { // Basic JS highlighting - adapted for light theme text contrast code.innerHTML = text .replace(/\b(const|let|var|function|return|if|else|for|while|class|import|export|from|default)\b/g, '$1') .replace(/\b(true|false|null|undefined)\b/g, '$1') .replace(/\b(console|log|error|warn)\b/g, '$1') .replace(/"([^"]*)"/g, '"$1"') .replace(/'([^']*)'/g, '\'$1\'') .replace(/\b(\d+)\b/g, '$1'); } else if (language === 'python' || language === 'py') { // Basic Python highlighting - adapted for light theme text contrast code.innerHTML = text .replace(/\b(def|class|import|from|return|if|else|elif|for|while|try|except|finally|with|as|in|is|None|True|False)\b/g, '$1') .replace(/\b(print|len|str|int|float|list|dict|set)\b/g, '$1') .replace(/"([^"]*)"/g, '"$1"') .replace(/'([^']*)'/g, '\'$1\'') .replace(/\b(\d+)\b/g, '$1'); } else if (language === 'html') { // Basic HTML highlighting code.innerHTML = text .replace(/(<[^&]*>)/g, '$1') .replace(/(<\/[^&]*>)/g, '$1') .replace(/(\w+)=/g, '$1='); } else if (language === 'css') { // Basic CSS highlighting code.innerHTML = text .replace(/([^{}]+)\s*{/g, '$1 {') .replace(/(\w+):/g, '$1:') .replace(/(#[0-9a-fA-F]{3,6})/g, '$1'); } } }, [children, language]); const copyToClipboard = async () => { try { await navigator.clipboard.writeText(children); setCopied(true); setTimeout(() => setCopied(false), 2000); } catch (err) { logger.error('Failed to copy text: ', err); } }; return (
{/* Header */}
{language}
{/* Code */}
        
          {children}
        
      
); }