feat(theme): 支持读取主题query参数

This commit is contained in:
dayjoy
2025-09-30 14:23:04 +08:00
parent 7c1241b26c
commit eb6eceffe9

View File

@@ -2,15 +2,31 @@ import "./App.css";
import React, { useEffect } from "react"; import React, { useEffect } from "react";
import { Routes } from "react-router-dom"; import { Routes } from "react-router-dom";
import {handleAuthTokenAndGroupIdFromUrl} from "./utils/auth"; import {handleAuthTokenAndGroupIdFromUrl} from "./utils/auth";
import { useTheme } from "@/components/theme-provider";
const App: React.FC = () => { const App: React.FC = () => {
const { setTheme } = useTheme();
useEffect(() => { useEffect(() => {
// 在组件挂载时处理URL中的authToken 和 groupId // 在组件挂载时处理URL中的authToken 和 groupId
handleAuthTokenAndGroupIdFromUrl(); handleAuthTokenAndGroupIdFromUrl();
}, []);
// 读取并处理URL中的 theme'light'|'dark')参数
const params = new URLSearchParams(window.location.search);
const themeParam = params.get('theme');
if (themeParam === 'light' || themeParam === 'dark') {
// 使用 ThemeProvider 的 setTheme 应用主题并存入本地storageKey: egret-ui-theme
setTheme(themeParam);
// 从URL中移除 theme 参数,保持地址整洁
const url = new URL(window.location.href);
url.searchParams.delete('theme');
window.history.replaceState(null, '', url.toString());
}
}, [setTheme]);
return ( return (
<div className="w-full h-full bg-gray-50"> <div className="w-full min-h-dvh bg-background text-foreground">
<main> <main>
<Routes></Routes> <Routes></Routes>
</main> </main>