|
1 | 1 | import clsx from "clsx"; |
2 | | -import { useEffect, useState } from "react"; |
3 | 2 | import { Sidebar } from "./components/widgets/Sidebar"; |
4 | 3 | import { AppRouter } from "./router/router"; |
5 | | -import { apiClient } from "./utils/request"; |
6 | | -import { Loading } from "./components/universal/Loading"; |
7 | | -import { useNavigate } from "react-router-dom"; |
8 | 4 | import { useSnapshot } from "valtio"; |
9 | | -import { app, server } from "./states/app"; |
10 | | -import { jump } from "@utils/path"; |
11 | | -import { toast, Toaster } from "sonner"; |
| 5 | +import { app } from "./states/app"; |
| 6 | +import useSWR from "swr"; |
| 7 | +import { InternelServerErrorPage } from "@pages/InternelServerErrorPage"; |
| 8 | +import { useAppCheck } from "@hooks/useAppCheck"; |
12 | 9 |
|
13 | 10 | function App() { |
14 | 11 | const appSnapshot = useSnapshot(app); |
15 | | - const [loading, setLoading] = useState(true); |
16 | | - const navigate = useNavigate(); |
17 | 12 |
|
18 | | - useEffect(() => { |
19 | | - apiClient("/ping") |
20 | | - .then(() => {}) |
21 | | - .catch(() => { |
22 | | - if (window.location.pathname != jump("/status")) { |
23 | | - toast.error("无法与后端通信,请检查服务状态"); |
24 | | - navigate(jump("/status")); |
| 13 | + const { error: gatewayError } = useSWR("/ping"); |
| 14 | + |
| 15 | + if (gatewayError) { |
| 16 | + return ( |
| 17 | + <InternelServerErrorPage |
| 18 | + title={"Mog Gateway 错误"} |
| 19 | + description={ |
| 20 | + "控制台似乎无法连接到 Mog 网关层,请检查 Mog 网关层的运行状态。" |
25 | 21 | } |
26 | | - return; |
27 | | - }) |
28 | | - .then(() => { |
29 | | - apiClient("/user/ping").then(() => { |
30 | | - apiClient("/user/check") |
31 | | - .then(() => { |
32 | | - (window.location.pathname == jump("/") || |
33 | | - window.location.pathname == jump("/login") || |
34 | | - window.location.pathname == jump("/register")) && |
35 | | - navigate(jump("/dashboard")); |
36 | | - app.authenticated = true; |
37 | | - app.showSidebar = true; |
38 | | - }) |
39 | | - .catch(() => { |
40 | | - console.log(11) |
41 | | - window.location.pathname != jump("/login") && |
42 | | - window.location.pathname != jump("/register") && |
43 | | - window.location.pathname != jump("/status") && |
44 | | - navigate(jump("/login")); |
45 | | - }); |
46 | | - }).catch(() => { |
47 | | - if (window.location.pathname != jump("/status")) { |
48 | | - toast.error("无法与后端通信,请检查服务状态"); |
49 | | - navigate(jump("/status")); |
50 | | - } |
51 | | - return; |
52 | | - }) |
53 | | - apiClient("/category").then((res) => { |
54 | | - server.categories = res.data; |
55 | | - }); |
56 | | - apiClient("/category?type=Tag").then((res) => { |
57 | | - server.tags = res.data; |
58 | | - }); |
59 | | - }) |
60 | | - .finally(() => { |
61 | | - setTimeout(() => { |
62 | | - setLoading(false); |
63 | | - }, 1000); |
64 | | - }); |
65 | | - }, []); |
| 22 | + /> |
| 23 | + ); |
| 24 | + } |
66 | 25 |
|
| 26 | + useAppCheck(); |
| 27 | + |
67 | 28 | return ( |
68 | 29 | <> |
69 | | - <Toaster /> |
70 | | - <Loading loading={loading} /> |
71 | | - <div className={clsx("app", "loading", !loading && "loaded")}> |
| 30 | + <div className={clsx("app")}> |
72 | 31 | {appSnapshot.showSidebar && <Sidebar />} |
73 | 32 | <div className="inner"> |
74 | 33 | <AppRouter /> |
|
0 commit comments