Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 | /** * ErrorNotification Component * * Displays error notifications as toast-style messages. * Integrates with the centralized error handling system. */ import { useState, useEffect, useCallback } from 'react'; import { motion, AnimatePresence } from 'framer-motion'; import { X, AlertCircle, AlertTriangle, Bug, WifiOff, ShieldAlert, Clock, ChevronDown, ChevronUp, } from 'lucide-react'; import { getUndismissedErrors, dismissError, dismissAll, type StoredError, } from '../lib/error-handling'; import { ErrorCategory, ErrorSeverity, formatErrorForToast, } from '../lib/error-types'; interface ErrorNotificationProps { /** Maximum number of visible notifications */ maxVisible?: number; /** Position on screen */ position?: 'top-right' | 'top-left' | 'bottom-right' | 'bottom-left'; /** Auto dismiss timeout in ms (0 = no auto dismiss) */ autoDismissMs?: number; /** Additional CSS classes */ className?: string; } const categoryIcons: Record<ErrorCategory, typeof AlertCircle> = { network: WifiOff, auth: ShieldAlert, permission: ShieldAlert, validation: AlertTriangle, config: AlertTriangle, server: Bug, client: AlertCircle, timeout: Clock, system: Bug, }; const severityColors: Record<ErrorSeverity, { bg: string; border: string; text: string; icon: string; }> = { critical: { bg: 'bg-red-50 dark:bg-red-900/20', border: 'border-red-200 dark:border-red-800', text: 'text-red-800 dark:text-red-200', icon: 'text-red-500', }, high: { bg: 'bg-orange-50 dark:bg-orange-900/20', border: 'border-orange-200 dark:border-orange-800', text: 'text-orange-800 dark:text-orange-200', icon: 'text-orange-500', }, medium: { bg: 'bg-yellow-50 dark:bg-yellow-900/20', border: 'border-yellow-200 dark:border-yellow-800', text: 'text-yellow-800 dark:text-yellow-200', icon: 'text-yellow-500', }, low: { bg: 'bg-blue-50 dark:bg-blue-900/20', border: 'border-blue-200 dark:border-blue-800', text: 'text-blue-800 dark:text-blue-200', icon: 'text-blue-500', }, }; function ErrorItem({ error, onDismiss, autoDismissMs, }: { error: StoredError; onDismiss: (id: string) => void; autoDismissMs: number; }) { const [expanded, setExpanded] = useState(false); const Icon = categoryIcons[error.category] || AlertCircle; const colors = severityColors[error.severity] || severityColors.medium; const { title, message } = formatErrorForToast(error); // Auto dismiss useEffect(() => { if (autoDismissMs > 0 && error.severity !== 'critical') { const timer = setTimeout(() => { onDismiss(error.id); }, autoDismissMs); return () => clearTimeout(timer); } }, [autoDismissMs, error.id, error.severity, onDismiss]); const hasDetails = error.stack || error.context; return ( <motion.div initial={{ opacity: 0, x: 300, scale: 0.9 }} animate={{ opacity: 1, x: 0, scale: 1 }} exit={{ opacity: 0, x: 300, scale: 0.9 }} className={` ${colors.bg} ${colors.border} ${colors.text} border rounded-lg shadow-lg p-4 min-w-[320px] max-w-[420px] `} > <div className="flex items-start gap-3"> <Icon className={`w-5 h-5 mt-0.5 flex-shrink-0 ${colors.icon}`} /> <div className="flex-1 min-w-0"> <div className="flex items-start justify-between gap-2"> <h4 className="font-medium text-sm">{title}</h4> <button onClick={() => onDismiss(error.id)} className="p-1 rounded hover:bg-black/10 dark:hover:bg-white/10 flex-shrink-0" > <X className="w-4 h-4" /> </button> </div> <p className="text-sm mt-1 opacity-90">{message}</p> {hasDetails && ( <button onClick={() => setExpanded(!expanded)} className="flex items-center gap-1 text-xs mt-2 opacity-70 hover:opacity-100" > {expanded ? ( <> <ChevronUp className="w-3 h-3" /> 隐藏详情 </> ) : ( <> <ChevronDown className="w-3 h-3" /> 显示详情 </> )} </button> )} {expanded && hasDetails && ( <motion.div initial={{ height: 0, opacity: 0 }} animate={{ height: 'auto', opacity: 1 }} exit={{ height: 0, opacity: 0 }} className="mt-2 p-2 bg-black/5 dark:bg-white/5 rounded text-xs font-mono overflow-auto max-h-32" > {error.context && ( <div className="mb-1"> <span className="opacity-70">Context: </span> {JSON.stringify(error.context, null, 2)} </div> )} {error.stack && ( <pre className="whitespace-pre-wrap opacity-70">{error.stack}</pre> )} </motion.div> )} <div className="flex items-center gap-2 mt-2 text-xs opacity-60"> <span>{error.category}</span> <span>•</span> <span>{new Date(error.timestamp).toLocaleTimeString()}</span> </div> </div> </div> </motion.div> ); } export function ErrorNotification({ maxVisible = 3, position = 'top-right', autoDismissMs = 10000, className = '', }: ErrorNotificationProps) { const [errors, setErrors] = useState<StoredError[]>([]); // Poll for new errors useEffect(() => { const updateErrors = () => { setErrors(getUndismissedErrors().slice(0, maxVisible)); }; updateErrors(); const interval = setInterval(updateErrors, 1000); return () => clearInterval(interval); }, [maxVisible]); const handleDismiss = useCallback((id: string) => { dismissError(id); setErrors(prev => prev.filter(e => e.id !== id)); }, []); const handleDismissAll = useCallback(() => { dismissAll(); setErrors([]); }, []); const positionClasses: Record<string, string> = { 'top-right': 'top-4 right-4', 'top-left': 'top-4 left-4', 'bottom-right': 'bottom-4 right-4', 'bottom-left': 'bottom-4 left-4', }; if (errors.length === 0) return null; return ( <div className={`fixed ${positionClasses[position]} z-50 flex flex-col gap-2 ${className}`} > <AnimatePresence mode="popLayout"> {errors.map(error => ( <ErrorItem key={error.id} error={error} onDismiss={handleDismiss} autoDismissMs={autoDismissMs} /> ))} </AnimatePresence> {errors.length > 1 && ( <motion.button initial={{ opacity: 0 }} animate={{ opacity: 1 }} onClick={handleDismissAll} className="text-xs text-gray-500 dark:text-gray-400 hover:text-gray-700 dark:hover:text-gray-200 text-center py-1" > 清除全部 ({errors.length}) </motion.button> )} </div> ); } /** * ErrorNotificationProvider - Include at app root */ export function ErrorNotificationProvider({ children, }: { children: React.ReactNode; }) { return ( <> {children} <ErrorNotification /> </> ); } export default ErrorNotification; |