Files
PathEditor/src/components/layout/ErrorBoundary.tsx
T
Serendipity d28861ff9c refactor: 抽取 Modal 组件、支持 JSON/CSV 导出、清理冗余代码
- 新增 Modal 组件,消除 3 个 Dialog 中重复的遮罩层/Escape/stopPropagation 代码
- PathEditDialog/HelpDialog/ImportDialog 改用 Modal 包裹
- handleExport 支持 JSON/CSV 两种格式(CSV 导出代码之前存在但从未接线)
- App.tsx 移除冗余的 initDarkMode 后重复设 store 的逻辑
- ErrorBoundary 添加 componentDidCatch 日志和 console.error

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
2026-05-26 00:51:32 +08:00

39 lines
1.2 KiB
TypeScript

import { Component, type ReactNode } from 'react';
interface Props { children: ReactNode; }
interface State { hasError: boolean; error: string; }
export class ErrorBoundary extends Component<Props, State> {
state: State = { hasError: false, error: '' };
static getDerivedStateFromError(e: Error): State {
console.error('[ErrorBoundary]', e);
return { hasError: true, error: e.message };
}
componentDidCatch(_e: Error, info: React.ErrorInfo) {
console.error('[ErrorBoundary] componentStack:', info.componentStack);
}
render() {
if (this.state.hasError) {
return (
<div className="flex items-center justify-center h-screen" style={{ backgroundColor: 'var(--app-bg)', color: 'var(--app-fg)' }}>
<div className="text-center space-y-4">
<h2 className="text-xl font-bold">应用出错</h2>
<p className="text-sm opacity-70">{this.state.error}</p>
<button
className="px-4 py-2 rounded border"
onClick={() => this.setState({ hasError: false })}
style={{ borderColor: 'var(--app-border)' }}
>
重试
</button>
</div>
</div>
);
}
return this.props.children;
}
}