"use client" import React, { useRef, useEffect, useCallback } from "react"; import "./style/work-flow.css"; import { Skeleton } from "@/components/ui/skeleton"; import { EditModal } from "@/components/ui/edit-modal"; import { TaskInfo } from "./work-flow/task-info"; import { MediaViewer } from "./work-flow/media-viewer"; import { ThumbnailGrid } from "./work-flow/thumbnail-grid"; import { useWorkflowData } from "./work-flow/use-workflow-data"; import { usePlaybackControls } from "./work-flow/use-playback-controls"; import { AlertCircle, RefreshCw, Pause, Play, ChevronLast, ChevronsLeft, Bot, BriefcaseBusiness, Scissors } from "lucide-react"; import { motion } from "framer-motion"; import { GlassIconButton } from '@/components/ui/glass-icon-button'; import { SaveEditUseCase } from "@/app/service/usecase/SaveEditUseCase"; import { useSearchParams } from "next/navigation"; import SmartChatBox from "@/components/SmartChatBox/SmartChatBox"; import { Drawer, Tooltip, notification } from 'antd'; import { showEditingNotification } from "@/components/pages/work-flow/editing-notification"; import { AIEditingIframeButton } from './work-flow/ai-editing-iframe'; const WorkFlow = React.memo(function WorkFlow() { useEffect(() => { console.log("init-WorkFlow"); return () => { console.log("unmount-WorkFlow"); // 销毁编辑通知 if (editingNotificationKey.current) { notification.destroy(editingNotificationKey.current); } }; }, []); const containerRef = useRef(null); const [isEditModalOpen, setIsEditModalOpen] = React.useState(false); const [activeEditTab, setActiveEditTab] = React.useState('1'); const [isSmartChatBoxOpen, setIsSmartChatBoxOpen] = React.useState(true); const [previewVideoUrl, setPreviewVideoUrl] = React.useState(null); const [previewVideoId, setPreviewVideoId] = React.useState(null); const [isFocusChatInput, setIsFocusChatInput] = React.useState(false); const [aiEditingInProgress, setAiEditingInProgress] = React.useState(false); const [isHovered, setIsHovered] = React.useState(false); const [aiEditingResult, setAiEditingResult] = React.useState(null); const aiEditingButtonRef = useRef<{ handleAIEditing: () => Promise }>(null); const [editingStatus, setEditingStatus] = React.useState<'initial' | 'idle' | 'success' | 'error'>('initial'); const searchParams = useSearchParams(); const episodeId = searchParams.get('episodeId') || ''; const userId = JSON.parse(localStorage.getItem("currentUser") || '{}').id || NaN; SaveEditUseCase.setProjectId(episodeId); let editingNotificationKey = useRef(`editing-${Date.now()}`); const [isHandleEdit, setIsHandleEdit] = React.useState(false); // 处理编辑计划生成完成的回调 const handleEditPlanGenerated = useCallback(() => { console.log('✨ 编辑计划生成完成,开始AI剪辑'); setIsHandleEdit(true); setEditingStatus('idle'); aiEditingButtonRef.current?.handleAIEditing(); editingNotificationKey.current = `editing-${Date.now()}`; showEditingNotification({ description: 'Performing intelligent editing...', successDescription: 'Editing successful', timeoutDescription: 'Editing failed, please try again', timeout: 5 * 60 * 1000, key: editingNotificationKey.current, onFail: () => { console.log('Editing failed'); // 清缓存 生成计划 视频重新分析 localStorage.removeItem(`isLoaded_plan_${episodeId}`); setEditingStatus('error'); // 3秒后关闭通知 setTimeout(() => { if (editingNotificationKey.current) { notification.destroy(editingNotificationKey.current); } }, 3000); } }); }, []); // 使用自定义 hooks 管理状态 const { taskObject, scriptData, isLoading, currentSketchIndex, currentLoadingText, setCurrentSketchIndex, isPauseWorkFlow, mode, setIsPauseWorkFlow, setAnyAttribute, applyScript, fallbackToStep, originalText, showGotoCutButton, generateEditPlan, handleRetryVideo } = useWorkflowData({ onEditPlanGenerated: handleEditPlanGenerated }); const { isVideoPlaying, toggleVideoPlay, } = usePlaybackControls(taskObject.videos.data, taskObject.currentStage); useEffect(() => { console.log('changedIndex_work-flow', currentSketchIndex, taskObject); }, [currentSketchIndex, taskObject]); // 监听粗剪是否完成,如果完成 更新 showEditingNotification 的状态 为完成,延时 3s 并关闭 useEffect(() => { if (taskObject.final.url && editingNotificationKey.current && isHandleEdit) { // 更新通知状态为完成 showEditingNotification({ isCompleted: true, description: 'Performing intelligent editing...', successDescription: 'Editing successful', timeoutDescription: 'Editing failed, please try again', timeout: 5 * 60 * 1000, key: editingNotificationKey.current, onComplete: () => { console.log('Editing successful'); localStorage.setItem(`isLoaded_plan_${episodeId}`, 'true'); setEditingStatus('success'); // 3秒后关闭通知 setTimeout(() => { if (editingNotificationKey.current) { notification.destroy(editingNotificationKey.current); } }, 3000); }, }); } }, [taskObject.final, isHandleEdit]); const handleEditModalOpen = useCallback((tab: string) => { setActiveEditTab(tab); setIsEditModalOpen(true); }, []); // AI剪辑回调函数 const handleAIEditingComplete = useCallback((finalVideoUrl: string) => { console.log('🎉 AI剪辑完成,最终视频URL:', finalVideoUrl); // 更新任务对象的最终视频状态 setAnyAttribute('final', { url: finalVideoUrl, note: 'ai_edited' }); // 切换到最终视频阶段 setAnyAttribute('currentStage', 'final_video'); setAiEditingInProgress(false); }, [setAnyAttribute]); const handleAIEditingError = useCallback((error: string) => { console.error('❌ AI剪辑失败:', error); // 这里可以显示错误提示 setAiEditingInProgress(false); }, []); // iframe智能剪辑回调函数 const handleIframeAIEditingComplete = useCallback((result: any) => { console.log('🎉 iframe AI剪辑完成,结果:', result);任务 // 保存剪辑结果 setAiEditingResult(result); // 更新任务对象的最终视频状态 setAnyAttribute('final', { url: result.videoUrl, note: 'ai_edited_iframe' }); // 切换到最终视频阶段 setAnyAttribute('currentStage', 'final_video'); setAiEditingInProgress(false); }, [setAnyAttribute]); const handleIframeAIEditingError = useCallback((error: string) => { console.error('❌ iframe AI剪辑失败:', error); setAiEditingInProgress(false); }, []); const handleIframeAIEditingProgress = useCallback((progress: number, message: string) => { console.log(`📊 AI剪辑进度: ${progress}% - ${message}`); setAiEditingInProgress(true); }, []); return (
{isLoading ? ( ) : (
setIsSmartChatBoxOpen(true)} setVideoPreview={(url, id) => { setPreviewVideoUrl(url); setPreviewVideoId(id); }} showGotoCutButton={showGotoCutButton || editingStatus !== 'idle'} onGotoCut={generateEditPlan} isSmartChatBoxOpen={isSmartChatBoxOpen} onRetryVideo={(video_id) => handleRetryVideo(video_id)} />
)}
{taskObject.currentStage !== 'script' && (
)}
{/* AI剪辑按钮 - 当可以跳转剪辑时显示 */} { showGotoCutButton && (
) } {/* 智能对话按钮 */}
setIsSmartChatBoxOpen(true)} className="backdrop-blur-lg" />
{/* 智能对话弹窗 */} setIsSmartChatBoxOpen(false)} > { setPreviewVideoUrl(null); setPreviewVideoId(null); }} aiEditingResult={aiEditingResult} /> { SaveEditUseCase.clearData(); setIsEditModalOpen(false) }} taskObject={taskObject} currentSketchIndex={currentSketchIndex} roles={taskObject.roles.data} setIsPauseWorkFlow={setIsPauseWorkFlow} isPauseWorkFlow={isPauseWorkFlow} fallbackToStep={fallbackToStep} originalText={originalText} />
) }); export default WorkFlow;