import React, { Component, type ReactNode } from 'react'; import { Box, Text } from 'ink'; interface ErrorBoundaryProps { children: ReactNode; fallback?: React.ComponentType<{ error: Error }>; } interface ErrorBoundaryState { hasError: boolean; error: Error | null; } /** * Error Boundary component to catch and display errors */ export class ErrorBoundary extends Component { constructor(props: ErrorBoundaryProps) { super(props); this.state = { hasError: false, error: null }; } static getDerivedStateFromError(error: Error): ErrorBoundaryState { return { hasError: true, error }; } componentDidCatch(error: Error, errorInfo: React.ErrorInfo): void { console.error('ErrorBoundary caught an error:', error, errorInfo); } componentDidUpdate(prevProps: ErrorBoundaryProps): void { // Reset error state when children change if (this.state.hasError && prevProps.children !== this.props.children) { this.setState({ hasError: false, error: null }); } } render(): ReactNode { if (this.state.hasError && this.state.error) { const { fallback: Fallback } = this.props; if (Fallback) { return ; } return ( Error: {this.state.error.message || 'Unknown error'} ); } return this.props.children; } }