index.tsx 6.1 KB
/**
 * 工作台首页。
 *
 * 立场:这是给「使用系统的人」看的页,不是给开发者看的诊断面板。
 * 页面上不出现主壳 / 会话 / 扩展 / 开发环境 / 令牌这类工程词——用户既不懂也不关心。
 * 只回答使用者真正会问的两个问题:
 *   1. 今天几号(真实日期)
 *   2. 我是谁(当前账号的姓名、账号、角色,角色显示中文名而非编码),
 *      并给出两条真实入口:查看个人资料、修改密码(都指向真实存在的 /profile)
 *
 * 不做的事(延续前几轮已确认的边界):不放业务入口、不放假数据统计、
 * 不写问候语与渐变光楔。视觉丰富度靠真实信息 + 分区栅格 + 内联 SVG,
 * 不靠往页面里塞元素。外观一律取 @uj/design-tokens 的 --uj-* 语义令牌。
 */
import type { ReactNode } from 'react';
import { useNavigate } from 'react-router-dom';
import {
  ArrowRightOutlined,
  CalendarOutlined,
  IdcardOutlined,
  KeyOutlined,
  UserOutlined,
} from '@ant-design/icons';
import { UButton } from '@uj/ui';
import { useAppStore } from '@/store/useAppStore';
import { roleLabels, useAccess, type RoleKey } from '@/access';
import { WorkbenchBackdrop } from './WorkbenchBackdrop';

const WEEKDAYS = ['日', '一', '二', '三', '四', '五', '六'] as const;

function todayText(): string {
  const now = new Date();
  return `${now.getFullYear()}年${now.getMonth() + 1}月${now.getDate()}日 · 星期${WEEKDAYS[now.getDay()]}`;
}

/** 角色编码翻成中文名(SYS_ADMIN → 超级管理员);未登记的值原样显示,不吞信息。 */
function roleName(code: string): string {
  return roleLabels[code as RoleKey] ?? code;
}

/** 身份名片右侧的图形:一张带头像的账号卡 + 确认标记。意思是「这是你的账号」。 */
function AccountArtwork() {
  return (
    <svg className="uj-wb-art" viewBox="0 0 240 156" aria-hidden="true" focusable="false">
      <rect x="0" y="0" width="240" height="156" rx="10" fill="var(--uj-primary-soft)" />
      <circle cx="120" cy="78" r="58" fill="none" stroke="var(--uj-primary)" strokeOpacity="0.12" />
      <rect
        x="42"
        y="38"
        width="156"
        height="82"
        rx="8"
        fill="var(--uj-surface)"
        stroke="var(--uj-primary)"
        strokeOpacity="0.28"
      />
      <circle cx="72" cy="68" r="13" fill="var(--uj-primary)" fillOpacity="0.14" />
      <circle cx="72" cy="64" r="5" fill="var(--uj-primary)" fillOpacity="0.7" />
      <path d="M63 76 a9 9 0 0 1 18 0" fill="var(--uj-primary)" fillOpacity="0.35" />
      <rect x="94" y="60" width="76" height="6" rx="3" fill="var(--uj-primary)" fillOpacity="0.38" />
      <rect x="94" y="74" width="52" height="5" rx="2.5" fill="var(--uj-primary)" fillOpacity="0.18" />
      <rect x="56" y="98" width="128" height="4" rx="2" fill="var(--uj-primary)" fillOpacity="0.12" />
      <circle cx="183" cy="111" r="15" fill="var(--uj-success-soft)" />
      <path
        d="M176 111 l5 5 l10 -11"
        fill="none"
        stroke="var(--uj-success)"
        strokeWidth="2.5"
        strokeLinecap="round"
        strokeLinejoin="round"
      />
    </svg>
  );
}

function ActionCard({
  icon,
  title,
  desc,
  action,
  onClick,
}: {
  icon: ReactNode;
  title: string;
  desc: string;
  action?: string;
  onClick?: () => void;
}) {
  return (
    <section className="uj-wb-action">
      <span className="uj-wb-action__icon" aria-hidden>
        {icon}
      </span>
      <h2 className="uj-wb-action__title">{title}</h2>
      <p className="uj-wb-action__desc">{desc}</p>
      {action && onClick ? (
        <div className="uj-wb-action__foot">
          <UButton type="link" onClick={onClick}>
            {action}
            <ArrowRightOutlined />
          </UButton>
        </div>
      ) : null}
    </section>
  );
}

export default function Dashboard() {
  const navigate = useNavigate();
  const { currentUser } = useAppStore();
  const { role, roleCodes } = useAccess();

  const displayRoles = roleCodes.length ? roleCodes.map(roleName) : role ? [roleName(role)] : [];
  const initial = (currentUser.name || currentUser.account || '用').trim().slice(0, 1);

  return (
    <div className="uj-wb">
      <WorkbenchBackdrop />
      <header className="uj-wb-head">
        <div>
          <span className="uj-wb-date">
            <CalendarOutlined aria-hidden />
            {todayText()}
          </span>
          <h1 className="uj-wb-title">工作台</h1>
          <p className="uj-wb-subtitle">在这里查看你的账号信息,管理登录密码。</p>
        </div>
      </header>

      <section className="uj-wb-hero" aria-label="我的账号">
        <div className="uj-wb-hero__main">
          <div className="uj-wb-avatar" aria-hidden>
            {initial}
          </div>
          <div className="uj-wb-hero__copy">
            <span className="uj-wb-label">当前账号</span>
            <div className="uj-wb-hero__name">{currentUser.name || '—'}</div>
            <div className="uj-wb-hero__account">
              <IdcardOutlined aria-hidden />
              {currentUser.account || '未返回账号'}
            </div>
            <div className="uj-wb-tags">
              {displayRoles.length ? (
                displayRoles.map((name) => (
                  <span className="uj-wb-tag" key={name}>
                    {name}
                  </span>
                ))
              ) : (
                <span className="uj-wb-tag">未分配角色</span>
              )}
            </div>
          </div>
        </div>
        <AccountArtwork />
      </section>

      <div className="uj-wb-actions">
        <ActionCard
          icon={<UserOutlined />}
          title="个人资料"
          desc="查看姓名、账号、手机号等你自己的信息。"
          action="查看资料"
          onClick={() => navigate('/profile')}
        />
        <ActionCard
          icon={<KeyOutlined />}
          title="修改密码"
          desc="定期更换登录密码,让账号更安全。"
          action="去修改"
          onClick={() => navigate('/profile')}
        />
      </div>
    </div>
  );
}