diff --git a/src/App.tsx b/src/App.tsx index e23b4a2..654573b 100644 --- a/src/App.tsx +++ b/src/App.tsx @@ -3,6 +3,7 @@ import React, { useEffect, useState } from "react" import { useTranslation } from "react-i18next" import { Route, BrowserRouter as Router, Routes } from "react-router-dom" +import ErrorBoundary from "./components/ErrorBoundary" import Footer from "./components/Footer" import Header from "./components/Header" import { InjectContext } from "./lib/inject" @@ -56,37 +57,39 @@ const App: React.FC = () => { return ( - {/* 固定定位的背景层 */} - {customBackgroundImage && ( + + {/* 固定定位的背景层 */} + {customBackgroundImage && ( +
+ )} + {customMobileBackgroundImage && ( +
+ )}
- )} - {customMobileBackgroundImage && ( -
- )} -
-
-
- - } /> - } /> - } /> - } /> - -
-
-
+ > +
+
+ + } /> + } /> + } /> + } /> + +
+
+
+ ) } diff --git a/src/components/ErrorBoundary.tsx b/src/components/ErrorBoundary.tsx new file mode 100644 index 0000000..331a766 --- /dev/null +++ b/src/components/ErrorBoundary.tsx @@ -0,0 +1,36 @@ +import React from "react" + +import ErrorPage from "../pages/ErrorPage" + +interface Props { + children: React.ReactNode +} + +interface State { + hasError: boolean + error?: Error +} + +class ErrorBoundary extends React.Component { + constructor(props: Props) { + super(props) + this.state = { hasError: false } + } + + static getDerivedStateFromError(error: Error): State { + return { + hasError: true, + error, + } + } + + render() { + if (this.state.hasError) { + return + } + + return this.props.children + } +} + +export default ErrorBoundary diff --git a/src/pages/ErrorPage.tsx b/src/pages/ErrorPage.tsx index 76f8d56..94a41b7 100644 --- a/src/pages/ErrorPage.tsx +++ b/src/pages/ErrorPage.tsx @@ -1,6 +1,4 @@ -import { Button } from "@/components/ui/button" import { useTranslation } from "react-i18next" -import { useNavigate } from "react-router-dom" interface ErrorPageProps { code?: string | number @@ -8,7 +6,6 @@ interface ErrorPageProps { } export default function ErrorPage({ code = "500", message }: ErrorPageProps) { - const navigate = useNavigate() const { t } = useTranslation() return ( @@ -16,14 +13,6 @@ export default function ErrorPage({ code = "500", message }: ErrorPageProps) {

{code}

{message || t("error.somethingWentWrong")}

-
- - -
)