import { listShortsOptions } from '@nalssigak/shared/api/client/@tanstack/react-query.gen'; import type { ShortItem } from '@nalssigak/shared/api/client/types.gen'; import { useQuery } from '@tanstack/react-query'; import { Image } from 'expo-image'; import { router, useLocalSearchParams } from 'expo-router'; import { useEffect, useRef, useState } from 'react'; import { FlatList, Pressable, View, type ViewToken } from 'react-native'; import { useSafeAreaInsets } from 'react-native-safe-area-context'; import Svg, { Path } from 'react-native-svg'; import { ShortPlayer, type PlayerHandle } from '@/components/shorts/player'; import { thumb } from '@/components/shorts/rail'; import { Text } from '@/components/text'; import { track } from '@/lib/analytics'; const BAR = 48; /** 쇼츠 화면: 그 영상부터 위아래로 넘겨 본다. 보이는 영상과 앞뒤만 소리 없이 미리 띄우고, 누르면 소리 켜기·끄기 (웹 ShortsPage) */ export default function ShortsScreen() { const { id } = useLocalSearchParams<{ id?: string }>(); const q = useQuery(listShortsOptions()); const { top, bottom } = useSafeAreaInsets(); // 처음 들어온 영상만 시작점으로 쓴다 const [firstId] = useState(id); const listed = q.data?.items ?? []; const items: ShortItem[] = firstId && listed.length && !listed.some((s) => s.id === firstId) ? [{ id: firstId, title: '' }, ...listed] : listed; const start = Math.max(0, items.findIndex((s) => s.id === firstId)); const [at, setAt] = useState(start); const [sound, setSound] = useState(false); const [ready, setReady] = useState>(new Set()); const [box, setBox] = useState({ w: 0, h: 0 }); const players = useRef(new Map()); const soundRef = useRef(false); const atRef = useRef(start); useEffect(() => { if (firstId) track('shorts_open', { video_id: firstId }); }, [firstId]); // 넘어온 영상만 처음부터 다시 틀고 소리를 맞춘다. 앞뒤는 소리만 끈다 (멈추면 유튜브 버튼이 떠서 멈추지 않는다) useEffect(() => { for (const [i, p] of players.current) { if (i === at) { p.command('seekTo', [0, true]); p.command(soundRef.current ? 'unMute' : 'mute'); p.command('playVideo'); } else p.command('mute'); } }, [at]); // FlatList 는 이 함수가 바뀌면 안 되어 처음 한 번만 만든다 const [onViewable] = useState(() => ({ viewableItems }: { viewableItems: ViewToken[] }) => { const i = viewableItems[0]?.index; if (i == null || i === atRef.current) return; atRef.current = i; setAt(i); const s = viewableItems[0].item; track('shorts_view', { video_id: s.id, index: i }); }); const toggleSound = () => { const on = !soundRef.current; soundRef.current = on; setSound(on); track('shorts_sound', { on }); players.current.get(at)?.command(on ? 'unMute' : 'mute'); }; const close = () => (router.canGoBack() ? router.back() : router.replace('/')); // 9:16 을 칸 안에 맞춘다 const w = Math.min(box.w, (box.h * 9) / 16); const h = (w * 16) / 9; return ( {!!items[at] && } {sound ? '소리 끄기' : '소리 켜기'} setBox({ w: e.nativeEvent.layout.width, h: e.nativeEvent.layout.height })}> {box.h > 0 && items.length > 0 && ( s.id} pagingEnabled showsVerticalScrollIndicator={false} initialScrollIndex={start} getItemLayout={(_, i) => ({ length: box.h, offset: box.h * i, index: i })} onViewableItemsChanged={onViewable} viewabilityConfig={{ itemVisiblePercentThreshold: 60 }} windowSize={3} renderItem={({ item: s, index: i }) => ( {/* 릴스처럼 앞뒤 영상을 소리 없이 미리 받아 둔다. 첫 장면 전엔 표지가 보이게 숨긴다 */} {Math.abs(i - at) <= 1 && ( { if (p) players.current.set(i, p); else players.current.delete(i); }} onPlaying={() => setReady((r) => (r.has(i) ? r : new Set(r).add(i)))} /> )} {/* 누르면 소리 켜기·끄기, 위아래로 밀면 목록이 넘어간다 */} )} /> )} ); }