import { Component, type ErrorInfo, type ReactNode } from "react"; interface ErrorBoundaryProps { children: ReactNode; fallback?: ReactNode; onDelete?: () => void; } interface ErrorBoundaryState { hasError: boolean; error: Error | null; } export class ErrorBoundary extends Component< ErrorBoundaryProps, ErrorBoundaryState > { constructor(props: ErrorBoundaryProps) { super(props); this.state = { hasError: false, error: null }; } static getDerivedStateFromError(error: Error): ErrorBoundaryState { return { hasError: true, error }; } componentDidCatch(error: Error, errorInfo: ErrorInfo) { console.error("ErrorBoundary caught an error:", error, errorInfo); } handleDelete = () => { this.props.onDelete?.(); }; render() { if (this.state.hasError) { if (this.props.fallback) { return this.props.fallback; } return ( ); } return this.props.children; } } export const ErrorFallback = ({ error, onDelete, }: { error: Error | null; onDelete?: () => void; }) => { return (
⚠️ {error?.name || "Error"}: {error?.message || "An error occurred"} {onDelete && ( )}
); };