feat(streaming): add watch button to episode cards and episode details page

- Add play overlay on episode thumbnails in SeasonPage (click to stream directly)
- Add clickable watch overlay on episode still image in EpisodePage
- Fix StreamingDialog rendering via React portal (document.body) to prevent ancestor overflow/max-width clipping
- Fix EpisodePage JSX parse error (streaming dialog was outside root element)
- Fix watch buttons not working: use useEntry hook for full entry data with entryIds instead of library store (which lacks entryIds)
- Fix preventDefault called after guard check, causing link navigation when tmdb_id was missing
- Pass seasons via useEntrySeasons to StreamingDialog from both pages so episodes sidebar renders correctly
- Apply final UI polish: entry title in dialog top bar, season selector label, episode format "E1 Title", watched badge always-on, unreleased episodes greyed out
This commit is contained in:
Mohammed Abad committed 2026-05-25 04:25:45 +01:00
1 parent afc032ca1c
commit 022c9a271b
7 files changed
+708 -518

No files matched your search

@@ -1,7 +1,7 @@
import { useCallback, useEffect, useMemo, useState } from 'react';
import { useCallback, useEffect, useMemo, useState } from 'react';
import { useStreamingStore } from '@/stores/useStreamingStore';
import { getEntryWatchProgress } from '@/lib/streaming';
import { StreamingPlayer } from './StreamingPlayer';
import { StreamingDialog } from './StreamingDialog';
import { Button } from '@/components/ui/button';
import { Play, Tv } from 'lucide-react';
import { useCreateEntrySeen } from '@/hooks/useEntrySeen';
@@ -14,35 +14,29 @@ interface StreamingSectionProps {
}
export function StreamingSection({ entry, seasons }: StreamingSectionProps) {
const {
isPlayerOpen,
initialize,
play,
currentRequest,
} = useStreamingStore();
const { initialize, play } = useStreamingStore();
// Local state — prevents player from auto-opening when navigating between entries
const [isDialogOpen, setIsDialogOpen] = useState(false);
const createEntrySeen = useCreateEntrySeen();
const createEpisodeSeen = useCreateEpisodeSeen();
const [currentSeason, setCurrentSeason] = useState<number>(1);
const [currentEpisode, setCurrentEpisode] = useState<number>(1);
// Initialize streaming system
// Initialize streaming system once
useEffect(() => {
initialize();
}, [initialize]);
// Determine continue watching state
// Watch progress from localStorage
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
// Determine "continue watching" position for TV shows
const continueWatchingInfo = useMemo(() => {
if (!entry.is_tvseries) return null;
// Check local progress first
if (watchProgress?.lastSeasonWatched && watchProgress?.lastEpisodeWatched) {
return {
season: watchProgress.lastSeasonWatched,
@@ -51,72 +45,43 @@ export function StreamingSection({ entry, seasons }: StreamingSectionProps) {
};
}
// Check episode seen data from the library
if (entry.episodeSeens && entry.episodeSeens.length > 0) {
// Find the last watched episode
if (entry.episodeSeens && entry.episodeSeens.length > 0 && seasons) {
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}`,
};
for (const season of seasons) {
const ep = season.episodes?.find((e) => e.id === last.episode_id);
if (ep) {
const nextEp = ep.episode_number + 1;
if (season.episodes?.some((e) => e.episode_number === nextEp)) {
return { season: season.season_number, episode: nextEp, label: `Continue S${season.season_number}:E${nextEp}` };
}
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` };
}
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 initialSeason = entry.is_tvseries ? (continueWatchingInfo?.season ?? 1) : 1;
const initialEpisode = entry.is_tvseries ? (continueWatchingInfo?.episode ?? 1) : 1;
const handlePlay = useCallback(() => {
const handleOpen = 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,
season: initialSeason,
episode: initialEpisode,
startAt: watchProgress?.showProgress?.[`s${initialSeason}e${initialEpisode}`]?.watched,
}
: {
type: 'movie' as const,
@@ -124,36 +89,18 @@ export function StreamingSection({ entry, seasons }: StreamingSectionProps) {
imdbId: entry.entryIds?.imdb_id || undefined,
startAt: watchProgress?.watched,
};
play(request);
}, [entry, currentSeason, currentEpisode, watchProgress, play]);
setIsDialogOpen(true);
}, [entry, initialSeason, initialEpisode, 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 handleClose = useCallback(() => {
setIsDialogOpen(false);
useStreamingStore.getState().closePlayer();
}, []);
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) {
@@ -166,7 +113,6 @@ export function StreamingSection({ entry, seasons }: StreamingSectionProps) {
});
}
} else {
// Movie
createEntrySeen.mutate({
entry_id: entry.id,
date_seen: new Date().toISOString().split('T')[0],
@@ -177,43 +123,38 @@ export function StreamingSection({ entry, seasons }: StreamingSectionProps) {
[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>
)}
const buttonLabel = entry.is_tvseries
? (continueWatchingInfo?.label ?? 'Start Watching')
: watchProgress && watchProgress.watched > 60
? 'Continue Watching'
: 'Watch Now';
{/* Player */}
{isPlayerOpen && (
<StreamingPlayer
entry={entry}
seasons={seasons}
currentSeason={currentSeason}
currentEpisode={currentEpisode}
onEpisodeChange={handleEpisodeChange}
onMarkAsWatched={handleMarkAsWatched}
/>
)}
</div>
return (
<>
<Button
onClick={handleOpen}
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" />
) : (
<Play className="h-4 w-4 fill-current" />
)}
{buttonLabel}
</Button>
<StreamingDialog
entry={entry}
seasons={seasons}
initialSeason={initialSeason}
initialEpisode={initialEpisode}
isOpen={isDialogOpen}
onClose={handleClose}
onMarkAsWatched={handleMarkAsWatched}
/>
</>
);
}