import React, { Component, ErrorInfo, ReactNode } from 'react'; import { AlertTriangle } from 'lucide-react'; import { withTranslation, WithTranslation } from 'react-i18next'; interface Props extends WithTranslation { children: ReactNode; fallback?: ReactNode; componentName?: string; } interface State { hasError: boolean; error: Error | null; errorInfo: ErrorInfo | null; } class ErrorBoundary extends Component { constructor(props: Props) { super(props); this.state = { hasError: false, error: null, errorInfo: null, }; } static getDerivedStateFromError(error: Error): State { return { hasError: true, error, errorInfo: null, }; } componentDidCatch(error: Error, errorInfo: ErrorInfo) { console.error('ErrorBoundary caught an error:', error, errorInfo); this.setState({ error, errorInfo, }); } render() { if (this.state.hasError) { if (this.props.fallback) { return this.props.fallback; } const { t } = this.props; return (

{this.props.componentName ? t('errors.errorInComponent', { componentName: this.props.componentName }) : t('errors.renderError')}

{t('errors.componentError')}

{t('errors.viewTechnicalDetails')}

{t('errors.errorLabel')}

{this.state.error?.toString()}

{this.state.errorInfo && ( <>

{t('errors.stackLabel')}

                        {this.state.errorInfo.componentStack}
                      
)}
); } return this.props.children; } } export default withTranslation()(ErrorBoundary);