import React, { Component as ReactComponent, ErrorInfo, ReactNode } from 'react'; import { AlertTriangle, RefreshCw, Home } from 'lucide-react'; import { Button } from './ui/button'; import { Card, CardHeader, CardTitle, CardContent } from './ui/card'; interface Props { children?: ReactNode; fallback?: ReactNode; onError?: (error: Error, errorInfo: ErrorInfo) => void; } interface State { hasError: boolean; error?: Error; errorInfo?: ErrorInfo; } export class ErrorBoundary extends ReactComponent { public state: State = { hasError: false, }; public static getDerivedStateFromError(error: Error): State { // Update state so the next render will show the fallback UI return { hasError: true, error }; } public componentDidCatch(error: Error, errorInfo: ErrorInfo) { console.error('ErrorBoundary caught an error:', error, errorInfo); this.setState({ error, errorInfo, }); // Call optional error handler if (this.props.onError) { this.props.onError(error, errorInfo); } } private handleReset = () => { this.setState({ hasError: false, error: undefined, errorInfo: undefined }); }; private handleReload = () => { window.location.reload(); }; public render() { if (this.state.hasError) { // If a custom fallback is provided, use it if (this.props.fallback) { return this.props.fallback; } // Default error UI return ; } return this.props.children; } } interface ErrorFallbackProps { error?: Error; errorInfo?: ErrorInfo; onReset: () => void; onReload: () => void; } export const ErrorFallback: React.FC = ({ error, errorInfo, onReset, onReload }) => { const isDev = process.env.NODE_ENV === 'development'; return (
Something went wrong

The application encountered an unexpected error

Don't worry, your data is safe. You can try the following options:

{isDev && error && (

Error Details (Development Mode)

Error Message:

{error.message}

{error.stack && (

Stack Trace:

                      {error.stack}
                    
)} {errorInfo?.componentStack && (

Component Stack:

                      {errorInfo.componentStack}
                    
)}
)} {!isDev && (

If this problem persists, please contact support with details about what you were doing when the error occurred.

)}
); };