/*
Copyright (C) 2025 QuantumNous
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as
published by the Free Software Foundation, either version 3 of the
License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see .
For commercial licensing, please contact support@quantumnous.com
*/
import { writeClipboardText } from '../../../../../helpers/clipboard';
import React, { useState, useEffect } from 'react';
import {
Card,
Button,
Progress,
Empty,
Space,
Typography,
Spin,
Tooltip,
} from '@/components/ui';
import {
Play,
Square,
Download,
RotateCcw,
Share2,
Film,
HelpCircle,
X,
Loader2,
} from 'lucide-react';
import { useTranslation } from 'react-i18next';
import { showError, showSuccess } from '../../../../../helpers';
import {
canCancelVideoTask,
canRegenerateVideoTask,
getVideoURLFromDetail,
normalizeVideoTaskStatus,
} from '../../../../../helpers/videoTask';
import './VideoPreviewPanel.css';
const { Title, Text, Paragraph } = Typography;
const VideoPreviewPanel = ({
formData,
generating,
currentTask,
onGenerate,
onCancel,
selectedModel,
selectedTaskForPlay, // 新增:从任务列表选择的任务
onRegenerate, // 新增:再生成回调
onClearSelectedTask, // 新增:清除选中任务的回调(关闭视频播放)
}) => {
const { t } = useTranslation();
const [windowWidth, setWindowWidth] = useState(window.innerWidth);
// 监听窗口大小变化
useEffect(() => {
const handleResize = () => {
setWindowWidth(window.innerWidth);
};
window.addEventListener('resize', handleResize);
return () => window.removeEventListener('resize', handleResize);
}, []);
// 根据屏幕宽度获取响应式配置
const getResponsiveConfig = () => {
if (windowWidth > 1600) {
return {
minHeight: '400px',
padding: '40px',
videoMaxHeight: '600px',
buttonSize: 'large',
titleSize: 4,
iconSize: 64,
progressWidth: 120,
};
}
if (windowWidth > 1280) {
return {
minHeight: '350px',
padding: '32px',
videoMaxHeight: '500px',
buttonSize: 'default',
titleSize: 5,
iconSize: 56,
progressWidth: 100,
};
}
if (windowWidth > 1024) {
return {
minHeight: '300px',
padding: '24px',
videoMaxHeight: '400px',
buttonSize: 'default',
titleSize: 5,
iconSize: 48,
progressWidth: 90,
};
}
// 小屏幕
return {
minHeight: '250px',
padding: '16px',
videoMaxHeight: '350px',
buttonSize: 'small',
titleSize: 6,
iconSize: 40,
progressWidth: 80,
};
};
const config = getResponsiveConfig();
// 处理下载
const handleDownload = () => {
const task = selectedTaskForPlay || currentTask;
const videoUrl = getVideoURLFromDetail(task);
if (videoUrl) {
const link = document.createElement('a');
link.href = videoUrl;
link.download = `video_${task.task_id || 'download'}.mp4`;
document.body.appendChild(link);
link.click();
document.body.removeChild(link);
}
};
// 处理分享
const handleShare = async () => {
const task = selectedTaskForPlay || currentTask;
const videoUrl = getVideoURLFromDetail(task);
if (videoUrl) {
if (navigator.share) {
try {
await navigator.share({
title: t('视频分享'),
text: task.prompt || t('AI生成的视频'),
url: videoUrl,
});
} catch (err) {
// 用户取消分享或分享失败
copyToClipboard(videoUrl);
}
} else {
// 不支持分享API,复制链接
copyToClipboard(videoUrl);
}
}
};
// 复制到剪贴板
const copyToClipboard = (text) => {
writeClipboardText(text)
.then(() => showSuccess(t('链接已复制')))
.catch(() => showError(t('复制失败,请手动复制')));
};
// 处理生成视频(同时关闭当前播放的视频)
const handleGenerate = () => {
// 先清除选中的任务,关闭视频播放窗口
if (onClearSelectedTask) {
onClearSelectedTask();
}
// 然后开始生成
if (onGenerate) {
onGenerate();
}
};
// 处理关闭视频播放窗口
const handleCloseVideo = () => {
if (onClearSelectedTask) {
onClearSelectedTask();
}
};
// 处理重新生成
const handleRegenerate = () => {
const task = selectedTaskForPlay || currentTask;
if (onRegenerate && task) {
onRegenerate(task);
} else {
// 如果没有提供 onRegenerate 回调,则直接调用 onGenerate
onGenerate();
}
};
// 判断按钮是否禁用
const isButtonDisabled =
(!formData.prompt && !formData.selectedTemplate) ||
!selectedModel ||
(typeof selectedModel === 'object' && selectedModel.level_locked) ||
(formData.mode === 'image2video' && !formData.image);
// 渲染顶部操作栏(始终显示)
const renderTopBar = () => {
// 优先显示从任务列表选择的任务
const displayTask = selectedTaskForPlay || currentTask;
const normalizedStatus = normalizeVideoTaskStatus(displayTask?.status);
const isCompleted = normalizedStatus === 'success';
// 检查是否有视频URL
let _hasVideo = false;
if (displayTask) {
const videoUrl = getVideoURLFromDetail(displayTask);
_hasVideo = isCompleted && !!videoUrl;
}
return (
768 ? '16px 20px' : '12px 16px',
display: 'flex',
alignItems: 'center',
gap: '12px',
backgroundColor: 'var(--ui-color-bg-1)',
borderBottom: '1px solid var(--ui-color-border)',
flexShrink: 0,
}}
>
{/* 禁用提示 */}
{isButtonDisabled && !generating && (
{!selectedModel && t('请选择模型')}
{selectedModel &&
typeof selectedModel === 'object' &&
selectedModel.level_locked &&
t('当前等级无权调用此模型')}
{selectedModel &&
!(
typeof selectedModel === 'object' && selectedModel.level_locked
) &&
!formData.prompt &&
!formData.selectedTemplate &&
t('请输入提示词或选择模板')}
{selectedModel &&
!(
typeof selectedModel === 'object' && selectedModel.level_locked
) &&
(formData.prompt || formData.selectedTemplate) &&
formData.mode === 'image2video' &&
!formData.image &&
t('请上传图片')}
)}
{/* 生成中状态提示 */}
{generating && displayTask && (
{displayTask.progress || '0%'}
)}
{t('使用提示')}
• {t('详细的提示词能获得更好的效果')}
• {t('建议使用高质量的首帧图片')}
• {t('视频生成可能需要 1-3 分钟')}
• {t('可在任务队列中查看历史记录')}
}
position='bottomRight'
trigger='hover'
showArrow={true}
>
}
type='tertiary'
size={windowWidth > 768 ? 'default' : 'small'}
/>
);
};
// 渲染预览区域
const renderPreview = () => {
// 优先显示从任务列表选择的任务
const displayTask = selectedTaskForPlay || currentTask;
// 统一转换状态为小写进行比较
const normalizedStatus = normalizeVideoTaskStatus(displayTask?.status);
// 生成完成 - 优先检查,包括从任务列表点击播放的情况
const isCompleted = normalizedStatus === 'success';
const videoUrl = getVideoURLFromDetail(displayTask);
const taskData = displayTask?.request || {};
// 如果是已完成的任务且有视频URL,直接显示(无论是从任务列表还是当前生成)
if (isCompleted && videoUrl) {
return (
{/* 视频播放器容器 */}
768 ? '24px' : '16px',
}}
>
{/* 关闭按钮 */}
}
type='tertiary'
theme='borderless'
size='small'
onClick={handleCloseVideo}
style={{
position: 'absolute',
top: '8px',
right: '8px',
zIndex: 10,
backgroundColor: 'rgba(0, 0, 0, 0.5)',
color: 'white',
borderRadius: '50%',
}}
/>
{/* 视频播放器 */}
{/* 操作按钮 */}
}
onClick={handleDownload}
>
{windowWidth > 768 ? t('下载视频') : t('下载')}
}
onClick={handleShare}
>
{t('分享')}
{canRegenerateVideoTask(displayTask) && (
}
onClick={handleRegenerate}
>
{windowWidth > 768 ? t('再生成') : t('重新')}
)}
{/* 视频信息 */}
768 ? '24px' : '16px',
padding: windowWidth > 768 ? '16px' : '12px',
backgroundColor: 'var(--ui-color-fill-0)',
borderRadius: '8px',
textAlign: 'left',
}}
>
{(taskData.prompt || displayTask.prompt) && (
768 ? '14px' : '12px' }}
>
{t('提示词')}:
768 ? '13px' : '11px',
marginTop: '4px',
}}
>
{taskData.prompt || displayTask.prompt}
)}
{taskData.negative_prompt && (
768 ? '14px' : '12px' }}
>
{t('反向提示词')}:
768 ? '13px' : '11px',
marginTop: '4px',
}}
>
{taskData.negative_prompt}
)}
768 ? '12px' : '10px' }}
>
{t('模型')}:{' '}
{taskData.model ||
displayTask.properties?.upstream_model_name ||
displayTask.properties?.origin_model_name ||
(typeof selectedModel === 'object'
? selectedModel?.model_name
: selectedModel)}{' '}
| {t('时长')}:{' '}
{taskData.duration || displayTask.properties?.duration || '-'}
s | {t('分辨率')}:{' '}
{taskData.resolution ||
displayTask.properties?.resolution ||
'-'}
);
}
// 生成中(只在没有 selectedTaskForPlay 时显示)
if (generating && displayTask && !selectedTaskForPlay) {
return (
768 ? 'large' : 'default'}
indicator={}
/>
{t('视频生成中...')}
768 ? '14px' : '12px' }}
>
{normalizedStatus === 'queued' && t('等待处理')}
{normalizedStatus === 'in_progress' && t('正在生成')}
{/* 进度条 */}
{displayTask.progress !== undefined &&
(() => {
// 解析进度值,处理字符串格式(如 "50%")
let percentValue = 0;
if (typeof displayTask.progress === 'string') {
percentValue =
parseInt(displayTask.progress.replace('%', '')) || 0;
} else if (typeof displayTask.progress === 'number') {
percentValue = displayTask.progress;
}
return (
);
})()}
{/* 任务信息 */}
768 ? '13px' : '11px' }}
>
{t('任务ID')}: {displayTask.task_id}
768 ? '13px' : '11px' }}
>
{t('模型')}:{' '}
{typeof selectedModel === 'object'
? selectedModel?.model_name
: selectedModel}
{displayTask.estimated_time && (
768 ? '13px' : '11px' }}
>
{t('预计时间')}: {displayTask.estimated_time}s
)}
{canCancelVideoTask(displayTask) && (
}
onClick={onCancel}
style={{ marginTop: '32px' }}
>
{t('取消生成')}
)}
);
}
// 生成失败 - 检查 'failed' 或 'failure' 状态
const isFailed =
normalizedStatus === 'failed' || normalizedStatus === 'failure';
if (isFailed) {
return (
{canRegenerateVideoTask(displayTask) && (
}
onClick={handleRegenerate}
style={{ marginTop: '24px' }}
>
{t('重新生成')}
)}
);
}
// 初始状态或图生视频预览
if (formData.mode === 'image2video' && formData.image) {
return (
{t('首帧预览')}

1024 ? '500px' : '350px',
borderRadius: '8px',
}}
/>
);
}
// 默认空状态 - 纯CSS电影感动画背景(无网络资源消耗)
return (
{/* CSS动画背景 - 流动光效(增强版) */}
{/* 动态光晕1 - 紫色 */}
{/* 动态光晕2 - 橙红色 */}
{/* 动态光晕3 - 金色中心 */}
{/* 电影宽银幕效果 - 上下黑边 */}
{/* 中央内容区域 */}
1024 ? '32px' : '24px',
marginBottom: windowWidth > 768 ? '32px' : '24px',
border: '1px solid rgba(255, 255, 255, 0.2)',
boxShadow: '0 8px 32px rgba(0, 0, 0, 0.3)',
transition: 'transform 0.3s ease',
}}
onMouseEnter={(e) => {
e.currentTarget.style.transform = 'scale(1.05)';
}}
onMouseLeave={(e) => {
e.currentTarget.style.transform = 'scale(1)';
}}
>
{t('AI 视频生成')}
768 ? '24px' : '16px',
textAlign: 'center',
fontSize: windowWidth > 768 ? '16px' : '14px',
padding: '0 16px',
color: 'rgba(255, 255, 255, 0.7)',
maxWidth: '500px',
lineHeight: '1.8',
textShadow: '0 2px 8px rgba(0, 0, 0, 0.5)',
}}
>
{formData.mode === 'text2video'
? t('输入提示词,让 AI 为你创造精彩视频')
: t('上传图片并输入提示词,生成动态视频')}
);
};
return (
{/* 始终显示的顶部操作栏 */}
{renderTopBar()}
{/* 内容区域 */}
{renderPreview()}
);
};
export default VideoPreviewPanel;