128 lines
6.2 KiB
TypeScript
128 lines
6.2 KiB
TypeScript
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<ReadonlySet<number>>(new Set());
|
|
const [box, setBox] = useState({ w: 0, h: 0 });
|
|
const players = useRef(new Map<number, PlayerHandle>());
|
|
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<ShortItem>[] }) => {
|
|
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 (
|
|
<View className="flex-1 bg-[#1d2233]" style={{ paddingTop: top, paddingBottom: bottom }}>
|
|
{!!items[at] && <Image source={{ uri: thumb(items[at].id) }} style={{ position: 'absolute', inset: 0, opacity: 0.7, transform: [{ scale: 1.25 }] }} blurRadius={40} contentFit="cover" />}
|
|
<View className="absolute inset-0 bg-black/35" />
|
|
<View className="flex-row items-center justify-between px-3" style={{ height: BAR }}>
|
|
<Pressable onPress={close} hitSlop={8} accessibilityRole="button" accessibilityLabel="닫기" className="size-9 items-center justify-center rounded-full bg-white/15">
|
|
<Svg viewBox="0 0 24 24" width={18} height={18} fill="none" stroke="#fff" strokeWidth={2.4} strokeLinecap="round">
|
|
<Path d="M6 6l12 12M18 6L6 18" />
|
|
</Svg>
|
|
</Pressable>
|
|
<Pressable onPress={toggleSound} accessibilityRole="button" accessibilityState={{ selected: sound }} className="h-9 justify-center rounded-full bg-white/15 px-3.5">
|
|
<Text className="text-[13px] font-bold text-white">{sound ? '소리 끄기' : '소리 켜기'}</Text>
|
|
</Pressable>
|
|
</View>
|
|
<View className="flex-1" onLayout={(e) => setBox({ w: e.nativeEvent.layout.width, h: e.nativeEvent.layout.height })}>
|
|
{box.h > 0 && items.length > 0 && (
|
|
<FlatList
|
|
data={items}
|
|
keyExtractor={(s) => 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 }) => (
|
|
<View style={{ height: box.h }} className="items-center justify-center">
|
|
<View style={{ width: w, height: h }}>
|
|
<Image source={{ uri: thumb(s.id) }} style={{ position: 'absolute', inset: 0 }} contentFit="cover" />
|
|
{/* 릴스처럼 앞뒤 영상을 소리 없이 미리 받아 둔다. 첫 장면 전엔 표지가 보이게 숨긴다 */}
|
|
{Math.abs(i - at) <= 1 && (
|
|
<View pointerEvents="none" style={{ position: 'absolute', inset: 0, opacity: ready.has(i) ? 1 : 0 }}>
|
|
<ShortPlayer
|
|
id={s.id}
|
|
ref={(p) => {
|
|
if (p) players.current.set(i, p);
|
|
else players.current.delete(i);
|
|
}}
|
|
onPlaying={() => setReady((r) => (r.has(i) ? r : new Set(r).add(i)))}
|
|
/>
|
|
</View>
|
|
)}
|
|
{/* 누르면 소리 켜기·끄기, 위아래로 밀면 목록이 넘어간다 */}
|
|
<Pressable onPress={toggleSound} accessibilityLabel={sound ? '소리 끄기' : '소리 켜기'} style={{ position: 'absolute', inset: 0 }} />
|
|
</View>
|
|
</View>
|
|
)}
|
|
/>
|
|
)}
|
|
</View>
|
|
</View>
|
|
);
|
|
}
|