import { Play } from "lucide-react"; import { type Video } from "@shared/schema"; import { useState, useRef, useEffect } from "react"; import Hls from "hls.js"; interface VideoCardProps { video: Video; onClick: (video: Video) => void; className?: string; hideOverlay?: boolean; } function formatDuration(seconds: number): string { const minutes = Math.floor(seconds / 60); const remainingSeconds = seconds % 60; return `${minutes}:${remainingSeconds.toString().padStart(2, '0')}`; } function formatViews(views: number): string { if (views >= 1000000) { return `${(views / 1000000).toFixed(1)}M views`; } else if (views >= 1000) { return `${(views / 1000).toFixed(1)}K views`; } return `${views} views`; } function formatDate(date: Date | string): string { const now = new Date(); const createdDate = typeof date === 'string' ? new Date(date) : date; if (!createdDate || isNaN(createdDate.getTime())) { return "Unknown"; } const diffTime = Math.abs(now.getTime() - createdDate.getTime()); const diffDays = Math.floor(diffTime / (1000 * 60 * 60 * 24)); if (diffDays === 0) return "Today"; if (diffDays === 1) return "1 day ago"; if (diffDays < 7) return `${diffDays} days ago`; if (diffDays < 30) return `${Math.floor(diffDays / 7)} week${Math.floor(diffDays / 7) > 1 ? 's' : ''} ago`; return `${Math.floor(diffDays / 30)} month${Math.floor(diffDays / 30) > 1 ? 's' : ''} ago`; } export default function VideoCard({ video, onClick, className = "", hideOverlay = false }: VideoCardProps) { const [isHovered, setIsHovered] = useState(false); const [showPreview, setShowPreview] = useState(false); const hoverTimeoutRef = useRef(); const videoRef = useRef(null); const hlsRef = useRef(null); const [currentTime, setCurrentTime] = useState(0); const [duration, setDuration] = useState(0); // Delay preview start to avoid loading on quick mouse passes useEffect(() => { if (isHovered) { // Shorter delay on mobile for better touch experience const isMobile = window.innerWidth < 768; const delay = isMobile ? 500 : 800; // 500ms on mobile, 800ms on desktop hoverTimeoutRef.current = setTimeout(() => { setShowPreview(true); }, delay); } else { if (hoverTimeoutRef.current) { clearTimeout(hoverTimeoutRef.current); } setShowPreview(false); // Clean up HLS when not hovering if (hlsRef.current) { hlsRef.current.destroy(); hlsRef.current = null; } } return () => { if (hoverTimeoutRef.current) { clearTimeout(hoverTimeoutRef.current); } if (hlsRef.current) { hlsRef.current.destroy(); } }; }, [isHovered]); // Initialize HLS when preview shows useEffect(() => { if (showPreview && videoRef.current && video.videoUrl.includes('.m3u8')) { const videoElement = videoRef.current; if (Hls.isSupported()) { const hls = new Hls({ startLevel: 0, // Start with lowest quality capLevelToPlayerSize: true, maxBufferLength: 5, // Minimal buffering }); hls.loadSource(video.videoUrl); hls.attachMedia(videoElement); hlsRef.current = hls; hls.on(Hls.Events.MANIFEST_PARSED, () => { videoElement.play().catch(e => console.log('Preview autoplay failed:', e)); }); hls.on(Hls.Events.MEDIA_ATTACHED, () => { videoElement.addEventListener('loadedmetadata', () => { setDuration(videoElement.duration); }); }); } else if (videoElement.canPlayType('application/vnd.apple.mpegurl')) { // Safari native HLS support videoElement.src = video.videoUrl; videoElement.addEventListener('loadedmetadata', () => { setDuration(videoElement.duration); }); videoElement.play().catch(e => console.log('Preview autoplay failed:', e)); } } }, [showPreview, video.videoUrl]); return (
setIsHovered(true)} onMouseLeave={() => setIsHovered(false)} onTouchStart={() => setIsHovered(true)} onTouchEnd={() => setIsHovered(false)} > {/* Video preview container */}
onClick?.(video)} > {/* Static thumbnail - always visible */} {video.title} { const target = e.target as HTMLImageElement; console.log('Thumbnail failed to load:', target.src); // Show placeholder immediately instead of trying multiple URLs target.style.display = 'none'; if (target.parentElement && !target.parentElement.querySelector('.thumbnail-placeholder')) { target.parentElement.style.background = 'linear-gradient(135deg, #1f2937, #374151)'; const placeholder = document.createElement('div'); placeholder.className = 'thumbnail-placeholder absolute inset-0 flex flex-col items-center justify-center text-white'; placeholder.innerHTML = '
🎬
Video
'; target.parentElement.appendChild(placeholder); } }} /> {/* Video preview - only load when hovering */} {showPreview && (
)} {/* Title overlay - only show when not playing preview and hideOverlay is false */} {!showPreview && !hideOverlay && (
{video.title}
{formatViews(video.views)} • {formatDate(video.createdAt)}
)}
); }