Files
Showy/react_frontend/src/components/streaming/StreamingSection.tsx
T
Mohammed Abad 022c9a271b 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
2026-05-25 04:25:45 +01:00

161 lines
5.6 KiB
TypeScript

import { useCallback, useEffect, useMemo, useState } from 'react';
import { useStreamingStore } from '@/stores/useStreamingStore';
import { getEntryWatchProgress } from '@/lib/streaming';
import { StreamingDialog } from './StreamingDialog';
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 { 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();
// Initialize streaming system once
useEffect(() => {
initialize();
}, [initialize]);
// Watch progress from localStorage
const watchProgress = useMemo(() => {
if (!entry.entryIds?.tmdb_id) return null;
return getEntryWatchProgress(entry.entryIds.tmdb_id);
}, [entry.entryIds?.tmdb_id]);
// Determine "continue watching" position for TV shows
const continueWatchingInfo = useMemo(() => {
if (!entry.is_tvseries) return null;
if (watchProgress?.lastSeasonWatched && watchProgress?.lastEpisodeWatched) {
return {
season: watchProgress.lastSeasonWatched,
episode: watchProgress.lastEpisodeWatched,
label: `Continue S${watchProgress.lastSeasonWatched}:E${watchProgress.lastEpisodeWatched}`,
};
}
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];
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}` };
}
}
}
return { season: 1, episode: 1, label: 'Start Watching' };
}, [entry, seasons, watchProgress]);
const initialSeason = entry.is_tvseries ? (continueWatchingInfo?.season ?? 1) : 1;
const initialEpisode = entry.is_tvseries ? (continueWatchingInfo?.episode ?? 1) : 1;
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: initialSeason,
episode: initialEpisode,
startAt: watchProgress?.showProgress?.[`s${initialSeason}e${initialEpisode}`]?.watched,
}
: {
type: 'movie' as const,
tmdbId: entry.entryIds.tmdb_id,
imdbId: entry.entryIds?.imdb_id || undefined,
startAt: watchProgress?.watched,
};
play(request);
setIsDialogOpen(true);
}, [entry, initialSeason, initialEpisode, watchProgress, play]);
const handleClose = useCallback(() => {
setIsDialogOpen(false);
useStreamingStore.getState().closePlayer();
}, []);
const handleMarkAsWatched = useCallback(
(season?: number, episode?: number) => {
if (entry.is_tvseries && season && episode && seasons) {
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 {
createEntrySeen.mutate({
entry_id: entry.id,
date_seen: new Date().toISOString().split('T')[0],
accuracy: 'EXACT',
});
}
},
[entry, seasons, createEntrySeen, createEpisodeSeen]
);
if (!entry.entryIds?.tmdb_id) return null;
const buttonLabel = entry.is_tvseries
? (continueWatchingInfo?.label ?? 'Start Watching')
: watchProgress && watchProgress.watched > 60
? 'Continue Watching'
: 'Watch Now';
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}
/>
</>
);
}