import React, { useState } from 'react'; import { ShieldAlert, AlertTriangle, Info, Copy, ClipboardCheck, ExternalLink, ChevronDown, ChevronUp } from 'lucide-react'; import { AdvancedErrorInfo } from '../types'; import { diagnoseError } from '../utils/errorDiagnoser'; import { safeCopyToClipboard } from '../utils/clipboard'; interface ErrorDisplayProps { error: AdvancedErrorInfo; onCopySuccess?: () => void; lang?: 'ar' | 'en'; } export function ErrorDisplay({ error, onCopySuccess, lang = 'ar' }: ErrorDisplayProps) { const [expanded, setExpanded] = useState(false); const [copied, setCopied] = useState(false); // Dynamic error diagnostic suggestions! const diagnostics = diagnoseError(error.message, error.statusCode, error.provider); const getSeverityStyle = (severity: string) => { switch (severity) { case 'critical': return { bg: 'bg-red-50/95 border-red-200', text: 'text-red-900', iconColor: 'text-red-600', icon: ShieldAlert, label: lang === 'ar' ? 'خطأ حرج (Critical)' : 'Critical Error' }; case 'warning': return { bg: 'bg-amber-50/95 border-amber-200', text: 'text-amber-900', iconColor: 'text-amber-600', icon: AlertTriangle, label: lang === 'ar' ? 'تنبيه تحذيري (Warning)' : 'Warning Alert' }; default: return { bg: 'bg-blue-50/95 border-blue-200', text: 'text-blue-900', iconColor: 'text-blue-600', icon: Info, label: lang === 'ar' ? 'معلومات عامة (Info)' : 'Diagnostic Info' }; } }; const style = getSeverityStyle(error.severity); const IconComponent = style.icon; const handleCopyDetails = () => { const detailsText = ` --- TELEMETRY DIAGNOSTICS REPORT --- Timestamp: ${error.timestamp} Provider: ${error.provider.toUpperCase()} Status Code: ${error.statusCode || 'N/A'} Error Message: ${error.message} Masked Key: ${error.maskedKey} Severity: ${error.severity.toUpperCase()} Root Cause Suggestion: ${diagnostics.rootCauseEn} Suggested Fix: ${diagnostics.suggestionEn} `.trim(); safeCopyToClipboard(detailsText); setCopied(true); setTimeout(() => setCopied(false), 2000); if (onCopySuccess) onCopySuccess(); }; return (
{lang === 'ar' ? diagnostics.rootCauseAr : diagnostics.rootCauseEn}
{lang === 'ar' ? diagnostics.suggestionAr : diagnostics.suggestionEn}
PROVIDER_ID : {error.provider.toUpperCase()}
MASKED_KEY : {error.maskedKey}
TIMESTAMP : {error.timestamp}
HTTP_STATUS : {error.statusCode || '200 OK (CORS Blocked)'}