import React, { Component, ErrorInfo, ReactNode } from 'react'; import { AlertTriangle, RotateCcw } from 'lucide-react'; import { Button } from '@/components/ui/button'; import { Card, CardContent, CardHeader, CardTitle } from '@/components/ui/card'; interface Props { children: ReactNode; fallback?: ReactNode; } interface State { hasError: boolean; error?: Error; } class ErrorBoundary extends Component { constructor(props: Props) { super(props); this.state = { hasError: false }; } static getDerivedStateFromError(error: Error): State { // Update state so the next render will show the fallback UI return { hasError: true, error }; } componentDidCatch(error: Error, errorInfo: ErrorInfo) { // Log error to console in development if (process.env.NODE_ENV === 'development') { console.error('Error caught by boundary:', error, errorInfo); } } handleReset = () => { this.setState({ hasError: false, error: undefined }); }; render() { if (this.state.hasError) { if (this.props.fallback) { return this.props.fallback; } return (
Something went wrong

We encountered an unexpected error. Please try refreshing the page or contact support if the problem persists.

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