main.tsx
1.69 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
import React from "react";
import { createRoot } from "react-dom/client";
import App from "./App";
import { applyTheme, useAppStore } from "./store/useAppStore";
import "./index.css";
applyTheme(useAppStore.getState().theme);
// 跟随系统时,系统主题切换要实时生效
window.matchMedia("(prefers-color-scheme: dark)").addEventListener("change", () => {
if (useAppStore.getState().theme === "system") applyTheme("system");
});
class ErrorBoundary extends React.Component<{ children: React.ReactNode }, { error: Error | null }> {
state = { error: null as Error | null };
static getDerivedStateFromError(error: Error) { return { error }; }
componentDidCatch(error: Error, info: React.ErrorInfo) {
console.error("界面渲染出错", error, info);
}
render() {
if (!this.state.error) return this.props.children;
return (
<div className="flex h-full items-center justify-center p-6">
<div className="card max-w-lg p-5">
<h1 className="text-[15px] font-semibold text-bad">界面出错了</h1>
<p className="mt-1 text-[13px] text-muted">
这一页渲染失败,其余功能不受影响。可以刷新重试;若反复出现,请把下面的信息反馈给我们。
</p>
<pre className="mt-3 max-h-48 overflow-auto rounded-lg bg-ink/5 p-2.5 font-mono text-[11px]">
{this.state.error.message}
</pre>
<button className="btn btn-primary mt-3" onClick={() => window.location.reload()}>刷新页面</button>
</div>
</div>
);
}
}
createRoot(document.getElementById("root")!).render(
<React.StrictMode>
<ErrorBoundary>
<App />
</ErrorBoundary>
</React.StrictMode>,
);