/** * 錯誤彈窗元件 * 統一顯示應用程式中的錯誤訊息 */ import React, { useEffect, useState } from 'react'; import './ErrorModal.css'; const ErrorModal = ({ error, onClose, autoClose = true, duration = 5000 }) => { const [isVisible, setIsVisible] = useState(false); const [countdown, setCountdown] = useState(duration / 1000); useEffect(() => { if (error) { setIsVisible(true); setCountdown(duration / 1000); if (autoClose) { const timer = setTimeout(() => { handleClose(); }, duration); const countdownInterval = setInterval(() => { setCountdown((prev) => Math.max(0, prev - 1)); }, 1000); return () => { clearTimeout(timer); clearInterval(countdownInterval); }; } } }, [error, autoClose, duration]); const handleClose = () => { setIsVisible(false); setTimeout(() => { if (onClose) onClose(); }, 300); }; if (!error) return null; const getSeverityClass = () => { if (!error.statusCode) return 'error'; if (error.statusCode >= 500) return 'error'; if (error.statusCode >= 400) return 'warning'; return 'info'; }; const getSeverityIcon = () => { const severity = getSeverityClass(); switch (severity) { case 'error': return '❌'; case 'warning': return '⚠️'; case 'info': return 'ℹ️'; default: return '❌'; } }; return (
{error.message}
{error.statusCode && ({JSON.stringify(error.details, null, 2)}