import { Component, type ErrorInfo, type ReactNode } from 'react' type ErrorBoundaryProps = { children: ReactNode fallback?: ReactNode } type ErrorBoundaryState = { hasError: boolean message: string } /** * 攔截子樹的 render 例外,避免單一元件(例如渲染 AI 任意輸出的 IslanderMarkdown) * 出錯就整頁白屏。 */ export class ErrorBoundary extends Component { state: ErrorBoundaryState = { hasError: false, message: '' } static getDerivedStateFromError(error: unknown): ErrorBoundaryState { return { hasError: true, message: error instanceof Error ? error.message : '發生未預期錯誤' } } componentDidCatch(error: Error, info: ErrorInfo) { console.error('ErrorBoundary caught error:', error, info.componentStack) } private handleReset = () => { this.setState({ hasError: false, message: '' }) } render() { if (!this.state.hasError) return this.props.children if (this.props.fallback) return this.props.fallback return (

這個區塊發生問題,請重試或重新整理頁面。

{this.state.message}

) } }