import React, { Component, type ReactNode } from "react"; import { Box, Text, useInput } from "ink"; interface Props { children: ReactNode; fallback?: ReactNode; onRetry?: () => void; } interface State { hasError: boolean; error: Error | null; errorInfo: React.ErrorInfo | null; } export class ErrorBoundary extends Component { constructor(props: Props) { super(props); this.state = { hasError: false, error: null, errorInfo: null, }; } static getDerivedStateFromError(error: Error): Partial { return { hasError: true, error, }; } override componentDidCatch(error: Error, errorInfo: React.ErrorInfo) { console.error("Error caught by ErrorBoundary:", error); console.error("Error info:", errorInfo); this.setState({ error, errorInfo, }); } handleRetry = () => { this.setState({ hasError: false, error: null, errorInfo: null, }); if (this.props.onRetry) { this.props.onRetry(); } }; override render() { if (this.state.hasError && this.state.error) { if (this.props.fallback) { return this.props.fallback; } return ( ); } return this.props.children; } } interface ErrorFallbackProps { error: Error; onRetry: () => void; } function ErrorFallback({ error, onRetry }: ErrorFallbackProps) { useInput(() => { onRetry(); }); return ( ⚠️ An Error Occurred Error: {error.message} {error.stack && ( Stack trace (first 5 lines): {error.stack.split('\n').slice(0, 5).map((line, index) => ( {line.trim()} ))} )} Press any key to retry ); }