import { ArrowRightOutlined, CopyOutlined, ReloadOutlined } from '@ant-design/icons' import { App, Button, Card, Checkbox, Col, Collapse, Descriptions, Drawer, Empty, Input, Pagination, Row, Select, Space, Tabs, Tag, theme, Timeline, Typography, } from 'antd' import { useQuery } from '@tanstack/react-query' import { useEffect, useMemo, useState } from 'react' import type { CSSProperties } from 'react' import { fetchAdminSemiAutoTaskHistory } from '@/services/admin/semi-auto' import type { SemiAutoTaskHistoryEvent, SemiAutoTraceItem } from '@/types/admin/semi-auto' import { formatAdminDateTime } from '@/utils/admin-time' import { describeSemiAutoTraceAction } from '@/utils/semi-auto-trace-action' import { useIsMobile } from '@/utils/use-is-mobile' const { Text } = Typography /** 非状态类事件:名称直接写清楚「谁做了什么」。 */ const EVENT_TITLES: Record = { task_created: '创建履约任务', customer_link_opened: '客户打开授权页', customer_profile_saved: '客户提交游戏角色', login_started: '客户开始登录', task_claimed: '客服领取任务', customer_reauth_requested: '客服重新发起授权', browser_session_opened: '客服生成浏览器登录票据', credential_session_restored: '使用已保存 CK 恢复登录', credential_invalidated: '平台 CK 已失效', task_completed: '客服完成履约', task_cancelled: '任务已取消', } /** 状态变更事件:按「变更后的状态」命名,读起来就是一句操作结果。 */ const LOGIN_EVENT_TITLES: Record = { waiting_code: '等待客户输入验证码', qr_pending: '生成登录二维码', scanned: '客户已扫码,等待确认', authenticated: '客户完成平台登录', failed: '平台登录失败', expired: '登录二维码已过期', } const BINDING_EVENT_TITLES: Record = { not_started: '绑定状态重置', qr_pending: '生成绑定二维码', scanned: '客户已扫码绑定', role_ready: '平台识别到游戏角色', confirmed: '客户确认绑定角色', failed: '角色绑定失败', expired: '绑定二维码已过期', } const CHANNEL_LABELS: Record = { customer_api: '客户接口', admin_api: '运营接口', huya_wup: '虎牙 WUP', huya_http: '虎牙 HTTP', external_connector: '外部 Connector', } const CHANNEL_COLORS: Record = { customer_api: 'blue', admin_api: 'cyan', huya_wup: 'purple', huya_http: 'geekblue', external_connector: 'gold', } const CHANNEL_OPTIONS = Object.keys(CHANNEL_LABELS).map((value) => ({ value, label: CHANNEL_LABELS[value], })) const ACTOR_LABELS: Record = { system: '系统', customer: '客户', admin: '客服', connector: '平台连接器', } const ACTOR_COLORS: Record = { system: 'default', customer: 'blue', admin: 'cyan', connector: 'purple', } const ACTOR_OPTIONS = Object.keys(ACTOR_LABELS).map((value) => ({ value, label: ACTOR_LABELS[value], })) const LOGIN_STATUS_LABELS: Record = { waiting_login: '等待登录', qr_pending: '待扫码', scanned: '已扫码', authenticated: '已登录', failed: '登录失败', expired: '登录过期', } const BINDING_STATUS_LABELS: Record = { not_started: '未开始', qr_pending: '待扫码', scanned: '已扫码', role_ready: '待确认角色', confirmed: '已确认', failed: '绑定失败', expired: '已过期', } const EXECUTION_STATUS_LABELS: Record = { waiting_customer: '等待客户', waiting_operator: '等待客服', processing: '处理中', completed: '已完成', failed: '失败', cancelled: '已取消', } /** antd 6 的 Timeline 预设色会被主题色覆盖,这里用固定色值保证语义色稳定。 */ const TIMELINE_COLORS = { gray: '#8c8c8c', green: '#52c41a', blue: '#1677ff', gold: '#faad14', cyan: '#13c2c2', red: '#ff4d4f', } type SemiAutoTaskHistoryDrawerProps = { open: boolean taskId: number | null onClose: () => void } export default function SemiAutoTaskHistoryDrawer({ open, taskId, onClose, }: SemiAutoTaskHistoryDrawerProps) { const { message } = App.useApp() const isMobile = useIsMobile() const { token } = theme.useToken() const [tab, setTab] = useState('events') const [eventActor, setEventActor] = useState('') const [onlyError, setOnlyError] = useState(false) const [channel, setChannel] = useState('') const [status, setStatus] = useState('') const [keywordDraft, setKeywordDraft] = useState('') const [keyword, setKeyword] = useState('') const [requestId, setRequestId] = useState('') const [page, setPage] = useState(1) const [pageSize, setPageSize] = useState(50) useEffect(() => { if (!open) return setTab('events') setEventActor('') setOnlyError(false) setChannel('') setStatus('') setKeywordDraft('') setKeyword('') setRequestId('') setPage(1) setPageSize(50) }, [open, taskId]) const params = useMemo( () => ({ page, pageSize, channel: channel || undefined, status: status || undefined, keyword: keyword.trim() || undefined, requestId: requestId || undefined, }), [channel, keyword, page, pageSize, requestId, status], ) const historyQuery = useQuery({ queryKey: ['admin-semi-auto-task-history', taskId, params], queryFn: () => fetchAdminSemiAutoTaskHistory(Number(taskId), params), enabled: open && Boolean(taskId), refetchInterval: open && Boolean(taskId) ? 8000 : false, }) const history = historyQuery.data?.data const events = history?.events || [] const traces = history?.traces const traceItems = traces?.items || [] const visibleEvents = events.filter((event) => { if (eventActor && event.actorType !== eventActor) return false if (onlyError && !eventIsFailure(event)) return false return true }) const preStyle: CSSProperties = { margin: '6px 0 0', padding: 10, background: token.colorFillQuaternary, border: `1px solid ${token.colorBorderSecondary}`, borderRadius: token.borderRadius, color: token.colorText, fontSize: 12, lineHeight: 1.55, maxHeight: 280, overflow: 'auto', whiteSpace: 'pre-wrap', wordBreak: 'break-all', } const copyText = (text: string, label: string) => { if (!text) { message.warning(`${label}为空`) return } void navigator.clipboard .writeText(text) .then(() => message.success(`${label}已复制`)) .catch(() => message.error('复制失败,请手动选择文本复制')) } const eventsPanel = ( {history?.task ? ( {LOGIN_STATUS_LABELS[history.task.loginStatus] || history.task.loginStatus} ), }, { key: 'binding', label: '绑定状态', children: ( {BINDING_STATUS_LABELS[history.task.bindingStatus] || history.task.bindingStatus} ), }, { key: 'execution', label: '执行状态', children: ( {EXECUTION_STATUS_LABELS[history.task.executionStatus] || history.task.executionStatus} ), }, { key: 'createdAt', label: '创建时间', children: formatAdminDateTime(history.task.createdAt), }, { key: 'completedAt', label: '完成时间', children: history.task.completedAt ? formatAdminDateTime(history.task.completedAt) : '—', }, { key: 'eventTotal', label: '事件数量', children: `${history.eventTotal} 条`, }, ]} /> ) : null} { setChannel(value || '') setPage(1) }} options={CHANNEL_OPTIONS} style={isMobile ? { flex: '1 1 150px', minWidth: 120 } : { width: 160 }} />