Repository navigation
fix: Optimize sidebar refresh to avoid redundant loading states - #1921
Conversation
Pre-merge checks and finishing touches✅ Passed checks (3 passed)
✨ Finishing touches
🧪 Generate unit tests
Tip 👮 Agentic pre-merge checks are now available in preview!Pro plan users can now enable pre-merge checks in their settings to enforce checklists before merging PRs.
Please see the documentation for more information. Example: reviews:
pre_merge_checks:
custom_checks:
- name: "Undocumented Breaking Changes"
mode: "warning"
instructions: |
Pass/fail criteria: All breaking changes to public APIs, CLI flags, environment variables, configuration keys, database schemas, or HTTP/GraphQL endpoints must be documented in the "Breaking Change" section of the PR description and in CHANGELOG.md. Exclude purely internal or private changes (e.g., code not exported from package entry points or explicitly marked as internal).Please share your feedback with us on this Discord post. Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
Actionable comments posted: 0
🧹 Nitpick comments (7)
web/src/hooks/common/useHeaderBar.js (1)
42-44: Unify the 200 ms minimum as a shared constantPassing 200 directly is fine; consider extracting a shared constant (e.g., SKELETON_MIN_MS) used here and in SiderBar to keep behavior consistent and tweakable.
- const isLoading = useMinimumLoadingTime(loading, 200); + const SKELETON_MIN_MS = 200; + const isLoading = useMinimumLoadingTime(loading, SKELETON_MIN_MS);web/src/components/layout/SiderBar.jsx (1)
61-62: Good alignment with header; consider centralizing the 200 ms valueLooks good and matches the header behavior. Recommend using a shared constant to avoid magic numbers drifting across components.
- const showSkeleton = useMinimumLoadingTime(sidebarLoading, 200); + const SKELETON_MIN_MS = 200; + const showSkeleton = useMinimumLoadingTime(sidebarLoading, SKELETON_MIN_MS);web/src/hooks/common/useSidebar.js (5)
24-27: Make EventTarget/CustomEvent usage SSR‑safeIf this hook is imported during SSR,
EventTarget/CustomEventmay be missing. Guard creation/usage to avoid ReferenceErrors.-const sidebarEventTarget = new EventTarget(); +const sidebarEventTarget = + typeof window !== 'undefined' && typeof window.EventTarget !== 'undefined' + ? new EventTarget() + : null;- sidebarEventTarget.dispatchEvent( - new CustomEvent(SIDEBAR_REFRESH_EVENT, { - detail: { sourceId: instanceIdRef.current, skipLoader: true }, - }), - ); + if (sidebarEventTarget && typeof window !== 'undefined' && typeof window.CustomEvent !== 'undefined') { + sidebarEventTarget.dispatchEvent( + new CustomEvent(SIDEBAR_REFRESH_EVENT, { + detail: { sourceId: instanceIdRef.current, skipLoader: true }, + }), + ); + }- sidebarEventTarget.addEventListener(SIDEBAR_REFRESH_EVENT, handleRefresh); + if (sidebarEventTarget) { + sidebarEventTarget.addEventListener(SIDEBAR_REFRESH_EVENT, handleRefresh); + } @@ - sidebarEventTarget.removeEventListener(SIDEBAR_REFRESH_EVENT, handleRefresh); + if (sidebarEventTarget) { + sidebarEventTarget.removeEventListener(SIDEBAR_REFRESH_EVENT, handleRefresh); + }Also applies to: 153-159, 183-187
84-94: Loader gating logic is sound; document the semantics
withLoadingtri‑state is clean:trueforces loader,falsesuppresses,undefineduses first‑load heuristic. Add a brief comment to avoid regressions.
106-124: DRY up default user config constructionThe default user config build is duplicated in success and error paths. Extract a helper to reduce drift.
- const defaultUserConfig = {}; - Object.keys(adminConfig).forEach((sectionKey) => { - if (adminConfig[sectionKey]?.enabled) { - defaultUserConfig[sectionKey] = { enabled: true }; - // 为每个管理员允许的模块设置默认值为true - Object.keys(adminConfig[sectionKey]).forEach((moduleKey) => { - if ( - moduleKey !== 'enabled' && - adminConfig[sectionKey][moduleKey] - ) { - defaultUserConfig[sectionKey][moduleKey] = true; - } - }); - } - }); - setUserConfig(defaultUserConfig); + setUserConfig(buildDefaultUserConfig(adminConfig)); @@ - const defaultUserConfig = {}; - Object.keys(adminConfig).forEach((sectionKey) => { - if (adminConfig[sectionKey]?.enabled) { - defaultUserConfig[sectionKey] = { enabled: true }; - Object.keys(adminConfig[sectionKey]).forEach((moduleKey) => { - if (moduleKey !== 'enabled' && adminConfig[sectionKey][moduleKey]) { - defaultUserConfig[sectionKey][moduleKey] = true; - } - }); - } - }); - setUserConfig(defaultUserConfig); + setUserConfig(buildDefaultUserConfig(adminConfig));Add this helper near the defaults:
function buildDefaultUserConfig(adminConfig) { const out = {}; Object.keys(adminConfig).forEach((sectionKey) => { if (adminConfig[sectionKey]?.enabled) { out[sectionKey] = { enabled: true }; Object.keys(adminConfig[sectionKey]).forEach((moduleKey) => { if (moduleKey !== 'enabled' && adminConfig[sectionKey][moduleKey]) { out[sectionKey][moduleKey] = true; } }); } }); return out; }Also applies to: 126-139
148-159: StabilizerefreshUserConfigidentityWrap with
useCallbackto avoid re‑creations and ease passing to children.-import { useState, useEffect, useMemo, useContext, useRef } from 'react'; +import { useState, useEffect, useMemo, useContext, useRef, useCallback } from 'react'; @@ - const refreshUserConfig = async () => { + const refreshUserConfig = useCallback(async () => { if (Object.keys(adminConfig).length > 0) { await loadUserConfig({ withLoading: false }); } @@ - }; + }, [adminConfig]);
95-105: Optional: cancel in‑flight request on unmountTo prevent state updates after unmount during rapid navigations, consider wiring an
AbortController(ifAPI.getsupports it) and early‑return when aborted.// Example pattern: useEffect(() => { const ac = new AbortController(); loadUserConfig({ signal: ac.signal }); // plumb through to API.get return () => ac.abort(); }, [adminConfig]);
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (3)
web/src/components/layout/SiderBar.jsx(1 hunks)web/src/hooks/common/useHeaderBar.js(1 hunks)web/src/hooks/common/useSidebar.js(5 hunks)
🔇 Additional comments (1)
web/src/hooks/common/useSidebar.js (1)
171-180: Good self‑origin event filterIgnoring events from the same
instanceIdavoids loops; combined withskipLoaderthis achieves the PR goal of no redundant skeletons on cross‑instance refresh.Please sanity‑check that a manual refresh from one tab updates another tab’s sidebar without showing the skeleton there, while the originating tab also avoids a skeleton.
…debar-perf fix: Optimize sidebar refresh to avoid redundant loading states
…debar-perf fix: Optimize sidebar refresh to avoid redundant loading states
…debar-perf fix: Optimize sidebar refresh to avoid redundant loading states
PR 类型
PR 是否包含破坏性更新?
PR 描述
此次修改针对顶部栏和侧边栏刷新后渲染速度过慢的问题。通过在
useSidebar中记录首次加载状态,仅在必要时展示加载动画,同时为刷新事件带上源标识和跳过骨架屏的标志,避免重复触发加载。另将顶部栏与侧边栏的最小骨架展示时间降至 200ms,提升首屏与刷新时的响应体验。Summary by CodeRabbit
New Features
Bug Fixes