'use client'; // Add this to ensure it's a client component import React, { useState, useRef } from 'react'; import { Edit2, Trash2, Play, Pause, Volume2, VolumeX, Maximize, Minimize } from 'lucide-react'; import { Button } from '@/components/ui/button'; import dynamic from 'next/dynamic'; interface VideoGridLayoutProps { videos: { id: string; url: string; title: string; date?: string; }[]; onEdit?: (id: string) => void; onDelete?: (id: string) => void; } function VideoGridLayoutComponent({ videos, onEdit, onDelete }: VideoGridLayoutProps) { const [hoveredId, setHoveredId] = useState(null); const [isPlaying, setIsPlaying] = useState<{ [key: string]: boolean }>({}); const [isMuted, setIsMuted] = useState<{ [key: string]: boolean }>({}); const [volume, setVolume] = useState<{ [key: string]: number }>({}); const [isFullscreen, setIsFullscreen] = useState<{ [key: string]: boolean }>({}); const videoRefs = useRef<{ [key: string]: HTMLVideoElement | null }>({}); const handleMouseEnter = (id: string) => { setHoveredId(id); }; const handleMouseLeave = (id: string) => { setHoveredId(null); // 暂停视频并重新静音以便下次预览 const video = videoRefs.current[id]; if (video) { video.pause(); video.muted = true; setIsPlaying(prev => ({ ...prev, [id]: false })); setIsMuted(prev => ({ ...prev, [id]: true })); } }; const togglePlay = (id: string) => { const video = videoRefs.current[id]; if (video) { if (video.paused) { // 在用户主动播放时取消静音 video.muted = isMuted[id] !== false; video.play(); setIsPlaying(prev => ({ ...prev, [id]: true })); } else { video.pause(); setIsPlaying(prev => ({ ...prev, [id]: false })); } } }; const toggleMute = (id: string, event: React.MouseEvent) => { event.stopPropagation(); // 防止触发父元素的点击事件 const video = videoRefs.current[id]; if (video) { video.muted = !video.muted; setIsMuted(prev => ({ ...prev, [id]: video.muted })); } }; const handleVolumeChange = (id: string, newVolume: number) => { const video = videoRefs.current[id]; if (video) { video.volume = newVolume; setVolume(prev => ({ ...prev, [id]: newVolume })); } }; const toggleFullscreen = (id: string, event: React.MouseEvent) => { event.stopPropagation(); const video = videoRefs.current[id]; if (video) { if (!document.fullscreenElement) { // 进入全屏 video.requestFullscreen?.() || (video as any).webkitRequestFullscreen?.() || (video as any).msRequestFullscreen?.(); setIsFullscreen(prev => ({ ...prev, [id]: true })); } else { // 退出全屏 document.exitFullscreen?.() || (document as any).webkitExitFullscreen?.() || (document as any).msExitFullscreen?.(); setIsFullscreen(prev => ({ ...prev, [id]: false })); } } }; return (
{videos.map((video) => (
handleMouseEnter(video.id)} onMouseLeave={() => handleMouseLeave(video.id)} > {/* 视频容器 */}
{/* 视频信息 */}

{video.title}

{video.date && (

{video.date}

)}
))}
); } // Export as a client-only component to prevent hydration issues export const VideoGridLayout = dynamic(() => Promise.resolve(VideoGridLayoutComponent), { ssr: false });