feat: 앱 쇼츠·구름 합치기 게임 — 오늘 피드 쇼츠 줄, /shorts 위아래로 넘겨 보기(유튜브 플레이어를 사이트 주소 기준 웹뷰로, 앞뒤 미리 받기, 눌러 소리), /game 은 웹 게임을 웹뷰로 띄우고 앱 지역·로그인을 넘김(react-native-webview) (GA shorts_open·shorts_view·shorts_sound)
Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
1 parent
5a88b08a44
commit
697c4bfb20
9 files changed
+332
-1
No files matched your search
@@ -0,0 +1,53 @@
|
||||
import { useImperativeHandle, useRef, type Ref } from 'react';
|
||||
import { WebView } from 'react-native-webview';
|
||||
|
||||
import { SITE_URL } from '@/lib/site';
|
||||
|
||||
const YT_ORIGIN = 'https://www.youtube-nocookie.com';
|
||||
|
||||
export type PlayerFunc = 'playVideo' | 'seekTo' | 'mute' | 'unMute';
|
||||
export type PlayerHandle = { command: (func: PlayerFunc, args?: unknown[]) => void };
|
||||
|
||||
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: SITE_URL });
|
||||
return `${YT_ORIGIN}/embed/${id}?${q}`;
|
||||
}
|
||||
|
||||
/**
|
||||
* 유튜브 플레이어를 날씨각 주소를 기준으로 한 작은 문서 안 iframe 으로 띄운다.
|
||||
* 앱 웹뷰에서 embed 주소를 바로 열면 보낸 곳(Referer)이 없어 유튜브가 막는다. 웹과 같이 IFrame API 메시지로 재생 상태를 받고 명령한다
|
||||
*/
|
||||
function html(id: string) {
|
||||
return `<!doctype html><html><head><meta name="viewport" content="width=device-width,initial-scale=1"><style>html,body{margin:0;height:100%;background:transparent;overflow:hidden}iframe{border:0;width:100%;height:100%}</style></head>
|
||||
<body><iframe id="f" src="${embed(id)}" allow="autoplay; encrypted-media"></iframe>
|
||||
<script>
|
||||
var f = document.getElementById('f');
|
||||
f.addEventListener('load', function () { f.contentWindow.postMessage(JSON.stringify({ event: 'listening', id: ${JSON.stringify(id)} }), '${YT_ORIGIN}'); });
|
||||
window.addEventListener('message', function (e) {
|
||||
if (e.origin !== '${YT_ORIGIN}') return;
|
||||
try { var m = JSON.parse(e.data); if (m.info && m.info.playerState === 1) window.ReactNativeWebView.postMessage('playing'); } catch (x) {}
|
||||
});
|
||||
window.cmd = function (func, args) { f.contentWindow.postMessage(JSON.stringify({ event: 'command', func: func, args: args || [] }), '${YT_ORIGIN}'); };
|
||||
</script></body></html>`;
|
||||
}
|
||||
|
||||
/** 소리 없이 자동재생하는 쇼츠 플레이어. 첫 장면이 나오면 onPlaying (그 전엔 위에 표지를 덮어 둔다) */
|
||||
export function ShortPlayer({ id, onPlaying, ref }: { id: string; onPlaying: () => void; ref?: Ref<PlayerHandle> }) {
|
||||
const view = useRef<WebView>(null);
|
||||
useImperativeHandle(ref, () => ({
|
||||
command: (func, args = []) => view.current?.injectJavaScript(`window.cmd && window.cmd(${JSON.stringify(func)}, ${JSON.stringify(args)}); true;`),
|
||||
}));
|
||||
return (
|
||||
<WebView
|
||||
ref={view}
|
||||
source={{ html: html(id), baseUrl: SITE_URL }}
|
||||
originWhitelist={['*']}
|
||||
onMessage={(e) => e.nativeEvent.data === 'playing' && onPlaying()}
|
||||
mediaPlaybackRequiresUserAction={false}
|
||||
allowsInlineMediaPlayback
|
||||
scrollEnabled={false}
|
||||
setSupportMultipleWindows={false}
|
||||
style={{ backgroundColor: 'transparent' }}
|
||||
/>
|
||||
);
|
||||
}
|
||||
@@ -0,0 +1,45 @@
|
||||
import { listShortsOptions } from '@nalssigak/shared/api/client/@tanstack/react-query.gen';
|
||||
import { useQuery } from '@tanstack/react-query';
|
||||
import { Image } from 'expo-image';
|
||||
import { LinearGradient } from 'expo-linear-gradient';
|
||||
import { Link } from 'expo-router';
|
||||
import { Pressable, View } from 'react-native';
|
||||
|
||||
import { Text } from '@/components/text';
|
||||
import { Rail } from '@/components/ui';
|
||||
|
||||
const SKELS = [0, 1, 2, 3];
|
||||
|
||||
export const thumb = (id: string) => `https://i.ytimg.com/vi/${id}/hqdefault.jpg`;
|
||||
|
||||
/** 오늘 화면 히어로 아래 쇼츠 줄. 누르면 쇼츠 화면에서 위아래로 넘겨 본다. 앱은 데이터를 아끼려고 줄에서는 표지만 (웹은 첫 카드를 소리 없이 재생) */
|
||||
export function ShortsRail() {
|
||||
const q = useQuery(listShortsOptions());
|
||||
const items = q.data?.items ?? [];
|
||||
if (q.isPending)
|
||||
return (
|
||||
<Rail>
|
||||
{SKELS.map((i) => (
|
||||
<View key={i} className="aspect-[9/16] w-[120px] rounded-xl bg-skel" />
|
||||
))}
|
||||
</Rail>
|
||||
);
|
||||
if (!items.length) return null;
|
||||
return (
|
||||
<View accessibilityLabel="날씨각 쇼츠">
|
||||
<Rail>
|
||||
{items.map((s) => (
|
||||
<Link key={s.id} href={{ pathname: '/shorts/[id]', params: { id: s.id } }} asChild>
|
||||
<Pressable accessibilityLabel={`${s.title} 보기`} className="aspect-[9/16] w-[120px] overflow-hidden rounded-xl bg-[#1d2233]">
|
||||
<Image source={{ uri: thumb(s.id) }} style={{ position: 'absolute', inset: 0 }} contentFit="cover" />
|
||||
<LinearGradient colors={['transparent', 'rgba(0,0,0,0.65)']} locations={[0.55, 1]} style={{ position: 'absolute', inset: 0 }} />
|
||||
<Text numberOfLines={3} className="absolute inset-x-2 bottom-2 text-[12px] leading-[16px] font-bold text-white">
|
||||
{s.title}
|
||||
</Text>
|
||||
</Pressable>
|
||||
</Link>
|
||||
))}
|
||||
</Rail>
|
||||
</View>
|
||||
);
|
||||
}
|
||||
Reference in new issue
Block a user