'use client'; // Add this to ensure it's a client component import React, { useState, useRef, useEffect } from 'react'; import { ChevronLeft, ChevronRight, Volume2, VolumeX, Play, Pause } from 'lucide-react'; import { Button } from '@/components/ui/button'; import dynamic from 'next/dynamic'; interface VideoScreenLayoutProps { videos: { id: string; url: string; title: string; }[]; } function VideoScreenLayoutComponent({ videos }: VideoScreenLayoutProps) { const [currentIndex, setCurrentIndex] = useState(0); const [isAnimating, setIsAnimating] = useState(false); const [isMuted, setIsMuted] = useState(true); // 默认静音 const [volume, setVolume] = useState(0.8); // 添加音量状态 const [isPlaying, setIsPlaying] = useState(true); // 播放状态 const containerRef = useRef(null); const videoRefs = useRef<(HTMLVideoElement | null)[]>([]); useEffect(() => { setCurrentIndex(videos.length > 2 ? 1 : 0); }, [videos.length]); // 确保视频refs数组长度与videos数组一致 useEffect(() => { videoRefs.current = videoRefs.current.slice(0, videos.length); }, [videos.length]); // 计算每个面板的样式 const getPanelStyle = (index: number) => { const position = index - currentIndex; const scale = Math.max(0.6, 1 - Math.abs(position) * 0.2); const zIndex = 10 - Math.abs(position); const opacity = Math.max(0.4, 1 - Math.abs(position) * 0.3); let transform = ` perspective(1000px) scale(${scale}) translateX(${position * 100}%) `; // 添加侧面板的 3D 旋转效果 if (position !== 0) { const rotateY = position > 0 ? -15 : 15; transform += ` rotateY(${rotateY}deg)`; } return { transform, zIndex, opacity, }; }; // 切换静音状态 const toggleMute = () => { const currentVideo = videoRefs.current[currentIndex]; if (currentVideo) { currentVideo.muted = !currentVideo.muted; setIsMuted(currentVideo.muted); } }; // 音量控制函数 const handleVolumeChange = (newVolume: number) => { setVolume(newVolume); const currentVideo = videoRefs.current[currentIndex]; if (currentVideo) { currentVideo.volume = newVolume; } }; // 应用音量设置到视频元素 const applyVolumeSettings = (videoElement: HTMLVideoElement) => { if (videoElement) { videoElement.volume = volume; videoElement.muted = isMuted; } }; // 处理切换 const handleSlide = (direction: 'prev' | 'next') => { if (isAnimating) return; setIsAnimating(true); const newIndex = direction === 'next' ? (currentIndex + 1) % videos.length : (currentIndex - 1 + videos.length) % videos.length; setCurrentIndex(newIndex); // 动画结束后重置状态并同步新视频的静音状态 setTimeout(() => { setIsAnimating(false); // 同步新视频的静音状态到UI并应用音量设置 const newVideo = videoRefs.current[newIndex]; if (newVideo) { setIsMuted(newVideo.muted); applyVolumeSettings(newVideo); // 根据当前播放状态控制新视频 if (isPlaying) { newVideo.play().catch(() => { setIsPlaying(false); }); } else { newVideo.pause(); } } }, 500); }; // 音量设置变化时应用到当前视频 useEffect(() => { const currentVideo = videoRefs.current[currentIndex]; if (currentVideo) { applyVolumeSettings(currentVideo); } }, [volume, isMuted, currentIndex]); // 播放状态变化时应用到当前视频 useEffect(() => { const currentVideo = videoRefs.current[currentIndex]; if (currentVideo) { if (isPlaying) { currentVideo.play().catch(() => { // 处理自动播放策略限制 setIsPlaying(false); }); } else { currentVideo.pause(); } } }, [isPlaying, currentIndex]); // 播放/暂停控制 const togglePlay = () => { setIsPlaying(!isPlaying); }; return (
{/* 视频面板容器 */}
{videos.map((video, index) => (
{/* 视频 - Add suppressHydrationWarning to prevent className mismatch warnings */}
))}
{/* 切换按钮 */}
); } // Export as a client-only component to prevent hydration issues export const VideoScreenLayout = dynamic(() => Promise.resolve(VideoScreenLayoutComponent), { ssr: false });