RippleWorkers / src /components /dashboard /CoverFrameSelector.tsx
damndeepesh
Initialize project with LavinMQ integration and Docker support
c7fbc2e
Raw History Blame Contribute Delete
6.78 kB
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<HTMLVideoElement>(null);
const canvasRef = useRef<HTMLCanvasElement>(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<HTMLInputElement>) => {
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<HTMLInputElement>) => {
if (e.target.files && e.target.files[0]) {
onSave(e.target.files[0]);
onClose();
}
};
if (!isOpen || !videoFile) return null;
return (
<div className="fixed inset-0 z-[100] flex items-center justify-center bg-black/80 backdrop-blur-sm p-4">
<div className="bg-white dark:bg-zinc-900 w-full max-w-lg rounded-2xl shadow-2xl overflow-hidden animate-in fade-in zoom-in-95 duration-200">
{/* Header */}
<div className="flex items-center justify-between p-4 border-b border-gray-100 dark:border-zinc-800">
<h3 className="font-bold text-gray-900 dark:text-white">Set Cover Frame</h3>
<button onClick={onClose} className="p-1 rounded-full hover:bg-gray-100 dark:hover:bg-zinc-800 transition-colors">
<X size={20} className="text-gray-500" />
</button>
</div>
{/* Content */}
<div className="p-6">
{/* Video Viewer */}
<div className="relative aspect-[9/16] bg-black rounded-lg overflow-hidden shadow-inner mb-6 mx-auto max-h-[60vh] w-auto">
<video
ref={videoRef}
className="w-full h-full object-contain"
onLoadedMetadata={handleLoadedMetadata}
playsInline
muted // Muted to avoid random audio blasts while seeking
/>
{/* Hidden Canvas for capture */}
<canvas ref={canvasRef} className="hidden" />
</div>
{/* Scrubber */}
<div className="mb-6">
<label className="text-xs font-semibold text-gray-500 uppercase tracking-wider mb-2 block">
Select Frame ({currentTime.toFixed(1)}s / {duration.toFixed(1)}s)
</label>
<input
type="range"
min={0}
max={duration}
step={0.1}
value={currentTime}
onChange={handleTimeChange}
className="w-full accent-green-500 h-2 bg-gray-200 dark:bg-zinc-700 rounded-lg appearance-none cursor-pointer"
/>
</div>
{/* Actions */}
<div className="flex gap-3">
<div className="flex-1">
<input
type="file"
id="custom-cover-upload"
className="hidden"
accept="image/*"
onChange={handleCustomUpload}
/>
<label
htmlFor="custom-cover-upload"
className="flex items-center justify-center gap-2 w-full py-2.5 border border-gray-200 dark:border-zinc-700 rounded-xl font-semibold text-gray-700 dark:text-gray-300 hover:bg-gray-50 dark:hover:bg-zinc-800 transition-all cursor-pointer text-sm"
>
<ImageIcon size={18} />
Upload Image
</label>
</div>
<button
type="button"
onClick={handleSaveFrame}
className="flex-1 flex items-center justify-center gap-2 py-2.5 bg-green-500 hover:bg-green-600 text-white rounded-xl font-semibold shadow-sm transition-all text-sm"
>
<Check size={18} />
Use This Frame
</button>
</div>
</div>
</div>
</div>
);
}