Files
monkey-ai-web/src/pages/ExperienceCenter/components/AICreation/VideoGenerationTab.jsx
T
zhanjunhao 8a48765a46 chore(repo): 移除重复版权文件头并纳入共享单测
集中保留原版权声明,移除文件头检查和全局测试忽略规则。验证:全量单测、受影响用例复测及静态检查通过。
2026-11-20 11:23:49 +08:00

294 lines
8.7 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 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 (
<div
style={{
height: '100%',
display: 'flex',
flexDirection: 'column',
position: 'relative',
overflow: 'hidden',
}}
>
<div
style={{
flex: 1,
overflow: layoutConfig.direction === 'row' ? 'hidden' : 'auto',
display: 'flex',
flexDirection: layoutConfig.direction === 'row' ? 'row' : 'column',
gap: layoutConfig.direction === 'column' ? '16px' : '0',
position: 'relative',
paddingBottom: '40px', // 为任务队列触发器留出空间
}}
>
{/* 左侧:输入区域(区域1) */}
<div
ref={inputPanelRef}
style={{
width:
layoutConfig.direction === 'row'
? layoutConfig.leftWidth
: '100%',
minWidth: layoutConfig.minLeftWidth,
height: layoutConfig.direction === 'row' ? '100%' : 'auto',
// 折叠态不能再预留 500px:面板收起后只剩一个 fixed 定位的浮动箭头,
// 这 500px 会变成页面顶部一整块空白
minHeight:
layoutConfig.direction === 'column' && !leftPanelCollapsed
? '500px'
: 'auto',
flexShrink: layoutConfig.direction === 'row' ? 0 : 1,
backgroundColor: 'var(--ui-color-bg-1)',
borderRight:
layoutConfig.direction === 'row'
? '1px solid var(--ui-color-border)'
: 'none',
borderBottom:
layoutConfig.direction === 'column'
? '1px solid var(--ui-color-border)'
: 'none',
display: 'flex',
flexDirection: 'column',
}}
>
<div
ref={inputScrollRef}
className='app-scrollbar'
style={{
height: '100%',
overflow: 'auto',
paddingBottom: layoutConfig.direction === 'row' ? '16px' : '16px',
}}
>
<VideoInputPanel
creation={creation}
onCollapse={() => setLeftPanelCollapsed(!leftPanelCollapsed)}
collapsed={leftPanelCollapsed}
collapsible={layoutConfig.direction === 'row'}
/>
</div>
</div>
{/* 右侧:预览+生成区域 */}
<div
style={{
flex: 1,
height: layoutConfig.direction === 'row' ? '100%' : 'auto',
minHeight: layoutConfig.direction === 'column' ? '600px' : 'auto',
minWidth: 0,
}}
>
<div
className='app-scrollbar'
style={{
height: '100%',
backgroundColor: 'var(--ui-color-bg-0)',
padding: windowWidth > 768 ? '16px' : '12px',
overflow: 'auto',
}}
>
<VideoPreviewPanel
formData={formData}
generating={generating}
currentTask={currentTask}
onGenerate={handleGenerate}
onCancel={handleCancel}
selectedModel={selectedModel}
submitBlock={submitBlock}
submitting={submitting}
quote={quote}
quoteLoading={quoteLoading}
quoteFailed={quoteFailed}
estSeconds={estSeconds}
selectedTaskForPlay={selectedTaskForPlay}
onRegenerate={handleRegenerate}
onClearSelectedTask={handleClearSelectedTask}
/>
</div>
</div>
</div>
{/* 底部:任务队列(区域4) - 使用 absolute 定位,相对于父容器 */}
<div
style={{
position: 'absolute',
bottom: 0,
left: 0,
right: 0,
zIndex: 1000,
}}
>
<VideoTaskQueue
visible={taskQueueVisible}
setVisible={setTaskQueueVisible}
tasks={tasks}
onRefresh={refreshTasks}
onDelete={deleteTask}
onDownload={downloadTask}
onPlay={handlePlayTask}
onRegenerate={handleRegenerate}
/>
</div>
</div>
);
};
export default VideoGenerationTab;