import { Component, ErrorInfo, ReactNode } from 'react'; interface ErrorBoundaryState { hasError: boolean; error: Error | null; errorInfo: ErrorInfo | null; } interface ErrorBoundaryProps { children: ReactNode; fallback?: ReactNode; } interface ErrorMessage { type: 'mkstack-error'; error: { message: string; stack?: string; componentStack?: string; url: string; timestamp: string; userAgent: string; }; } export class ErrorBoundary extends Component { constructor(props: ErrorBoundaryProps) { super(props); this.state = { hasError: false, error: null, errorInfo: null, }; } static getDerivedStateFromError(error: Error): Partial { return { hasError: true, error, }; } componentDidCatch(error: Error, errorInfo: ErrorInfo) { console.error('Error caught by ErrorBoundary:', error, errorInfo); // Send error details via postMessage for iframe embedding const errorMessage: ErrorMessage = { type: 'mkstack-error', error: { message: error.message, stack: error.stack || undefined, componentStack: errorInfo.componentStack || undefined, url: window.location.href, timestamp: new Date().toISOString(), userAgent: navigator.userAgent, }, }; // Send to parent window if in iframe, or broadcast to all if (window.parent !== window) { window.parent.postMessage(errorMessage, '*'); } else { window.postMessage(errorMessage, '*'); } this.setState({ error, errorInfo, }); } handleReset = () => { this.setState({ hasError: false, error: null, errorInfo: null, }); }; render() { if (this.state.hasError) { if (this.props.fallback) { return this.props.fallback; } return (

Something went wrong

An unexpected error occurred. The error has been reported.

Error details
Message:

{this.state.error?.message}

{this.state.error?.stack && (
Stack trace:
                        {this.state.error.stack}
                      
)}
); } return this.props.children; } }