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

import { Button, Drawer, StatePlaceholder, Tag } from '../../../packages/ui/src';
import { getAgentDetail, type AgentDetail } from '../../api';
import { buildAgentListSearch, type AgentListRouteState } from './agentRouteState';

export function AgentDetailDrawer({ agentId, routeState }: { agentId?: string; routeState: AgentListRouteState }) {
  const navigate = useNavigate();
  const [loading, setLoading] = useState(false);
  const [loadError, setLoadError] = useState(false);
  const [detail, setDetail] = useState<AgentDetail | null>(null);

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

  const closeDrawer = () => {
    navigate(`/agents${buildAgentListSearch({ ...routeState, detail: undefined })}`);
  };

  if (!agentId) {
    return null;
  }

  return (
    <Drawer
      open
      title="Agent详情"
      width={520}
      onClose={closeDrawer}
      footer={
        <div className="flex items-center justify-end gap-3">
          <Button variant="outline" onClick={closeDrawer}>关闭</Button>
          <Button variant="primary" onClick={() => navigate(`/agents/${agentId}/edit${buildAgentListSearch(routeState)}`)}>编辑</Button>
        </div>
      }
    >
      {loading ? (
        <StatePlaceholder state="empty" title="加载中" description="正在加载 Agent 详情" />
      ) : loadError ? (
        <StatePlaceholder state="error" title="详情加载失败" description="当前 Agent 详情无法展示" />
      ) : detail ? (
        <div className="grid gap-4 text-sm">
          <section className="grid gap-2">
            <strong className="text-base">{detail.name}</strong>
            <span className="text-[var(--gl-color-text-secondary)]">ID: {detail.agent_id}</span>
          </section>
          <div className="grid gap-3">
            <div className="grid grid-cols-[100px_1fr] gap-2 items-start">
              <span className="text-[var(--gl-color-text-secondary)]">所属岗位</span>
              <div className="flex flex-wrap gap-2">
                {detail.positions.map((position) => <Tag key={position.id}>{position.name}</Tag>)}
              </div>
            </div>
            <div className="grid grid-cols-[100px_1fr] gap-2 items-start">
              <span className="text-[var(--gl-color-text-secondary)]">Agent描述</span>
              <span>{detail.description}</span>
            </div>
            <div className="grid grid-cols-[100px_1fr] gap-2 items-start">
              <span className="text-[var(--gl-color-text-secondary)]">状态</span>
              <div className="flex flex-wrap gap-2">
                <Tag status={detail.status === 'enable' ? 'success' : 'warning'}>{detail.status === 'enable' ? '已上架' : '已停用'}</Tag>
              </div>
            </div>
            <div className="grid grid-cols-[100px_1fr] gap-2 items-start">
              <span className="text-[var(--gl-color-text-secondary)]">绑定 Skill</span>
              <div className="flex flex-wrap gap-2">
                {detail.skills.map((skill) => <Tag key={skill.skill_code}>{skill.skill_name}</Tag>)}
              </div>
            </div>
          </div>
        </div>
      ) : null}
    </Drawer>
  );
}