SkillDetailDrawer.tsx 3.06 KB
import { useEffect, useState } from 'react';
import { useNavigate } from 'react-router-dom';

import { Button, Drawer, StatePlaceholder } from '../../../packages/ui/src';
import { getSkillDetail, type SkillDetail } from '../../api';
import { SkillMarkdown } from './SkillMarkdown';
import { buildSkillListSearch, type SkillListRouteState } from './skillRouteState';

export function SkillDetailDrawer({ routeState, skillCode }: { routeState: SkillListRouteState; skillCode?: string }) {
  const navigate = useNavigate();
  const [loading, setLoading] = useState(false);
  const [loadError, setLoadError] = useState(false);
  const [detail, setDetail] = useState<SkillDetail | null>(null);

  useEffect(() => {
    if (!skillCode) return;
    setLoading(true);
    setLoadError(false);
    setDetail(null);
    getSkillDetail(skillCode)
      .then((data) => {
        setDetail(data);
        setLoading(false);
      })
      .catch(() => {
        setLoadError(true);
        setLoading(false);
      });
  }, [skillCode]);

  const closeDrawer = () => {
    navigate(`/skills${buildSkillListSearch({ keyword: routeState.keyword, detail: undefined })}`);
  };

  if (!skillCode) {
    return null;
  }

  return (
    <Drawer
      open
      title="Skill详情"
      width={560}
      onClose={closeDrawer}
      footer={
        <div className="flex items-center justify-end gap-3">
          <Button variant="outline" onClick={closeDrawer}>关闭</Button>
        </div>
      }
    >
      {loading ? (
        <StatePlaceholder state="empty" title="加载中" description="正在加载 Skill 详情" />
      ) : loadError ? (
        <StatePlaceholder state="error" title="详情加载失败" description="当前 Skill 详情无法展示" />
      ) : detail ? (
        <div className="flex h-[calc(100vh-220px)] flex-col gap-4 overflow-y-auto pr-1 text-sm" data-testid="skill-drawer-scroll">
          <section className="grid content-start gap-3">
            <div className="grid grid-cols-[100px_1fr] items-start gap-2">
              <span className="text-[var(--gl-color-text-secondary)]">Skill编码:</span>
              <span>{detail.skill_code}</span>
            </div>
            <div className="grid grid-cols-[100px_1fr] items-start gap-2">
              <span className="text-[var(--gl-color-text-secondary)]">Skill名称:</span>
              <span>{detail.skill_name}</span>
            </div>
            <div className="grid grid-cols-[100px_1fr] items-start gap-2">
              <span className="text-[var(--gl-color-text-secondary)]">说明:</span>
              <span>{detail.description || '-'}</span>
            </div>
          </section>
          <section className="grid content-start gap-2">
            <strong>使用说明</strong>
            {detail.doc_markdown ? (
              <SkillMarkdown markdown={detail.doc_markdown} />
            ) : (
              <StatePlaceholder state="empty" title="暂无使用说明" description="当前 Skill 还没有可展示的 Markdown 内容" />
            )}
          </section>
        </div>
      ) : null}
    </Drawer>
  );
}