Skip to content

Commit 7d7ffc0

Browse files
authored
Merge pull request #1921 from RedwindA/refactor/improve-sidebar-perf
fix: Optimize sidebar refresh to avoid redundant loading states
2 parents 3154440 + fa45cb5 commit 7d7ffc0

File tree

3 files changed

+39
-11
lines changed

3 files changed

+39
-11
lines changed

web/src/components/layout/SiderBar.jsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ const SiderBar = ({ onNavigate = () => {} }) => {
5858
loading: sidebarLoading,
5959
} = useSidebar();
6060

61-
const showSkeleton = useMinimumLoadingTime(sidebarLoading);
61+
const showSkeleton = useMinimumLoadingTime(sidebarLoading, 200);
6262

6363
const [selectedKeys, setSelectedKeys] = useState(['home']);
6464
const [chatItems, setChatItems] = useState([]);

web/src/hooks/common/useHeaderBar.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ export const useHeaderBar = ({ onMobileMenuToggle, drawerOpen }) => {
4040
const location = useLocation();
4141

4242
const loading = statusState?.status === undefined;
43-
const isLoading = useMinimumLoadingTime(loading);
43+
const isLoading = useMinimumLoadingTime(loading, 200);
4444

4545
const systemName = getSystemName();
4646
const logo = getLogo();

web/src/hooks/common/useSidebar.js

Lines changed: 37 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ along with this program. If not, see <https://www.gnu.org/licenses/>.
1717
For commercial licensing, please contact [email protected]
1818
*/
1919

20-
import { useState, useEffect, useMemo, useContext } from 'react';
20+
import { useState, useEffect, useMemo, useContext, useRef } from 'react';
2121
import { StatusContext } from '../../context/Status';
2222
import { API } from '../../helpers';
2323

@@ -29,6 +29,13 @@ export const useSidebar = () => {
2929
const [statusState] = useContext(StatusContext);
3030
const [userConfig, setUserConfig] = useState(null);
3131
const [loading, setLoading] = useState(true);
32+
const instanceIdRef = useRef(null);
33+
const hasLoadedOnceRef = useRef(false);
34+
35+
if (!instanceIdRef.current) {
36+
const randomPart = Math.random().toString(16).slice(2);
37+
instanceIdRef.current = `sidebar-${Date.now()}-${randomPart}`;
38+
}
3239

3340
// 默认配置
3441
const defaultAdminConfig = {
@@ -74,9 +81,17 @@ export const useSidebar = () => {
7481
}, [statusState?.status?.SidebarModulesAdmin]);
7582

7683
// 加载用户配置的通用方法
77-
const loadUserConfig = async () => {
84+
const loadUserConfig = async ({ withLoading } = {}) => {
85+
const shouldShowLoader =
86+
typeof withLoading === 'boolean'
87+
? withLoading
88+
: !hasLoadedOnceRef.current;
89+
7890
try {
79-
setLoading(true);
91+
if (shouldShowLoader) {
92+
setLoading(true);
93+
}
94+
8095
const res = await API.get('/api/user/self');
8196
if (res.data.success && res.data.data.sidebar_modules) {
8297
let config;
@@ -122,18 +137,25 @@ export const useSidebar = () => {
122137
});
123138
setUserConfig(defaultUserConfig);
124139
} finally {
125-
setLoading(false);
140+
if (shouldShowLoader) {
141+
setLoading(false);
142+
}
143+
hasLoadedOnceRef.current = true;
126144
}
127145
};
128146

129147
// 刷新用户配置的方法(供外部调用)
130148
const refreshUserConfig = async () => {
131-
if (Object.keys(adminConfig).length > 0) {
132-
await loadUserConfig();
149+
if (Object.keys(adminConfig).length > 0) {
150+
await loadUserConfig({ withLoading: false });
133151
}
134152

135153
// 触发全局刷新事件,通知所有useSidebar实例更新
136-
sidebarEventTarget.dispatchEvent(new CustomEvent(SIDEBAR_REFRESH_EVENT));
154+
sidebarEventTarget.dispatchEvent(
155+
new CustomEvent(SIDEBAR_REFRESH_EVENT, {
156+
detail: { sourceId: instanceIdRef.current, skipLoader: true },
157+
}),
158+
);
137159
};
138160

139161
// 加载用户配置
@@ -146,9 +168,15 @@ export const useSidebar = () => {
146168

147169
// 监听全局刷新事件
148170
useEffect(() => {
149-
const handleRefresh = () => {
171+
const handleRefresh = (event) => {
172+
if (event?.detail?.sourceId === instanceIdRef.current) {
173+
return;
174+
}
175+
150176
if (Object.keys(adminConfig).length > 0) {
151-
loadUserConfig();
177+
loadUserConfig({
178+
withLoading: event?.detail?.skipLoader ? false : undefined,
179+
});
152180
}
153181
};
154182

0 commit comments

Comments
 (0)