"use client" import { useEffect, useRef, useState } from "react" import dynamic from "next/dynamic" import Link from "next/link" import { RiCheckboxCircleFill } from "react-icons/ri" import { cn } from "@/lib/utils" import { VideoInfo } from "@/types" import { formatDuration } from "@/lib/formatDuration" import { formatTimeAgo } from "@/lib/formatTimeAgo" import { isCertifiedUser } from "@/app/certification" import { transparentImage } from "@/lib/transparentImage" const DefaultAvatar = dynamic(() => import("../default-avatar"), { loading: () => null, }) export function VideoCard({ video, className = "", layout = "normal", onSelect, index }: { video: VideoInfo className?: string layout?: "normal" | "compact" onSelect?: (video: VideoInfo) => void index: number }) { const ref = useRef(null) const [duration, setDuration] = useState(0) const [channelThumbnail, setChannelThumbnail] = useState(video.channel.thumbnail) const [videoThumbnail, setVideoThumbnail] = useState( `https://huggingface.co/datasets/jbilcke-hf/ai-tube-index/resolve/main/videos/${video.id}.webp` ) const [videoThumbnailReady, setVideoThumbnailReady] = useState(false) const [shouldLoadVideo, setShouldLoadVideo] = useState(false) const isCompact = layout === "compact" const handlePointerEnter = () => { // ref.current?.load() ref.current?.play() } const handlePointerLeave = () => { ref.current?.pause() // ref.current?.load() } const handleLoad = () => { if (ref.current?.readyState) { setDuration(ref.current.duration) } } const handleClick = () => { onSelect?.(video) } const handleBadChannelThumbnail = () => { try { if (channelThumbnail) { setChannelThumbnail("") } } catch (err) { } } useEffect(() => { setTimeout(() => { setShouldLoadVideo(true) }, index * 500) }, [index]) return (
{/* VIDEO BLOCK */}
{videoThumbnailReady && shouldLoadVideo ?
{formatDuration(duration)}
{/* TEXT BLOCK */}
{ isCompact ? null : channelThumbnail ?
: }

{video.label}

{video.channel.label}
{isCertifiedUser(video.channel.datasetUser) ?
: null}
0 views
ยท
{formatTimeAgo(video.updatedAt)}
) }