Files
BeyondCXAnalytics-Demo/frontend/components/ErrorBoundary.tsx
Claude 9bc1a1c0d3 refactor: implement i18n in core components (phase 1)
Refactored key components to use react-i18next translations:
- ErrorBoundary: error messages and labels
- ProgressStepper: step labels
- DimensionCard: health status labels, descriptions, benchmark text, action buttons
- DashboardTabs: back button, footer, default title

Added translation keys to es.json and en.json:
- dashboard.defaultTitle
- All health status and benchmark keys already existed

Build verified successfully. Remaining 31 components to be refactored in subsequent commits.

https://claude.ai/code/session_4f888c33-8937-4db8-8a9d-ddc9ac51a725
2026-02-06 18:37:40 +00:00

98 lines
3.0 KiB
TypeScript

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<Props, State> {
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 (
<div className="bg-amber-50 border-2 border-amber-200 rounded-lg p-6">
<div className="flex items-start gap-3">
<AlertTriangle className="text-amber-600 flex-shrink-0 mt-1" size={24} />
<div className="flex-1">
<h3 className="text-lg font-semibold text-amber-900 mb-2">
{this.props.componentName
? t('errors.errorInComponent', { componentName: this.props.componentName })
: t('errors.renderError')}
</h3>
<p className="text-amber-800 mb-3">
{t('errors.componentError')}
</p>
<details className="text-sm">
<summary className="cursor-pointer text-amber-700 font-medium mb-2">
{t('errors.viewTechnicalDetails')}
</summary>
<div className="bg-white rounded p-3 mt-2 font-mono text-xs overflow-auto max-h-40">
<p className="text-red-600 font-semibold mb-1">{t('errors.errorLabel')}</p>
<p className="text-slate-700 mb-3">{this.state.error?.toString()}</p>
{this.state.errorInfo && (
<>
<p className="text-red-600 font-semibold mb-1">{t('errors.stackLabel')}</p>
<pre className="text-slate-600 whitespace-pre-wrap">
{this.state.errorInfo.componentStack}
</pre>
</>
)}
</div>
</details>
<button
onClick={() => window.location.reload()}
className="mt-4 px-4 py-2 bg-amber-600 text-white rounded hover:bg-amber-700 transition-colors"
>
{t('dashboard.reloadPage')}
</button>
</div>
</div>
</div>
);
}
return this.props.children;
}
}
export default withTranslation()(ErrorBoundary);