fix: 修复顶部导航和侧边栏菜单连点bug,封装公共侧边栏菜单组件并统一ui样式与交互
This commit is contained in:
@@ -1,11 +1,6 @@
|
||||
import { I18N_TECHNICAL_TEXT } from '../../i18n/technicalText.js';
|
||||
import {
|
||||
ChevronDown as ActionChevronDown,
|
||||
ChevronRight as ActionChevronRight,
|
||||
ChevronLeft as ActionChevronLeft,
|
||||
} from '@/components/common/icons/ActionIcons';
|
||||
import React, { useContext, useEffect, useMemo, useRef, useState } from 'react';
|
||||
import { Link, useLocation } from 'react-router-dom';
|
||||
import React, { useContext, useEffect, useMemo } from 'react';
|
||||
import { useLocation } from 'react-router-dom';
|
||||
import { useTranslation } from 'react-i18next';
|
||||
import {
|
||||
BarChart3,
|
||||
@@ -34,6 +29,13 @@ import {
|
||||
UserCog,
|
||||
} from 'lucide-react';
|
||||
import { useSidebarCollapsed } from '../../hooks/common/useSidebarCollapsed';
|
||||
import { useSidebarNavigation } from '../../hooks/common/useTopNavigation';
|
||||
import { useSidebarGroups } from '../../hooks/common/useSidebarGroups';
|
||||
import {
|
||||
CenterSidebar,
|
||||
CenterSidebarGroup,
|
||||
CenterSidebarItem,
|
||||
} from '../../components/layout/components/CenterSidebar';
|
||||
import { StatusContext } from '../../context/Status';
|
||||
import { normalizeSidebarModulesAdmin } from '../../constants/sidebarModules';
|
||||
import { canAccessOrgSettings } from './orgFeatureFlags';
|
||||
@@ -111,6 +113,7 @@ const OrgSidebar = ({
|
||||
const [collapsedState, toggleCollapsed] = useSidebarCollapsed();
|
||||
// 移动端抽屉里始终展开,不允许折叠
|
||||
const collapsed = mobile ? false : collapsedState;
|
||||
const followSidebarLink = useSidebarNavigation(onNavigate);
|
||||
const [statusState] = useContext(StatusContext);
|
||||
const orgConfig = useMemo(
|
||||
() =>
|
||||
@@ -534,31 +537,11 @@ const OrgSidebar = ({
|
||||
[groups, items],
|
||||
);
|
||||
|
||||
const navRef = useRef(null);
|
||||
|
||||
// 二级分组折叠:仅记录用户手动收起的组,跨会话记忆
|
||||
const FOLDED_GROUPS_KEY = 'org_sidebar_folded_groups';
|
||||
const [foldedGroups, setFoldedGroups] = useState(() => {
|
||||
try {
|
||||
const saved = JSON.parse(localStorage.getItem(FOLDED_GROUPS_KEY) || '[]');
|
||||
return Array.isArray(saved) ? saved : [];
|
||||
} catch {
|
||||
return [];
|
||||
}
|
||||
});
|
||||
const toggleGroup = (key) => {
|
||||
setFoldedGroups((prev) => {
|
||||
const next = prev.includes(key)
|
||||
? prev.filter((k) => k !== key)
|
||||
: [...prev, key];
|
||||
try {
|
||||
localStorage.setItem(FOLDED_GROUPS_KEY, JSON.stringify(next));
|
||||
} catch {
|
||||
/* 隐私模式等场景下写入失败可忽略,仅丢失记忆 */
|
||||
}
|
||||
return next;
|
||||
});
|
||||
};
|
||||
const {
|
||||
keys: foldedGroups,
|
||||
toggle: toggleGroup,
|
||||
setGroup,
|
||||
} = useSidebarGroups('org_sidebar_folded_groups');
|
||||
|
||||
const getSelectedKey = () => {
|
||||
if (location.pathname === '/org/member-accounts') {
|
||||
@@ -580,141 +563,91 @@ const OrgSidebar = ({
|
||||
const activeGroup = groupedItems.find((g) =>
|
||||
g.items.some((it) => it.itemKey === selectedKey),
|
||||
)?.key;
|
||||
if (!activeGroup) return;
|
||||
setFoldedGroups((prev) =>
|
||||
prev.includes(activeGroup) ? prev.filter((k) => k !== activeGroup) : prev,
|
||||
);
|
||||
}, [selectedKey, groupedItems, location.key]);
|
||||
|
||||
// 选中项滚入可视区(分组较多时避免高亮项被折叠区/滚动区遮住)
|
||||
useEffect(() => {
|
||||
const el = navRef.current?.querySelector('[data-active="true"]');
|
||||
el?.scrollIntoView({ block: 'nearest' });
|
||||
}, [selectedKey, location.search]);
|
||||
setGroup(activeGroup, false);
|
||||
}, [selectedKey, groupedItems, location.key, setGroup]);
|
||||
|
||||
return (
|
||||
<aside
|
||||
className='ui-center-sidebar ui-org-sidebar'
|
||||
data-collapsed={collapsed}
|
||||
data-mobile={mobile || undefined}
|
||||
>
|
||||
{!collapsed && (
|
||||
<div className='ui-org-identity'>
|
||||
<LayoutGrid size={24} strokeWidth={1.65} aria-hidden='true' />
|
||||
<div className='ui-org-identity-copy'>
|
||||
<span className='ui-org-name' title={orgName || t('企业中心')}>
|
||||
{orgName || t('企业中心')}
|
||||
</span>
|
||||
{orgRole && <span className='ui-org-role'>{roleLabel}</span>}
|
||||
</div>
|
||||
{organizations.length > 1 && (
|
||||
<select
|
||||
aria-label={t('切换企业')}
|
||||
className='mt-3 w-full rounded-md border border-[var(--border-subtle)] bg-[var(--surface-card)] px-2 py-1 text-sm'
|
||||
value={currentOrgId}
|
||||
onChange={(event) =>
|
||||
onOrganizationChange?.(Number(event.target.value))
|
||||
}
|
||||
>
|
||||
{!organizations.some((org) => org.id === currentOrgId) && (
|
||||
<option value={currentOrgId}>{orgName}</option>
|
||||
)}
|
||||
{organizations.map((org) => (
|
||||
<option key={org.id} value={org.id}>
|
||||
{org.name}
|
||||
</option>
|
||||
))}
|
||||
</select>
|
||||
)}
|
||||
</div>
|
||||
)}
|
||||
<nav ref={navRef} className='ui-center-sidebar-nav'>
|
||||
{groupedItems.map((group) => {
|
||||
const folded = foldedGroups.includes(group.key);
|
||||
return (
|
||||
<div key={group.key} className='ui-org-nav-group'>
|
||||
{!collapsed && (
|
||||
<button
|
||||
type='button'
|
||||
onClick={() => toggleGroup(group.key)}
|
||||
aria-expanded={!folded}
|
||||
className='app-action-button ui-org-group-toggle'
|
||||
>
|
||||
<span>{group.label}</span>
|
||||
<ActionChevronDown
|
||||
size={13}
|
||||
className={folded ? 'is-folded' : ''}
|
||||
aria-hidden='true'
|
||||
/>
|
||||
</button>
|
||||
)}
|
||||
<div className='ui-org-group-items' hidden={folded && !collapsed}>
|
||||
{group.items.map((item) => {
|
||||
// 企业分佣二级导航(weiyuan eb3936cf0);企业侧栏无菜单搜索,query 恒为空
|
||||
if (item.commissionAccess)
|
||||
return (
|
||||
<OrgCommissionNavigation
|
||||
key={item.itemKey}
|
||||
collapsed={collapsed}
|
||||
expandSidebar={toggleCollapsed}
|
||||
query=''
|
||||
renderText={(text) => text}
|
||||
onNavigate={onNavigate}
|
||||
/>
|
||||
);
|
||||
const isSelected = selectedKey === item.itemKey;
|
||||
const IconComponent = item.icon;
|
||||
return (
|
||||
<Link
|
||||
key={item.itemKey}
|
||||
to={item.itemKey}
|
||||
onClick={() => onNavigate?.()}
|
||||
data-active={isSelected || undefined}
|
||||
aria-current={isSelected ? 'page' : undefined}
|
||||
title={collapsed ? item.text : undefined}
|
||||
className='ui-center-menu-item'
|
||||
>
|
||||
<IconComponent
|
||||
size={16}
|
||||
strokeWidth={1.5}
|
||||
aria-hidden='true'
|
||||
/>
|
||||
{!collapsed && (
|
||||
<>
|
||||
<span className='ui-center-menu-label'>
|
||||
{item.text}
|
||||
</span>
|
||||
{item.beta && (
|
||||
<span className='ui-org-beta'>
|
||||
{I18N_TECHNICAL_TEXT.BETA}
|
||||
</span>
|
||||
)}
|
||||
</>
|
||||
)}
|
||||
</Link>
|
||||
);
|
||||
})}
|
||||
</div>
|
||||
<CenterSidebar
|
||||
className='ui-org-sidebar'
|
||||
collapsed={collapsed}
|
||||
mobile={mobile}
|
||||
onToggle={toggleCollapsed}
|
||||
header={
|
||||
!collapsed && (
|
||||
<div className='ui-org-identity'>
|
||||
<LayoutGrid size={24} strokeWidth={1.65} aria-hidden='true' />
|
||||
<div className='ui-org-identity-copy'>
|
||||
<span className='ui-org-name' title={orgName || t('企业中心')}>
|
||||
{orgName || t('企业中心')}
|
||||
</span>
|
||||
{orgRole && <span className='ui-org-role'>{roleLabel}</span>}
|
||||
</div>
|
||||
);
|
||||
})}
|
||||
</nav>
|
||||
{!mobile && (
|
||||
<button
|
||||
type='button'
|
||||
onClick={toggleCollapsed}
|
||||
className='app-action-button ui-center-sidebar-collapse'
|
||||
aria-label={collapsed ? t('展开导航') : t('收起导航')}
|
||||
{organizations.length > 1 && (
|
||||
<select
|
||||
aria-label={t('切换企业')}
|
||||
className='mt-3 w-full rounded-md border border-[var(--border-subtle)] bg-[var(--surface-card)] px-2 py-1 text-sm'
|
||||
value={currentOrgId}
|
||||
onChange={(event) =>
|
||||
onOrganizationChange?.(Number(event.target.value))
|
||||
}
|
||||
>
|
||||
{!organizations.some((org) => org.id === currentOrgId) && (
|
||||
<option value={currentOrgId}>{orgName}</option>
|
||||
)}
|
||||
{organizations.map((org) => (
|
||||
<option key={org.id} value={org.id}>
|
||||
{org.name}
|
||||
</option>
|
||||
))}
|
||||
</select>
|
||||
)}
|
||||
</div>
|
||||
)
|
||||
}
|
||||
>
|
||||
{groupedItems.map((group) => (
|
||||
<CenterSidebarGroup
|
||||
key={group.key}
|
||||
label={group.label}
|
||||
folded={foldedGroups.includes(group.key)}
|
||||
collapsed={collapsed}
|
||||
onToggle={() => toggleGroup(group.key)}
|
||||
>
|
||||
{collapsed ? (
|
||||
<ActionChevronRight size={16} />
|
||||
) : (
|
||||
<ActionChevronLeft size={16} />
|
||||
)}
|
||||
{!collapsed && <span>{t('收起导航')}</span>}
|
||||
</button>
|
||||
)}
|
||||
</aside>
|
||||
{group.items.map((item) => {
|
||||
if (item.commissionAccess) {
|
||||
return (
|
||||
<OrgCommissionNavigation
|
||||
key={item.itemKey}
|
||||
collapsed={collapsed}
|
||||
expandSidebar={toggleCollapsed}
|
||||
followSidebarLink={followSidebarLink}
|
||||
/>
|
||||
);
|
||||
}
|
||||
const Icon = item.icon;
|
||||
return (
|
||||
<CenterSidebarItem
|
||||
key={item.itemKey}
|
||||
to={item.itemKey}
|
||||
label={item.text}
|
||||
collapsed={collapsed}
|
||||
active={selectedKey === item.itemKey}
|
||||
icon={<Icon size={16} strokeWidth={1.5} aria-hidden='true' />}
|
||||
onClick={(event) => followSidebarLink(item.itemKey, event)}
|
||||
trailing={
|
||||
!collapsed &&
|
||||
item.beta && (
|
||||
<span className='ui-org-beta'>
|
||||
{I18N_TECHNICAL_TEXT.BETA}
|
||||
</span>
|
||||
)
|
||||
}
|
||||
/>
|
||||
);
|
||||
})}
|
||||
</CenterSidebarGroup>
|
||||
))}
|
||||
</CenterSidebar>
|
||||
);
|
||||
};
|
||||
|
||||
|
||||
Reference in New Issue
Block a user