import { useQuery } from '@tanstack/react-query' import { useEffect, useRef, useState } from 'react' import { listShortsOptions } from '../api/client/@tanstack/react-query.gen' import type { ShortItem } from '../api/client' import { cx } from '../lib/utils' import { Rail, Sec, Skel, SkelRail, TAP44 } from './ui' // 유튜브는 플레이어가 200×200 이상이어야 해서, 카드 안 미리보기는 200px 너비로 띄우고 줄여 보인다. // 위 로고·아래 제목 줄이 작은 카드에서 겹쳐 보이지 않게 가운데를 1.4배로 키워 위아래를 잘라 낸다 const CARD_W = 120 const PLAYER_W = 200 const SCALE = (CARD_W / PLAYER_W) * 1.4 // 전체 화면 창의 영상 칸: 닫기 줄 아래 남은 높이와 화면 너비 안에 9:16으로 맞춘다 const FIT = { width: 'min(100vw, calc((100dvh - env(safe-area-inset-top, 0px) - 52px) * 9 / 16))', aspectRatio: '9 / 16' } const YT_ORIGIN = 'https://www.youtube-nocookie.com' function embed(id: string, controls: boolean, autoplay = true) { const q = new URLSearchParams({ autoplay: autoplay ? '1' : '0', mute: '1', playsinline: '1', loop: '1', playlist: id, controls: controls ? '1' : '0', rel: '0', modestbranding: '1', enablejsapi: '1', origin: window.location.origin, }) return `${YT_ORIGIN}/embed/${id}?${q}` } /** 이미 띄운 플레이어에 재생·일시정지 (IFrame API 메시지) */ function command(frame: HTMLIFrameElement | null | undefined, func: 'playVideo' | 'pauseVideo') { frame?.contentWindow?.postMessage(JSON.stringify({ event: 'command', func, args: [] }), YT_ORIGIN) } /** 화면에 보이는 동안만 true */ function useVisible(threshold = 0.5) { const [el, ref] = useState(null) const [visible, setVisible] = useState(false) useEffect(() => { if (!el) return const io = new IntersectionObserver(([e]) => setVisible(e.isIntersecting), { threshold }) io.observe(el) return () => io.disconnect() }, [el, threshold]) return [ref, visible] as const } /** 오늘 화면 히어로 아래 쇼츠 줄: 첫 카드만 소리 없이 재생, 누르면 전체 화면에서 위아래로 넘겨 본다 */ export function ShortsRail() { const q = useQuery(listShortsOptions()) const items = q.data?.items ?? [] const [ref, visible] = useVisible() const [openAt, setOpenAt] = useState(null) if (q.isPending) { return ( ) } if (!items.length) return null return (
{items.map((s, i) => ( setOpenAt(i)} /> ))} {openAt != null && setOpenAt(null)} />}
) } function ShortCard({ s, playing, onOpen }: { s: ShortItem; playing: boolean; onOpen: () => void }) { return (