import React, { Component } from 'react'; import type { ReactNode } from 'react'; import { AlertTriangle, RefreshCw } from 'lucide-react'; import { Button } from './Button'; import i18n from '../../i18n/config'; interface Props { children: ReactNode; fallback?: ReactNode; } interface State { hasError: boolean; error: Error | null; } export class ErrorBoundary extends Component { constructor(props: Props) { super(props); this.state = { hasError: false, error: null }; } static getDerivedStateFromError(error: Error): State { return { hasError: true, error }; } componentDidCatch(error: Error, errorInfo: React.ErrorInfo) { if (process.env.NODE_ENV === 'development') { console.error('Error caught by boundary:', error, errorInfo); console.error('Component stack:', errorInfo.componentStack); console.error('Error message:', error.message); console.error('Error stack:', error.stack); } } handleReset = () => { this.setState({ hasError: false, error: null }); window.location.reload(); }; render() { if (this.state.hasError) { if (this.props.fallback) { return <>{this.props.fallback}; } return (

{i18n.t('errors.somethingWentWrong')}

{this.state.error?.message || i18n.t('errors.tryAgainLater')}

); } return this.props.children; } } // Page-level error boundary with more prominent UI export class PageErrorBoundary extends Component { constructor(props: Props) { super(props); this.state = { hasError: false, error: null }; } static getDerivedStateFromError(error: Error): State { return { hasError: true, error }; } componentDidCatch(error: Error, errorInfo: React.ErrorInfo) { if (process.env.NODE_ENV === 'development') { console.error('Page error:', error, errorInfo); } } handleReset = () => { this.setState({ hasError: false, error: null }); window.location.href = '/'; }; render() { if (this.state.hasError) { return (

{i18n.t('errors.oopsSomethingWentWrong')}

{i18n.t('errors.unexpectedError')}

{import.meta.env.DEV && this.state.error && (
{i18n.t('errors.errorDetails')}
                  {this.state.error.stack}
                
)}
); } return this.props.children; } }