Files
Showy/react_frontend/src/pages/EpisodePage.tsx
T

307 lines
10 KiB
TypeScript

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<EpisodeSeen | null>(null);
const [deleteTarget, setDeleteTarget] = useState<number | null>(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 (
<div className="space-y-6">
<Skeleton className="h-48 w-full rounded-lg" />
<Skeleton className="h-6 w-48" />
<Skeleton className="h-4 w-80" />
</div>
);
}
return (
<div className="space-y-8 mx-auto max-w-4xl">
{/* Breadcrumb: Title / Season x / Episode x */}
<div className="flex items-center gap-1.5 text-sm text-muted-foreground">
<Link
to={`/entry/${entryId}`}
className="hover:text-foreground transition-colors truncate max-w-50"
>
{entryTitle || "Entry"}
</Link>
<span>/</span>
<Link
to={`/entry/${entryId}/season/${seasonId}`}
className="hover:text-foreground transition-colors"
>
Season {seasonNumber ?? "?"}
</Link>
<span>/</span>
<span className="text-foreground font-medium">
Episode {episode.episode_number}
</span>
</div>
{/* Top bar: nav breadcrumb + Mark Watched */}
<div className="flex items-center justify-between gap-4">
<div className="flex items-center gap-2 text-sm text-muted-foreground">
<span className="font-medium text-foreground">
S{String(seasonNumber ?? 0).padStart(2, "0")} E
{String(episode.episode_number).padStart(2, "0")}
</span>
</div>
<Button size="sm" onClick={() => setShowAddDialog(true)}>
<Eye className="mr-1.5 h-4 w-4" />
Mark as Watched
</Button>
</div>
{/* High-quality thumbnail — click to watch */}
{episode.still_path && (
<div
className="relative overflow-hidden rounded-xl cursor-pointer group"
onClick={() => {
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);
}}
>
<img
src={getTmdbPosterUrl(episode.still_path, "w1280")}
alt={episode.name}
className="w-full max-h-96 object-cover transition-[filter] group-hover:brightness-50"
/>
<div className="absolute inset-0 flex items-center justify-center opacity-0 group-hover:opacity-100 transition-opacity">
<div className="flex items-center gap-2 bg-black/60 backdrop-blur-sm rounded-full px-5 py-2.5">
<PlayCircle className="h-6 w-6 text-white" />
<span className="text-white font-medium text-sm">
Watch Episode
</span>
</div>
</div>
</div>
)}
{/* Info */}
<div className="space-y-3">
<h1 className="text-2xl font-bold">{episode.name}</h1>
<div className="flex flex-wrap items-center gap-3 text-sm text-muted-foreground">
{episode.air_date && <span>{formatDate(episode.air_date)}</span>}
{episode.runtime && (
<span className="flex items-center gap-1">
<Clock className="h-3 w-3" />
{formatRuntime(episode.runtime)}
</span>
)}
</div>
{episode.overview && (
<p className="text-sm text-muted-foreground leading-relaxed max-w-2xl">
{episode.overview}
</p>
)}
</div>
{/* Guest Stars */}
{episode.guest_stars && episode.guest_stars.length > 0 && (
<>
<Separator />
<CastList credits={episode.guest_stars} />
</>
)}
{/* Watch History */}
{watchHistory.length > 0 && (
<>
<Separator />
<EpisodeWatchHistory
watchHistory={watchHistory}
onEdit={(id) => {
const rec = watchHistory.find((r) => r.id === id) as
| EpisodeSeen
| undefined;
if (rec) setEditRecord(rec);
}}
onDelete={(id) => setDeleteTarget(id)}
/>
</>
)}
{/* Prev / Next navigation */}
<Separator />
<div className="flex items-center justify-between gap-2">
{prevEpisode ? (
<Button
variant="outline"
size="sm"
className="flex items-center gap-1 max-w-60"
onClick={() =>
navigate(
`/entry/${entryId}/season/${seasonId}/episode/${prevEpisode.id}`,
)
}
>
<ChevronLeft className="h-4 w-4 shrink-0" />
<span className="truncate">
E{prevEpisode.episode_number}: {prevEpisode.name}
</span>
</Button>
) : (
<div />
)}
{nextEpisode ? (
<Button
variant="outline"
size="sm"
className="flex items-center gap-1 max-w-60 ml-auto"
onClick={() =>
navigate(
`/entry/${entryId}/season/${seasonId}/episode/${nextEpisode.id}`,
)
}
>
<span className="truncate">
E{nextEpisode.episode_number}: {nextEpisode.name}
</span>
<ChevronRight className="h-4 w-4 shrink-0" />
</Button>
) : (
<div />
)}
</div>
{/* Add watch record dialog */}
{showAddDialog && (
<EpisodeSeenEditDialog
open={showAddDialog}
onOpenChange={setShowAddDialog}
mode="create"
episodeId={episode.id}
entryId={entryIdNum}
seasonId={parseInt(seasonId!, 10)}
/>
)}
{/* Edit dialog */}
{editRecord && (
<EpisodeSeenEditDialog
open={!!editRecord}
onOpenChange={(open) => {
if (!open) setEditRecord(null);
}}
mode="edit"
record={editRecord}
entryId={entryIdNum}
seasonId={parseInt(seasonId!, 10)}
/>
)}
{/* Delete confirm dialog */}
<ConfirmDialog
open={deleteTarget !== null}
onOpenChange={(open) => {
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 && (
<StreamingDialog
isOpen={streamingOpen}
onClose={() => {
setStreamingOpen(false);
useStreamingStore.getState().closePlayer();
}}
entry={entry}
seasons={entrySeasons}
/>
)}
</div>
);
}