feat(frontend): implement full in-app streaming system with 4 APIs
- Add streaming types, interfaces, and normalized response structures - Create StreamingManager with URL building, validation, and security - Implement provider adapters for VidLink (2 servers), VidKing (1 server), AnyEmbed (5 servers) - Add Zustand streaming store with server selection, fallback, and retry logic - Build StreamingPlayer iframe component with fullscreen and keyboard shortcuts - Add ServerSelector dropdown with 8 servers and active state indicator - Create EpisodeNavigator with season tabs and episode grid - Implement AutoMarkWatchedPrompt for one-time completion opt-in - Add StreamingSection with Watch Now / Continue Watching entry point - Integrate StreamingSection into EntryDetailsPage
This commit is contained in:
1 parent
1a2ec5c505
commit
54014ae01f
19 files changed
+2617
No files matched your search
@@ -0,0 +1,219 @@
|
||||
import { useCallback, useEffect, useMemo, useState } from 'react';
|
||||
import { useStreamingStore } from '@/stores/useStreamingStore';
|
||||
import { getEntryWatchProgress } from '@/lib/streaming';
|
||||
import { StreamingPlayer } from './StreamingPlayer';
|
||||
import { Button } from '@/components/ui/button';
|
||||
import { Play, Tv } from 'lucide-react';
|
||||
import { useCreateEntrySeen } from '@/hooks/useEntrySeen';
|
||||
import { useCreateEpisodeSeen } from '@/hooks/useEpisodeSeen';
|
||||
import type { Entry, Season } from '@/types';
|
||||
|
||||
interface StreamingSectionProps {
|
||||
entry: Entry;
|
||||
seasons?: Season[];
|
||||
}
|
||||
|
||||
export function StreamingSection({ entry, seasons }: StreamingSectionProps) {
|
||||
const {
|
||||
isPlayerOpen,
|
||||
initialize,
|
||||
play,
|
||||
currentRequest,
|
||||
} = useStreamingStore();
|
||||
|
||||
const createEntrySeen = useCreateEntrySeen();
|
||||
const createEpisodeSeen = useCreateEpisodeSeen();
|
||||
|
||||
const [currentSeason, setCurrentSeason] = useState<number>(1);
|
||||
const [currentEpisode, setCurrentEpisode] = useState<number>(1);
|
||||
|
||||
// Initialize streaming system
|
||||
useEffect(() => {
|
||||
initialize();
|
||||
}, [initialize]);
|
||||
|
||||
// Determine continue watching state
|
||||
const watchProgress = useMemo(() => {
|
||||
if (!entry.entryIds?.tmdb_id) return null;
|
||||
return getEntryWatchProgress(entry.entryIds.tmdb_id);
|
||||
}, [entry.entryIds?.tmdb_id]);
|
||||
|
||||
// For TV shows, figure out the "continue watching" episode
|
||||
const continueWatchingInfo = useMemo(() => {
|
||||
if (!entry.is_tvseries) return null;
|
||||
|
||||
// Check local progress first
|
||||
if (watchProgress?.lastSeasonWatched && watchProgress?.lastEpisodeWatched) {
|
||||
return {
|
||||
season: watchProgress.lastSeasonWatched,
|
||||
episode: watchProgress.lastEpisodeWatched,
|
||||
label: `Continue S${watchProgress.lastSeasonWatched}:E${watchProgress.lastEpisodeWatched}`,
|
||||
};
|
||||
}
|
||||
|
||||
// Check episode seen data from the library
|
||||
if (entry.episodeSeens && entry.episodeSeens.length > 0) {
|
||||
// Find the last watched episode
|
||||
const sorted = [...entry.episodeSeens].sort(
|
||||
(a, b) => new Date(b.date_seen).getTime() - new Date(a.date_seen).getTime()
|
||||
);
|
||||
const last = sorted[0];
|
||||
|
||||
// Find the episode in seasons to get season/episode numbers
|
||||
if (seasons) {
|
||||
for (const season of seasons) {
|
||||
const ep = season.episodes?.find((e) => e.id === last.episode_id);
|
||||
if (ep) {
|
||||
// Suggest the next episode
|
||||
const nextEp = ep.episode_number + 1;
|
||||
const hasNext = season.episodes?.some((e) => e.episode_number === nextEp);
|
||||
if (hasNext) {
|
||||
return {
|
||||
season: season.season_number,
|
||||
episode: nextEp,
|
||||
label: `Continue S${season.season_number}:E${nextEp}`,
|
||||
};
|
||||
}
|
||||
// Try next season
|
||||
const nextSeason = seasons.find((s) => s.season_number === season.season_number + 1);
|
||||
if (nextSeason?.episodes?.length) {
|
||||
return {
|
||||
season: nextSeason.season_number,
|
||||
episode: 1,
|
||||
label: `Continue S${nextSeason.season_number}:E1`,
|
||||
};
|
||||
}
|
||||
// No more episodes, show where they left off
|
||||
return {
|
||||
season: season.season_number,
|
||||
episode: ep.episode_number,
|
||||
label: `Rewatch S${season.season_number}:E${ep.episode_number}`,
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Default to S1E1
|
||||
return { season: 1, episode: 1, label: 'Start Watching' };
|
||||
}, [entry, seasons, watchProgress]);
|
||||
|
||||
// Set initial episode from continue watching
|
||||
useEffect(() => {
|
||||
if (continueWatchingInfo) {
|
||||
setCurrentSeason(continueWatchingInfo.season);
|
||||
setCurrentEpisode(continueWatchingInfo.episode);
|
||||
}
|
||||
}, [continueWatchingInfo]);
|
||||
|
||||
const handlePlay = useCallback(() => {
|
||||
if (!entry.entryIds?.tmdb_id) return;
|
||||
|
||||
const request = entry.is_tvseries
|
||||
? {
|
||||
type: 'tv' as const,
|
||||
tmdbId: entry.entryIds.tmdb_id,
|
||||
imdbId: entry.entryIds?.imdb_id || undefined,
|
||||
season: currentSeason,
|
||||
episode: currentEpisode,
|
||||
startAt: watchProgress?.showProgress?.[`s${currentSeason}e${currentEpisode}`]?.watched,
|
||||
}
|
||||
: {
|
||||
type: 'movie' as const,
|
||||
tmdbId: entry.entryIds.tmdb_id,
|
||||
imdbId: entry.entryIds?.imdb_id || undefined,
|
||||
startAt: watchProgress?.watched,
|
||||
};
|
||||
|
||||
play(request);
|
||||
}, [entry, currentSeason, currentEpisode, watchProgress, play]);
|
||||
|
||||
const handleEpisodeChange = useCallback(
|
||||
(season: number, episode: number) => {
|
||||
setCurrentSeason(season);
|
||||
setCurrentEpisode(episode);
|
||||
|
||||
if (!entry.entryIds?.tmdb_id) return;
|
||||
|
||||
// Get progress for this specific episode
|
||||
const epProgress = watchProgress?.showProgress?.[`s${season}e${episode}`];
|
||||
|
||||
play({
|
||||
type: 'tv',
|
||||
tmdbId: entry.entryIds.tmdb_id,
|
||||
imdbId: entry.entryIds?.imdb_id || undefined,
|
||||
season,
|
||||
episode,
|
||||
startAt: epProgress?.watched,
|
||||
});
|
||||
},
|
||||
[entry, watchProgress, play]
|
||||
);
|
||||
|
||||
const handleMarkAsWatched = useCallback(
|
||||
(season?: number, episode?: number) => {
|
||||
if (entry.is_tvseries && season && episode && seasons) {
|
||||
// Find the episode ID
|
||||
const seasonData = seasons.find((s) => s.season_number === season);
|
||||
const episodeData = seasonData?.episodes?.find((e) => e.episode_number === episode);
|
||||
if (episodeData) {
|
||||
createEpisodeSeen.mutate({
|
||||
entry_id: entry.id,
|
||||
episode_id: episodeData.id,
|
||||
date_seen: new Date().toISOString().split('T')[0],
|
||||
accuracy: 'EXACT',
|
||||
season_id: seasonData!.id,
|
||||
});
|
||||
}
|
||||
} else {
|
||||
// Movie
|
||||
createEntrySeen.mutate({
|
||||
entry_id: entry.id,
|
||||
date_seen: new Date().toISOString().split('T')[0],
|
||||
accuracy: 'EXACT',
|
||||
});
|
||||
}
|
||||
},
|
||||
[entry, seasons, createEntrySeen, createEpisodeSeen]
|
||||
);
|
||||
|
||||
// Don't show if no TMDB ID
|
||||
if (!entry.entryIds?.tmdb_id) return null;
|
||||
|
||||
return (
|
||||
<div className="space-y-3">
|
||||
{/* Play button - shown when player is closed */}
|
||||
{!isPlayerOpen && (
|
||||
<Button
|
||||
onClick={handlePlay}
|
||||
size="lg"
|
||||
className="w-full sm:w-auto gap-2 h-11 px-6 text-sm font-medium"
|
||||
>
|
||||
{entry.is_tvseries ? (
|
||||
<>
|
||||
<Tv className="h-4 w-4" />
|
||||
{continueWatchingInfo?.label || 'Start Watching'}
|
||||
</>
|
||||
) : (
|
||||
<>
|
||||
<Play className="h-4 w-4 fill-current" />
|
||||
{watchProgress && watchProgress.watched > 60 ? 'Continue Watching' : 'Watch Now'}
|
||||
</>
|
||||
)}
|
||||
</Button>
|
||||
)}
|
||||
|
||||
{/* Player */}
|
||||
{isPlayerOpen && (
|
||||
<StreamingPlayer
|
||||
entry={entry}
|
||||
seasons={seasons}
|
||||
currentSeason={currentSeason}
|
||||
currentEpisode={currentEpisode}
|
||||
onEpisodeChange={handleEpisodeChange}
|
||||
onMarkAsWatched={handleMarkAsWatched}
|
||||
/>
|
||||
)}
|
||||
</div>
|
||||
);
|
||||
}
|
||||
Reference in new issue
Block a user