feat: add real swipe playback and episode selection

This commit is contained in:
Zhou Weidong committed 2026-11-10 16:13:48 +08:00
1 parent 360a3c35a3
commit 0b8fe9ad9b
4 files changed
+6 -4

No files matched your search

+2 -2
View File
@@ -8,5 +8,5 @@ import { normalizePage, normalizeDrama } from '../services/normalizers';
import { useApi } from '../hooks/useApi';
import { useAppStore } from '../app/store';
function FeedItem({ video }) { const ref = useRef(null); useEffect(() => { const element = ref.current; const observer = new IntersectionObserver(([entry]) => { if (entry.isIntersecting) element.play().catch(() => {}); else element.pause(); }, { threshold: 0.7 }); observer.observe(element); return () => observer.disconnect(); }, []); return <article className="feed-item"><video ref={ref} src={video.preview || video.videoUrl} poster={video.image} muted loop playsInline controls={false} /><div className="feed-overlay"><span className="feed-badge">YMB SHORTS</span><h2>{video.title}</h2><p>{video.channel}</p><Link to={`/watch/${video.id}`} className="primary-button"><Play size={15} fill="currentColor" /> {video.watch || 'Watch drama'}</Link></div></article>; }
export function ShortsFeedPage() { const { locale } = useAppStore(); const api = useApi(() => appApi.dramas().then((payload) => normalizePage(payload).list.map((item) => normalizeDrama(item))), []); const items = api.data?.length ? api.data : videos.map((item) => ({ ...item, image: `/assets/${item.image}` })); return <section className="shorts-feed-page"><div className="feed-heading"><div><span>YMB SHORTS</span><h1>{locale.shortTitle}</h1></div><p>{locale.swipeHint || 'Swipe up to discover the next story'}</p></div>{api.loading && <div className="api-note">Loading real shorts…</div>}{items.map((video) => <FeedItem key={video.id} video={video} />)}</section>; }
function FeedItem({ video }) { const ref = useRef(null); useEffect(() => { const element = ref.current; const observer = new IntersectionObserver(([entry]) => { if (entry.isIntersecting) element.play().catch(() => {}); else element.pause(); }, { threshold: 0.7 }); observer.observe(element); return () => observer.disconnect(); }, []); const src = video.preview?.playUrl || video.videoUrl || ''; return <article className="feed-item"><video ref={ref} src={src} poster={video.image} muted loop playsInline controls={false} /><div className="feed-overlay"><span className="feed-badge">YMB SHORTS</span><h2>{video.title}</h2><p>{video.channel}</p>{src ? <Link to={`/watch/${video.id}`} className="primary-button"><Play size={15} fill="currentColor" /> {video.watch || 'Watch drama'}</Link> : <span className="api-note warning">Video unavailable</span>}</div></article>; }
export function ShortsFeedPage() { const { locale } = useAppStore(); const api = useApi(() => appApi.dramas().then((payload) => normalizePage(payload).list.map((item) => normalizeDrama(item, locale.code))), [locale.code]); const items = api.data || []; return <section className="shorts-feed-page"><div className="feed-heading"><div><span>YMB SHORTS</span><h1>{locale.shortTitle}</h1></div><p>{locale.swipeHint || 'Swipe up to discover the next story'}</p></div>{api.loading && <div className="api-note">Loading real shorts…</div>}{api.error && <div className="api-note warning">{api.error}</div>}{!api.loading && !items.length && <div className="api-note">{locale.empty}</div>}{items.map((video) => <FeedItem key={video.id} video={video} />)}</section>; }
+2 -1
View File
@@ -1,5 +1,6 @@
// Author: fruit.com.cn
import { useParams } from 'react-router-dom';
import { useState } from 'react';
import { videos } from '../data/catalog';
import { MediaCard } from '../components/MediaCard';
import { useAppStore } from '../app/store';
@@ -9,4 +10,4 @@ import { useApi } from '../hooks/useApi';
import { VideoPlayer } from '../components/player/VideoPlayer';
import { resolvePlaybackUrl } from '../services/normalizers';
export function WatchPage() { const { id } = useParams(); const { locale } = useAppStore(); const fallback = videos.find((item) => item.id === id) || videos[0]; const drama = useApi(() => appApi.drama(id).then((data) => normalizeDrama(data, locale.code)), [id, locale.code]); const video = drama.data || fallback; const episode = video.episodes?.[0]; const playback = useApi(() => episode?.id ? appApi.episodePlay(episode.id) : Promise.resolve(null), [episode?.id]); const signedUrl = resolvePlaybackUrl(playback.data?.playUrl || playback.data?.url || playback.data?.signedUrl); return <div className="watch-layout"><div><div className="player">{signedUrl ? <VideoPlayer src={signedUrl} poster={video.image} /> : <><img src={video.image?.startsWith('/') ? video.image : `/assets/${video.image}`} alt={video.title} /><div className="player-state">{playback.loading ? locale.loading : locale.playbackUnavailable}</div></>}</div><h1 className="watch-title">{video.title}</h1><p className="watch-subtitle">{video.channel} · {video.views}</p>{playback.error && <div className="api-note warning">{playback.error}</div>}</div><aside className="up-next"><h2>{locale.picks}</h2>{videos.slice(0, 4).map((item) => <MediaCard key={item.id} video={item} />)}</aside></div>; }
export function WatchPage() { const { id } = useParams(); const { locale } = useAppStore(); const fallback = videos.find((item) => item.id === id) || videos[0]; const drama = useApi(() => appApi.drama(id).then((data) => normalizeDrama(data, locale.code)), [id, locale.code]); const video = drama.data || fallback; const [selectedEpisodeId, setSelectedEpisodeId] = useState(null); const episode = video.episodes?.find((item) => String(item.id) === String(selectedEpisodeId)) || video.episodes?.[0]; const playback = useApi(() => episode?.id ? appApi.episodePlay(episode.id) : Promise.resolve(null), [episode?.id]); const signedUrl = resolvePlaybackUrl(playback.data?.playUrl); const image = video.image?.startsWith('/') || video.image?.startsWith('http') ? video.image : `/assets/${video.image}`; return <div className="watch-layout"><div><div className="player">{signedUrl ? <VideoPlayer src={signedUrl} poster={image} /> : <><img src={image} alt={video.title} /><div className="player-state">{playback.loading ? locale.loading : playback.error || locale.playbackUnavailable}</div></>}</div><h1 className="watch-title">{video.title}</h1><p className="watch-subtitle">{video.channel} · {video.views}</p><section className="episode-picker"><div className="episode-picker-heading"><h2>{locale.episodes || 'Episodes'}</h2><span>{video.episodes?.length || 0}</span></div><div className="episode-grid">{(video.episodes || []).map((item) => <button key={item.id} className={`episode-button ${String(item.id) === String(episode?.id) ? 'active' : ''} ${item.locked ? 'locked' : ''}`} onClick={() => setSelectedEpisodeId(item.id)}><b>{item.number}</b><span>{item.title || `Episode ${item.number}`}</span>{item.locked && <small>🔒</small>}</button>)}</div></section></div><aside className="up-next"><h2>{locale.picks}</h2>{videos.slice(0, 4).map((item) => <MediaCard key={item.id} video={item} />)}</aside></div>; }