52 lines
1.5 KiB
TypeScript
52 lines
1.5 KiB
TypeScript
import React, { Component, type ReactNode } from 'react';
|
|
import { browserLog } from '@/utils/browserLog';
|
|
|
|
interface Props {
|
|
children: ReactNode;
|
|
fallback?: ReactNode;
|
|
}
|
|
|
|
interface State {
|
|
hasError: boolean;
|
|
error?: Error;
|
|
}
|
|
|
|
export class ErrorBoundary extends Component<Props, State> {
|
|
constructor(props: Props) {
|
|
super(props);
|
|
this.state = { hasError: false };
|
|
}
|
|
|
|
static getDerivedStateFromError(error: Error): State {
|
|
return { hasError: true, error };
|
|
}
|
|
|
|
override componentDidCatch(error: Error, errorInfo: React.ErrorInfo) {
|
|
browserLog.error('ErrorBoundary caught an error:', error, errorInfo);
|
|
}
|
|
|
|
override render() {
|
|
if (this.state.hasError) {
|
|
return (
|
|
this.props.fallback || (
|
|
<div className="flex items-center justify-center h-full bg-background">
|
|
<div className="text-center p-8">
|
|
<h2 className="text-lg font-semibold text-text-primary mb-2">Something went wrong</h2>
|
|
<p className="text-sm text-text-secondary mb-4">
|
|
{this.state.error?.message || 'An unexpected error occurred'}
|
|
</p>
|
|
<button
|
|
onClick={() => window.location.reload()}
|
|
className="px-4 py-2 bg-primary-500 text-white rounded-md text-sm font-medium hover:bg-primary-600 transition-colors"
|
|
>
|
|
Reload Page
|
|
</button>
|
|
</div>
|
|
</div>
|
|
)
|
|
);
|
|
}
|
|
|
|
return this.props.children;
|
|
}
|
|
}
|