Files
monkey-ai-web/src/pages/ExperienceCenter/components/AICreation/Video/VideoTaskQueue.jsx
T
zhanjunhao 34e318454d fix(ui): 统一三中心表格交互并优化企业及配套页面
- 公共表格:统一分组标签、状态开关和危险操作确认,支持防重复提交和失败重试,修复浮层触发组件的 ref 转发。
- 控制台:统一用户和渠道编辑抽屉,修正分组回填与提交格式,对齐系统设置操作区和预约列表。
- 企业中心:调整技能中心页签和窄屏布局,优化质量监控提示栏、效能分析及成本对账,统一消耗明细和审计日志筛选。
- 用户中心及公共页面:优化密钥和消费记录展示,对齐模型超市筛选,统一公开与内嵌客户端下载页。
- 同步多语言文案、组件规范与移动端视口要求。
2026-11-24 15:00:30 +08:00

409 lines
14 KiB
React
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import { confirmTableAction } from '@/components/common/table/confirmTableAction';
import {
Download as ActionDownload,
RefreshCw as ActionRefreshCw,
Trash2 as ActionTrash2,
AlertCircle as ActionAlertCircle,
ChevronDown as ActionChevronDown,
} from '@/components/common/icons/ActionIcons';
import React, { useState, useId } from 'react';
import { Button as AntButton } from 'antd';
import {
Table,
Button,
Space,
Tag,
Progress,
Modal,
Badge,
Pagination,
} from '@/components/ui';
import { ListVideo } 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,
page = 1,
pageSize = 10,
total = 0,
loading = false,
onPageChange,
onPageSizeChange,
onRefresh,
onDelete,
onDownload,
onPlay, // 新增:播放回调
onRegenerate, // 新增:再生成回调
}) => {
const { t } = useTranslation();
// 断点判定必须按「宽度」。本组件此前 20 多处响应式判断写的是 !isCompact,
// 判的是窗口高度——390x844 的手机上 844 > 768 成立,于是整个队列走桌面分支:
// 8 列合计 1190px 塞进 ~340px 容器、按钮全带文字,实际完全用不了。
const isCompact = useIsMobile();
const [selectedTasks, setSelectedTasks] = useState([]);
// 服务端分页:tasks 为当前页数据,翻页 / 改每页条数时由 useVideoGeneration 重新请求
// GET /api/video/tasks?p=&page_size=。分页器放在表格滚动区之外(与密钥管理一致),避免抖动
const queueId = useId();
// 勾选只针对当前页,翻页或数据刷新后剔除已不在当前页的任务
const pageTaskIds = new Set(tasks.map((task) => task.task_id));
const visibleSelected = selectedTasks.filter((id) => pageTaskIds.has(id));
if (visibleSelected.length !== selectedTasks.length) {
setSelectedTasks(visibleSelected);
}
// 状态标签映射
const getStatusTag = (status) => {
// 统一转换为小写进行比较
const normalizedStatus = normalizeVideoTaskStatus(status);
// 状态统一为“文字 + 边框”标签,不带图标;居中由列 align 与 .video-queue-status 控制
const statusMap = {
queued: { color: 'blue', text: t('排队中') },
in_progress: { color: 'cyan', text: t('生成中') },
success: { color: 'green', text: t('已完成') },
completed: { color: 'green', text: t('已完成') },
failure: { color: 'red', text: t('失败') },
failed: { color: 'red', text: t('失败') },
canceled: { color: 'grey', text: t('已取消') },
};
const config = statusMap[normalizedStatus] || {
color: 'grey',
text: status,
};
return (
<Tag color={config.color} className='video-queue-status'>
{config.text}
</Tag>
);
};
// 批量下载 - 只下载已完成的任务
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: 200,
// 完整展示任务 ID(纯文本,不截断)
render: (text) => text || '-',
},
{
title: t('模式'),
dataIndex: 'action',
width: !isCompact ? 100 : 80,
render: (text) => {
// action 字段存储的是 text2video 或 image2video
const isText2Video = text === 'text2video' || text === 'TEXT2VIDEO';
return (
<Tag size={!isCompact ? 'default' : 'small'}>
{isText2Video ? t('文生视频') : t('图生视频')}
</Tag>
);
},
},
{
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,
align: 'center',
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 <Progress percent={percentValue} showInfo size='small' />;
},
},
{
title: t('创建时间'),
dataIndex: 'created_at',
width: !isCompact ? 160 : 140,
render: (timestamp) => new Date(timestamp * 1000).toLocaleString('zh-CN'),
},
{
title: t('操作'),
dataIndex: 'operate',
width: 200,
fixed: 'right',
render: (text, record) => {
const normalizedStatus = normalizeVideoTaskStatus(record.status);
return (
// 操作列与密钥管理列表一致:文字链接按钮(主色 / 删除为危险色),不带图标与描边
<div className='video-queue-actions'>
{/* 播放和下载按钮:仅根据状态显示,视频URL在点击时才获取 */}
{normalizedStatus === 'success' && (
<>
<AntButton
type='link'
size='small'
className='video-queue-link'
onClick={() => onPlay && onPlay(record)}
>
{t('播放')}
</AntButton>
<AntButton
type='link'
size='small'
className='video-queue-link'
onClick={() => onDownload(record)}
>
{t('下载')}
</AntButton>
</>
)}
{/* 再生成按钮 - 对所有已完成或失败的任务显示 */}
{(normalizedStatus === 'success' ||
normalizedStatus === 'failure') &&
canRegenerateVideoTask(record) && (
<AntButton
type='link'
size='small'
className='video-queue-link'
onClick={() => onRegenerate && onRegenerate(record)}
>
{t('再生成')}
</AntButton>
)}
<AntButton
type='link'
size='small'
danger
className='video-queue-link'
onClick={() => {
confirmTableAction({
title: t('确定删除此任务?'),
content: t('删除后将无法恢复'),
icon: (
<ActionAlertCircle size={24} style={{ color: '#f59e0b' }} />
),
okText: t('确定'),
cancelText: t('取消'),
okType: 'danger',
focusable: { autoFocusButton: 'cancel' },
onOk: () => onDelete(record.task_id),
centered: true,
});
}}
>
{t('删除')}
</AntButton>
</div>
);
},
},
];
// 统计信息:总数取服务端 total,分状态计数为当前页
const taskStats = {
total,
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 (
// 外层 section 在桌面端只占折叠条高度;展开内容由 .video-queue-panel 自底向上浮层覆盖,
// 不挤压上方的参数 / 预览区域(样式见 VideoWorkspace.css)。Esc 收起并把焦点还给切换按钮。
<section
className='video-queue'
data-expanded={visible || undefined}
onKeyDown={(event) => {
if (event.key === 'Escape' && visible) {
event.stopPropagation();
setVisible(false);
event.currentTarget.querySelector('.video-queue-toggle')?.focus();
}
}}
>
<div className='video-queue-panel'>
<h2 className='video-queue-heading'>
<button
type='button'
className='video-queue-toggle'
aria-expanded={visible}
aria-controls={queueId}
onClick={() => setVisible(!visible)}
>
<ListVideo size={19} aria-hidden='true' />
<span>{t('任务队列')}</span>
<span className='video-queue-count'>{taskStats.total}</span>
{taskStats.in_progress > 0 && (
<Badge count={taskStats.in_progress} type='danger' />
)}
<ActionChevronDown
size={16}
className='video-queue-chevron'
aria-hidden='true'
/>
</button>
</h2>
<div id={queueId} className='video-queue-content' hidden={!visible}>
<div className='video-queue-toolbar'>
<Space wrap>
<Button
size={!isCompact ? 'default' : 'small'}
icon={<ActionRefreshCw size={14} />}
onClick={onRefresh}
>
{t('刷新')}
</Button>
{selectedTasks.length > 0 && (
<>
<Button
size={!isCompact ? 'default' : 'small'}
icon={<ActionDownload size={14} />}
onClick={handleBatchDownload}
>
{t('批量下载')} ({selectedTasks.length})
</Button>
<Button
size={!isCompact ? 'default' : 'small'}
type='danger'
icon={<ActionTrash2 size={14} />}
onClick={() => {
Modal.warning({
title: t('确定删除选中的任务?'),
content: t(
'将删除 {{count}} 个任务,删除后将无法恢复',
{
count: selectedTasks.length,
},
),
icon: (
<ActionAlertCircle
size={24}
style={{ color: '#f59e0b' }}
/>
),
okText: t('确定删除'),
cancelText: t('取消'),
onOk: handleBatchDelete,
centered: true,
okButtonProps: { type: 'danger' },
});
}}
>
{t('批量删除')} ({selectedTasks.length})
</Button>
</>
)}
</Space>
{/* 统计信息 */}
<Space wrap>
<Tag size={!isCompact ? 'default' : 'small'} color='blue'>
{t('排队')}: {taskStats.queued}
</Tag>
<Tag size={!isCompact ? 'default' : 'small'} color='cyan'>
{t('生成中')}: {taskStats.in_progress}
</Tag>
<Tag size={!isCompact ? 'default' : 'small'} color='green'>
{t('完成')}: {taskStats.completed}
</Tag>
<Tag size={!isCompact ? 'default' : 'small'} color='red'>
{t('失败')}: {taskStats.failed}
</Tag>
</Space>
</div>
{/* 任务表格 */}
<div className='video-queue-table app-scrollbar'>
<Table
className='video-queue-grid'
columns={columns}
dataSource={tasks}
loading={loading}
rowKey='task_id'
tableLayout='fixed'
scroll={{
x: columns.reduce((sum, column) => sum + column.width, 0),
}}
pagination={false}
rowSelection={{
selectedRowKeys: selectedTasks,
onChange: setSelectedTasks,
getCheckboxProps: (record) => ({
disabled: record.status === 'in_progress',
}),
}}
size='middle'
empty={t('暂无任务')}
/>
</div>
{total > 0 ? (
<Pagination
className='video-queue-pagination'
total={total}
currentPage={page}
pageSize={pageSize}
onPageChange={(next) => onPageChange?.(next)}
onPageSizeChange={(size) => onPageSizeChange?.(size)}
/>
) : null}
</div>
</div>
</section>
);
};
export default VideoTaskQueue;