AdminShell.tsx 6.28 KB
import { useState } from 'react';
import { Outlet, useLocation, useNavigate } from 'react-router-dom';
import { RobotIcon, SettingIcon, UsergroupIcon } from 'tdesign-icons-react';
import {
  BusinessAdminShell,
  type BusinessAdminShellBreadcrumbItem,
  type BusinessAdminShellMenuItem,
} from '@gl/business-components';
import { Dialog } from 'tdesign-react';

import { logout } from '../auth/localAuth';

const navigationItems: BusinessAdminShellMenuItem[] = [
  {
    key: 'agent-management',
    label: 'Agent管理',
    icon: <RobotIcon />,
    children: [
      { key: 'agent-list', label: 'Agent列表' },
      { key: 'agent-tag-list', label: '岗位标签管理' },
      { key: 'skill-list', label: 'Skill目录' },
    ],
  },
  {
    key: 'agent-group-management',
    label: 'Agent群管理',
    icon: <UsergroupIcon />,
    children: [{ key: 'channel-code-list', label: '渠道活码' }],
  },
  {
    key: 'official-site-management',
    label: '官网配置',
    icon: <SettingIcon />,
    children: [
      { key: 'official-site-job-tabs', label: '岗位展示配置' },
      { key: 'official-site-methodology', label: '方法论展示配置' },
    ],
  },
];

const routeByMenuKey: Record<string, string> = {
  'agent-list': '/agents',
  'agent-tag-list': '/agent-tags',
  'skill-list': '/skills',
  'channel-code-list': '/channel-codes',
  'official-site-methodology': '/official-site/methodology',
  'official-site-job-tabs': '/official-site/job-tabs',
};

export function AdminShell() {
  const location = useLocation();
  const navigate = useNavigate();
  const [logoutConfirmOpen, setLogoutConfirmOpen] = useState(false);
  const activeNavigationKey = location.pathname.startsWith('/skills')
    ? 'skill-list'
    : location.pathname.startsWith('/agent-tags')
      ? 'agent-tag-list'
    : location.pathname.startsWith('/official-site/methodology')
      ? 'official-site-methodology'
    : location.pathname.startsWith('/official-site/job-tabs')
      ? 'official-site-job-tabs'
    : location.pathname.startsWith('/agents')
      ? 'agent-list'
    : location.pathname.startsWith('/channel-codes')
      ? 'channel-code-list'
      : undefined;
  const expandedMenuKeys = navigationItems
    .filter((item) => item.children?.some((child) => child.key === activeNavigationKey))
    .map((item) => item.key);

  const makeMenuBreadcrumb = (
    parentLabel: string,
    childLabel: string,
    childPath: string,
    independentPageName?: string,
  ): BusinessAdminShellBreadcrumbItem[] => {
    const items: BusinessAdminShellBreadcrumbItem[] = [
      { key: 'parent', label: parentLabel },
      {
        key: 'child',
        label: childLabel,
        onClick: independentPageName ? () => navigate(childPath) : undefined,
      },
    ];

    if (independentPageName) {
      items.push({ key: 'independent-page', label: independentPageName });
    }

    return items;
  };

  const breadcrumbItems = location.pathname.startsWith('/skills')
    ? makeMenuBreadcrumb('Agent管理', 'Skill目录', '/skills')
    : location.pathname.startsWith('/agent-tags/new')
      ? makeMenuBreadcrumb('Agent管理', '岗位标签管理', '/agent-tags', '新增岗位标签')
    : location.pathname.startsWith('/agent-tags/') && location.pathname.endsWith('/edit')
      ? makeMenuBreadcrumb('Agent管理', '岗位标签管理', '/agent-tags', '编辑岗位标签')
    : location.pathname.startsWith('/agent-tags')
      ? makeMenuBreadcrumb('Agent管理', '岗位标签管理', '/agent-tags')
    : location.pathname.startsWith('/official-site/methodology')
      ? makeMenuBreadcrumb('官网配置', '方法论展示配置', '/official-site/methodology')
    : location.pathname.startsWith('/official-site/job-tabs/new')
      ? makeMenuBreadcrumb('官网配置', '岗位展示配置', '/official-site/job-tabs', '新增岗位展示')
    : location.pathname.startsWith('/official-site/job-tabs/') && location.pathname.endsWith('/edit')
      ? makeMenuBreadcrumb('官网配置', '岗位展示配置', '/official-site/job-tabs', '编辑岗位展示')
    : location.pathname.startsWith('/official-site/job-tabs/') && location.pathname.endsWith('/agents')
      ? makeMenuBreadcrumb('官网配置', '岗位展示配置', '/official-site/job-tabs', '展示Agent配置')
    : location.pathname.startsWith('/official-site/job-tabs')
      ? makeMenuBreadcrumb('官网配置', '岗位展示配置', '/official-site/job-tabs')
    : location.pathname.startsWith('/channel-codes/new')
      ? makeMenuBreadcrumb('Agent群管理', '渠道活码', '/channel-codes', '新增渠道活码')
    : location.pathname.startsWith('/channel-codes/') && location.pathname.endsWith('/edit')
      ? makeMenuBreadcrumb('Agent群管理', '渠道活码', '/channel-codes', '编辑渠道活码')
    : location.pathname.startsWith('/channel-codes')
      ? makeMenuBreadcrumb('Agent群管理', '渠道活码', '/channel-codes')
    : location.pathname.startsWith('/agents/new')
      ? makeMenuBreadcrumb('Agent管理', 'Agent列表', '/agents', '新增Agent')
    : location.pathname.startsWith('/agents/') && location.pathname.endsWith('/edit')
      ? makeMenuBreadcrumb('Agent管理', 'Agent列表', '/agents', '编辑Agent')
      : makeMenuBreadcrumb('Agent管理', 'Agent列表', '/agents');

  return (
    <BusinessAdminShell
      systemName="岗位Agent管理后台"
      menuItems={navigationItems}
      activeMenuKey={activeNavigationKey}
      expandedMenuKeys={expandedMenuKeys}
      breadcrumbItems={breadcrumbItems}
      user={{ name: '管理员' }}
      onMenuChange={(key) => {
        const nextRoute = routeByMenuKey[key];
        if (nextRoute) navigate(nextRoute);
      }}
      onLogout={() => setLogoutConfirmOpen(true)}
    >
      <Outlet />
      <Dialog
        visible={logoutConfirmOpen}
        header="确认退出登录?"
        cancelBtn="取消"
        confirmBtn="确定"
        destroyOnClose
        onClose={() => setLogoutConfirmOpen(false)}
        onConfirm={() => {
          setLogoutConfirmOpen(false);
          logout();
          navigate('/login', { replace: true, state: { from: location } });
        }}
      >
        <p className="m-0 text-sm leading-[var(--gl-line-height-md)] text-[var(--gl-color-text-secondary)]">
          退出后将返回登录页,当前后台操作上下文不会保留。
        </p>
      </Dialog>
    </BusinessAdminShell>
  );
}