/*
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 React, { useState, useEffect, useRef } from 'react';
import { useTranslation } from 'react-i18next';
import VideoInputPanel from './Video/VideoInputPanel';
import VideoPreviewPanel from './Video/VideoPreviewPanel';
import VideoTaskQueue from './Video/VideoTaskQueue';
import { useVideoGeneration } from '../../../../hooks/video/useVideoGeneration';
import { showInfo } from '../../../../helpers';
const VideoGenerationTab = () => {
const { t } = useTranslation();
const [leftPanelCollapsed, setLeftPanelCollapsed] = useState(false);
const [taskQueueVisible, setTaskQueueVisible] = useState(false);
const [windowWidth, setWindowWidth] = useState(window.innerWidth);
const [selectedTaskForPlay, setSelectedTaskForPlay] = useState(null);
// 输入面板容器:再生成回填后滚动到面板顶部
const inputPanelRef = useRef(null);
const inputScrollRef = useRef(null);
// 监听窗口大小变化
useEffect(() => {
const handleResize = () => {
setWindowWidth(window.innerWidth);
};
window.addEventListener('resize', handleResize);
return () => window.removeEventListener('resize', handleResize);
}, []);
const creation = useVideoGeneration();
const {
// 表单数据
formData,
// 模型相关
selectedModel,
// 生成相关
generating,
currentTask,
handleGenerate,
handleCancel,
submitBlock,
submitting,
quote,
quoteLoading,
quoteFailed,
estSeconds,
// 任务列表
tasks,
refreshTasks,
deleteTask,
getRegenerationTask,
prefillFromTask,
downloadTask,
getTaskVideoUrl,
} = creation;
// 处理任务列表的播放按钮
const handlePlayTask = async (task) => {
// 获取最新的预签名URL
const videoUrl = await getTaskVideoUrl(task.task_id);
if (videoUrl) {
// 更新 task 对象中的 video_url
const updatedTask = { ...task };
// 解析或初始化 data 对象
let data = {};
if (updatedTask.data) {
try {
data =
typeof updatedTask.data === 'string'
? JSON.parse(updatedTask.data)
: { ...updatedTask.data };
} catch (e) {
console.error('Failed to parse task data:', e);
}
}
// 设置 video_url
data.video_url = videoUrl;
updatedTask.data = data;
setSelectedTaskForPlay(updatedTask);
} else {
setSelectedTaskForPlay(task);
}
};
// 处理关闭视频播放(清除选中的任务)
const handleClearSelectedTask = () => {
setSelectedTaskForPlay(null);
};
// 处理再生成按钮:用原任务参数回填输入面板,由用户确认后再点击生成
const handleRegenerate = async (task) => {
const detail = await getRegenerationTask(task.task_id);
if (!detail || !prefillFromTask(detail)) return;
setLeftPanelCollapsed(false);
setTaskQueueVisible(false);
showInfo(t('已回填原参数,确认后点击生成'));
// 等面板展开渲染后再滚动
requestAnimationFrame(() => {
inputScrollRef.current?.scrollTo?.({ top: 0, behavior: 'smooth' });
inputPanelRef.current?.scrollIntoView?.({
behavior: 'smooth',
block: 'start',
});
});
};
// 响应式布局配置 - 两列布局(左侧配置区 + 右侧预览区)
const getLayoutConfig = () => {
// 超大屏幕 (>1920px): 25% - 75%
if (windowWidth > 1920) {
return {
leftWidth: '25%',
centerWidth: '75%',
minLeftWidth: '350px',
minCenterWidth: '800px',
direction: 'row',
};
}
// 大屏幕 (1600-1920px): 28% - 72%
if (windowWidth > 1600) {
return {
leftWidth: '28%',
centerWidth: '72%',
minLeftWidth: '320px',
minCenterWidth: '700px',
direction: 'row',
};
}
// 中等屏幕 (1280-1600px): 32% - 68%
if (windowWidth > 1280) {
return {
leftWidth: '32%',
centerWidth: '68%',
minLeftWidth: '300px',
minCenterWidth: '600px',
direction: 'row',
};
}
// 小屏幕 (1024-1280px): 35% - 65%
if (windowWidth > 1024) {
return {
leftWidth: '35%',
centerWidth: '65%',
minLeftWidth: '280px',
minCenterWidth: '500px',
direction: 'row',
};
}
// 超小屏幕 (<1024px): 单列布局
return {
leftWidth: '100%',
centerWidth: '100%',
minLeftWidth: '100%',
minCenterWidth: '100%',
direction: 'column',
};
};
const layoutConfig = getLayoutConfig();
return (
{/* 左侧:输入区域(区域1) */}
setLeftPanelCollapsed(!leftPanelCollapsed)}
collapsed={leftPanelCollapsed}
collapsible={layoutConfig.direction === 'row'}
/>
{/* 右侧:预览+生成区域 */}
768 ? '16px' : '12px',
overflow: 'auto',
}}
>
{/* 底部:任务队列(区域4) - 使用 absolute 定位,相对于父容器 */}
);
};
export default VideoGenerationTab;