feat: 홈 쇼츠 미리보기 — 데스크톱은 마우스를 올린 카드만 재생(0.2초 머물 때), 터치 기기는 첫 터치 뒤 첫 카드. 쇼츠_미리보기에 trigger(hover·auto) 추가
This commit is contained in:
1 parent
38ae9374fd
commit
de39195a61
1 file changed
+44
-14
+44
-14
@@ -75,16 +75,21 @@ function useVisible<T extends Element>(threshold = 0.5) {
|
||||
return [ref, visible] as const
|
||||
}
|
||||
|
||||
// 마우스를 올릴 수 있는 기기(데스크톱)인지. 터치 기기는 올리기가 없어 첫 터치 뒤 첫 카드를 자동 재생한다
|
||||
const CAN_HOVER = typeof window !== 'undefined' && window.matchMedia('(hover: hover) and (pointer: fine)').matches
|
||||
// 마우스가 줄 위를 지나가기만 할 땐 플레이어를 받지 않게, 이만큼 머물러야 재생한다
|
||||
const HOVER_PLAY_MS = 200
|
||||
|
||||
// scroll 은 넣지 않는다: 브라우저가 스크롤 위치를 되돌릴 때도 나서 조작으로 볼 수 없다
|
||||
const TOUCH_EVENTS = ['pointerdown', 'touchstart', 'wheel', 'keydown', 'mousemove'] as const
|
||||
const TOUCH_EVENTS = ['pointerdown', 'touchstart', 'keydown'] as const
|
||||
// 한 번 조작했으면 다른 화면에 다녀와도 그대로 둔다
|
||||
let touchedOnce = false
|
||||
|
||||
/** 사용자가 화면을 처음 조작(누르기·굴리기·키)한 뒤부터 true. 유튜브 플레이어(약 850KB)를 첫 화면 로드에서 빼려고 쓴다 */
|
||||
function useTouched() {
|
||||
/** 터치 기기에서 사용자가 화면을 처음 조작(누르기·키)한 뒤부터 true. 유튜브 플레이어(약 850KB)를 첫 화면 로드에서 빼려고 쓴다 */
|
||||
function useTouched(enabled: boolean) {
|
||||
const [touched, setTouched] = useState(touchedOnce)
|
||||
useEffect(() => {
|
||||
if (touchedOnce) return
|
||||
if (!enabled || touchedOnce) return
|
||||
const opts = { capture: true, passive: true }
|
||||
const off = () => TOUCH_EVENTS.forEach((t) => window.removeEventListener(t, on, opts))
|
||||
const on = () => {
|
||||
@@ -94,16 +99,19 @@ function useTouched() {
|
||||
}
|
||||
TOUCH_EVENTS.forEach((t) => window.addEventListener(t, on, opts))
|
||||
return off
|
||||
}, [])
|
||||
}, [enabled])
|
||||
return touched
|
||||
}
|
||||
|
||||
/** 오늘 화면 히어로 아래 쇼츠 줄: 첫 조작 뒤 첫 카드만 소리 없이 재생, 누르면 쇼츠 페이지(/shorts/영상ID)에서 위아래로 넘겨 본다 */
|
||||
/**
|
||||
* 오늘 화면 히어로 아래 쇼츠 줄: 누르면 쇼츠 페이지(/shorts/영상ID)에서 위아래로 넘겨 본다.
|
||||
* 줄에서는 소리 없이 미리 보인다 — 데스크톱은 마우스를 올린 카드, 터치 기기는 첫 터치 뒤 첫 카드
|
||||
*/
|
||||
export function ShortsRail() {
|
||||
const q = useQuery(listShortsOptions())
|
||||
const items = q.data?.items ?? []
|
||||
const [ref, visible] = useVisible<HTMLElement>()
|
||||
const touched = useTouched()
|
||||
const touched = useTouched(!CAN_HOVER && !IS_TOSS)
|
||||
|
||||
if (q.isPending) {
|
||||
return (
|
||||
@@ -123,18 +131,30 @@ export function ShortsRail() {
|
||||
<Rail label="쇼츠 목록">
|
||||
{items.map((s, i) => (
|
||||
// 토스 앱 웹뷰는 유튜브 플레이어가 무거워, 줄에선 표지만 보이고 누르면 재생
|
||||
<ShortCard key={s.id} s={s} first={i === 0} playing={!IS_TOSS && i === 0 && visible && touched} />
|
||||
<ShortCard key={s.id} s={s} first={i === 0} auto={!IS_TOSS && !CAN_HOVER && i === 0 && visible && touched} />
|
||||
))}
|
||||
</Rail>
|
||||
</section>
|
||||
)
|
||||
}
|
||||
|
||||
function ShortCard({ s, first, playing }: { s: ShortItem; first: boolean; playing: boolean }) {
|
||||
/** auto: 올리지 않아도 재생 (터치 기기의 첫 카드) */
|
||||
function ShortCard({ s, first, auto }: { s: ShortItem; first: boolean; auto: boolean }) {
|
||||
const [hover, setHover] = useState(false)
|
||||
const hoverTimer = useRef(0)
|
||||
useEffect(() => () => window.clearTimeout(hoverTimer.current), [])
|
||||
const enter = () => {
|
||||
hoverTimer.current = window.setTimeout(() => setHover(true), HOVER_PLAY_MS)
|
||||
}
|
||||
const leave = () => {
|
||||
window.clearTimeout(hoverTimer.current)
|
||||
setHover(false)
|
||||
}
|
||||
const playing = auto || hover
|
||||
// 재생이 시작된 뒤에만 플레이어를 보인다: 자동재생이 막히면(아이폰 절전 등) 유튜브 재생 버튼 대신 표지 그대로
|
||||
const [started, setStarted] = useState(false)
|
||||
const frame = useRef<HTMLIFrameElement>(null)
|
||||
// 플레이어는 반복 재생하며 재생 상태를 여러 번 보내므로 미리보기 이벤트는 한 번만
|
||||
// 플레이어는 반복 재생하며 재생 상태를 여러 번 보내고 마우스도 여러 번 올리므로, 미리보기 이벤트는 카드마다 한 번만
|
||||
const previewSent = useRef(false)
|
||||
useEffect(() => {
|
||||
if (!playing) return
|
||||
@@ -145,16 +165,26 @@ function ShortCard({ s, first, playing }: { s: ShortItem; first: boolean; playin
|
||||
setStarted(true)
|
||||
if (previewSent.current) return
|
||||
previewSent.current = true
|
||||
track('shorts_preview', { video_id: s.id })
|
||||
track('shorts_preview', { video_id: s.id, trigger: auto ? 'auto' : 'hover' })
|
||||
} catch {
|
||||
return
|
||||
}
|
||||
}
|
||||
window.addEventListener('message', onMessage)
|
||||
return () => window.removeEventListener('message', onMessage)
|
||||
}, [playing, s.id])
|
||||
return () => {
|
||||
window.removeEventListener('message', onMessage)
|
||||
// 플레이어를 내렸다 다시 띄우면 첫 장면을 받을 때까지 다시 표지
|
||||
setStarted(false)
|
||||
}
|
||||
}, [playing, auto, s.id])
|
||||
return (
|
||||
<Link to={`/shorts/${s.id}`} aria-label={`${s.title} 보기`} className="relative block aspect-[9/16] w-[120px] cursor-pointer overflow-hidden rounded-xl bg-[#1d2233] p-0 text-left text-white">
|
||||
<Link
|
||||
to={`/shorts/${s.id}`}
|
||||
aria-label={`${s.title} 보기`}
|
||||
onMouseEnter={CAN_HOVER && !IS_TOSS ? enter : undefined}
|
||||
onMouseLeave={CAN_HOVER && !IS_TOSS ? leave : undefined}
|
||||
className="relative block aspect-[9/16] w-[120px] cursor-pointer overflow-hidden rounded-xl bg-[#1d2233] p-0 text-left text-white"
|
||||
>
|
||||
{/* 첫 카드 표지는 첫 화면에서 가장 큰 그림(LCP)이라 미루지 않고 먼저 받는다 */}
|
||||
<img src={`https://i.ytimg.com/vi/${s.id}/hqdefault.jpg`} alt="" loading={first ? 'eager' : 'lazy'} fetchPriority={first ? 'high' : 'auto'} className="absolute inset-0 size-full object-cover" />
|
||||
{playing && (
|
||||
|
||||
Reference in new issue
Block a user