import React, { type ReactNode } from 'react'; import { AlertTriangle, RefreshCw } from 'lucide-react'; import { Button } from '@/components/ui/button'; // ──────────────────────────────────────────────────────────────────────────── // ErrorBoundary Component // ──────────────────────────────────────────────────────────────────────────── interface ErrorBoundaryProps { children?: ReactNode; } interface ErrorBoundaryState { hasError: boolean; error: Error | null; componentStack: string | null; } class ErrorBoundary extends React.Component { constructor(props: ErrorBoundaryProps) { super(props); this.state = { hasError: false, error: null, componentStack: null }; } static getDerivedStateFromError(error: Error): Partial { return { hasError: true, error }; } componentDidCatch(error: Error, info: React.ErrorInfo) { console.error('ErrorBoundary caught an error:', { error, componentStack: info?.componentStack, }); this.setState({ error, componentStack: info?.componentStack ?? null }); } handleReset = () => { this.setState({ hasError: false, error: null, componentStack: null }); }; handleReload = () => { window.location.reload(); }; render() { if (!this.state.hasError) { return this.props.children; } const { error, componentStack } = this.state; return (

Something went wrong

An unexpected error occurred. You can try to recover by reloading the page or resetting this component.

{error && (

Error Message

                {String(error)}
              
)} {componentStack && (

Component Stack (for debugging)

                {componentStack}
              
)}
); } } // ──────────────────────────────────────────────────────────────────────────── // withErrorBoundary HOC // ──────────────────────────────────────────────────────────────────────────── export function withErrorBoundary

( Component: React.ComponentType

, displayName: string = Component.displayName || Component.name || 'Component', ) { function WrappedComponent(props: P) { return ( ); } WrappedComponent.displayName = `withErrorBoundary(${displayName})`; return WrappedComponent; } export default ErrorBoundary;