feat: 오늘 화면 히어로 아래 날씨각 유튜브 쇼츠 줄 — 첫 카드 소리 없이 자동재생, 누르면 전체 화면에서 위아래로 넘겨 보기. 목록은 /v1/shorts(API 키 있으면 채널 쇼츠, 없으면 설정한 영상 ID), 1시간 캐시
This commit is contained in:
1 parent
f9988855d3
commit
b718955927
15 files changed
+875
-6
No files matched your search
@@ -0,0 +1,139 @@
|
||||
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
|
||||
|
||||
function embed(id: string, controls: boolean) {
|
||||
const q = new URLSearchParams({ autoplay: '1', mute: '1', playsinline: '1', loop: '1', playlist: id, controls: controls ? '1' : '0', rel: '0', modestbranding: '1' })
|
||||
return `https://www.youtube-nocookie.com/embed/${id}?${q}`
|
||||
}
|
||||
|
||||
/** 화면에 보이는 동안만 true */
|
||||
function useVisible<T extends Element>(threshold = 0.5) {
|
||||
const [el, ref] = useState<T | null>(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<HTMLElement>()
|
||||
const [openAt, setOpenAt] = useState<number | null>(null)
|
||||
|
||||
if (q.isPending) {
|
||||
return (
|
||||
<section aria-hidden="true">
|
||||
<Sec title="날씨각 쇼츠" />
|
||||
<SkelRail>
|
||||
{[0, 1, 2, 3].map((i) => (
|
||||
<Skel key={i} className="aspect-[9/16] w-[120px] rounded-xl" />
|
||||
))}
|
||||
</SkelRail>
|
||||
</section>
|
||||
)
|
||||
}
|
||||
if (!items.length) return null
|
||||
|
||||
return (
|
||||
<section ref={ref} aria-label="날씨각 쇼츠">
|
||||
<Sec title="날씨각 쇼츠" />
|
||||
<Rail label="쇼츠 목록">
|
||||
{items.map((s, i) => (
|
||||
<ShortCard key={s.id} s={s} playing={i === 0 && visible && openAt == null} onOpen={() => setOpenAt(i)} />
|
||||
))}
|
||||
</Rail>
|
||||
{openAt != null && <ShortsViewer items={items} start={openAt} onClose={() => setOpenAt(null)} />}
|
||||
</section>
|
||||
)
|
||||
}
|
||||
|
||||
function ShortCard({ s, playing, onOpen }: { s: ShortItem; playing: boolean; onOpen: () => void }) {
|
||||
return (
|
||||
<button type="button" onClick={onOpen} aria-label={`${s.title} 보기`} className="relative aspect-[9/16] w-[120px] cursor-pointer overflow-hidden rounded-xl bg-[#1d2233] p-0 text-left text-white">
|
||||
<img src={`https://i.ytimg.com/vi/${s.id}/hqdefault.jpg`} alt="" loading="lazy" className="absolute inset-0 size-full object-cover" />
|
||||
{playing && (
|
||||
<iframe
|
||||
src={embed(s.id, false)}
|
||||
title={s.title}
|
||||
allow="autoplay; encrypted-media; picture-in-picture"
|
||||
tabIndex={-1}
|
||||
className="pointer-events-none absolute top-1/2 left-1/2 border-0"
|
||||
style={{ width: PLAYER_W, height: (PLAYER_W * 16) / 9, transform: `translate(-50%, -50%) scale(${SCALE})` }}
|
||||
/>
|
||||
)}
|
||||
<span className="absolute inset-0 bg-[linear-gradient(180deg,transparent_55%,rgba(0,0,0,.65))]" aria-hidden="true" />
|
||||
<span className="absolute inset-x-2 bottom-2 line-clamp-3 text-[12px] leading-[1.3] font-bold">{s.title}</span>
|
||||
</button>
|
||||
)
|
||||
}
|
||||
|
||||
/** 전체 화면 재생 창: 네이티브 <dialog>라 ESC·포커스 가두기가 기본. 보이는 영상 하나만 띄운다 */
|
||||
function ShortsViewer({ items, start, onClose }: { items: ShortItem[]; start: number; onClose: () => void }) {
|
||||
const dialog = useRef<HTMLDialogElement>(null)
|
||||
const list = useRef<HTMLDivElement>(null)
|
||||
const [at, setAt] = useState(start)
|
||||
|
||||
useEffect(() => {
|
||||
const el = dialog.current
|
||||
if (!el) return
|
||||
el.showModal()
|
||||
list.current?.children[start]?.scrollIntoView({ block: 'start' })
|
||||
const root = document.documentElement
|
||||
const before = root.style.overflow
|
||||
root.style.overflow = 'hidden'
|
||||
return () => {
|
||||
root.style.overflow = before
|
||||
}
|
||||
}, [start])
|
||||
|
||||
useEffect(() => {
|
||||
const box = list.current
|
||||
if (!box) return
|
||||
const io = new IntersectionObserver(
|
||||
(entries) => {
|
||||
for (const e of entries) if (e.isIntersecting) setAt(Number((e.target as HTMLElement).dataset.i))
|
||||
},
|
||||
{ root: box, threshold: 0.6 },
|
||||
)
|
||||
for (const c of box.children) io.observe(c)
|
||||
return () => io.disconnect()
|
||||
}, [])
|
||||
|
||||
return (
|
||||
<dialog ref={dialog} aria-label="날씨각 쇼츠" onClose={onClose} className="fixed inset-0 m-0 size-full max-h-none max-w-none bg-black p-0 text-white">
|
||||
{/* 유튜브 플레이어 위쪽 로고·소리 버튼을 가리지 않게 닫기 줄을 따로 둔다 */}
|
||||
<div className="flex h-[calc(env(safe-area-inset-top,0px)+52px)] items-end px-3 pb-2">
|
||||
<button type="button" onClick={onClose} aria-label="닫기" className={cx('relative grid size-9 cursor-pointer place-items-center rounded-full bg-white/15 text-xl text-white', TAP44)}>
|
||||
×
|
||||
</button>
|
||||
</div>
|
||||
<div ref={list} className="h-[calc(100dvh-env(safe-area-inset-top,0px)-52px)] snap-y snap-mandatory overflow-y-auto [scrollbar-width:none]">
|
||||
{items.map((s, i) => (
|
||||
<div key={s.id} data-i={i} className="relative grid h-full snap-start place-items-center">
|
||||
{i === at ? (
|
||||
<iframe src={embed(s.id, true)} title={s.title} allow="autoplay; encrypted-media; picture-in-picture; fullscreen" allowFullScreen className="aspect-[9/16] h-full max-w-full border-0" />
|
||||
) : (
|
||||
<img src={`https://i.ytimg.com/vi/${s.id}/hqdefault.jpg`} alt="" className="aspect-[9/16] h-full max-w-full object-cover opacity-60" />
|
||||
)}
|
||||
</div>
|
||||
))}
|
||||
</div>
|
||||
</dialog>
|
||||
)
|
||||
}
|
||||
@@ -24,6 +24,7 @@ import { MyGakRow } from './myGak'
|
||||
import { fromShare } from './myGakState'
|
||||
import { PostCard, PostCardSkel, PostFrame } from './Post'
|
||||
import { usePinnedLike, type PinnedKey } from './pinnedLike'
|
||||
import { ShortsRail } from './Shorts'
|
||||
import { FeedComposer, NearTalks, TalkCard, TalkGroup } from './talk'
|
||||
import { topicName, useTalkGroups } from './talkState'
|
||||
import { useRegion } from './regionContext'
|
||||
@@ -639,6 +640,7 @@ export function TodayPage() {
|
||||
// 피드 칸은 전국 공통이라 지역이 바뀌어도 글 번호가 같다. 지역마다 새로 그려 넘긴 슬라이드·펼친 상태가 남지 않게 한다
|
||||
<section key={region.code} aria-label="오늘" aria-busy={load === 'loading'} className={cx('transition-opacity', feed.isPlaceholderData && 'opacity-60')}>
|
||||
<Hero hero={f?.hero} post={f?.hero_post} today={today.data} ranked={stories} all={f?.stories ?? []} feedLoad={load} todayLoad={loadOf(today)} />
|
||||
<ShortsRail />
|
||||
<FeedComposer onPosted={(t) => setMine((m) => [t, ...m])} />
|
||||
{mine.map((t) => (
|
||||
<TalkCard key={t.talk_id} t={t} onGone={() => setMine((m) => m.filter((x) => x.talk_id !== t.talk_id))} />
|
||||
|
||||
Reference in new issue
Block a user