aa23a263fe
- 移除 react-i18next、i18next 及相关依赖
- 删除旧的 i18n/ 目录和 useLazyTranslation 工具
- 新增 utils/chromeI18n.ts 类型安全 wrapper(useI18n Hook + getMessage)
- 生成 public/_locales/{zh,en}/messages.json(298 个翻译 key)
- 批量更新 39+ 组件文件的导入和翻译调用
- 转换翻译键格式:namespace:key → namespace_key
- 修复 ErrorBoundary/PageErrorBoundary 从 withTranslation HOC 改为直接调用 getMessage
- 更新 vitest.setup.ts mock 加载实际翻译文本
- 修复 11 个测试文件的断言以匹配中文翻译
- TypeScript、ESLint、547 项测试全部通过
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
79 lines
2.4 KiB
TypeScript
79 lines
2.4 KiB
TypeScript
import { Component, ErrorInfo, ReactNode } from 'react';
|
|
import { AlertCircle, RefreshCw } from 'lucide-react';
|
|
import { Button } from '@/components/ui/button';
|
|
import { getMessage } from '@/utils/chromeI18n';
|
|
|
|
interface Props {
|
|
children: ReactNode;
|
|
}
|
|
|
|
interface State {
|
|
hasError: boolean;
|
|
error: Error | null;
|
|
}
|
|
|
|
class ErrorBoundary extends Component<Props, State> {
|
|
state: State = {
|
|
hasError: false,
|
|
error: null,
|
|
};
|
|
|
|
static getDerivedStateFromError(error: Error): State {
|
|
return { hasError: true, error };
|
|
}
|
|
|
|
componentDidCatch(error: Error, errorInfo: ErrorInfo) {
|
|
console.error('Uncaught error:', error, errorInfo);
|
|
}
|
|
|
|
componentDidUpdate(prevProps: Props) {
|
|
if (this.state.hasError && prevProps.children !== this.props.children) {
|
|
this.setState({ hasError: false, error: null });
|
|
}
|
|
}
|
|
|
|
private handleReset = () => {
|
|
window.location.reload();
|
|
};
|
|
|
|
render() {
|
|
if (this.state.hasError) {
|
|
return (
|
|
<div className="flex flex-col items-center justify-center mt-16 mx-auto max-w-md">
|
|
<div className="p-6 text-center rounded-xl border border-destructive/20 bg-destructive/5 shadow-sm">
|
|
<div className="flex h-16 w-16 items-center justify-center rounded-full bg-destructive/10 text-destructive mx-auto mb-4">
|
|
<AlertCircle className="h-8 w-8" />
|
|
</div>
|
|
<h2 className="text-xl font-extrabold text-destructive mb-2">
|
|
{getMessage('errorBoundary_title')}
|
|
</h2>
|
|
<p className="text-sm text-muted-foreground mb-6">
|
|
{getMessage('errorBoundary_description')}
|
|
</p>
|
|
{this.state.error && (
|
|
<div className="mb-6 p-4 rounded-lg bg-zinc-950 dark:bg-zinc-900 text-left max-h-[200px] overflow-auto border border-border/40">
|
|
<pre className="font-mono text-xs whitespace-pre-wrap break-all text-zinc-200 selection:bg-zinc-700">
|
|
{this.state.error.toString()}
|
|
</pre>
|
|
</div>
|
|
)}
|
|
<Button
|
|
variant="destructive"
|
|
onClick={this.handleReset}
|
|
className="rounded-lg font-bold shadow-sm"
|
|
>
|
|
<RefreshCw className="mr-2 h-4 w-4" />
|
|
{getMessage('errorBoundary_refresh')}
|
|
</Button>
|
|
</div>
|
|
</div>
|
|
);
|
|
}
|
|
|
|
return this.props.children;
|
|
}
|
|
}
|
|
|
|
export { ErrorBoundary };
|
|
export default ErrorBoundary;
|