'use client' import { Component, type ErrorInfo, type ReactNode } from 'react' import { cardStyles, buttonStyles, cn } from '@/lib/ui-styles' interface TabErrorBoundaryProps { tabKey: string children: ReactNode onRetry?: () => Promise | void fallbackTitle?: string fallbackDescription?: string } interface TabErrorBoundaryState { hasError: boolean error?: Error } export class TabErrorBoundary extends Component { state: TabErrorBoundaryState = { hasError: false, error: undefined, } static getDerivedStateFromError(error: Error): TabErrorBoundaryState { return { hasError: true, error } } componentDidCatch(error: Error, info: ErrorInfo) { console.error(`Tab "${this.props.tabKey}" render error:`, error, info) } componentDidUpdate(prevProps: TabErrorBoundaryProps) { if (prevProps.tabKey !== this.props.tabKey && this.state.hasError) { this.reset() } } reset = () => { this.setState({ hasError: false, error: undefined }) } handleRetry = async () => { this.reset() await this.props.onRetry?.() } render() { if (this.state.hasError) { return (

{this.props.fallbackTitle || '模块加载失败'}

{this.props.fallbackDescription || '请重试或联系管理员'}

) } return this.props.children } } export default TabErrorBoundary