import React, { useState, useEffect } from "react"; import { MoonIcon, SunIcon } from "@radix-ui/react-icons"; import { Button } from "@radix-ui/themes"; const THEME_KEY = "theme-preference"; // 修改主题脚本,确保在服务端和客户端都能正确初始化 const themeScript = ` (function() { try { const savedTheme = localStorage.getItem("${THEME_KEY}"); const prefersDark = window.matchMedia("(prefers-color-scheme: dark)").matches; const theme = savedTheme || (prefersDark ? "dark" : "light"); document.documentElement.dataset.theme = theme; document.documentElement.classList.remove('light', 'dark'); document.documentElement.classList.add(theme); } catch (e) { console.error('[ThemeScript] Error:', e); document.documentElement.dataset.theme = 'light'; document.documentElement.classList.remove('light', 'dark'); document.documentElement.classList.add('light'); } })() `; // ThemeScript 组件需要尽早执行 export const ThemeScript = () => { return (