diff --git a/src/App.tsx b/src/App.tsx index 2bdc76c..65914ed 100644 --- a/src/App.tsx +++ b/src/App.tsx @@ -6,6 +6,7 @@ import { Route, BrowserRouter as Router, Routes } from "react-router-dom" import ErrorBoundary from "./components/ErrorBoundary" import Footer from "./components/Footer" import Header, { RefreshToast } from "./components/Header" +import { useBackground } from "./hooks/use-background" import { useTheme } from "./hooks/use-theme" import { InjectContext } from "./lib/inject" import { fetchSetting } from "./lib/nezha-api" @@ -25,6 +26,7 @@ const App: React.FC = () => { const { i18n } = useTranslation() const { setTheme } = useTheme() const [isCustomCodeInjected, setIsCustomCodeInjected] = useState(false) + const { backgroundImage: customBackgroundImage } = useBackground() // 检测是否强制指定了主题颜色 const forceTheme = @@ -60,10 +62,6 @@ const App: React.FC = () => { i18n.changeLanguage(settingData?.data?.config?.language) } - const customBackgroundImage = - // @ts-expect-error CustomBackgroundImage is a global variable - (window.CustomBackgroundImage as string) !== "" ? window.CustomBackgroundImage : undefined - const customMobileBackgroundImage = // @ts-expect-error CustomMobileBackgroundImage is a global variable (window.CustomMobileBackgroundImage as string) !== "" ? window.CustomMobileBackgroundImage : undefined diff --git a/src/components/CycleTransferStatsClient.tsx b/src/components/CycleTransferStatsClient.tsx index ebae6bb..85419ca 100644 --- a/src/components/CycleTransferStatsClient.tsx +++ b/src/components/CycleTransferStatsClient.tsx @@ -22,9 +22,7 @@ interface CycleTransferStatsClientProps { export const CycleTransferStatsClient: React.FC = ({ name, from, to, max, serverStats, className }) => { const { t } = useTranslation() - const customBackgroundImage = - // @ts-expect-error CustomBackgroundImage is a global variable - (window.CustomBackgroundImage as string) !== "" ? window.CustomBackgroundImage : undefined + const customBackgroundImage = (window.CustomBackgroundImage as string) !== "" ? window.CustomBackgroundImage : undefined return (
{ if (server.country_code) { diff --git a/src/components/GroupSwitch.tsx b/src/components/GroupSwitch.tsx index 0298634..960c95f 100644 --- a/src/components/GroupSwitch.tsx +++ b/src/components/GroupSwitch.tsx @@ -11,9 +11,7 @@ export default function GroupSwitch({ currentTab: string setCurrentTab: (tab: string) => void }) { - const customBackgroundImage = - // @ts-expect-error CustomBackgroundImage is a global variable - (window.CustomBackgroundImage as string) !== "" ? window.CustomBackgroundImage : undefined + const customBackgroundImage = (window.CustomBackgroundImage as string) !== "" ? window.CustomBackgroundImage : undefined const scrollRef = useRef(null) const tagRefs = useRef(tabs.map(() => createRef())) diff --git a/src/components/Header.tsx b/src/components/Header.tsx index 4af0483..e40524f 100644 --- a/src/components/Header.tsx +++ b/src/components/Header.tsx @@ -1,11 +1,13 @@ import { ModeToggle } from "@/components/ThemeSwitcher" import { Separator } from "@/components/ui/separator" import { Skeleton } from "@/components/ui/skeleton" +import { useBackground } from "@/hooks/use-background" import { useWebSocketContext } from "@/hooks/use-websocket-context" import { fetchLoginUser, fetchSetting } from "@/lib/nezha-api" import { cn } from "@/lib/utils" import { useQuery } from "@tanstack/react-query" import { AnimatePresence, m } from "framer-motion" +import { ImageMinus } from "lucide-react" import { DateTime } from "luxon" import { useEffect, useRef, useState } from "react" import { useTranslation } from "react-i18next" @@ -18,6 +20,7 @@ import { Button } from "./ui/button" function Header() { const { t } = useTranslation() const navigate = useNavigate() + const { backgroundImage, updateBackground } = useBackground() const { data: settingData, isLoading } = useQuery({ queryKey: ["setting"], @@ -38,10 +41,6 @@ function Header() { // @ts-expect-error CustomDesc is a global variable const customDesc = window.CustomDesc || t("nezha") - const customBackgroundImage = - // @ts-expect-error CustomBackgroundImage is a global variable - (window.CustomBackgroundImage as string) !== "" ? window.CustomBackgroundImage : undefined - useEffect(() => { const link = document.querySelector("link[rel*='icon']") || document.createElement("link") // @ts-expect-error set link.type @@ -57,6 +56,22 @@ function Header() { document.title = siteName || "哪吒监控 Nezha Monitoring" }, [siteName]) + const handleBackgroundToggle = () => { + if (window.CustomBackgroundImage) { + // Store the current background image before removing it + sessionStorage.setItem("savedBackgroundImage", window.CustomBackgroundImage) + updateBackground(undefined) + } else { + // Restore the saved background image + const savedImage = sessionStorage.getItem("savedBackgroundImage") + if (savedImage) { + updateBackground(savedImage) + } + } + } + + const customBackgroundImage = backgroundImage + return (
@@ -87,6 +102,18 @@ function Header() {
+ {(customBackgroundImage || sessionStorage.getItem("savedBackgroundImage")) && ( + + )}