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

import { StatePlaceholder } from '@gl/business-components';
import { CloseIcon } from 'tdesign-icons-react';
import { Button, Drawer, Tag } from 'tdesign-react';
import { getAgentDetail, isStewardAgent, type AgentDetail } from '../../api';
import { useTrackPageView } from '../../api/tracking';
import { useDeferredEffect } from '../../hooks/useDeferredEffect';
import { buildAgentListSearch, type AgentListRouteState } from './agentRouteState';

const STEWARD_POSITION_LABEL = '系统调度';

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);

  useTrackPageView('agent_detail', { loaded: !loading && !loadError && detail !== null });

  useDeferredEffect(() => {
    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
      visible
      header="Agent详情"
      size="520px"
      className="detail-drawer"
      destroyOnClose
      closeBtn={<Button aria-label="关闭Agent详情" icon={<CloseIcon />} shape="square" variant="text" />}
      onClose={closeDrawer}
      footer={
        <div className="flex items-center justify-end gap-3">
          <Button variant="outline" onClick={closeDrawer}>关闭</Button>
          <Button theme="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">
                {/* 管家 Agent 固定显示 "系统调度" */}
                {detail && isStewardAgent(detail) ? (
                  <Tag>{STEWARD_POSITION_LABEL}</Tag>
                ) : (
                  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 theme={detail.status === 'enable' ? 'success' : 'warning'} variant="light">{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>
  );
}