"use client" import { useEffect, useRef, useState } from "react" import Link from "next/link" import { RiCheckboxCircleFill } from "react-icons/ri" import { cn } from "@/lib/utils" import { MediaDisplayLayout, VideoInfo } from "@/types" import { formatDuration } from "@/lib/formatDuration" import { formatTimeAgo } from "@/lib/formatTimeAgo" import { isCertifiedUser } from "@/app/certification" import { transparentImage } from "@/lib/transparentImage" import { DefaultAvatar } from "../default-avatar" export function TrackCard({ media, className = "", layout = "grid", onSelect, index }: { media: VideoInfo className?: string layout?: MediaDisplayLayout onSelect?: (media: VideoInfo) => void index: number }) { const ref = useRef(null) const [duration, setDuration] = useState(0) const [channelThumbnail, setChannelThumbnail] = useState(media.channel.thumbnail) const [mediaThumbnail, setMediaThumbnail] = useState( `https://huggingface.co./datasets/jbilcke-hf/ai-tube-index/resolve/main/videos/${media.id}.webp` ) const [mediaThumbnailReady, setMediaThumbnailReady] = useState(false) const [shouldLoadMedia, setShouldLoadMedia] = useState(false) const isTable = layout === "table" const isCompact = layout === "vertical" 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?.(media) } const handleBadChannelThumbnail = () => { try { if (channelThumbnail) { setChannelThumbnail("") } } catch (err) { } } useEffect(() => { setTimeout(() => { setShouldLoadMedia(true) }, index * 1500) }, [index]) return (
{/* THUMBNAIL BLOCK */}
{!isTable && mediaThumbnailReady && shouldLoadMedia ?
{isTable ? null :
{formatDuration(duration)}
}
{/* TEXT BLOCK */}
{ isTable || isCompact ? null : channelThumbnail ?
: }

{media.label}

{media.channel.label}
{isCertifiedUser(media.channel.datasetUser) ?
: null}
{isTable ? null :
{media.numberOfViews} views
ยท
{formatTimeAgo(media.updatedAt)}
}
) }