"use client"; import { useState, useEffect, useRef, useCallback } from 'react'; import { Loader2, Download } from 'lucide-react'; import { useRouter } from 'next/navigation'; import './style/create-to-video2.css'; import { getScriptEpisodeListNew } from "@/api/script_episode"; import { ChatInputBox } from '@/components/ChatInputBox/ChatInputBox'; import cover_image1 from '@/public/assets/cover_image3.jpg'; import { motion } from 'framer-motion'; import { Tooltip, Button } from 'antd'; import { GlassIconButton } from '@/components/ui/glass-icon-button'; import { downloadVideo, getFirstFrame } from '@/utils/tools'; // ideaText已迁移到ChatInputBox组件中 export default function CreateToVideo2() { const router = useRouter(); const [episodeList, setEpisodeList] = useState([]); const [currentPage, setCurrentPage] = useState(1); const [totalPages, setTotalPages] = useState(1); const [perPage] = useState(12); const [isLoading, setIsLoading] = useState(false); const [hasMore, setHasMore] = useState(true); const [isLoadingMore, setIsLoadingMore] = useState(false); const scrollContainerRef = useRef(null); const [userId, setUserId] = useState(0); const [isLoadingDownloadBtn, setIsLoadingDownloadBtn] = useState(false); // 添加一个 ref 来跟踪当前正在加载的页码 const loadingPageRef = useRef(null); // 在客户端挂载后读取localStorage // 监听滚动事件,实现无限加载 // 修改滚动处理函数,添加节流 const handleScroll = useCallback(() => { if (!scrollContainerRef.current || !hasMore || isLoadingMore || isLoading) return; const { scrollTop, scrollHeight, clientHeight } = scrollContainerRef.current; if (scrollHeight - scrollTop - clientHeight < 100) { // 直接使用 currentPage,不再使用 setCurrentPage 的回调 const nextPage = currentPage + 1; if (nextPage <= totalPages) { getEpisodeList(userId, nextPage, true); } } }, [hasMore, isLoadingMore, isLoading, totalPages, userId, currentPage]); useEffect(() => { if (typeof window !== 'undefined') { const currentUser = JSON.parse(localStorage.getItem('currentUser') || '{}'); console.log('currentUser', currentUser); setUserId(currentUser.id); getEpisodeList(currentUser.id, 1, false); } }, []); // 添加滚动监听 useEffect(() => { const scrollContainer = scrollContainerRef.current; if (scrollContainer) { scrollContainer.addEventListener('scroll', handleScroll); return () => scrollContainer.removeEventListener('scroll', handleScroll); } }, [handleScroll]); // 修改获取剧集列表函数 const getEpisodeList = async (userId: number, page: number = 1, loadMore: boolean = false) => { // 检查是否正在加载该页 if (loadingPageRef.current === page) return; if (isLoading || (isLoadingMore && !loadMore)) return; // 设置当前正在加载的页码 loadingPageRef.current = page; if (loadMore) { setIsLoadingMore(true); } else { setIsLoading(true); } try { const params = { user_id: String(userId), page, per_page: perPage }; const episodeListResponse = await getScriptEpisodeListNew(params); if (episodeListResponse.code === 0) { const { movie_projects, total_pages } = episodeListResponse.data; // 确保数据不重复 if (loadMore) { setEpisodeList(prev => { const newProjects = movie_projects.filter( project => !prev.some(p => p.project_id === project.project_id) ); return [...prev, ...newProjects]; }); } else { setEpisodeList(movie_projects); } setTotalPages(total_pages); setHasMore(page < total_pages); setCurrentPage(page); } } catch (error) { console.error('Failed to fetch episode list:', error); } finally { setIsLoading(false); setIsLoadingMore(false); // 清除当前加载页码 loadingPageRef.current = null; } }; // 视频上传和创建功能已迁移到ChatInputBox组件中 // 所有视频工具相关的函数和配置已迁移到ChatInputBox组件中 const StatusBadge = (status: string) => { return ( {/* 进行中 脉冲小圆点 */} {status === 'pending' && ( <> {/* 状态文字 */} )} {/* 失败 */} {status === 'failed' && ( <> FAILED )} ) } // 创建一个视频引用Map const videoRefs = useRef>(new Map()); const handleMouseEnter = (projectId: string) => { const videoElement = videoRefs.current.get(projectId); if (videoElement) { videoElement.play().catch(() => { console.log('Video autoplay prevented'); }); } }; const handleMouseLeave = (projectId: string) => { const videoElement = videoRefs.current.get(projectId); if (videoElement) { videoElement.pause(); videoElement.currentTime = 0; } }; const setVideoRef = (projectId: string, element: HTMLVideoElement | null) => { if (element) { videoRefs.current.set(projectId, element); } else { videoRefs.current.delete(projectId); } }; const renderProjectCard = (project: any) => { return (
handleMouseEnter(project.project_id)} onMouseLeave={() => handleMouseLeave(project.project_id)} data-alt="project-card" > {/* 视频/图片区域 */}
router.push(`/movies/work-flow?episodeId=${project.project_id}`)}> {(project.final_video_url || project.final_simple_video_url || project.video_urls) ? (
); }; return (
{episodeList.length > 0 && ( /* 优化的剧集网格 */
{episodeList.map(renderProjectCard)}
{/* 加载更多指示器 */} {isLoadingMore && (
)}
)} {episodeList.length === 0 && isLoading && (
)}
{/* 视频工具组件 - 使用独立组件 */} {!isLoading && }
); }