返回 oh-my-ppt
RendererErrorBoundary.tsx
根目录 / src / renderer / src / components / RendererErrorBoundary.tsx
1 import { Component, type ErrorInfo, type ReactNode } from 'react'
2
3 type RendererErrorBoundaryProps = {
4 children: ReactNode
5 }
6
7 type RendererErrorBoundaryState = {
8 error: Error | null
9 }
10
11 export class RendererErrorBoundary extends Component<
12 RendererErrorBoundaryProps,
13 RendererErrorBoundaryState
14 > {
15 state: RendererErrorBoundaryState = { error: null }
16
17 static getDerivedStateFromError(error: Error): RendererErrorBoundaryState {
18 return { error }
19 }
20
21 componentDidCatch(error: Error, errorInfo: ErrorInfo): void {
22 console.error('[renderer] React tree crashed', error, errorInfo)
23 }
24
25 render(): ReactNode {
26 if (!this.state.error) return this.props.children
27
28 return (
29 <main className="flex h-full min-h-screen items-center justify-center bg-[#f4eddf] p-6 text-[#3e4a32]">
30 <section className="w-full max-w-md rounded-2xl border border-[#d8cfbc] bg-white/85 p-6 text-center shadow-lg">
31 <h1 className="organic-serif text-2xl font-semibold">页面遇到错误</h1>
32 <p className="mt-3 text-sm leading-6 text-[#6f6658]">
33 当前页面无法继续运行,刷新应用即可恢复。
34 </p>
35 <button
36 type="button"
37 className="mt-5 rounded-lg bg-[#5d6b4d] px-4 py-2 text-sm font-medium text-white transition-colors hover:bg-[#4d5a40]"
38 onClick={() => window.location.reload()}
39 >
40 刷新应用
41 </button>
42 {import.meta.env.DEV && (
43 <pre className="mt-4 max-h-32 overflow-auto whitespace-pre-wrap rounded-lg bg-black/5 p-3 text-left text-xs text-[#8f3f31]">
44 {this.state.error.message}
45 </pre>
46 )}
47 </section>
48 </main>
49 )
50 }
51 }
52
52 lines Plain Text