import { useMemo, useState } from 'react' import { motion } from 'framer-motion' import { Play } from '../../lib/icons' import { useYoutubeViewer } from '../../stores/youtube-viewer-store' import type { TmdbVideo } from '../../api/tmdb' interface Props { videos: TmdbVideo[] | null | undefined } const TAB_ORDER: Array<{ key: string; label: string }> = [ { key: 'Trailer', label: 'Trailers' }, { key: 'Teaser', label: 'Teasers' }, { key: 'Featurette', label: 'Featurettes' }, { key: 'Behind the Scenes', label: 'Behind the scenes' }, { key: 'Clip', label: 'Clips' }, { key: 'Bloopers', label: 'Bloopers' }, ] /** * Tabbed video gallery sourced from TMDB videos.results. We filter to * YouTube-hosted videos since that's what TMDB overwhelmingly returns * and what we can thumbnail without an extra API. Tabs only appear for * categories that actually have videos. */ export default function VideosSection({ videos }: Props) { const buckets = useMemo(() => { const m = new Map() for (const v of videos || []) { if (v.site !== 'YouTube' || !v.key) continue const list = m.get(v.type) || [] list.push(v) m.set(v.type, list) } // Stable sort within each bucket: official first, then most recent. for (const [, list] of m) { list.sort((a, b) => { if (a.official !== b.official) return a.official ? -1 : 1 return (b.published_at || '').localeCompare(a.published_at || '') }) } return m }, [videos]) const tabs = TAB_ORDER.filter(t => (buckets.get(t.key)?.length ?? 0) > 0) const [activeKey, setActiveKey] = useState(() => tabs[0]?.key || 'Trailer') if (tabs.length === 0) return null const active = buckets.get(activeKey) || [] return (
{tabs.map(t => { const count = buckets.get(t.key)?.length ?? 0 const on = activeKey === t.key return ( ) })}
{active.map(v => ( ))}
) } function VideoCard({ video }: { video: TmdbVideo }) { const show = useYoutubeViewer(s => s.show) const thumb = `https://img.youtube.com/vi/${video.key}/hqdefault.jpg` return ( ) }