Initial import of weiyuan frontend
This commit is contained in:
@@ -0,0 +1,784 @@
|
||||
/*
|
||||
Copyright (C) 2025 QuantumNous
|
||||
|
||||
This program is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU Affero General Public License as
|
||||
published by the Free Software Foundation, either version 3 of the
|
||||
License, or (at your option) any later version.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU Affero General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Affero General Public License
|
||||
along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
|
||||
For commercial licensing, please contact support@quantumnous.com
|
||||
*/
|
||||
import React, { useContext, useEffect, useMemo, useRef, useState } from 'react';
|
||||
import { Link, useLocation } from 'react-router-dom';
|
||||
import { useTranslation } from 'react-i18next';
|
||||
import {
|
||||
BarChart2,
|
||||
Users,
|
||||
Key,
|
||||
FileSearch,
|
||||
Receipt,
|
||||
Settings,
|
||||
ChevronDown,
|
||||
ChevronLeft,
|
||||
ChevronRight,
|
||||
Building2,
|
||||
Search,
|
||||
X,
|
||||
CreditCard,
|
||||
Network,
|
||||
FolderKanban,
|
||||
LayoutGrid,
|
||||
Wallet,
|
||||
FileClock,
|
||||
UserRound,
|
||||
BrainCircuit,
|
||||
LibraryBig,
|
||||
Blocks,
|
||||
ListChecks,
|
||||
Bot,
|
||||
ShieldCheck,
|
||||
Cable,
|
||||
Gauge,
|
||||
Sparkles,
|
||||
Activity,
|
||||
} from 'lucide-react';
|
||||
import { useSidebarCollapsed } from '../../hooks/common/useSidebarCollapsed';
|
||||
import { StatusContext } from '../../context/Status';
|
||||
import { normalizeSidebarModulesAdmin } from '../../constants/sidebarModules';
|
||||
import { canAccessOrgSettings } from './orgFeatureFlags';
|
||||
import {
|
||||
CLOUD_BRAIN_UI_ENABLED,
|
||||
isCloudBrainOrgPath,
|
||||
} from '../../config/cloudBrainUi';
|
||||
|
||||
const pathFeatureRequirements = {
|
||||
'/org/cloud-brain': 'cloud_brain',
|
||||
'/org/knowledge': 'knowledge_base',
|
||||
'/org/skill-center': 'skill_market',
|
||||
'/org/skill-assignments': 'skill_market',
|
||||
'/org/experts': 'expert',
|
||||
'/org/agent-audit': 'cloud_brain',
|
||||
'/org/connectors': 'connector',
|
||||
'/org/effectiveness': 'agent_effectiveness',
|
||||
'/org/quality': 'quality_monitor',
|
||||
'/org/departments': 'dept',
|
||||
'/org/projects': 'project',
|
||||
'/org/budgets': 'budget',
|
||||
'/org/requests': 'approval',
|
||||
'/org/project-manage': 'project',
|
||||
};
|
||||
|
||||
const isFeatureEnabled = (flags, key) => {
|
||||
if (!key) return true;
|
||||
const value = flags?.[key];
|
||||
return value === true || value === 'true' || value === '1' || value === 1;
|
||||
};
|
||||
|
||||
const OrgSidebar = ({
|
||||
orgName,
|
||||
orgRole,
|
||||
featureFlags,
|
||||
isDeptManager = false,
|
||||
isProjectManager = false,
|
||||
canManageKnowledge = false,
|
||||
canManageSkills = false,
|
||||
canAssignSkills = false,
|
||||
canManageExperts = false,
|
||||
canManageConnectors = false,
|
||||
canAuditAgentMetadata = false,
|
||||
canViewAgentEffect = false,
|
||||
canViewQuality = false,
|
||||
isMemberActor = true,
|
||||
onNavigate,
|
||||
mobile = false,
|
||||
}) => {
|
||||
const { t } = useTranslation();
|
||||
const location = useLocation();
|
||||
const [collapsedState, toggleCollapsed] = useSidebarCollapsed();
|
||||
// 移动端抽屉里始终展开,不允许折叠
|
||||
const collapsed = mobile ? false : collapsedState;
|
||||
const [statusState] = useContext(StatusContext);
|
||||
const orgConfig = useMemo(
|
||||
() =>
|
||||
normalizeSidebarModulesAdmin(statusState?.status?.SidebarModulesAdmin)
|
||||
.orgCenter,
|
||||
[statusState?.status?.SidebarModulesAdmin],
|
||||
);
|
||||
|
||||
const isAdmin =
|
||||
orgRole === 'owner' || orgRole === 'admin' || orgRole === 'super_admin';
|
||||
// owner/超管可管理完整设置;普通管理员仅在平台授权数据安全自助配置时进入该页。
|
||||
const canManageSettings = canAccessOrgSettings(orgRole, featureFlags);
|
||||
const roleLabel = useMemo(() => {
|
||||
if (orgRole === 'super_admin') return t('超级管理员');
|
||||
if (orgRole === 'owner') return t('所有者');
|
||||
if (orgRole === 'admin') return t('企业管理员');
|
||||
if (orgRole === 'dept_admin' && isProjectManager)
|
||||
return t('部门/项目管理员');
|
||||
if (orgRole === 'dept_admin' || isDeptManager) return t('部门管理员');
|
||||
if (isProjectManager) return t('项目管理员');
|
||||
return t('成员');
|
||||
}, [t, orgRole, isDeptManager, isProjectManager]);
|
||||
|
||||
// 菜单分组(云平台风格的分组导航,给扁平的十几个入口建立层次)
|
||||
const groups = useMemo(
|
||||
() => [
|
||||
{ key: 'overview', label: t('概览') },
|
||||
{ key: 'ai', label: t('AI 能力管理') },
|
||||
{ key: 'org', label: t('组织管理') },
|
||||
{ key: 'governance', label: t('额度与治理') },
|
||||
{ key: 'records', label: t('财务与日志') },
|
||||
{ key: 'settings', label: t('系统设置') },
|
||||
],
|
||||
[t],
|
||||
);
|
||||
|
||||
// 统一单一品牌色高亮(云平台风格),不再每项一种颜色
|
||||
const items = useMemo(() => {
|
||||
if (orgConfig?.enabled === false) {
|
||||
return [];
|
||||
}
|
||||
|
||||
// adminOnly 仅 owner/admin/super_admin 可见;其余所有成员可见
|
||||
const all = [
|
||||
{
|
||||
itemKey: '/org/workbench',
|
||||
text: t('工作台'),
|
||||
icon: LayoutGrid,
|
||||
group: 'overview',
|
||||
},
|
||||
{
|
||||
itemKey: '/org/cloud-brain',
|
||||
text: t('云端大脑'),
|
||||
icon: BrainCircuit,
|
||||
group: 'overview',
|
||||
beta: true,
|
||||
},
|
||||
{
|
||||
itemKey: '/org/knowledge',
|
||||
text: t('企业知识'),
|
||||
icon: LibraryBig,
|
||||
group: 'overview',
|
||||
adminOnly: true,
|
||||
knowledgePermission: true,
|
||||
beta: true,
|
||||
},
|
||||
{
|
||||
// 我的技能 + 业务系统授权合并页:成员自助视图,归入概览组,
|
||||
// 让「AI 能力管理」成为纯管理员分组。
|
||||
itemKey: '/org/my-capabilities',
|
||||
text: t('我的 AI 能力'),
|
||||
icon: Sparkles,
|
||||
group: 'overview',
|
||||
},
|
||||
{
|
||||
// 企业技能库 + 企业自研 Skill 合并页;平台技能的启用在技能超市完成
|
||||
itemKey: '/org/skill-center',
|
||||
text: t('技能中心'),
|
||||
icon: Blocks,
|
||||
group: 'ai',
|
||||
adminOnly: true,
|
||||
skillManage: true,
|
||||
},
|
||||
{
|
||||
itemKey: '/org/skill-assignments',
|
||||
text: t('分发与锁定'),
|
||||
icon: ListChecks,
|
||||
group: 'ai',
|
||||
assignmentPermission: true,
|
||||
},
|
||||
{
|
||||
itemKey: '/org/experts',
|
||||
text: t('企业专家工坊'),
|
||||
icon: Bot,
|
||||
group: 'ai',
|
||||
adminOnly: true,
|
||||
expertManage: true,
|
||||
},
|
||||
{
|
||||
itemKey: '/org/connectors',
|
||||
text: t('Connector 管理'),
|
||||
icon: Cable,
|
||||
group: 'ai',
|
||||
adminOnly: true,
|
||||
connectorManage: true,
|
||||
},
|
||||
{
|
||||
itemKey: '/org/effectiveness',
|
||||
text: t('效能分析'),
|
||||
icon: Gauge,
|
||||
group: 'overview',
|
||||
adminOnly: true,
|
||||
effectView: true,
|
||||
},
|
||||
{
|
||||
itemKey: '/org/quality',
|
||||
text: t('调用质量'),
|
||||
icon: Activity,
|
||||
group: 'overview',
|
||||
adminOnly: true,
|
||||
qualityView: true,
|
||||
},
|
||||
{
|
||||
itemKey: '/org/agent-audit',
|
||||
text: t('Agent 任务审计'),
|
||||
icon: ShieldCheck,
|
||||
group: 'records',
|
||||
agentAudit: true,
|
||||
},
|
||||
{
|
||||
itemKey: '/org/dashboard',
|
||||
text: t('数据看板'),
|
||||
icon: BarChart2,
|
||||
group: 'overview',
|
||||
adminOnly: true,
|
||||
},
|
||||
{
|
||||
itemKey: '/org/members',
|
||||
text: t('员工管理'),
|
||||
icon: Users,
|
||||
group: 'org',
|
||||
adminOnly: true,
|
||||
},
|
||||
{
|
||||
itemKey: '/org/dept-manage',
|
||||
text: t('部门管理'),
|
||||
icon: Network,
|
||||
group: 'org',
|
||||
deptScope: true,
|
||||
},
|
||||
{
|
||||
itemKey: '/org/project-manage',
|
||||
text: t('我的项目'),
|
||||
icon: FolderKanban,
|
||||
group: 'org',
|
||||
projectScope: true,
|
||||
},
|
||||
{
|
||||
itemKey: '/org/departments',
|
||||
text: t('部门管理'),
|
||||
icon: Network,
|
||||
group: 'org',
|
||||
adminOnly: true,
|
||||
},
|
||||
{
|
||||
itemKey: '/org/projects',
|
||||
text: t('项目管理'),
|
||||
icon: FolderKanban,
|
||||
group: 'org',
|
||||
adminOnly: true,
|
||||
},
|
||||
{
|
||||
itemKey: '/org/budgets',
|
||||
text: t('预算中心'),
|
||||
icon: Wallet,
|
||||
group: 'governance',
|
||||
adminOnly: true,
|
||||
},
|
||||
{
|
||||
itemKey: '/org/requests',
|
||||
text: isAdmin ? t('审批中心') : t('我的申请'),
|
||||
icon: FileClock,
|
||||
group: 'governance',
|
||||
},
|
||||
{
|
||||
itemKey: '/org/tokens',
|
||||
text: t('令牌管理'),
|
||||
icon: Key,
|
||||
group: 'governance',
|
||||
},
|
||||
{
|
||||
itemKey: '/org/recharge',
|
||||
text: t('企业充值'),
|
||||
icon: CreditCard,
|
||||
group: 'records',
|
||||
adminOnly: true,
|
||||
},
|
||||
{
|
||||
itemKey: '/org/logs',
|
||||
text: isAdmin || isDeptManager ? t('消耗明细') : t('我的消耗明细'),
|
||||
icon: Receipt,
|
||||
group: 'records',
|
||||
},
|
||||
{
|
||||
itemKey: '/org/audit',
|
||||
text: t('审计日志'),
|
||||
icon: FileSearch,
|
||||
group: 'records',
|
||||
adminOnly: true,
|
||||
},
|
||||
{
|
||||
itemKey: '/org/personal-settings',
|
||||
text: t('个人设置'),
|
||||
icon: UserRound,
|
||||
group: 'settings',
|
||||
},
|
||||
{
|
||||
itemKey: '/org/settings',
|
||||
text: t('企业设置'),
|
||||
icon: Settings,
|
||||
group: 'settings',
|
||||
ownerOnly: true,
|
||||
},
|
||||
];
|
||||
|
||||
const visibilityByPath = {
|
||||
'/org/workbench': 'workbench',
|
||||
'/org/cloud-brain': 'cloudBrain',
|
||||
'/org/knowledge': 'knowledge',
|
||||
'/org/skill-assignments': 'skillAssignments',
|
||||
'/org/experts': 'experts',
|
||||
'/org/agent-audit': 'agentAudit',
|
||||
'/org/connectors': 'connectors',
|
||||
'/org/effectiveness': 'effectiveness',
|
||||
'/org/quality': 'quality',
|
||||
'/org/dashboard': 'dashboard',
|
||||
'/org/members': 'members',
|
||||
'/org/departments': 'departments',
|
||||
'/org/projects': 'projects',
|
||||
'/org/dept-manage': 'deptManage',
|
||||
'/org/project-manage': 'projectManage',
|
||||
'/org/budgets': 'budgets',
|
||||
'/org/requests': 'requests',
|
||||
'/org/tokens': 'tokens',
|
||||
'/org/logs': 'logs',
|
||||
'/org/audit': 'audit',
|
||||
'/org/recharge': 'recharge',
|
||||
'/org/settings': 'settings',
|
||||
};
|
||||
|
||||
return all.filter((item) => {
|
||||
if (!CLOUD_BRAIN_UI_ENABLED && isCloudBrainOrgPath(item.itemKey)) {
|
||||
return false;
|
||||
}
|
||||
if (item.adminOnly && !isAdmin) return false;
|
||||
// 平台超管(无成员身份)访问成员型入口必被 member_actor_required 拒绝。
|
||||
if (!isMemberActor && item.itemKey === '/org/my-capabilities')
|
||||
return false;
|
||||
if (item.knowledgePermission && !canManageKnowledge) return false;
|
||||
if (item.skillManage && !canManageSkills) return false;
|
||||
// 与路由守卫一致:分发页要求 (isAdmin || isDeptManager) && canAssignSkills,
|
||||
// 只查权限位会给无管辖部门的 dept_admin 一个点击即被重定向的死入口。
|
||||
if (
|
||||
item.assignmentPermission &&
|
||||
!(canAssignSkills && (isAdmin || isDeptManager))
|
||||
)
|
||||
return false;
|
||||
if (item.expertManage && !canManageExperts) return false;
|
||||
// 不变量:除 can_assign_skills 外,全部 can_* 都由
|
||||
// MemberHasAdminPermission 计算,对非 owner/admin 恒 false——即权限位
|
||||
// 本身已蕴含 isAdmin,菜单无需重复判角色。路由守卫(Organization/index.jsx)
|
||||
// 写成 isAdmin && can_xxx 是防御性冗余,两者当前等价。
|
||||
// ⚠️ 若将来把某个权限下放给 dept_admin(做法参照 can_assign_skills 的
|
||||
// `Role == DeptAdmin || MemberHasAdminPermission(...)`),该不变量即被打破,
|
||||
// 必须同步在这里补角色条件,否则会变成"菜单可见但路由守卫拒绝"的死入口。
|
||||
if (item.agentAudit && !canAuditAgentMetadata) return false;
|
||||
if (item.effectView && !canViewAgentEffect) return false;
|
||||
if (item.qualityView && !canViewQuality) return false;
|
||||
if (item.connectorManage && !canManageConnectors) return false;
|
||||
// 与路由守卫一致:管理员只有 data_security 授权后才显示设置入口。
|
||||
if (item.ownerOnly && !canManageSettings) return false;
|
||||
// 部门管理:仅部门负责人可见(与路由门控一致;纯管理员用员工管理页,避免点了被重定向)
|
||||
if (item.deptScope && !isDeptManager) return false;
|
||||
// 我的项目:仅项目经理可见(同上,纯管理员用项目管理页)
|
||||
if (item.projectScope && !isProjectManager) return false;
|
||||
const key = visibilityByPath[item.itemKey];
|
||||
const requiredFeature = pathFeatureRequirements[item.itemKey];
|
||||
// 合并页跨两个模块开关/功能位,任一页签可见即显示入口(与路由守卫同口径)
|
||||
if (item.itemKey === '/org/my-capabilities') {
|
||||
return (
|
||||
(orgConfig?.mySkills !== false &&
|
||||
isFeatureEnabled(featureFlags, 'skill_market')) ||
|
||||
(orgConfig?.myConnectors !== false &&
|
||||
isFeatureEnabled(featureFlags, 'connector'))
|
||||
);
|
||||
}
|
||||
if (item.itemKey === '/org/skill-center') {
|
||||
return (
|
||||
isFeatureEnabled(featureFlags, 'skill_market') &&
|
||||
(orgConfig?.skillLibrary !== false ||
|
||||
orgConfig?.privateSkills !== false)
|
||||
);
|
||||
}
|
||||
if (item.itemKey === '/org/members') {
|
||||
return (
|
||||
(orgConfig?.members !== false ||
|
||||
orgConfig?.memberAccounts !== false) &&
|
||||
isFeatureEnabled(featureFlags, requiredFeature)
|
||||
);
|
||||
}
|
||||
return (
|
||||
(!key || orgConfig?.[key] !== false) &&
|
||||
isFeatureEnabled(featureFlags, requiredFeature)
|
||||
);
|
||||
});
|
||||
}, [
|
||||
t,
|
||||
isAdmin,
|
||||
canManageSettings,
|
||||
isDeptManager,
|
||||
isProjectManager,
|
||||
canManageKnowledge,
|
||||
canManageSkills,
|
||||
canAssignSkills,
|
||||
canManageExperts,
|
||||
canAuditAgentMetadata,
|
||||
canViewAgentEffect,
|
||||
canViewQuality,
|
||||
isMemberActor,
|
||||
canManageConnectors,
|
||||
orgConfig,
|
||||
featureFlags,
|
||||
]);
|
||||
|
||||
// 按分组聚合,保持 groups 顺序,自动跳过空组
|
||||
const groupedItems = useMemo(
|
||||
() =>
|
||||
groups
|
||||
.map((g) => ({ ...g, items: items.filter((it) => it.group === g.key) }))
|
||||
.filter((g) => g.items.length > 0),
|
||||
[groups, items],
|
||||
);
|
||||
|
||||
// 菜单搜索:命中项按分组展示;搜索中忽略折叠状态,保证结果可见
|
||||
const [searchText, setSearchText] = useState('');
|
||||
const searchInputRef = useRef(null);
|
||||
const navRef = useRef(null);
|
||||
const query = searchText.trim().toLowerCase();
|
||||
const visibleGroups = useMemo(() => {
|
||||
if (!query) return groupedItems;
|
||||
return groupedItems
|
||||
.map((g) => ({
|
||||
...g,
|
||||
items: g.items.filter((it) => it.text.toLowerCase().includes(query)),
|
||||
}))
|
||||
.filter((g) => g.items.length > 0);
|
||||
}, [groupedItems, query]);
|
||||
|
||||
// 二级分组折叠:仅记录用户手动收起的组,跨会话记忆
|
||||
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 getSelectedKey = () => {
|
||||
if (location.pathname === '/org/member-accounts') {
|
||||
return '/org/members';
|
||||
}
|
||||
if (items.some((item) => item.itemKey === location.pathname)) {
|
||||
return location.pathname;
|
||||
}
|
||||
if (location.pathname === '/org' || location.pathname === '/org/') {
|
||||
return items[0]?.itemKey || '';
|
||||
}
|
||||
return '';
|
||||
};
|
||||
|
||||
const selectedKey = getSelectedKey();
|
||||
|
||||
// 当前路由所在分组自动展开(仅本次会话,不覆盖用户的持久化选择)
|
||||
useEffect(() => {
|
||||
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]);
|
||||
|
||||
// 选中项滚入可视区(分组较多时避免高亮项被折叠区/滚动区遮住)
|
||||
useEffect(() => {
|
||||
const el = navRef.current?.querySelector('[data-active="true"]');
|
||||
el?.scrollIntoView({ block: 'nearest' });
|
||||
}, [selectedKey]);
|
||||
|
||||
// 搜索命中片段高亮
|
||||
const renderItemText = (text) => {
|
||||
if (!query) return text;
|
||||
const idx = text.toLowerCase().indexOf(query);
|
||||
if (idx === -1) return text;
|
||||
return (
|
||||
<>
|
||||
{text.slice(0, idx)}
|
||||
<mark className='rounded-sm bg-brand/20 px-0.5 text-inherit'>
|
||||
{text.slice(idx, idx + query.length)}
|
||||
</mark>
|
||||
{text.slice(idx + query.length)}
|
||||
</>
|
||||
);
|
||||
};
|
||||
|
||||
return (
|
||||
<div
|
||||
className={`
|
||||
relative flex flex-col h-full transition-all duration-300 ease-out
|
||||
bg-[var(--surface-sidebar)]
|
||||
border-r border-[var(--border-subtle)]
|
||||
${collapsed ? 'w-[68px]' : 'w-[240px]'}
|
||||
`}
|
||||
>
|
||||
{/* 标题区域 */}
|
||||
{!collapsed && (
|
||||
<div className='px-4 pt-5 pb-3'>
|
||||
<div className='flex items-center gap-2'>
|
||||
<div className='p-1.5 rounded-lg bg-brand/10 text-brand'>
|
||||
<Building2 size={16} />
|
||||
</div>
|
||||
<div className='min-w-0'>
|
||||
<span className='font-bold text-gray-900 dark:text-white block truncate text-sm'>
|
||||
{orgName || t('企业中心')}
|
||||
</span>
|
||||
{orgRole && (
|
||||
<span className='text-[10px] text-gray-500 dark:text-gray-400'>
|
||||
{roleLabel}
|
||||
</span>
|
||||
)}
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
)}
|
||||
|
||||
{/* 菜单搜索(折叠态显示放大镜按钮,点击先展开侧栏再聚焦输入框) */}
|
||||
{!collapsed ? (
|
||||
<div className='px-3 pb-1'>
|
||||
<div className='relative'>
|
||||
<Search
|
||||
size={14}
|
||||
className='pointer-events-none absolute left-2.5 top-1/2 -translate-y-1/2 text-gray-400 dark:text-gray-500'
|
||||
/>
|
||||
<input
|
||||
ref={searchInputRef}
|
||||
type='text'
|
||||
value={searchText}
|
||||
onChange={(e) => setSearchText(e.target.value)}
|
||||
onKeyDown={(e) => {
|
||||
if (e.key === 'Escape') setSearchText('');
|
||||
}}
|
||||
placeholder={t('搜索菜单')}
|
||||
aria-label={t('搜索菜单')}
|
||||
className='h-8 w-full rounded-lg border border-transparent bg-[var(--semi-color-fill-0)] pl-8 pr-7 text-sm text-[var(--text-primary)] outline-none transition-colors duration-200 placeholder:text-gray-400 focus:border-brand/40 focus:bg-[var(--surface-card)] dark:placeholder:text-gray-500'
|
||||
/>
|
||||
{searchText && (
|
||||
<button
|
||||
type='button'
|
||||
onClick={() => setSearchText('')}
|
||||
aria-label={t('清空搜索')}
|
||||
className='absolute right-2 top-1/2 -translate-y-1/2 rounded p-0.5 text-gray-400 transition-colors hover:text-gray-700 dark:hover:text-gray-200'
|
||||
>
|
||||
<X size={13} />
|
||||
</button>
|
||||
)}
|
||||
</div>
|
||||
</div>
|
||||
) : (
|
||||
<div className='px-3 pb-1 pt-3'>
|
||||
<button
|
||||
type='button'
|
||||
onClick={() => {
|
||||
toggleCollapsed();
|
||||
setTimeout(() => searchInputRef.current?.focus(), 350);
|
||||
}}
|
||||
aria-label={t('搜索菜单')}
|
||||
className='flex w-full items-center justify-center rounded-lg p-2 text-gray-500 transition-colors duration-200 hover:bg-brand/10 hover:text-brand-deep dark:text-gray-400 dark:hover:bg-brand/15 dark:hover:text-brand-from'
|
||||
>
|
||||
<Search size={16} />
|
||||
</button>
|
||||
</div>
|
||||
)}
|
||||
|
||||
{/* 导航区域 */}
|
||||
<nav
|
||||
ref={navRef}
|
||||
className='app-scrollbar relative flex-1 px-3 pt-1 pb-2 overflow-y-auto overflow-x-hidden'
|
||||
>
|
||||
{query && visibleGroups.length === 0 && (
|
||||
<div className='px-2 py-8 text-center text-xs text-gray-400 dark:text-gray-500'>
|
||||
{t('没有匹配的菜单')}
|
||||
</div>
|
||||
)}
|
||||
{visibleGroups.map((group, gi) => {
|
||||
// 搜索中强制展开,保证命中项可见;平时遵循用户的折叠选择
|
||||
const folded = !query && foldedGroups.includes(group.key);
|
||||
return (
|
||||
<div key={group.key} className={gi > 0 ? 'mt-3' : ''}>
|
||||
{/* 分组标题(展开态,可点击折叠)/ 分隔线(折叠态) */}
|
||||
{!collapsed ? (
|
||||
<button
|
||||
type='button'
|
||||
onClick={() => toggleGroup(group.key)}
|
||||
aria-expanded={!folded}
|
||||
className='mb-1 flex w-full select-none items-center gap-1.5 rounded-md px-2 py-1 text-[11px] font-semibold uppercase tracking-wider text-gray-400 transition-colors duration-200 hover:bg-[var(--semi-color-fill-0)] hover:text-gray-600 dark:text-gray-500 dark:hover:text-gray-300'
|
||||
>
|
||||
<span className='truncate'>{group.label}</span>
|
||||
<span className='ml-auto text-[10px] font-normal tabular-nums opacity-70'>
|
||||
{group.items.length}
|
||||
</span>
|
||||
<ChevronDown
|
||||
size={13}
|
||||
className={`shrink-0 transition-transform duration-200 ${folded ? '-rotate-90' : ''}`}
|
||||
/>
|
||||
</button>
|
||||
) : (
|
||||
gi > 0 && (
|
||||
<div className='mx-2 mb-3 h-px bg-gray-200/50 dark:bg-neutral-700/50' />
|
||||
)
|
||||
)}
|
||||
|
||||
{/* grid-rows 0fr→1fr 实现平滑的折叠动画 */}
|
||||
<div
|
||||
className={`grid transition-[grid-template-rows] duration-200 ease-out ${
|
||||
folded && !collapsed ? 'grid-rows-[0fr]' : 'grid-rows-[1fr]'
|
||||
}`}
|
||||
>
|
||||
<div className='overflow-hidden'>
|
||||
<div className='space-y-1.5 pb-0.5'>
|
||||
{group.items.map((item) => {
|
||||
const isSelected = selectedKey === item.itemKey;
|
||||
const IconComponent = item.icon;
|
||||
|
||||
return (
|
||||
<Link
|
||||
key={item.itemKey}
|
||||
to={item.itemKey}
|
||||
onClick={() => onNavigate?.()}
|
||||
data-active={isSelected || undefined}
|
||||
className={`
|
||||
group relative flex items-center gap-3 rounded-lg transition-colors duration-200
|
||||
${collapsed ? 'justify-center p-3' : 'px-3 py-2'}
|
||||
${
|
||||
isSelected
|
||||
? 'bg-brand/10 dark:bg-brand/15'
|
||||
: 'hover:bg-brand/5 dark:hover:bg-brand/10'
|
||||
}
|
||||
`}
|
||||
>
|
||||
{isSelected && (
|
||||
<div className='absolute left-0 top-1/2 -translate-y-1/2 w-0.5 h-5 rounded-r-full bg-brand transition-all duration-200' />
|
||||
)}
|
||||
|
||||
<div
|
||||
className={`
|
||||
relative flex-shrink-0 p-1.5 rounded-lg transition-colors duration-200
|
||||
${
|
||||
isSelected
|
||||
? 'bg-brand text-white'
|
||||
: 'text-gray-500 dark:text-gray-400 group-hover:text-brand-deep dark:group-hover:text-brand-from'
|
||||
}
|
||||
`}
|
||||
>
|
||||
<IconComponent size={18} strokeWidth={2} />
|
||||
</div>
|
||||
|
||||
{!collapsed && (
|
||||
<span className='flex min-w-0 items-center gap-2'>
|
||||
<span
|
||||
className={`
|
||||
text-sm transition-colors truncate
|
||||
${
|
||||
isSelected
|
||||
? 'text-brand-deep dark:text-brand-from font-semibold'
|
||||
: 'font-medium text-gray-600 dark:text-gray-300 group-hover:text-brand-deep dark:group-hover:text-brand-from'
|
||||
}
|
||||
`}
|
||||
>
|
||||
{renderItemText(item.text)}
|
||||
</span>
|
||||
{item.beta && (
|
||||
<span className='shrink-0 rounded-full border border-orange-300/70 bg-orange-50 px-1.5 py-0.5 font-mono text-[8px] font-bold tracking-wider text-orange-700 dark:border-orange-700/50 dark:bg-orange-950/40 dark:text-orange-300'>
|
||||
BETA
|
||||
</span>
|
||||
)}
|
||||
</span>
|
||||
)}
|
||||
|
||||
{collapsed && (
|
||||
<div
|
||||
className='
|
||||
absolute left-full ml-2 px-2.5 py-1.5
|
||||
bg-gray-900 dark:bg-white text-white dark:text-gray-900
|
||||
text-xs font-medium rounded-lg shadow-lg
|
||||
opacity-0 invisible group-hover:opacity-100 group-hover:visible
|
||||
transition-all duration-200 whitespace-nowrap z-50
|
||||
'
|
||||
>
|
||||
{item.text}
|
||||
<div
|
||||
className='absolute left-0 top-1/2 -translate-x-1 -translate-y-1/2
|
||||
border-4 border-transparent border-r-gray-900 dark:border-r-white'
|
||||
/>
|
||||
</div>
|
||||
)}
|
||||
</Link>
|
||||
);
|
||||
})}
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
);
|
||||
})}
|
||||
</nav>
|
||||
|
||||
{/* 底部分隔线 */}
|
||||
{!mobile && (
|
||||
<div className='mx-4 h-px bg-gradient-to-r from-transparent via-gray-200/60 dark:via-neutral-700/60 to-transparent' />
|
||||
)}
|
||||
|
||||
{/* 折叠按钮(移动端抽屉里不显示) */}
|
||||
{!mobile && (
|
||||
<div className='relative p-3'>
|
||||
<button
|
||||
onClick={toggleCollapsed}
|
||||
className={`
|
||||
flex items-center justify-center gap-2 w-full py-2 rounded-lg
|
||||
text-gray-500 dark:text-gray-400 text-sm font-medium
|
||||
border border-[var(--border-subtle)]
|
||||
hover:bg-brand/10 dark:hover:bg-brand/15
|
||||
hover:text-brand-deep dark:hover:text-brand-from
|
||||
hover:border-brand/30 transition-colors duration-200
|
||||
${collapsed ? 'px-2' : 'px-3'}
|
||||
`}
|
||||
>
|
||||
{collapsed ? (
|
||||
<ChevronRight size={16} strokeWidth={2.5} />
|
||||
) : (
|
||||
<>
|
||||
<ChevronLeft size={16} strokeWidth={2.5} />
|
||||
<span>{t('收起')}</span>
|
||||
</>
|
||||
)}
|
||||
</button>
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
);
|
||||
};
|
||||
|
||||
export default OrgSidebar;
|
||||
Reference in New Issue
Block a user