import { useQuery } from '@tanstack/react-query' import { type CSSProperties, type PointerEvent as ReactPointerEvent, useEffect, useEffectEvent, useLayoutEffect, useRef, useState } from 'react' import { Link, useNavigate, useParams } from 'react-router' import { listShortsOptions } from '@nalssigak/shared/api/client/@tanstack/react-query.gen' import type { ShortItem } from '@nalssigak/shared/api/client' import { cx } from '../lib/utils' import { Rail, 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 // 쇼츠 페이지의 영상 칸: 위 헤더(54px)·버튼 줄(48px)과 아래 탭 바(--shorts-tab) 사이 높이와 화면 너비 안에 9:16으로 맞춘다 const FIT = { width: 'min(100vw, calc((100dvh - env(safe-area-inset-top, 0px) - 54px - 48px - var(--shorts-tab, 0px)) * 9 / 16))', aspectRatio: '9 / 16' } /** 아래 탭 바 높이 (기기마다 안전 영역이 달라 실제로 잰다) */ function useTabBarHeight() { const [h, setH] = useState(0) useLayoutEffect(() => { const nav = document.querySelector('nav.b-tabbar') if (!nav) return const ro = new ResizeObserver(() => setH(nav.offsetHeight)) ro.observe(nav) return () => ro.disconnect() }, []) return h } const YT_ORIGIN = 'https://www.youtube-nocookie.com' function embed(id: string) { const q = new URLSearchParams({ autoplay: '1', mute: '1', playsinline: '1', loop: '1', playlist: id, controls: '0', rel: '0', modestbranding: '1', enablejsapi: '1', origin: window.location.origin, }) return `${YT_ORIGIN}/embed/${id}?${q}` } type Func = 'playVideo' | 'pauseVideo' | 'seekTo' | 'mute' | 'unMute' /** 이미 띄운 플레이어에 명령 (IFrame API 메시지) */ function command(frame: HTMLIFrameElement | null | undefined, func: Func, args: unknown[] = []) { frame?.contentWindow?.postMessage(JSON.stringify({ event: 'command', func, args }), YT_ORIGIN) } const PLAYING = 1 // 마우스로 이만큼 끌면 다음·이전 영상으로 넘긴다 const SWIPE_PX = 30 /** 화면에 보이는 동안만 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 } /** 오늘 화면 히어로 아래 쇼츠 줄: 첫 카드만 소리 없이 재생, 누르면 쇼츠 페이지(/shorts/영상ID)에서 위아래로 넘겨 본다 */ export function ShortsRail() { const q = useQuery(listShortsOptions()) const items = q.data?.items ?? [] const [ref, visible] = useVisible() if (q.isPending) { return ( ) } if (!items.length) return null return (
{items.map((s, i) => ( ))}
) } function ShortCard({ s, playing }: { s: ShortItem; playing: boolean }) { return ( {playing && (