"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/general" 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" import { formatLargeNumber } from "@/lib/formatLargeNumber" import { usePlaylist } from "@/lib/usePlaylist" export function TrackCard({ media, className = "", layout = "grid", onSelect, selected, index }: { media: VideoInfo className?: string layout?: MediaDisplayLayout onSelect?: (media: VideoInfo) => void selected?: boolean 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 playlist = usePlaylist() const isTable = layout === "table" const isMicro = layout === "micro" 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 handleBadChannelThumbnail = () => { try { if (channelThumbnail) { setChannelThumbnail("") } } catch (err) { } } useEffect(() => { setTimeout(() => { setShouldLoadMedia(true) }, index * 1500) }, [index]) return (
onSelect?.(media)} > {/* 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 :
{formatLargeNumber(media.numberOfViews)} views
ยท
{formatTimeAgo(media.updatedAt)}
} {isTable ?
{formatDuration(media.duration || 0)}
: null}
) }