修复主题切换后事件解析异常导致崩溃

This commit is contained in:
JSR
2026-03-18 18:59:52 +08:00
parent e64450a7df
commit eacba654c9
2 changed files with 12 additions and 5 deletions
+5 -3
View File
@@ -18,13 +18,15 @@ export const ThemeProvider: React.FC<{ children: React.ReactNode }> = ({ childre
const currentThemeRef = useRef<themeConfig | null>(null)
const applyThemeConfig = useCallback((theme: themeConfig) => {
const { tdesign, custom } = theme.config
if (!theme) return
const tdesign = theme.config?.tdesign || {}
const custom = theme.config?.custom || {}
const root = document.documentElement
const prevKeys = appliedStyleKeysRef.current
for (const k of prevKeys) root.style.removeProperty(k)
const nextKeys: string[] = []
root.setAttribute("theme-mode", theme.mode)
root.setAttribute("theme-mode", theme.mode || "light")
const brandColor = tdesign.brandColor || "#1677FF"
const hex = brandColor.replace("#", "")
@@ -38,7 +40,7 @@ export const ThemeProvider: React.FC<{ children: React.ReactNode }> = ({ childre
nextKeys.push("--ss-primary-rgb")
if (brandColor) {
const colorMap = generateColorMap(brandColor, theme.mode)
const colorMap = generateColorMap(brandColor, theme.mode || "light")
Object.entries(colorMap).forEach(([key, value]) => {
root.style.setProperty(key, value)
nextKeys.push(key)
+7 -2
View File
@@ -53,8 +53,13 @@ const api = {
deleteTheme: (themeId: string): Promise<{ success: boolean }> =>
invoke("theme_delete", { themeId }),
onThemeChanged: (callback: (theme: themeConfig) => void): Promise<UnlistenFn> => {
return listen<{ theme: themeConfig }>("theme:updated", (event) => {
callback(event.payload.theme)
return listen<themeConfig | { theme?: themeConfig }>("theme:updated", (event) => {
const payload = event.payload as themeConfig | { theme?: themeConfig } | undefined
const theme =
payload && typeof payload === "object" && "theme" in payload
? payload.theme
: (payload as themeConfig | undefined)
if (theme) callback(theme)
})
},