From ae42eed6e27ac3171ef7ca6da8c9b44f15331409 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=89=8B=E7=93=9C=E4=B8=80=E5=8D=81=E9=9B=AA?= Date: Sat, 3 Jan 2026 15:36:42 +0800 Subject: [PATCH] Fix font reset on unmount with unsaved changes Added a ref to track unsaved changes and updated the cleanup logic to only restore the saved font settings if there are unsaved changes. This prevents the font from being unintentionally reset when the page is refreshed or the component is unmounted without changes. --- .../src/pages/dashboard/config/theme.tsx | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/packages/napcat-webui-frontend/src/pages/dashboard/config/theme.tsx b/packages/napcat-webui-frontend/src/pages/dashboard/config/theme.tsx index 08a26faf2..1f6bb1eb4 100644 --- a/packages/napcat-webui-frontend/src/pages/dashboard/config/theme.tsx +++ b/packages/napcat-webui-frontend/src/pages/dashboard/config/theme.tsx @@ -163,9 +163,15 @@ const ThemeConfigCard = () => { const [dataLoaded, setDataLoaded] = useState(false); const [hasUnsavedChanges, setHasUnsavedChanges] = useState(false); - // 使用 useRef 存储 style 标签引用 + // 使用 useRef 存储 style 标签引用和状态 const styleTagRef = useRef(null); const originalDataRef = useRef(null); + const hasUnsavedChangesRef = useRef(false); + + // 同步 hasUnsavedChanges 到 ref,供 cleanup 函数使用 + useEffect(() => { + hasUnsavedChangesRef.current = hasUnsavedChanges; + }, [hasUnsavedChanges]); // 在组件挂载时创建 style 标签,并在卸载时清理 // 同时在卸载时恢复字体到已保存的状态(避免"伪自动保存"问题) @@ -174,8 +180,9 @@ const ThemeConfigCard = () => { document.head.appendChild(styleTag); styleTagRef.current = styleTag; return () => { - // 组件卸载时,恢复到已保存的字体设置 - if (originalDataRef.current?.fontMode) { + // 组件卸载时,只有在有未保存更改时才恢复到已保存的字体设置 + // 避免在刷新页面后字体被意外重置 + if (hasUnsavedChangesRef.current && originalDataRef.current?.fontMode) { applyFont(originalDataRef.current.fontMode); } if (styleTagRef.current) {