-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
fix(dashboard): 自定义侧边栏中错误展开“更多功能”页面问题 (#5405) #5670
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -52,6 +52,21 @@ export function clearSidebarCustomization() { | |
| export function resolveSidebarItems(defaultItems, customization, options = {}) { | ||
| const { cloneItems = false, assembleMoreGroup = false } = options; | ||
|
|
||
| const normalizeKeys = (keys = []) => { | ||
| const list = Array.isArray(keys) ? keys : []; | ||
| const deduped = []; | ||
| const seen = new Set(); | ||
|
|
||
| list.forEach((key) => { | ||
| if (typeof key !== 'string') return; | ||
| if (seen.has(key)) return; | ||
| seen.add(key); | ||
| deduped.push(key); | ||
| }); | ||
|
|
||
| return deduped; | ||
| }; | ||
|
Comment on lines
+55
to
+68
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
|
|
||
| const all = new Map(); | ||
| const defaultMain = []; | ||
| const defaultMore = []; | ||
|
|
@@ -70,9 +85,23 @@ export function resolveSidebarItems(defaultItems, customization, options = {}) { | |
| }); | ||
|
|
||
| const hasCustomization = Boolean(customization); | ||
| const mainKeys = hasCustomization ? customization.mainItems || [] : defaultMain; | ||
| const moreKeys = hasCustomization ? customization.moreItems || [] : defaultMore; | ||
| const used = hasCustomization ? new Set([...mainKeys, ...moreKeys]) : new Set(defaultMain.concat(defaultMore)); | ||
| let mainKeys = hasCustomization ? normalizeKeys(customization.mainItems || []) : [...defaultMain]; | ||
| let moreKeys = hasCustomization ? normalizeKeys(customization.moreItems || []) : [...defaultMore]; | ||
|
|
||
| if (hasCustomization) { | ||
| mainKeys = mainKeys.filter(title => all.has(title)); | ||
| moreKeys = moreKeys.filter(title => all.has(title)); | ||
| } | ||
|
|
||
| if (hasCustomization) { | ||
| // 如果同一项同时出现在主区与更多区,主区优先。 | ||
| const mainSet = new Set(mainKeys); | ||
| moreKeys = moreKeys.filter(title => !mainSet.has(title)); | ||
| } | ||
|
|
||
| const used = hasCustomization | ||
| ? new Set([...mainKeys, ...moreKeys]) | ||
| : new Set(defaultMain.concat(defaultMore)); | ||
|
|
||
| const mainItems = mainKeys | ||
| .map(title => all.get(title)) | ||
|
|
@@ -119,7 +148,13 @@ export function resolveSidebarItems(defaultItems, customization, options = {}) { | |
| } | ||
| } | ||
|
|
||
| return { mainItems, moreItems, merged }; | ||
| return { | ||
| mainItems, | ||
| moreItems, | ||
| merged, | ||
| normalizedMainKeys: [...mainKeys], | ||
| normalizedMoreKeys: [...moreKeys] | ||
| }; | ||
| } | ||
|
|
||
| /** | ||
|
|
@@ -129,9 +164,29 @@ export function resolveSidebarItems(defaultItems, customization, options = {}) { | |
| */ | ||
| export function applySidebarCustomization(defaultItems) { | ||
| const customization = getSidebarCustomization(); | ||
| const { merged } = resolveSidebarItems(defaultItems, customization, { | ||
| const { | ||
| merged, | ||
| normalizedMainKeys, | ||
| normalizedMoreKeys | ||
| } = resolveSidebarItems(defaultItems, customization, { | ||
| cloneItems: true, | ||
| assembleMoreGroup: true | ||
| }); | ||
|
|
||
| if (customization) { | ||
| const rawMainKeys = Array.isArray(customization.mainItems) ? customization.mainItems : []; | ||
| const rawMoreKeys = Array.isArray(customization.moreItems) ? customization.moreItems : []; | ||
| const hasChanged = | ||
| JSON.stringify(rawMainKeys) !== JSON.stringify(normalizedMainKeys) || | ||
| JSON.stringify(rawMoreKeys) !== JSON.stringify(normalizedMoreKeys); | ||
|
Comment on lines
+177
to
+181
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 当前检测变更的逻辑没有正确处理 const hasChanged =
!Array.isArray(customization.mainItems) ||
!Array.isArray(customization.moreItems) ||
JSON.stringify(customization.mainItems) !== JSON.stringify(normalizedMainKeys) ||
JSON.stringify(customization.moreItems) !== JSON.stringify(normalizedMoreKeys); |
||
|
|
||
| if (hasChanged) { | ||
| setSidebarCustomization({ | ||
| mainItems: normalizedMainKeys, | ||
| moreItems: normalizedMoreKeys | ||
| }); | ||
| } | ||
| } | ||
|
|
||
| return merged || defaultItems; | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
issue (complexity): 可以考虑抽取一个可复用的侧边栏自定义配置归一化(normalization)辅助方法,并让
applySidebarCustomization负责持久化逻辑,这样resolveSidebarItems就能专注在根据 key 解析 items 上。在保留当前行为(归一化 + 持久化)的前提下,通过以下方式降低耦合度与复杂度:
applySidebarCustomization负责持久化和变更检测resolveSidebarItems只专注于根据 key 解析出 items1. 抽取归一化辅助函数
将归一化流水线从
resolveSidebarItems中抽离出来,使其可复用:这样一来,
resolveSidebarItems就可以被简化,也不再需要返回归一化之后的 keys:2. 在
applySidebarCustomization内部显式地进行变更检测使用同一个归一化辅助函数进行持久化,同时用一个小而明确的数组相等性辅助方法替代
JSON.stringify:要点:
resolveSidebarItems不再返回normalizedMainKeys/normalizedMoreKeys,其职责被限定为将 keys 转换为 items。normalizeSidebarCustomization)是一条单一的、可组合的流水线,被resolveSidebarItems和applySidebarCustomization共同复用。applySidebarCustomization通过areArraysShallowEqual而不是JSON.stringify来进行清晰的持久化与变更检测。Original comment in English
issue (complexity): Consider extracting a reusable sidebar customization normalization helper and letting
applySidebarCustomizationhandle persistence so thatresolveSidebarItemsstays focused on resolving items from keys.You can keep the new behavior (normalization + persistence) but reduce coupling/complexity by:
applySidebarCustomizationown persistence & change detectionresolveSidebarItemsfocused on item resolution1. Extract a normalization helper
Move the normalization pipeline out of
resolveSidebarItemsand make it reusable:Now
resolveSidebarItemscan be simplified and no longer needs to return normalized keys:2. Make change detection explicit and local to
applySidebarCustomizationUse the same helper for persistence and a small explicit equality helper instead of
JSON.stringify:Key points:
resolveSidebarItemsno longer returnsnormalizedMainKeys/normalizedMoreKeys, keeping its responsibility focused on converting keys → items.normalizeSidebarCustomization) is a single, composable pipeline used by bothresolveSidebarItemsandapplySidebarCustomization.applySidebarCustomizationhandles persistence and change detection clearly viaareArraysShallowEqualinstead ofJSON.stringify.