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,41 @@
|
||||
import { Button } from '@/components/ui/button';
|
||||
import { CheckCircle2, X } from 'lucide-react';
|
||||
|
||||
interface AutoMarkWatchedPromptProps {
|
||||
onAccept: () => void;
|
||||
onDecline: () => void;
|
||||
}
|
||||
|
||||
export function AutoMarkWatchedPrompt({ onAccept, onDecline }: AutoMarkWatchedPromptProps) {
|
||||
return (
|
||||
<div className="absolute bottom-4 left-1/2 -translate-x-1/2 z-30 animate-in slide-in-from-bottom-4 fade-in duration-300">
|
||||
<div className="flex items-center gap-3 bg-popover/95 backdrop-blur-lg border border-border rounded-xl px-4 py-3 shadow-xl max-w-sm">
|
||||
<CheckCircle2 className="h-5 w-5 text-primary shrink-0" />
|
||||
<div className="flex-1 min-w-0">
|
||||
<p className="text-sm font-medium">Auto-mark as watched?</p>
|
||||
<p className="text-xs text-muted-foreground">
|
||||
Automatically mark episodes/movies as watched when you finish them
|
||||
</p>
|
||||
</div>
|
||||
<div className="flex items-center gap-1.5 shrink-0">
|
||||
<Button
|
||||
size="sm"
|
||||
variant="default"
|
||||
className="h-7 text-xs px-3"
|
||||
onClick={onAccept}
|
||||
>
|
||||
Enable
|
||||
</Button>
|
||||
<Button
|
||||
size="sm"
|
||||
variant="ghost"
|
||||
className="h-7 w-7 p-0"
|
||||
onClick={onDecline}
|
||||
>
|
||||
<X className="h-3.5 w-3.5" />
|
||||
</Button>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
@@ -0,0 +1,114 @@
|
||||
import { useMemo } from 'react';
|
||||
import { Button } from '@/components/ui/button';
|
||||
import { ScrollArea, ScrollBar } from '@/components/ui/scroll-area';
|
||||
import { ChevronLeft, ChevronRight } from 'lucide-react';
|
||||
import type { Season } from '@/types';
|
||||
|
||||
interface EpisodeNavigatorProps {
|
||||
seasons: Season[];
|
||||
currentSeason?: number;
|
||||
currentEpisode?: number;
|
||||
onEpisodeChange?: (season: number, episode: number) => void;
|
||||
}
|
||||
|
||||
export function EpisodeNavigator({
|
||||
seasons,
|
||||
currentSeason,
|
||||
currentEpisode,
|
||||
onEpisodeChange,
|
||||
}: EpisodeNavigatorProps) {
|
||||
const activeSeason = useMemo(
|
||||
() => seasons.find((s) => s.season_number === currentSeason),
|
||||
[seasons, currentSeason]
|
||||
);
|
||||
|
||||
const episodes = activeSeason?.episodes || [];
|
||||
|
||||
const hasPrev = currentEpisode && currentEpisode > 1;
|
||||
const hasNext = currentEpisode && activeSeason && currentEpisode < (activeSeason.episode_count || episodes.length);
|
||||
|
||||
// Check if there's a next season
|
||||
const hasNextSeason = useMemo(() => {
|
||||
if (!currentSeason) return false;
|
||||
return seasons.some((s) => s.season_number === currentSeason + 1);
|
||||
}, [seasons, currentSeason]);
|
||||
|
||||
return (
|
||||
<div className="mt-3 space-y-2">
|
||||
{/* Season selector */}
|
||||
<div className="flex items-center gap-2 px-1">
|
||||
<ScrollArea className="flex-1">
|
||||
<div className="flex gap-1.5 pb-1">
|
||||
{seasons
|
||||
.filter((s) => s.season_number > 0)
|
||||
.sort((a, b) => a.season_number - b.season_number)
|
||||
.map((season) => (
|
||||
<Button
|
||||
key={season.id}
|
||||
variant={season.season_number === currentSeason ? 'default' : 'outline'}
|
||||
size="sm"
|
||||
className="shrink-0 h-7 text-xs px-3"
|
||||
onClick={() => onEpisodeChange?.(season.season_number, 1)}
|
||||
>
|
||||
S{season.season_number}
|
||||
</Button>
|
||||
))}
|
||||
</div>
|
||||
<ScrollBar orientation="horizontal" />
|
||||
</ScrollArea>
|
||||
</div>
|
||||
|
||||
{/* Episode navigation */}
|
||||
<div className="flex items-center gap-2">
|
||||
<Button
|
||||
variant="ghost"
|
||||
size="icon"
|
||||
className="h-8 w-8 shrink-0"
|
||||
disabled={!hasPrev}
|
||||
onClick={() => hasPrev && onEpisodeChange?.(currentSeason!, currentEpisode! - 1)}
|
||||
>
|
||||
<ChevronLeft className="h-4 w-4" />
|
||||
</Button>
|
||||
|
||||
<ScrollArea className="flex-1">
|
||||
<div className="flex gap-1 pb-1">
|
||||
{episodes
|
||||
.sort((a, b) => a.episode_number - b.episode_number)
|
||||
.map((ep) => (
|
||||
<Button
|
||||
key={ep.id}
|
||||
variant={ep.episode_number === currentEpisode ? 'default' : 'ghost'}
|
||||
size="sm"
|
||||
className={`shrink-0 h-7 w-7 p-0 text-xs ${
|
||||
ep.episode_number === currentEpisode
|
||||
? ''
|
||||
: 'text-muted-foreground hover:text-foreground'
|
||||
}`}
|
||||
onClick={() => onEpisodeChange?.(currentSeason!, ep.episode_number)}
|
||||
>
|
||||
{ep.episode_number}
|
||||
</Button>
|
||||
))}
|
||||
</div>
|
||||
<ScrollBar orientation="horizontal" />
|
||||
</ScrollArea>
|
||||
|
||||
<Button
|
||||
variant="ghost"
|
||||
size="icon"
|
||||
className="h-8 w-8 shrink-0"
|
||||
disabled={!hasNext && !hasNextSeason}
|
||||
onClick={() => {
|
||||
if (hasNext) {
|
||||
onEpisodeChange?.(currentSeason!, currentEpisode! + 1);
|
||||
} else if (hasNextSeason) {
|
||||
onEpisodeChange?.(currentSeason! + 1, 1);
|
||||
}
|
||||
}}
|
||||
>
|
||||
<ChevronRight className="h-4 w-4" />
|
||||
</Button>
|
||||
</div>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
@@ -0,0 +1,50 @@
|
||||
import { useStreamingStore } from '@/stores/useStreamingStore';
|
||||
import { Button } from '@/components/ui/button';
|
||||
import {
|
||||
DropdownMenu,
|
||||
DropdownMenuContent,
|
||||
DropdownMenuItem,
|
||||
DropdownMenuTrigger,
|
||||
} from '@/components/ui/dropdown-menu';
|
||||
import { Monitor, Check, Loader2 } from 'lucide-react';
|
||||
|
||||
export function ServerSelector() {
|
||||
const { servers, activeServer, isLoading, switchServer } = useStreamingStore();
|
||||
|
||||
return (
|
||||
<DropdownMenu>
|
||||
<DropdownMenuTrigger asChild>
|
||||
<Button
|
||||
variant="ghost"
|
||||
size="sm"
|
||||
className="h-8 gap-1.5 text-white/80 hover:text-white hover:bg-white/10 text-xs font-medium"
|
||||
>
|
||||
{isLoading ? (
|
||||
<Loader2 className="h-3.5 w-3.5 animate-spin" />
|
||||
) : (
|
||||
<Monitor className="h-3.5 w-3.5" />
|
||||
)}
|
||||
<span className="hidden sm:inline">{activeServer?.name || 'Select Server'}</span>
|
||||
<span className="sm:hidden">Server</span>
|
||||
</Button>
|
||||
</DropdownMenuTrigger>
|
||||
<DropdownMenuContent
|
||||
align="start"
|
||||
className="w-52 bg-popover/95 backdrop-blur-lg"
|
||||
>
|
||||
{servers.map((server) => (
|
||||
<DropdownMenuItem
|
||||
key={server.id}
|
||||
onClick={() => switchServer(server.id)}
|
||||
className="flex items-center justify-between"
|
||||
>
|
||||
<span className="text-sm">{server.name}</span>
|
||||
{server.id === activeServer?.id && (
|
||||
<Check className="h-3.5 w-3.5 text-primary" />
|
||||
)}
|
||||
</DropdownMenuItem>
|
||||
))}
|
||||
</DropdownMenuContent>
|
||||
</DropdownMenu>
|
||||
);
|
||||
}
|
||||
@@ -0,0 +1,386 @@
|
||||
import { useCallback, useEffect, useRef, useState } from 'react';
|
||||
import { useStreamingStore } from '@/stores/useStreamingStore';
|
||||
import {
|
||||
isValidEventOrigin,
|
||||
updateProgressFromEvent,
|
||||
isConsideredWatched,
|
||||
logWatchCompleted,
|
||||
getServerSandboxPolicy,
|
||||
getServerAllowedFeatures,
|
||||
isValidEmbedUrl,
|
||||
} from '@/lib/streaming';
|
||||
import type { PlayerEvent } from '@/lib/streaming';
|
||||
import { ServerSelector } from './ServerSelector';
|
||||
import { EpisodeNavigator } from './EpisodeNavigator';
|
||||
import { AutoMarkWatchedPrompt } from './AutoMarkWatchedPrompt';
|
||||
import { Skeleton } from '@/components/ui/skeleton';
|
||||
import { Button } from '@/components/ui/button';
|
||||
import {
|
||||
X,
|
||||
Maximize2,
|
||||
Minimize2,
|
||||
AlertTriangle,
|
||||
RefreshCw,
|
||||
SkipForward,
|
||||
} from 'lucide-react';
|
||||
import type { Entry, Season } from '@/types';
|
||||
|
||||
interface StreamingPlayerProps {
|
||||
entry: Entry;
|
||||
seasons?: Season[];
|
||||
currentSeason?: number;
|
||||
currentEpisode?: number;
|
||||
onEpisodeChange?: (season: number, episode: number) => void;
|
||||
onMarkAsWatched?: (season?: number, episode?: number) => void;
|
||||
}
|
||||
|
||||
export function StreamingPlayer({
|
||||
entry,
|
||||
seasons,
|
||||
currentSeason,
|
||||
currentEpisode,
|
||||
onEpisodeChange,
|
||||
onMarkAsWatched,
|
||||
}: StreamingPlayerProps) {
|
||||
const {
|
||||
currentStreamUrl,
|
||||
isLoading,
|
||||
error,
|
||||
activeServer,
|
||||
isPlayerOpen,
|
||||
autoMarkWatched,
|
||||
autoMarkPromptShown,
|
||||
closePlayer,
|
||||
tryNextServer,
|
||||
play,
|
||||
currentRequest,
|
||||
} = useStreamingStore();
|
||||
|
||||
const iframeRef = useRef<HTMLIFrameElement>(null);
|
||||
const [isFullscreen, setIsFullscreen] = useState(false);
|
||||
const [iframeLoaded, setIframeLoaded] = useState(false);
|
||||
const [showAutoMarkPrompt, setShowAutoMarkPrompt] = useState(false);
|
||||
const [markedAsWatched, setMarkedAsWatched] = useState<Set<string>>(new Set());
|
||||
const containerRef = useRef<HTMLDivElement>(null);
|
||||
|
||||
// Handle fullscreen
|
||||
const toggleFullscreen = useCallback(() => {
|
||||
if (!containerRef.current) return;
|
||||
|
||||
if (!document.fullscreenElement) {
|
||||
containerRef.current.requestFullscreen().then(() => setIsFullscreen(true));
|
||||
} else {
|
||||
document.exitFullscreen().then(() => setIsFullscreen(false));
|
||||
}
|
||||
}, []);
|
||||
|
||||
// Listen for fullscreen change
|
||||
useEffect(() => {
|
||||
const handler = () => setIsFullscreen(!!document.fullscreenElement);
|
||||
document.addEventListener('fullscreenchange', handler);
|
||||
return () => document.removeEventListener('fullscreenchange', handler);
|
||||
}, []);
|
||||
|
||||
// Keyboard shortcuts
|
||||
useEffect(() => {
|
||||
const handler = (e: KeyboardEvent) => {
|
||||
if (!isPlayerOpen) return;
|
||||
|
||||
switch (e.key) {
|
||||
case 'Escape':
|
||||
if (isFullscreen) {
|
||||
document.exitFullscreen();
|
||||
} else {
|
||||
closePlayer();
|
||||
}
|
||||
break;
|
||||
case 'f':
|
||||
case 'F':
|
||||
if (!e.ctrlKey && !e.metaKey) {
|
||||
toggleFullscreen();
|
||||
}
|
||||
break;
|
||||
}
|
||||
};
|
||||
|
||||
window.addEventListener('keydown', handler);
|
||||
return () => window.removeEventListener('keydown', handler);
|
||||
}, [isPlayerOpen, isFullscreen, closePlayer, toggleFullscreen]);
|
||||
|
||||
// Listen for player events from iframe
|
||||
useEffect(() => {
|
||||
const handler = (event: MessageEvent) => {
|
||||
if (!isValidEventOrigin(event.origin)) return;
|
||||
|
||||
// Handle VidLink progress data
|
||||
if (event.data?.type === 'MEDIA_DATA') {
|
||||
// VidLink specific progress tracking
|
||||
return;
|
||||
}
|
||||
|
||||
// Handle player events from VidLink and VidKing
|
||||
if (event.data?.type === 'PLAYER_EVENT') {
|
||||
const data = event.data.data;
|
||||
const playerEvent: PlayerEvent = {
|
||||
event: data.event,
|
||||
currentTime: data.currentTime || 0,
|
||||
duration: data.duration || 0,
|
||||
mediaType: entry.is_tvseries ? 'tv' : 'movie',
|
||||
tmdbId: entry.entryIds?.tmdb_id || 0,
|
||||
season: currentSeason,
|
||||
episode: currentEpisode,
|
||||
progress: data.progress,
|
||||
};
|
||||
|
||||
// Update progress
|
||||
updateProgressFromEvent(
|
||||
playerEvent,
|
||||
entry.title,
|
||||
entry.poster_path || undefined
|
||||
);
|
||||
|
||||
// Check for completion
|
||||
if (playerEvent.event === 'ended' || isConsideredWatched(
|
||||
playerEvent.currentTime,
|
||||
playerEvent.duration,
|
||||
autoMarkWatched.threshold
|
||||
)) {
|
||||
handleWatchCompleted(playerEvent);
|
||||
}
|
||||
}
|
||||
|
||||
// Handle VidKing string events
|
||||
if (typeof event.data === 'string') {
|
||||
try {
|
||||
const parsed = JSON.parse(event.data);
|
||||
if (parsed?.type === 'PLAYER_EVENT') {
|
||||
const data = parsed.data;
|
||||
if (data.event === 'ended') {
|
||||
handleWatchCompleted({
|
||||
event: 'ended',
|
||||
currentTime: data.currentTime || data.timestamp || 0,
|
||||
duration: data.duration || 0,
|
||||
mediaType: entry.is_tvseries ? 'tv' : 'movie',
|
||||
tmdbId: entry.entryIds?.tmdb_id || 0,
|
||||
season: currentSeason,
|
||||
episode: currentEpisode,
|
||||
});
|
||||
}
|
||||
}
|
||||
} catch {
|
||||
// Not JSON, ignore
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
window.addEventListener('message', handler);
|
||||
return () => window.removeEventListener('message', handler);
|
||||
}, [entry, currentSeason, currentEpisode, autoMarkWatched]);
|
||||
|
||||
const handleWatchCompleted = useCallback(
|
||||
(event: PlayerEvent) => {
|
||||
const key = entry.is_tvseries
|
||||
? `${event.tmdbId}-s${currentSeason}e${currentEpisode}`
|
||||
: `${event.tmdbId}`;
|
||||
|
||||
// Don't mark the same thing twice in this session
|
||||
if (markedAsWatched.has(key)) return;
|
||||
|
||||
// Log the watch event
|
||||
logWatchCompleted({
|
||||
tmdbId: event.tmdbId,
|
||||
type: event.mediaType,
|
||||
title: entry.title,
|
||||
season: currentSeason,
|
||||
episode: currentEpisode,
|
||||
completedAt: Date.now(),
|
||||
duration: event.duration,
|
||||
});
|
||||
|
||||
if (autoMarkWatched.enabled) {
|
||||
// Auto-mark as watched
|
||||
onMarkAsWatched?.(currentSeason, currentEpisode);
|
||||
setMarkedAsWatched((prev) => new Set(prev).add(key));
|
||||
} else if (!autoMarkPromptShown) {
|
||||
// Show the prompt
|
||||
setShowAutoMarkPrompt(true);
|
||||
}
|
||||
},
|
||||
[entry, currentSeason, currentEpisode, autoMarkWatched, autoMarkPromptShown, markedAsWatched, onMarkAsWatched]
|
||||
);
|
||||
|
||||
// Handle next episode
|
||||
const handleNextEpisode = useCallback(() => {
|
||||
if (!entry.is_tvseries || !seasons || !currentSeason || !currentEpisode) return;
|
||||
|
||||
const season = seasons.find((s) => s.season_number === currentSeason);
|
||||
if (!season?.episodes) return;
|
||||
|
||||
const nextEp = currentEpisode + 1;
|
||||
const episodeExists = season.episodes.some((e) => e.episode_number === nextEp);
|
||||
|
||||
if (episodeExists) {
|
||||
onEpisodeChange?.(currentSeason, nextEp);
|
||||
} else {
|
||||
// Try next season
|
||||
const nextSeason = seasons.find((s) => s.season_number === currentSeason + 1);
|
||||
if (nextSeason?.episodes?.length) {
|
||||
onEpisodeChange?.(nextSeason.season_number, 1);
|
||||
}
|
||||
}
|
||||
}, [entry, seasons, currentSeason, currentEpisode, onEpisodeChange]);
|
||||
|
||||
// Reset iframe loaded state when URL changes
|
||||
useEffect(() => {
|
||||
setIframeLoaded(false);
|
||||
}, [currentStreamUrl]);
|
||||
|
||||
if (!isPlayerOpen) return null;
|
||||
|
||||
return (
|
||||
<div
|
||||
ref={containerRef}
|
||||
className={`relative w-full bg-black rounded-xl overflow-hidden ${
|
||||
isFullscreen ? 'fixed inset-0 z-100 rounded-none' : 'aspect-video'
|
||||
}`}
|
||||
>
|
||||
{/* Top controls bar */}
|
||||
<div className="absolute top-0 left-0 right-0 z-20 flex items-center justify-between p-3 bg-linear-to-b from-black/80 to-transparent opacity-0 hover:opacity-100 transition-opacity duration-200">
|
||||
<div className="flex items-center gap-2">
|
||||
<ServerSelector />
|
||||
{entry.is_tvseries && seasons && currentSeason && currentEpisode && (
|
||||
<span className="text-white/70 text-sm font-medium px-2">
|
||||
S{currentSeason}:E{currentEpisode}
|
||||
</span>
|
||||
)}
|
||||
</div>
|
||||
<div className="flex items-center gap-1">
|
||||
{entry.is_tvseries && (
|
||||
<Button
|
||||
variant="ghost"
|
||||
size="icon"
|
||||
className="h-8 w-8 text-white/80 hover:text-white hover:bg-white/10"
|
||||
onClick={handleNextEpisode}
|
||||
title="Next Episode"
|
||||
>
|
||||
<SkipForward className="h-4 w-4" />
|
||||
</Button>
|
||||
)}
|
||||
<Button
|
||||
variant="ghost"
|
||||
size="icon"
|
||||
className="h-8 w-8 text-white/80 hover:text-white hover:bg-white/10"
|
||||
onClick={toggleFullscreen}
|
||||
title={isFullscreen ? 'Exit Fullscreen (F)' : 'Fullscreen (F)'}
|
||||
>
|
||||
{isFullscreen ? (
|
||||
<Minimize2 className="h-4 w-4" />
|
||||
) : (
|
||||
<Maximize2 className="h-4 w-4" />
|
||||
)}
|
||||
</Button>
|
||||
<Button
|
||||
variant="ghost"
|
||||
size="icon"
|
||||
className="h-8 w-8 text-white/80 hover:text-white hover:bg-white/10"
|
||||
onClick={closePlayer}
|
||||
title="Close Player (Esc)"
|
||||
>
|
||||
<X className="h-4 w-4" />
|
||||
</Button>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
{/* Loading state */}
|
||||
{(isLoading || !iframeLoaded) && !error && (
|
||||
<div className="absolute inset-0 z-10 flex items-center justify-center bg-black">
|
||||
<div className="flex flex-col items-center gap-3">
|
||||
<div className="h-10 w-10 rounded-full border-2 border-primary border-t-transparent animate-spin" />
|
||||
<p className="text-white/60 text-sm">Loading stream...</p>
|
||||
</div>
|
||||
</div>
|
||||
)}
|
||||
|
||||
{/* Error state */}
|
||||
{error && (
|
||||
<div className="absolute inset-0 z-10 flex items-center justify-center bg-black/95">
|
||||
<div className="flex flex-col items-center gap-4 max-w-sm text-center px-6">
|
||||
<div className="h-14 w-14 rounded-full bg-destructive/10 flex items-center justify-center">
|
||||
<AlertTriangle className="h-7 w-7 text-destructive" />
|
||||
</div>
|
||||
<div className="space-y-1">
|
||||
<h3 className="text-white font-semibold">Stream Unavailable</h3>
|
||||
<p className="text-white/60 text-sm">{error.message}</p>
|
||||
</div>
|
||||
<div className="flex gap-2">
|
||||
<Button
|
||||
variant="outline"
|
||||
size="sm"
|
||||
onClick={tryNextServer}
|
||||
className="bg-white/5 border-white/20 text-white hover:bg-white/10"
|
||||
>
|
||||
<RefreshCw className="mr-1.5 h-3.5 w-3.5" />
|
||||
Try Another Server
|
||||
</Button>
|
||||
<Button
|
||||
variant="outline"
|
||||
size="sm"
|
||||
onClick={closePlayer}
|
||||
className="bg-white/5 border-white/20 text-white hover:bg-white/10"
|
||||
>
|
||||
Close
|
||||
</Button>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
)}
|
||||
|
||||
{/* Video iframe */}
|
||||
{currentStreamUrl && isValidEmbedUrl(currentStreamUrl) && (
|
||||
<iframe
|
||||
ref={iframeRef}
|
||||
src={currentStreamUrl}
|
||||
className="absolute inset-0 w-full h-full"
|
||||
frameBorder="0"
|
||||
allowFullScreen
|
||||
sandbox={activeServer ? getServerSandboxPolicy(activeServer.id) : undefined}
|
||||
allow={activeServer ? getServerAllowedFeatures(activeServer.id) : undefined}
|
||||
onLoad={() => setIframeLoaded(true)}
|
||||
onError={() => {
|
||||
useStreamingStore.getState().setError({
|
||||
server: activeServer!,
|
||||
message: 'Failed to load stream. The server may be unavailable.',
|
||||
code: 'UNAVAILABLE',
|
||||
});
|
||||
}}
|
||||
/>
|
||||
)}
|
||||
|
||||
{/* Episode navigator (bottom bar for TV) */}
|
||||
{entry.is_tvseries && seasons && isPlayerOpen && !isFullscreen && (
|
||||
<EpisodeNavigator
|
||||
seasons={seasons}
|
||||
currentSeason={currentSeason}
|
||||
currentEpisode={currentEpisode}
|
||||
onEpisodeChange={onEpisodeChange}
|
||||
/>
|
||||
)}
|
||||
|
||||
{/* Auto mark watched prompt */}
|
||||
{showAutoMarkPrompt && (
|
||||
<AutoMarkWatchedPrompt
|
||||
onAccept={() => {
|
||||
useStreamingStore.getState().enableAutoMarkWatched();
|
||||
setShowAutoMarkPrompt(false);
|
||||
// Mark the current one
|
||||
onMarkAsWatched?.(currentSeason, currentEpisode);
|
||||
}}
|
||||
onDecline={() => {
|
||||
useStreamingStore.getState().disableAutoMarkWatched();
|
||||
setShowAutoMarkPrompt(false);
|
||||
}}
|
||||
/>
|
||||
)}
|
||||
</div>
|
||||
);
|
||||
}
|
||||
@@ -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>
|
||||
);
|
||||
}
|
||||
@@ -0,0 +1,5 @@
|
||||
export { StreamingPlayer } from './StreamingPlayer';
|
||||
export { StreamingSection } from './StreamingSection';
|
||||
export { ServerSelector } from './ServerSelector';
|
||||
export { EpisodeNavigator } from './EpisodeNavigator';
|
||||
export { AutoMarkWatchedPrompt } from './AutoMarkWatchedPrompt';
|
||||
Reference in new issue
Block a user