From 6e41115d404bb3325a76a6a285bac4513a64041a Mon Sep 17 00:00:00 2001 From: zhanjunhao <642307404@qq.com> Date: Fri, 27 Nov 2026 18:58:52 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E4=BF=AE=E5=A4=8D=E9=A1=B6=E9=83=A8?= =?UTF-8?q?=E5=AF=BC=E8=88=AA=E5=92=8C=E4=BE=A7=E8=BE=B9=E6=A0=8F=E8=8F=9C?= =?UTF-8?q?=E5=8D=95=E8=BF=9E=E7=82=B9bug=EF=BC=8C=E5=B0=81=E8=A3=85?= =?UTF-8?q?=E5=85=AC=E5=85=B1=E4=BE=A7=E8=BE=B9=E6=A0=8F=E8=8F=9C=E5=8D=95?= =?UTF-8?q?=E7=BB=84=E4=BB=B6=E5=B9=B6=E7=BB=9F=E4=B8=80ui=E6=A0=B7?= =?UTF-8?q?=E5=BC=8F=E4=B8=8E=E4=BA=A4=E4=BA=92?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- docs/CHANGELOG.md | 8 + docs/ui/GLOBAL_UI_BASELINE.md | 5 +- src/App.jsx | 11 +- src/components/layout/PageLayout.jsx | 11 +- src/components/layout/SiderBar.jsx | 285 ++++++------------ .../layout/components/CenterSidebar.jsx | 192 ++++++++++++ .../components/CenterSidebarLoading.jsx | 65 ++-- src/hooks/common/useSidebarCollapsed.js | 5 + src/hooks/common/useSidebarGroups.js | 44 +++ src/hooks/common/useSidebarToggleGuard.js | 16 + src/hooks/common/useTopNavigation.js | 16 +- src/pages/CommissionV2/PartnerPortal.jsx | 74 +++-- src/pages/CommissionV2/partnerPortal.css | 5 - src/pages/CommissionV2/styles.css | 67 ++-- src/pages/Docs/index.jsx | 85 +++--- src/pages/Docs/styles.css | 105 ++----- .../components/ExperienceSidebar.jsx | 125 ++++---- src/pages/ExperienceCenter/index.jsx | 29 +- .../Organization/OrgCommissionNavigation.jsx | 115 +++---- src/pages/Organization/OrgResolver.jsx | 2 +- src/pages/Organization/OrgSidebar.jsx | 259 ++++++---------- src/pages/Organization/index.jsx | 5 +- src/pages/UserCenter/UserCenterSidebar.jsx | 74 ++--- src/pages/UserCenter/index.jsx | 13 +- src/styles/center-layout.css | 57 +++- .../unit/service-dashboard-navigation.test.js | 7 + 26 files changed, 875 insertions(+), 805 deletions(-) create mode 100644 src/components/layout/components/CenterSidebar.jsx create mode 100644 src/hooks/common/useSidebarGroups.js create mode 100644 src/hooks/common/useSidebarToggleGuard.js diff --git a/docs/CHANGELOG.md b/docs/CHANGELOG.md index 8d1e94df..b89417cf 100644 --- a/docs/CHANGELOG.md +++ b/docs/CHANGELOG.md @@ -10,6 +10,14 @@ ## 2026-11-27 +### 统一页面级侧栏并修复高频连点 + +- **范围** 体验中心、用户中心、控制台、企业中心、文档和伙伴工作台。 +- **变更** 页面链接去重与节流,统一侧栏外壳、菜单项、分组、折叠和加载占位。 +- **影响** 当前项连点不再重复写入导航历史,桌面折叠偏好跨页面同步,移动抽屉与底栏保留各自的入口。 +- **回滚** 恢复本次侧栏组件、样式和交互 hook。 +- **关联** 全站 UI 基线第 4.3 节。 + ### 优化右侧顶栏快捷操作 - **范围** 顶栏教程、WeyAi 下载、充值、推广、主题、语言和头像菜单。 diff --git a/docs/ui/GLOBAL_UI_BASELINE.md b/docs/ui/GLOBAL_UI_BASELINE.md index de89645f..cfef8670 100644 --- a/docs/ui/GLOBAL_UI_BASELINE.md +++ b/docs/ui/GLOBAL_UI_BASELINE.md @@ -65,8 +65,9 @@ ### 4.3 侧栏 -- 控制台侧栏显示、路由选中项变化或滚动区尺寸变化后,当前菜单超出可视范围时,仅滚动菜单区域使其完整可见;已可见时不移动,不滚动正文、不抢焦点,也不强制展开用户手动收起的分组。 -- 企业中心与用户中心共用 `src/styles/center-layout.css`:展开 216px、收起 64px;菜单行高 42px、行距 6px,图标 16px / 1.5px 线宽;折叠入口文案“收起导航”。 +- 页面级侧栏共用 `CenterSidebar` 和 `src/styles/center-layout.css`:展开 216px、收起 64px;菜单行高 42px、行距 6px,图标 16px / 1.5px 线宽;折叠入口文案“收起导航”。抽屉保留完整文字,窄屏的底部或横向导航沿用各页面的布局。 +- 当前菜单超出可视范围时,仅滚动菜单区域使其完整可见;已可见时不移动。侧栏使用当前路由去重,快速切换保留最新目标。折叠按钮和分组操作过滤同一控件的连点。 +- 体验、用户、控制台和企业中心的加载占位复用侧栏外壳,保持宽度、菜单起点及底部折叠条位置。企业身份和分组标题保留对应占位。 - 企业信息区为无底框网格图标(24px / 1.65 线宽),内边距 28px 16px 26px,图文间距 10px;企业名与身份 12px / 20px 行高,身份上距 2px。 - 个人侧栏只保留容器外侧分隔线,内层 Menu 不叠加 `border-inline-end`;企业侧栏不提供菜单搜索。 - 内容外壳设 `min-width: 0`,防止宽表格挤压;桌面滚动区保留稳定槽位并补足 16px 留白,移动端不补。 diff --git a/src/App.jsx b/src/App.jsx index 9898822f..b47e3d6f 100644 --- a/src/App.jsx +++ b/src/App.jsx @@ -515,7 +515,9 @@ function App() { element={ CLOUD_BRAIN_UI_ENABLED ? ( - } key={location.pathname}> + } + > @@ -530,14 +532,13 @@ function App() { moduleAuthConfig.experienceCenter ? ( } - key={location.pathname} + fallback={} > ) : ( - } key={location.pathname}> + }> ) @@ -960,7 +961,7 @@ function App() { element={ {isOrgCenterAvailable ? ( - }> + }> ) : ( diff --git a/src/components/layout/PageLayout.jsx b/src/components/layout/PageLayout.jsx index 1d011553..799e18dd 100644 --- a/src/components/layout/PageLayout.jsx +++ b/src/components/layout/PageLayout.jsx @@ -12,6 +12,7 @@ import React, { } from 'react'; import { useIsMobile } from '../../hooks/common/useIsMobile'; import { useSidebarCollapsed } from '../../hooks/common/useSidebarCollapsed'; +import { useSidebarToggleGuard } from '../../hooks/common/useSidebarToggleGuard'; import { CenterSidebarLoading } from './components/CenterSidebarLoading'; import { useTranslation } from 'react-i18next'; import { API } from '../../helpers/api'; @@ -67,6 +68,7 @@ const PageLayout = () => { const isMobile = useIsMobile(); const [collapsed] = useSidebarCollapsed(); const [drawerOpen, setDrawerOpen] = useState(false); + const canToggleSidebar = useSidebarToggleGuard(); const { i18n } = useTranslation(); const location = useLocation(); @@ -360,7 +362,9 @@ const PageLayout = () => { > setDrawerOpen((prev) => !prev)} + onMobileMenuToggle={() => { + if (canToggleSidebar('drawer')) setDrawerOpen((open) => !open); + }} drawerOpen={drawerOpen} /> @@ -418,7 +422,10 @@ const PageLayout = () => { > + } > {}, forceExpanded = false }) => { const { t } = useTranslation(); const [persistedCollapsed, toggleCollapsed] = useSidebarCollapsed(); const collapsed = forceExpanded ? false : persistedCollapsed; + const followSidebarLink = useSidebarNavigation(onNavigate); const { isModuleVisible, hasSectionVisibleModules, @@ -99,7 +102,6 @@ const SiderBar = ({ onNavigate = () => {}, forceExpanded = false }) => { }, [platformView]); const [selectedKeys, setSelectedKeys] = useState(['home']); - const navigationRef = useRef(null); // 平台关闭智能路由后,仍有生效中出境同意的普通用户保留入口以便撤回(§9.5,与企业"仅授权模式"同口径) const [routerConsentGranted, setRouterConsentGranted] = useState( () => readPersonalConsentCache() === '1', @@ -120,8 +122,9 @@ const SiderBar = ({ onNavigate = () => {}, forceExpanded = false }) => { }; }, [smartRouterEnabled]); const [chatItems, setChatItems] = useState([]); - const [openedKeys, setOpenedKeys] = useState([]); - const [foldedGroups, setFoldedGroups] = useState([]); + const submenus = useSidebarGroups(); + const groups = useSidebarGroups(); + const { setGroup: setSubmenu } = submenus; const location = useLocation(); const [routerMapState, setRouterMapState] = useState(routerMap); @@ -549,9 +552,7 @@ const SiderBar = ({ onNavigate = () => {}, forceExpanded = false }) => { setSelectedKeys([ validChildKeys.includes(childKey) ? childKey : 'commission-overview', ]); - setOpenedKeys((keys) => - keys.includes('commission') ? keys : [...keys, 'commission'], - ); + setSubmenu('commission', true); return; } let matchingKeys = getConsoleMenuKeys(currentPath, routerMapState); @@ -570,39 +571,7 @@ const SiderBar = ({ onNavigate = () => {}, forceExpanded = false }) => { if (matchingKeys.length) { setSelectedKeys(matchingKeys); } - }, [location.pathname, location.search, routerMapState]); - - useEffect(() => { - const navigation = navigationRef.current; - if (!navigation || sidebarLoading) return; - let frame; - const scheduleReveal = () => { - cancelAnimationFrame(frame); - frame = requestAnimationFrame(() => revealActiveSidebarItem(navigation)); - }; - scheduleReveal(); - const resizeObserver = new ResizeObserver(scheduleReveal); - resizeObserver.observe(navigation); - const mutationObserver = new MutationObserver(scheduleReveal); - mutationObserver.observe(navigation, { - childList: true, - subtree: true, - attributes: true, - attributeFilter: ['hidden'], - }); - return () => { - cancelAnimationFrame(frame); - resizeObserver.disconnect(); - mutationObserver.disconnect(); - }; - }, [ - sidebarLoading, - selectedKeys, - collapsed, - forceExpanded, - openedKeys, - foldedGroups, - ]); + }, [location.pathname, location.search, routerMapState, setSubmenu]); const renderMenuIcon = (key) => React.cloneElement(getLucideIcon(key), { @@ -614,186 +583,106 @@ const SiderBar = ({ onNavigate = () => {}, forceExpanded = false }) => { }); const renderGroup = (key, label, items) => ( -
- {!collapsed && ( - - )} - -
+ groups.toggle(key)} + > + {items} + ); - // nested:二级菜单项(无图标,靠缩进与引导线表达层级);forceActive:收起态父项代表任一子项选中 const renderNavItem = ( item, { nested = false, forceActive = false } = {}, ) => { if (item.className === 'tableHiddle') return null; - const active = forceActive || selectedKeys.includes(item.itemKey); + const to = routerMapState[item.itemKey] || routerMap[item.itemKey] || '#'; return ( - - {!nested && renderMenuIcon(item.itemKey)} - {!collapsed && ( - {item.text} - )} - + to={to} + label={item.text} + active={forceActive || selectedKeys.includes(item.itemKey)} + collapsed={collapsed} + nested={nested} + icon={!nested && renderMenuIcon(item.itemKey)} + onClick={(event) => followSidebarLink(to, event)} + /> ); }; const renderSubItem = (item) => { if (!item.items?.length) return renderNavItem(item); - const open = openedKeys.includes(item.itemKey); const childActive = item.items.some((child) => selectedKeys.includes(child.itemKey), ); - // 收起态没有展开空间:父项作为入口直达模块首页,任一子项选中时父项高亮 - if (collapsed) { - return renderNavItem( - { ...item, items: undefined }, - { forceActive: childActive }, - ); - } - const groupId = `ui-console-submenu-${item.itemKey}`; + if (collapsed) return renderNavItem(item, { forceActive: childActive }); + return ( -
- - -
+ submenus.toggle(item.itemKey)} + > + {item.items.map((child) => renderNavItem(child, { nested: true }))} + ); }; if (sidebarLoading) - return ; + return ( + + ); return ( -
- - - {/* 底部折叠按钮 —— 抽屉态不提供:收起后抽屉会变成 60px 窄条,等于自废 */} - {!forceExpanded && ( - - )} -
+ {/* 权限管理区域:至少有一项可见时显示 */} + {authzItems.length > 0 && + renderGroup( + 'authz-group', + t('权限管理'), + authzItems.map((item) => renderNavItem(item)), + )} + ); }; diff --git a/src/components/layout/components/CenterSidebar.jsx b/src/components/layout/components/CenterSidebar.jsx new file mode 100644 index 00000000..8434614c --- /dev/null +++ b/src/components/layout/components/CenterSidebar.jsx @@ -0,0 +1,192 @@ +import React, { useEffect, useId, useRef } from 'react'; +import { Link } from 'react-router-dom'; +import { useTranslation } from 'react-i18next'; +import { + ChevronDown, + ChevronLeft, + ChevronRight, +} from '@/components/common/icons/ActionIcons'; +import { revealActiveSidebarItem } from '../../../helpers/sidebarScroll'; + +export function CenterSidebar({ + children, + className = '', + collapsed = false, + loading = false, + mobile = false, + header, + footer, + onToggle, + label, + navigationClassName = '', +}) { + const { t } = useTranslation(); + const navigation = useRef(null); + const navId = useId(); + + useEffect(() => { + const node = navigation.current; + if (loading || !node) return; + let frame; + const schedule = () => { + cancelAnimationFrame(frame); + frame = requestAnimationFrame(() => revealActiveSidebarItem(node)); + }; + const resize = new ResizeObserver(schedule); + resize.observe(node); + const mutation = new MutationObserver(schedule); + mutation.observe(node, { + subtree: true, + childList: true, + attributes: true, + attributeFilter: ['aria-current', 'hidden'], + }); + schedule(); + return () => { + cancelAnimationFrame(frame); + resize.disconnect(); + mutation.disconnect(); + }; + }, [collapsed, loading]); + + const CollapseIcon = collapsed ? ChevronRight : ChevronLeft; + return ( + + ); +} + +export function CenterSidebarItem({ + to, + label, + icon, + active, + collapsed, + nested, + trailing, + className = '', + ...props +}) { + const Element = to ? Link : 'button'; + return ( + + {icon} + {!collapsed && {label}} + {trailing} + + ); +} + +export function CenterSidebarGroup({ + children, + label, + collapsed, + folded, + onToggle, +}) { + const id = useId(); + return ( +
+ {!collapsed && ( + + )} + +
+ ); +} + +export function CenterSidebarSubmenu({ + children, + label, + icon, + collapsed, + open, + childActive, + onToggle, + className = '', +}) { + const id = useId(); + return ( +
+
+ ); +} diff --git a/src/components/layout/components/CenterSidebarLoading.jsx b/src/components/layout/components/CenterSidebarLoading.jsx index c20ca633..cb115765 100644 --- a/src/components/layout/components/CenterSidebarLoading.jsx +++ b/src/components/layout/components/CenterSidebarLoading.jsx @@ -1,25 +1,56 @@ import React from 'react'; import { useTranslation } from 'react-i18next'; import { useSidebarCollapsed } from '../../../hooks/common/useSidebarCollapsed'; +import { CenterSidebar } from './CenterSidebar'; const rows = [0, 1, 2, 3, 4, 5]; -export function CenterSidebarLoading({ forceExpanded = false }) { - const [collapsed] = useSidebarCollapsed(); - const { t } = useTranslation(); +export function CenterSidebarLoading({ + forceExpanded = false, + variant = 'user', +}) { + const [persistedCollapsed] = useSidebarCollapsed(); + const collapsed = !forceExpanded && persistedCollapsed; + const identity = variant === 'org' && !collapsed; + const grouped = !collapsed && (variant === 'org' || variant === 'console'); return ( - + ) + } + footer={ + !forceExpanded && ( + + ) + } > - {!forceExpanded && ( - - )} - + ); } -export function CenterRouteLoading() { +export function CenterRouteLoading({ variant }) { const { t } = useTranslation(); return (
- +
{t('加载中...')}
diff --git a/src/hooks/common/useSidebarCollapsed.js b/src/hooks/common/useSidebarCollapsed.js index 8a5bdb86..b4c93fe6 100644 --- a/src/hooks/common/useSidebarCollapsed.js +++ b/src/hooks/common/useSidebarCollapsed.js @@ -1,6 +1,8 @@ import { useCallback, useSyncExternalStore } from 'react'; const KEY = 'default_collapse_sidebar'; +const TOGGLE_INTERVAL = 180; +let lastToggleAt = -Infinity; /* * 侧栏折叠态是「一份跨组件共享的状态」,不能用 useState—— @@ -65,6 +67,9 @@ export const useSidebarCollapsed = () => { ); const toggle = useCallback(() => { + const now = performance.now(); + if (now - lastToggleAt < TOGGLE_INTERVAL) return; + lastToggleAt = now; setCollapsedState(!collapsedState); }, []); diff --git a/src/hooks/common/useSidebarGroups.js b/src/hooks/common/useSidebarGroups.js new file mode 100644 index 00000000..5d29905c --- /dev/null +++ b/src/hooks/common/useSidebarGroups.js @@ -0,0 +1,44 @@ +import { useCallback, useRef, useState } from 'react'; +import { useSidebarToggleGuard } from './useSidebarToggleGuard'; + +function readGroups(storageKey) { + if (!storageKey) return []; + try { + const saved = JSON.parse(localStorage.getItem(storageKey)); + return Array.isArray(saved) ? saved : []; + } catch { + return []; + } +} + +export function useSidebarGroups(storageKey) { + const [keys, setKeys] = useState(() => readGroups(storageKey)); + const current = useRef(keys); + const canToggle = useSidebarToggleGuard(); + + const setGroup = useCallback( + (key, included, persist = false) => { + const previous = current.current; + if (!key || previous.includes(key) === included) return; + const next = included + ? [...previous, key] + : previous.filter((item) => item !== key); + current.current = next; + setKeys(next); + if (persist && storageKey) { + try { + localStorage.setItem(storageKey, JSON.stringify(next)); + } catch { + // Keep the in-memory preference if storage is unavailable. + } + } + }, + [storageKey], + ); + + const toggle = (key) => { + if (canToggle(key)) setGroup(key, !current.current.includes(key), true); + }; + + return { keys, toggle, setGroup }; +} diff --git a/src/hooks/common/useSidebarToggleGuard.js b/src/hooks/common/useSidebarToggleGuard.js new file mode 100644 index 00000000..2b5ac547 --- /dev/null +++ b/src/hooks/common/useSidebarToggleGuard.js @@ -0,0 +1,16 @@ +import { useCallback, useRef } from 'react'; + +const TOGGLE_INTERVAL = 180; + +export function useSidebarToggleGuard() { + const lastToggle = useRef(new Map()); + + return useCallback((key) => { + const now = performance.now(); + if (now - (lastToggle.current.get(key) ?? -Infinity) < TOGGLE_INTERVAL) { + return false; + } + lastToggle.current.set(key, now); + return true; + }, []); +} diff --git a/src/hooks/common/useTopNavigation.js b/src/hooks/common/useTopNavigation.js index 0c2fcb93..86edb4f2 100644 --- a/src/hooks/common/useTopNavigation.js +++ b/src/hooks/common/useTopNavigation.js @@ -6,6 +6,14 @@ const NAVIGATION_INTERVAL = 180; const normalizePathname = (path) => path.replace(/\/+$/, '') || '/'; export function useTopNavigation() { + return useThrottledNavigation(); +} + +export function useSidebarNavigation(onNavigate) { + return useThrottledNavigation({ exact: true, onNavigate }); +} + +function useThrottledNavigation({ exact = false, onNavigate } = {}) { const location = useLocation(); const navigate = useNavigate(); const timer = useRef(); @@ -18,7 +26,7 @@ export function useTopNavigation() { !to || event?.defaultPrevented || (event && - (event.button !== 0 || + ((event.button != null && event.button !== 0) || event.metaKey || event.altKey || event.ctrlKey || @@ -34,9 +42,10 @@ export function useTopNavigation() { if ( normalizePathname(target.pathname) === normalizePathname(current.pathname) && - (!target.search || target.search === current.search) && - (!target.hash || target.hash === current.hash) + (target.search === current.search || (!exact && !target.search)) && + (target.hash === current.hash || (!exact && !target.hash)) ) { + onNavigate?.(); return; } @@ -46,6 +55,7 @@ export function useTopNavigation() { if (window.history.state?.key !== originKey) return; lastNavigationAt.current = performance.now(); navigate(to); + onNavigate?.(); }; const wait = NAVIGATION_INTERVAL - (performance.now() - lastNavigationAt.current); diff --git a/src/pages/CommissionV2/PartnerPortal.jsx b/src/pages/CommissionV2/PartnerPortal.jsx index 71d7ffd2..4181edde 100644 --- a/src/pages/CommissionV2/PartnerPortal.jsx +++ b/src/pages/CommissionV2/PartnerPortal.jsx @@ -49,6 +49,13 @@ import { import { BillingColumns, AdjustmentColumns } from './operations'; import { commissionLabel } from './labels'; import { PARTNER_NAVIGATION, partnerView } from './partnerNavigation'; +import { useSidebarToggleGuard } from '../../hooks/common/useSidebarToggleGuard'; +import { useSidebarCollapsed } from '../../hooks/common/useSidebarCollapsed'; +import { useIsMobile } from '../../hooks/common/useIsMobile'; +import { + CenterSidebar, + CenterSidebarItem, +} from '../../components/layout/components/CenterSidebar'; import './partnerPortal.css'; import OrgPageHeader from '../Organization/components/OrgPageHeader'; import OrgModal from '../Organization/components/OrgModal'; @@ -100,6 +107,10 @@ export default function PartnerPortal({ const [searchParams, setSearchParams] = useSearchParams(); const [localActive, setLocalActive] = useState('overview'); const active = embedded ? partnerView(location.search) : localActive; + const canChangeSidebarTab = useSidebarToggleGuard(); + const mobile = useIsMobile(); + const [persistedCollapsed, toggleCollapsed] = useSidebarCollapsed(); + const sidebarCollapsed = !mobile && persistedCollapsed; const internalSearch = useRef(null); const setActive = (key) => { if (!embedded) { @@ -1307,31 +1318,54 @@ export default function PartnerPortal({ className={`commission-v2 commission-admin-workspace partner-portal${embedded ? ' partner-portal--embedded' : ''}`} > {!embedded && ( - + + )}