import React, { Component, ErrorInfo, ReactNode } from 'react'; import { Alert, AlertDescription, AlertTitle } from '@/components/ui/alert'; import { Button } from '@/components/ui/button'; import { Card, CardContent } from '@/components/ui/card'; import { AlertTriangle, RefreshCw } from 'lucide-react'; interface Props { children: ReactNode; fallback?: ReactNode; onReset?: () => void; } interface State { hasError: boolean; error: Error | null; } 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: ErrorInfo): void { console.error('ErrorBoundary caught an error:', error, errorInfo); } handleReset = (): void => { this.setState({ hasError: false, error: null, }); if (this.props.onReset) { this.props.onReset(); } }; render(): ReactNode { if (this.state.hasError) { if (this.props.fallback) { return this.props.fallback; } return ( Something went wrong

{this.state.error?.message || 'An unexpected error occurred. Please try again.'}

); } return this.props.children; } } export default ErrorBoundary;