import { Play as ActionPlay, Download as ActionDownload, RefreshCw as ActionRefreshCw, Trash2 as ActionTrash2, AlertCircle as ActionAlertCircle, } from '@/components/common/icons/ActionIcons'; import React, { useState, useEffect } from 'react'; import { Table, Button, Space, Tag, Progress, Tooltip, Modal, Badge, } from '@/components/ui'; import { ChevronUp, ChevronDown, CheckCircle2, AlertCircle, Clock, Loader2, } from 'lucide-react'; import { useTranslation } from 'react-i18next'; import { useIsMobile } from '../../../../../hooks/common/useIsMobile'; import { canRegenerateVideoTask, normalizeVideoTaskStatus, } from '../../../../../helpers/videoTask'; const VideoTaskQueue = ({ visible, setVisible, tasks, onRefresh, onDelete, onDownload, onPlay, // 新增:播放回调 onRegenerate, // 新增:再生成回调 }) => { const { t } = useTranslation(); // 断点判定必须按「宽度」。本组件此前 20 多处响应式判断写的是 !isCompact, // 判的是窗口高度——390x844 的手机上 844 > 768 成立,于是整个队列走桌面分支: // 8 列合计 1190px 塞进 ~340px 容器、按钮全带文字,实际完全用不了。 const isCompact = useIsMobile(); const [selectedTasks, setSelectedTasks] = useState([]); const [windowHeight, setWindowHeight] = useState(window.innerHeight); // 监听窗口高度变化 useEffect(() => { const handleResize = () => { setWindowHeight(window.innerHeight); }; window.addEventListener('resize', handleResize); return () => window.removeEventListener('resize', handleResize); }, []); // 计算任务队列高度:不超过屏幕高度的20%(五分之一) // 同时设置最小高度150px,最大高度300px const getQueueHeight = () => { const maxHeightPercent = windowHeight * 0.2; // 屏幕高度的20% // 手机端工具栏本身会换行占掉 ~100px,再按 20%(844px 屏上仅 169px)会让表格只剩 // 不到 70px,连表头带分页都放不下。窄屏改用更宽松的下限。 const minHeight = isCompact ? 300 : 150; const maxHeight = isCompact ? 420 : 300; // 在最小和最大值之间取适当的高度 return Math.max(minHeight, Math.min(maxHeightPercent, maxHeight)); }; const queueHeight = getQueueHeight(); // 状态标签映射 const getStatusTag = (status) => { // 统一转换为小写进行比较 const normalizedStatus = normalizeVideoTaskStatus(status); const statusMap = { queued: { color: 'blue', text: t('排队中'), icon: }, in_progress: { color: 'cyan', text: t('生成中'), icon: , }, success: { color: 'green', text: t('已完成'), icon: , }, completed: { color: 'green', text: t('已完成'), icon: , }, failure: { color: 'red', text: t('失败'), icon: , }, failed: { color: 'red', text: t('失败'), icon: , }, canceled: { color: 'grey', text: t('已取消'), icon: , }, }; const config = statusMap[normalizedStatus] || { color: 'grey', text: status, }; return ( {config.icon} {config.text} ); }; // 批量下载 - 只下载已完成的任务 const handleBatchDownload = () => { selectedTasks.forEach((taskId) => { const task = tasks.find((t) => t.task_id === taskId); if (task) { const normalizedStatus = normalizeVideoTaskStatus(task.status); // 只下载已完成的任务,视频URL会在 onDownload 中获取 if (normalizedStatus === 'success') { onDownload(task); } } }); }; // 批量删除 const handleBatchDelete = () => { selectedTasks.forEach((taskId) => { onDelete(taskId); }); setSelectedTasks([]); }; // 表格列定义 const columns = [ { title: t('任务ID'), dataIndex: 'task_id', width: !isCompact ? 120 : 100, render: (text) => ( {text?.substring(0, 8)}... ), }, { title: t('模式'), dataIndex: 'action', width: !isCompact ? 100 : 80, render: (text) => { // action 字段存储的是 text2video 或 image2video const isText2Video = text === 'text2video' || text === 'TEXT2VIDEO'; return ( {isText2Video ? t('文生视频') : t('图生视频')} ); }, }, { title: t('模型'), dataIndex: 'model', width: !isCompact ? 150 : 120, render: (model) => model || '-', }, { title: t('提示词'), dataIndex: 'prompt', width: !isCompact ? 200 : 150, ellipsis: { showTitle: true }, render: (prompt) => prompt || '-', }, { title: t('状态'), dataIndex: 'status', width: !isCompact ? 120 : 100, render: (status) => getStatusTag(status), }, { title: t('进度'), dataIndex: 'progress', width: !isCompact ? 120 : 100, render: (progress, record) => { const normalizedStatus = normalizeVideoTaskStatus(record.status); if (normalizedStatus === 'success') return '100%'; if (normalizedStatus === 'failure') return '-'; // 去除百分号并转换为数字 const percentValue = typeof progress === 'string' ? parseInt(progress.replace('%', '')) : progress || 0; return ; }, }, { title: t('创建时间'), dataIndex: 'created_at', width: !isCompact ? 160 : 140, render: (timestamp) => new Date(timestamp * 1000).toLocaleString('zh-CN'), }, { title: t('操作'), dataIndex: 'operate', width: !isCompact ? 220 : 180, fixed: 'right', render: (text, record) => { const normalizedStatus = normalizeVideoTaskStatus(record.status); return ( {/* 播放和下载按钮:仅根据状态显示,视频URL在点击时才获取 */} {normalizedStatus === 'success' && ( <> )} {/* 再生成按钮 - 对所有已完成或失败的任务显示 */} {(normalizedStatus === 'success' || normalizedStatus === 'failure') && canRegenerateVideoTask(record) && ( )} ); }, }, ]; // 统计信息 const taskStats = { total: tasks.length, queued: tasks.filter((t) => normalizeVideoTaskStatus(t.status) === 'queued') .length, in_progress: tasks.filter( (t) => normalizeVideoTaskStatus(t.status) === 'in_progress', ).length, completed: tasks.filter((t) => { return normalizeVideoTaskStatus(t.status) === 'success'; }).length, failed: tasks.filter((t) => { return normalizeVideoTaskStatus(t.status) === 'failure'; }).length, }; return ( <> {/* 折叠触发器 */}
setVisible(!visible)} > {visible ? : } {t('任务队列')} ({taskStats.total}) {taskStats.in_progress > 0 && ( )}
{/* 任务队列抽屉 */}
{/* 工具栏 */}
{selectedTasks.length > 0 && ( <> )} {/* 统计信息 */} {t('排队')}: {taskStats.queued} {t('生成中')}: {taskStats.in_progress} {t('完成')}: {taskStats.completed} {t('失败')}: {taskStats.failed}
{/* 任务表格 */}
({ disabled: record.status === 'in_progress', }), }} size='small' empty={t('暂无任务')} /> ); }; export default VideoTaskQueue;