import { useState, useRef, useEffect } from "react"; import { X, Image as ImageIcon, Check } from "lucide-react"; interface CoverFrameSelectorProps { isOpen: boolean; onClose: () => void; videoFile: File | string | null; onSave: (file: File) => void; } export default function CoverFrameSelector({ isOpen, onClose, videoFile, onSave }: CoverFrameSelectorProps) { const videoRef = useRef(null); const canvasRef = useRef(null); const [currentTime, setCurrentTime] = useState(0); const [duration, setDuration] = useState(0); // Reset when opening with a new file useEffect(() => { let objectUrl: string | null = null; if (isOpen && videoFile && videoRef.current) { if (typeof videoFile === "string") { videoRef.current.src = videoFile; // If it's a remote URL, we must set crossOrigin to anonymous to allow canvas capture videoRef.current.crossOrigin = "anonymous"; } else { objectUrl = URL.createObjectURL(videoFile); videoRef.current.src = objectUrl; videoRef.current.removeAttribute("crossorigin"); } videoRef.current.currentTime = 0; } return () => { if (objectUrl) URL.revokeObjectURL(objectUrl); }; }, [isOpen, videoFile]); const handleTimeChange = (e: React.ChangeEvent) => { const time = parseFloat(e.target.value); setCurrentTime(time); if (videoRef.current) { videoRef.current.currentTime = time; } }; const handleLoadedMetadata = () => { if (videoRef.current) { setDuration(videoRef.current.duration); } }; const handleSaveFrame = () => { if (!videoRef.current || !canvasRef.current) return; const video = videoRef.current; const canvas = canvasRef.current; // Set canvas to match video dimensions canvas.width = video.videoWidth; canvas.height = video.videoHeight; // Draw current frame const ctx = canvas.getContext("2d"); ctx?.drawImage(video, 0, 0, canvas.width, canvas.height); // Convert to Blob/File canvas.toBlob((blob) => { if (blob) { const file = new File([blob], "cover_frame.jpg", { type: "image/jpeg" }); onSave(file); onClose(); } }, "image/jpeg", 0.95); }; const handleCustomUpload = (e: React.ChangeEvent) => { if (e.target.files && e.target.files[0]) { onSave(e.target.files[0]); onClose(); } }; if (!isOpen || !videoFile) return null; return (
{/* Header */}

Set Cover Frame

{/* Content */}
{/* Video Viewer */}
{/* Scrubber */}
{/* Actions */}
); }