From 42f62f7d5b503dceb0cea14b2b9d0eb9593c9cb3 Mon Sep 17 00:00:00 2001 From: Bot Date: Thu, 16 Apr 2026 17:18:52 +0800 Subject: [PATCH] feat: synchronize UI branding with backend settings dynamically --- src/App.tsx | 29 ++++++++++++++++++++++++++--- src/types/nezha-api.ts | 5 +++++ 2 files changed, 31 insertions(+), 3 deletions(-) diff --git a/src/App.tsx b/src/App.tsx index e76091b..2213387 100644 --- a/src/App.tsx +++ b/src/App.tsx @@ -1,4 +1,5 @@ import { useQuery } from "@tanstack/react-query"; +import { DateTime } from "luxon"; import type React from "react"; import { useEffect, useState } from "react"; import { useTranslation } from "react-i18next"; @@ -40,16 +41,38 @@ const MainApp: React.FC = () => { InjectContext(settingData?.data?.config?.custom_code); setIsCustomCodeInjected(true); } - }, [settingData?.data?.config?.custom_code]); + + // 同步自定义配置到全局变量 + const config = settingData?.data?.config; + if (config) { + if (config.custom_logo) window.CustomLogo = config.custom_logo; + if (config.custom_description) + window.CustomDesc = config.custom_description; + if (config.custom_links) window.CustomLinks = config.custom_links; + + const hour = DateTime.now().hour; + const isNight = hour >= 18 || hour < 6; + + if (isNight && config.background_image_night) { + window.CustomBackgroundImage = config.background_image_night; + } else if (!isNight && config.background_image_day) { + window.CustomBackgroundImage = config.background_image_day; + } + window.CustomMobileBackgroundImage = window.CustomBackgroundImage; + } + }, [settingData]); // 检测是否强制指定了主题颜色 const forceTheme = - window.ForceTheme as string !== "" ? window.ForceTheme : undefined; + (window.ForceTheme as string) !== "" ? window.ForceTheme : undefined; useEffect(() => { const savedTheme = localStorage.getItem("vite-ui-theme"); // Only auto-apply ForceTheme if the user hasn't manually picked one (or picked 'system') - if ((!savedTheme || savedTheme === "system") && (forceTheme === "dark" || forceTheme === "light")) { + if ( + (!savedTheme || savedTheme === "system") && + (forceTheme === "dark" || forceTheme === "light") + ) { setTheme(forceTheme); } }, [forceTheme, setTheme]); diff --git a/src/types/nezha-api.ts b/src/types/nezha-api.ts index 44b27bc..aff831e 100644 --- a/src/types/nezha-api.ts +++ b/src/types/nezha-api.ts @@ -150,6 +150,11 @@ type SettingConfig = { user_template: string; admin_template: string; custom_code: string; + custom_logo?: string; + custom_description?: string; + custom_links?: string; + background_image_day?: string; + background_image_night?: string; }; export interface SettingResponse {