- 新增 ErrorBoundary 组件: 捕获组件崩溃,显示友好错误页+重试按钮 - App: 用 ErrorBoundary 包裹,标题改为「先知 Profeto」 - Matches 页面: - 预测模式切换 (单次/多 Agent) - 加载中动画指示 - 预测中状态提示 - 错误提示可关闭
54 lines
1.4 KiB
TypeScript
54 lines
1.4 KiB
TypeScript
import { Component, ErrorInfo, ReactNode } from 'react'
|
|
|
|
interface Props {
|
|
children: ReactNode
|
|
fallback?: ReactNode
|
|
}
|
|
|
|
interface State {
|
|
hasError: boolean
|
|
error: Error | null
|
|
}
|
|
|
|
export class ErrorBoundary extends Component<Props, State> {
|
|
public state: State = {
|
|
hasError: false,
|
|
error: null,
|
|
}
|
|
|
|
public static getDerivedStateFromError(error: Error): State {
|
|
return { hasError: true, error }
|
|
}
|
|
|
|
public componentDidCatch(error: Error, errorInfo: ErrorInfo) {
|
|
console.error('Uncaught error:', error, errorInfo)
|
|
}
|
|
|
|
public render() {
|
|
if (this.state.hasError) {
|
|
if (this.props.fallback) {
|
|
return this.props.fallback
|
|
}
|
|
return (
|
|
<div className="min-h-screen flex items-center justify-center bg-gray-50 p-6">
|
|
<div className="bg-white rounded-lg border border-red-200 p-6 max-w-md text-center space-y-3">
|
|
<div className="text-4xl">⚠️</div>
|
|
<h2 className="text-lg font-semibold text-gray-800">页面出现错误</h2>
|
|
<p className="text-sm text-gray-500">
|
|
{this.state.error?.message || '未知错误'}
|
|
</p>
|
|
<button
|
|
onClick={() => this.setState({ hasError: false, error: null })}
|
|
className="bg-blue-600 text-white text-sm px-4 py-2 rounded hover:bg-blue-700"
|
|
>
|
|
重试
|
|
</button>
|
|
</div>
|
|
</div>
|
|
)
|
|
}
|
|
|
|
return this.props.children
|
|
}
|
|
}
|