import React, { Component, ErrorInfo, ReactNode } from 'react'; import '@/styles/error-boundary.css'; interface Props { children: ReactNode; fallback?: ReactNode; onError?: (error: Error, errorInfo: ErrorInfo) => void; resetKeys?: Array; resetOnPropsChange?: boolean; } interface State { hasError: boolean; error?: Error; } export class ErrorBoundary extends Component { private resetTimeoutId: number | null = null; public state: State = { hasError: false, }; public static getDerivedStateFromError(error: Error): State { return { hasError: true, error }; } public componentDidCatch(error: Error, errorInfo: ErrorInfo) { console.error('ErrorBoundary caught an error:', error, errorInfo); this.props.onError?.(error, errorInfo); } public componentDidUpdate(prevProps: Props) { const { resetKeys, resetOnPropsChange } = this.props; const { hasError } = this.state; if (hasError && prevProps.resetKeys !== resetKeys) { if (resetKeys?.some((key, idx) => prevProps.resetKeys?.[idx] !== key)) { this.resetErrorBoundary(); } } if (hasError && resetOnPropsChange && prevProps.children !== this.props.children) { this.resetErrorBoundary(); } } private resetErrorBoundary = () => { if (this.resetTimeoutId) { clearTimeout(this.resetTimeoutId); } this.resetTimeoutId = window.setTimeout(() => { this.setState({ hasError: false, error: undefined }); }, 0); }; public render() { if (this.state.hasError) { return ( this.props.fallback || (

Something went wrong

An unexpected error occurred. Please try refreshing the page.

) ); } return this.props.children; } } export function withErrorBoundary( Component: React.ComponentType, errorBoundaryProps?: Omit ) { const WrappedComponent = (props: T) => ( ); WrappedComponent.displayName = `withErrorBoundary(${Component.displayName || Component.name})`; return WrappedComponent; }