修复"边栏"隐藏后无法即时生效问题

This commit is contained in:
F。 2025-09-01 09:52:52 +08:00
parent 9ba37555fd
commit 354e866a5b
2 changed files with 32 additions and 4 deletions

View File

@ -44,6 +44,7 @@ import CodeViewer from '../../../playground/CodeViewer';
import { StatusContext } from '../../../../context/Status'; import { StatusContext } from '../../../../context/Status';
import { UserContext } from '../../../../context/User'; import { UserContext } from '../../../../context/User';
import { useUserPermissions } from '../../../../hooks/common/useUserPermissions'; import { useUserPermissions } from '../../../../hooks/common/useUserPermissions';
import { useSidebar } from '../../../../hooks/common/useSidebar';
const NotificationSettings = ({ const NotificationSettings = ({
t, t,
@ -97,6 +98,9 @@ const NotificationSettings = ({
isSidebarModuleAllowed, isSidebarModuleAllowed,
} = useUserPermissions(); } = useUserPermissions();
// 使useSidebar
const { refreshUserConfig } = useSidebar();
// //
const handleSectionChange = (sectionKey) => { const handleSectionChange = (sectionKey) => {
return (checked) => { return (checked) => {
@ -132,6 +136,9 @@ const NotificationSettings = ({
}); });
if (res.data.success) { if (res.data.success) {
showSuccess(t('侧边栏设置保存成功')); showSuccess(t('侧边栏设置保存成功'));
// useSidebar
await refreshUserConfig();
} else { } else {
showError(res.data.message); showError(res.data.message);
} }
@ -334,7 +341,7 @@ const NotificationSettings = ({
loading={sidebarLoading} loading={sidebarLoading}
className='!rounded-lg' className='!rounded-lg'
> >
{t('保存边栏设置')} {t('保存设置')}
</Button> </Button>
</> </>
) : ( ) : (

View File

@ -21,6 +21,10 @@ import { useState, useEffect, useMemo, useContext } from 'react';
import { StatusContext } from '../../context/Status'; import { StatusContext } from '../../context/Status';
import { API } from '../../helpers'; import { API } from '../../helpers';
// 创建一个全局事件系统来同步所有useSidebar实例
const sidebarEventTarget = new EventTarget();
const SIDEBAR_REFRESH_EVENT = 'sidebar-refresh';
export const useSidebar = () => { export const useSidebar = () => {
const [statusState] = useContext(StatusContext); const [statusState] = useContext(StatusContext);
const [userConfig, setUserConfig] = useState(null); const [userConfig, setUserConfig] = useState(null);
@ -124,9 +128,11 @@ export const useSidebar = () => {
// 刷新用户配置的方法(供外部调用) // 刷新用户配置的方法(供外部调用)
const refreshUserConfig = async () => { const refreshUserConfig = async () => {
if (Object.keys(adminConfig).length > 0) { // 移除adminConfig的条件限制直接刷新用户配置
await loadUserConfig(); await loadUserConfig();
}
// 触发全局刷新事件通知所有useSidebar实例更新
sidebarEventTarget.dispatchEvent(new CustomEvent(SIDEBAR_REFRESH_EVENT));
}; };
// 加载用户配置 // 加载用户配置
@ -137,6 +143,21 @@ export const useSidebar = () => {
} }
}, [adminConfig]); }, [adminConfig]);
// 监听全局刷新事件
useEffect(() => {
const handleRefresh = () => {
if (Object.keys(adminConfig).length > 0) {
loadUserConfig();
}
};
sidebarEventTarget.addEventListener(SIDEBAR_REFRESH_EVENT, handleRefresh);
return () => {
sidebarEventTarget.removeEventListener(SIDEBAR_REFRESH_EVENT, handleRefresh);
};
}, [adminConfig]);
// 计算最终的显示配置 // 计算最终的显示配置
const finalConfig = useMemo(() => { const finalConfig = useMemo(() => {
const result = {}; const result = {};