import { useState } from "react"; import { useParams, useNavigate, Link } from "react-router-dom"; import { useQuery } from "@tanstack/react-query"; import { seasonsApi } from "@/api/seasons"; import { useSeason } from "@/hooks/useSeasons"; import { useEntry, useEntrySeasons } from "@/hooks/useEntries"; import { useLibraryStore } from "@/stores/useLibraryStore"; import { useEpisodeSeenByEpisode, useDeleteEpisodeSeen, } from "@/hooks/useEpisodeSeen"; import { StreamingDialog } from "@/components/streaming/StreamingDialog"; import { useStreamingStore } from "@/stores/useStreamingStore"; import { Skeleton } from "@/components/ui/skeleton"; import { Button } from "@/components/ui/button"; import { Separator } from "@/components/ui/separator"; import { EpisodeWatchHistory } from "@/components/entries/EpisodeWatchHistory"; import { EpisodeSeenEditDialog } from "@/components/dialogs/EpisodeSeenEditDialog"; import { ConfirmDialog } from "@/components/dialogs/ConfirmDialog"; import { CastList } from "@/components/entries/CastList"; import { Eye, ChevronLeft, ChevronRight, Clock, PlayCircle, } from "lucide-react"; import { getTmdbPosterUrl, formatDate, formatRuntime } from "@/lib/media-utils"; import type { Episode, EpisodeSeen } from "@/types"; export function EpisodePage() { const { entryId, seasonId, episodeId } = useParams<{ entryId: string; seasonId: string; episodeId: string; }>(); const navigate = useNavigate(); const { data: episodes = [] } = useQuery({ queryKey: ["seasons", parseInt(seasonId!, 10), "episodes"], queryFn: () => seasonsApi.getEpisodes(parseInt(seasonId!, 10)), }); const { data: season } = useSeason(parseInt(seasonId!, 10)); const { data: entry } = useEntry(parseInt(entryId!, 10)); const { data: entrySeasons } = useEntrySeasons(parseInt(entryId!, 10)); const entryTitle = entry?.title ?? useLibraryStore.getState().entries.get(parseInt(entryId!, 10))?.title; const seasonNumber = season?.season_number; const { initialize, play } = useStreamingStore(); const [streamingOpen, setStreamingOpen] = useState(false); const epId = parseInt(episodeId!, 10); const entryIdNum = parseInt(entryId!, 10); const episode: Episode | undefined = episodes.find( (ep: Episode) => ep.id === epId, ); const { data: watchHistory = [] } = useEpisodeSeenByEpisode(epId); const deleteEpisodeSeen = useDeleteEpisodeSeen(); const [editRecord, setEditRecord] = useState(null); const [deleteTarget, setDeleteTarget] = useState(null); const [showAddDialog, setShowAddDialog] = useState(false); // Find prev/next episode const currentIndex = episodes.findIndex((ep: Episode) => ep.id === epId); const prevEpisode: Episode | undefined = currentIndex > 0 ? episodes[currentIndex - 1] : undefined; const nextEpisode: Episode | undefined = currentIndex < episodes.length - 1 ? episodes[currentIndex + 1] : undefined; if (!episode) { return (
); } return (
{/* Breadcrumb: Title / Season x / Episode x */}
{entryTitle || "Entry"} / Season {seasonNumber ?? "?"} / Episode {episode.episode_number}
{/* Top bar: nav breadcrumb + Mark Watched */}
S{String(seasonNumber ?? 0).padStart(2, "0")} E {String(episode.episode_number).padStart(2, "0")}
{/* High-quality thumbnail — click to watch */} {episode.still_path && (
{ if (!entry?.entryIds?.tmdb_id || !seasonNumber) return; initialize(); play({ type: "tv", tmdbId: entry.entryIds.tmdb_id, imdbId: entry.entryIds?.imdb_id || undefined, season: seasonNumber, episode: episode.episode_number, }); setStreamingOpen(true); }} > {episode.name}
Watch Episode
)} {/* Info */}

{episode.name}

{episode.air_date && {formatDate(episode.air_date)}} {episode.runtime && ( {formatRuntime(episode.runtime)} )}
{episode.overview && (

{episode.overview}

)}
{/* Guest Stars */} {episode.guest_stars && episode.guest_stars.length > 0 && ( <> )} {/* Watch History */} {watchHistory.length > 0 && ( <> { const rec = watchHistory.find((r) => r.id === id) as | EpisodeSeen | undefined; if (rec) setEditRecord(rec); }} onDelete={(id) => setDeleteTarget(id)} /> )} {/* Prev / Next navigation */}
{prevEpisode ? ( ) : (
)} {nextEpisode ? ( ) : (
)}
{/* Add watch record dialog */} {showAddDialog && ( )} {/* Edit dialog */} {editRecord && ( { if (!open) setEditRecord(null); }} mode="edit" record={editRecord} entryId={entryIdNum} seasonId={parseInt(seasonId!, 10)} /> )} {/* Delete confirm dialog */} { if (!open) setDeleteTarget(null); }} title="Delete Watch Record" description="Are you sure you want to remove this watch record? This cannot be undone." confirmLabel="Delete" variant="destructive" onConfirm={() => { if (deleteTarget !== null) { deleteEpisodeSeen.mutate({ id: deleteTarget, entryId: entryIdNum, episodeId: epId, seasonId: parseInt(seasonId!, 10), }); setDeleteTarget(null); } }} /> {/* Streaming dialog */} {entry && streamingOpen && ( { setStreamingOpen(false); useStreamingStore.getState().closePlayer(); }} entry={entry} seasons={entrySeasons} /> )}
); }