343 lines
12 KiB
TypeScript
343 lines
12 KiB
TypeScript
import { useState } from "react";
|
||
import { useParams, Link } from "react-router-dom";
|
||
import {
|
||
useSeason,
|
||
useSeasonEpisodes,
|
||
useRefreshSeason,
|
||
} from "@/hooks/useSeasons";
|
||
import { useEntry, useEntrySeasons } from "@/hooks/useEntries";
|
||
import { useLibraryStore } from "@/stores/useLibraryStore";
|
||
import { EpisodeSeenEditDialog } from "@/components/dialogs/EpisodeSeenEditDialog";
|
||
import { MarkSeasonWatchedDialog } from "@/components/dialogs/MarkSeasonWatchedDialog";
|
||
import { SeasonRatingDialog } from "@/components/dialogs/SeasonRatingDialog";
|
||
import { StreamingDialog } from "@/components/streaming/StreamingDialog";
|
||
import { useStreamingStore } from "@/stores/useStreamingStore";
|
||
import { Skeleton } from "@/components/ui/skeleton";
|
||
import { Progress } from "@/components/ui/progress";
|
||
import { Button } from "@/components/ui/button";
|
||
import {
|
||
DropdownMenu,
|
||
DropdownMenuContent,
|
||
DropdownMenuItem,
|
||
DropdownMenuTrigger,
|
||
} from "@/components/ui/dropdown-menu";
|
||
import {
|
||
ArrowLeft,
|
||
ListChecks,
|
||
MoreHorizontal,
|
||
Play,
|
||
RefreshCw,
|
||
Star,
|
||
Tv,
|
||
CheckCircle2,
|
||
} from "lucide-react";
|
||
import {
|
||
DetailHero,
|
||
DetailSection,
|
||
HeroChip,
|
||
IconAction,
|
||
MetaItem,
|
||
} from "@/components/entries/DetailHero";
|
||
import {
|
||
EpisodeViewToggle,
|
||
SeasonEpisodes,
|
||
} from "@/components/entries/SeasonEpisodes";
|
||
import {
|
||
isEpisodeWatched,
|
||
loadEpisodeView,
|
||
nextUnwatchedEpisode,
|
||
saveEpisodeView,
|
||
type EpisodeView,
|
||
} from "@/lib/episodes";
|
||
import {
|
||
getBackdropUrl,
|
||
getTmdbPosterUrl,
|
||
getYear,
|
||
formatDate,
|
||
formatRuntime,
|
||
} from "@/lib/media-utils";
|
||
import type { Episode } from "@/types";
|
||
|
||
export function SeasonPage() {
|
||
const { entryId, seasonId } = useParams<{
|
||
entryId: string;
|
||
seasonId: string;
|
||
}>();
|
||
const entryIdNum = parseInt(entryId!, 10);
|
||
const seasonIdNum = parseInt(seasonId!, 10);
|
||
const { data: season, isLoading } = useSeason(seasonIdNum);
|
||
const { data: episodes = [] } = useSeasonEpisodes(seasonIdNum);
|
||
const { data: entry } = useEntry(entryIdNum);
|
||
const { data: entrySeasons } = useEntrySeasons(entryIdNum);
|
||
const libraryEntry = useLibraryStore((s) => s.entries.get(entryIdNum));
|
||
const show = entry ?? libraryEntry;
|
||
const { initialize, play } = useStreamingStore();
|
||
const refreshSeason = useRefreshSeason();
|
||
const [dialogEpisode, setDialogEpisode] = useState<Episode | null>(null);
|
||
const [showMarkAllDialog, setShowMarkAllDialog] = useState(false);
|
||
const [showRatingDialog, setShowRatingDialog] = useState(false);
|
||
const [streamingOpen, setStreamingOpen] = useState(false);
|
||
const [view, setView] = useState<EpisodeView>(loadEpisodeView);
|
||
|
||
const changeView = (next: EpisodeView) => {
|
||
setView(next);
|
||
saveEpisodeView(next);
|
||
};
|
||
|
||
const handleWatchEpisode = (episode: Episode) => {
|
||
if (!entry?.entryIds?.tmdb_id || !season) return;
|
||
initialize();
|
||
play({
|
||
type: "tv",
|
||
tmdbId: entry.entryIds.tmdb_id,
|
||
imdbId: entry.entryIds?.imdb_id || undefined,
|
||
season: season.season_number,
|
||
episode: episode.episode_number,
|
||
});
|
||
setStreamingOpen(true);
|
||
};
|
||
|
||
if (isLoading || !season) {
|
||
return (
|
||
<div className="space-y-6">
|
||
<Skeleton className="-mx-4 -mt-4 h-72 md:-mx-6 md:-mt-6" />
|
||
<div className="mx-auto max-w-6xl space-y-3">
|
||
{Array.from({ length: 5 }).map((_, i) => (
|
||
<Skeleton key={i} className="h-20 w-full rounded-xl" />
|
||
))}
|
||
</div>
|
||
</div>
|
||
);
|
||
}
|
||
|
||
const watchedCount = episodes.filter(isEpisodeWatched).length;
|
||
const totalEpisodes = episodes.length;
|
||
const progress = totalEpisodes > 0 ? (watchedCount / totalEpisodes) * 100 : 0;
|
||
const totalRuntime = episodes.reduce((sum, ep) => sum + (ep.runtime ?? 0), 0);
|
||
const airDates = episodes
|
||
.map((ep) => ep.air_date)
|
||
.filter(Boolean)
|
||
.sort() as string[];
|
||
const nextUp = nextUnwatchedEpisode(episodes);
|
||
const userRating = season.user_rating ?? 0;
|
||
const backdrop = show ? getBackdropUrl(show) : null;
|
||
|
||
return (
|
||
<div className="animate-page-enter">
|
||
<DetailHero backdrop={backdrop}>
|
||
<Link
|
||
to={`/entry/${entryId}`}
|
||
className="mb-6 inline-flex items-center gap-1.5 rounded-full border border-border/60 bg-background/60 py-1 pl-2 pr-3 text-sm font-medium backdrop-blur transition-colors hover:bg-accent"
|
||
>
|
||
<ArrowLeft className="h-4 w-4" />
|
||
Back to {show?.title ?? "show"}
|
||
</Link>
|
||
<div className="flex flex-col items-center gap-6 sm:flex-row sm:items-end sm:gap-8">
|
||
<img
|
||
src={getTmdbPosterUrl(season.poster_path, "w500")}
|
||
alt={season.name}
|
||
className="aspect-2/3 w-36 shrink-0 rounded-xl object-cover shadow-2xl shadow-black/50 ring-1 ring-white/10 sm:w-48 lg:w-52"
|
||
/>
|
||
|
||
<div className="min-w-0 flex-1 space-y-4 text-center sm:text-left">
|
||
<div className="flex flex-wrap items-center justify-center gap-2 sm:justify-start">
|
||
<Link to={`/entry/${entryId}`}>
|
||
<HeroChip>
|
||
<Tv />
|
||
{show?.title ?? "Show"}
|
||
</HeroChip>
|
||
</Link>
|
||
<HeroChip>Season {season.season_number}</HeroChip>
|
||
{progress === 100 && totalEpisodes > 0 && (
|
||
<span className="inline-flex items-center gap-1 rounded-full bg-green-500/15 px-2.5 py-0.5 text-xs font-medium text-green-600 dark:text-green-400">
|
||
<CheckCircle2 className="h-3 w-3" /> Completed
|
||
</span>
|
||
)}
|
||
</div>
|
||
|
||
<h1 className="select-text text-4xl font-extrabold leading-[1.05] tracking-tight text-balance sm:text-5xl">
|
||
{season.name}
|
||
</h1>
|
||
|
||
<div className="flex flex-wrap items-center justify-center gap-x-3 gap-y-1 text-sm text-muted-foreground sm:justify-start">
|
||
{season.air_date && (
|
||
<span className="font-medium text-foreground/90">
|
||
{getYear(season.air_date)}
|
||
</span>
|
||
)}
|
||
<MetaItem>
|
||
{totalEpisodes} {totalEpisodes === 1 ? "episode" : "episodes"}
|
||
</MetaItem>
|
||
{totalRuntime > 0 && (
|
||
<MetaItem>{formatRuntime(totalRuntime)}</MetaItem>
|
||
)}
|
||
{airDates.length > 1 && (
|
||
<MetaItem>
|
||
{formatDate(airDates[0])} – {formatDate(airDates.at(-1)!)}
|
||
</MetaItem>
|
||
)}
|
||
</div>
|
||
|
||
{/* Ratings + progress */}
|
||
<div className="flex flex-wrap items-center justify-center gap-x-5 gap-y-3 sm:justify-start">
|
||
<button
|
||
type="button"
|
||
onClick={() => setShowRatingDialog(true)}
|
||
className="inline-flex items-center gap-1.5 rounded-full border border-border/60 bg-background/60 px-3 py-1 text-sm backdrop-blur transition-colors hover:bg-accent"
|
||
>
|
||
<Star
|
||
className={`h-4 w-4 ${userRating > 0 ? "fill-yellow-400 text-yellow-400" : "text-muted-foreground"}`}
|
||
/>
|
||
{userRating > 0 ? (
|
||
<span className="font-semibold tabular-nums">
|
||
{userRating}/10
|
||
</span>
|
||
) : (
|
||
<span className="text-muted-foreground">Rate season</span>
|
||
)}
|
||
</button>
|
||
{season.tmdb_rating ? (
|
||
<span className="text-sm text-muted-foreground tabular-nums">
|
||
TMDB{" "}
|
||
<span className="font-semibold text-sky-500">
|
||
{season.tmdb_rating.toFixed(1)}
|
||
</span>
|
||
</span>
|
||
) : null}
|
||
<div className="flex w-56 items-center gap-2">
|
||
<Progress value={progress} className="h-2 flex-1" />
|
||
<span className="text-xs font-medium tabular-nums text-muted-foreground">
|
||
{watchedCount}/{totalEpisodes}
|
||
</span>
|
||
</div>
|
||
</div>
|
||
|
||
{/* Actions */}
|
||
<div className="flex flex-wrap items-center justify-center gap-2 pt-1 sm:justify-start">
|
||
{nextUp && (
|
||
<Button
|
||
size="lg"
|
||
className="h-11 gap-2 px-6"
|
||
onClick={() => handleWatchEpisode(nextUp)}
|
||
disabled={!entry?.entryIds?.tmdb_id}
|
||
>
|
||
<Play className="h-4 w-4 fill-current" />
|
||
{watchedCount > 0 ? "Continue" : "Start"}: E
|
||
{nextUp.episode_number}
|
||
</Button>
|
||
)}
|
||
<IconAction
|
||
label="Mark season watched"
|
||
onClick={() => setShowMarkAllDialog(true)}
|
||
>
|
||
<ListChecks className="h-4 w-4" />
|
||
</IconAction>
|
||
<IconAction
|
||
label={userRating > 0 ? "Change rating" : "Rate season"}
|
||
onClick={() => setShowRatingDialog(true)}
|
||
active={userRating > 0}
|
||
>
|
||
<Star className="h-4 w-4" />
|
||
</IconAction>
|
||
<DropdownMenu>
|
||
<DropdownMenuTrigger
|
||
render={
|
||
<Button
|
||
variant="outline"
|
||
size="icon-lg"
|
||
className="h-11 w-11 rounded-full border-border/60 bg-background/60 backdrop-blur"
|
||
aria-label="More actions"
|
||
/>
|
||
}
|
||
>
|
||
<MoreHorizontal className="h-4 w-4" />
|
||
</DropdownMenuTrigger>
|
||
<DropdownMenuContent align="end" className="w-48">
|
||
<DropdownMenuItem
|
||
onClick={() => refreshSeason.mutate(season.id)}
|
||
disabled={refreshSeason.isPending}
|
||
>
|
||
<RefreshCw
|
||
className={`mr-2 h-4 w-4 ${refreshSeason.isPending ? "animate-spin" : ""}`}
|
||
/>
|
||
{refreshSeason.isPending
|
||
? "Refreshing..."
|
||
: "Refresh season"}
|
||
</DropdownMenuItem>
|
||
</DropdownMenuContent>
|
||
</DropdownMenu>
|
||
</div>
|
||
</div>
|
||
</div>
|
||
</DetailHero>
|
||
|
||
<div className="mx-auto mt-4 max-w-6xl space-y-10">
|
||
{season.overview && (
|
||
<DetailSection title="Overview">
|
||
<p className="select-text max-w-prose text-[15px] leading-relaxed text-foreground/85">
|
||
{season.overview}
|
||
</p>
|
||
</DetailSection>
|
||
)}
|
||
|
||
<DetailSection
|
||
title="Episodes"
|
||
action={<EpisodeViewToggle view={view} onChange={changeView} />}
|
||
>
|
||
<SeasonEpisodes
|
||
episodes={episodes}
|
||
view={view}
|
||
entryId={entryIdNum}
|
||
seasonId={seasonIdNum}
|
||
onPlay={handleWatchEpisode}
|
||
onMarkWatched={setDialogEpisode}
|
||
/>
|
||
</DetailSection>
|
||
</div>
|
||
|
||
{/* Episode Seen Dialog */}
|
||
{dialogEpisode && (
|
||
<EpisodeSeenEditDialog
|
||
open={!!dialogEpisode}
|
||
onOpenChange={(open) => {
|
||
if (!open) setDialogEpisode(null);
|
||
}}
|
||
mode="create"
|
||
episodeId={dialogEpisode.id}
|
||
entryId={entryIdNum}
|
||
seasonId={seasonIdNum}
|
||
/>
|
||
)}
|
||
|
||
{/* Mark Season Watched dialog */}
|
||
<MarkSeasonWatchedDialog
|
||
open={showMarkAllDialog}
|
||
onOpenChange={setShowMarkAllDialog}
|
||
entryId={entryIdNum}
|
||
seasonId={seasonIdNum}
|
||
episodes={episodes}
|
||
/>
|
||
|
||
{/* Season Rating dialog */}
|
||
<SeasonRatingDialog
|
||
open={showRatingDialog}
|
||
onOpenChange={setShowRatingDialog}
|
||
season={season}
|
||
/>
|
||
|
||
{/* Streaming dialog */}
|
||
{entry && streamingOpen && (
|
||
<StreamingDialog
|
||
isOpen={streamingOpen}
|
||
onClose={() => {
|
||
setStreamingOpen(false);
|
||
useStreamingStore.getState().closePlayer();
|
||
}}
|
||
entry={entry}
|
||
seasons={entrySeasons}
|
||
/>
|
||
)}
|
||
</div>
|
||
);
|
||
}
|