Backend: Added PUT /seasons/:id/refresh endpoint that fetches updated TMDB data for a single season. Frontend: - New SeasonRatingDialog for rating seasons via StarRating - useRefreshSeason and useUpdateSeason hooks with query invalidation - SeasonPage now shows inline season rating (user + TMDB) - Mark Watched, Rate, and Refresh actions moved under a dropdown menu
366 lines
14 KiB
TypeScript
366 lines
14 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 { StarRating } from "@/components/ui/star-rating";
|
|
import {
|
|
DropdownMenu,
|
|
DropdownMenuContent,
|
|
DropdownMenuItem,
|
|
DropdownMenuTrigger,
|
|
} from "@/components/ui/dropdown-menu";
|
|
import {
|
|
Eye,
|
|
CheckCircle2,
|
|
ListChecks,
|
|
PlayCircle,
|
|
Ellipsis,
|
|
RefreshCw,
|
|
Star,
|
|
} from "lucide-react";
|
|
import {
|
|
Tooltip,
|
|
TooltipContent,
|
|
TooltipProvider,
|
|
TooltipTrigger,
|
|
} from "@/components/ui/tooltip";
|
|
import { getTmdbPosterUrl, getYear, formatDate } from "@/lib/media-utils";
|
|
import type { Episode, EpisodeSeen } from "@/types";
|
|
|
|
export function SeasonPage() {
|
|
const { entryId, seasonId } = useParams<{
|
|
entryId: string;
|
|
seasonId: string;
|
|
}>();
|
|
const { data: season, isLoading } = useSeason(parseInt(seasonId!, 10));
|
|
const { data: episodes = [] } = useSeasonEpisodes(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 { 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 handleWatchEpisode = (season: number, episodeNumber: number) => {
|
|
if (!entry?.entryIds?.tmdb_id) return;
|
|
initialize();
|
|
play({
|
|
type: "tv",
|
|
tmdbId: entry.entryIds.tmdb_id,
|
|
imdbId: entry.entryIds?.imdb_id || undefined,
|
|
season,
|
|
episode: episodeNumber,
|
|
});
|
|
setStreamingOpen(true);
|
|
};
|
|
|
|
if (isLoading || !season) {
|
|
return (
|
|
<div className="space-y-6">
|
|
<div className="flex gap-6">
|
|
<Skeleton className="h-56 w-36 shrink-0 rounded-lg" />
|
|
<div className="flex-1 space-y-3">
|
|
<Skeleton className="h-6 w-48" />
|
|
<Skeleton className="h-4 w-32" />
|
|
<Skeleton className="h-4 w-64" />
|
|
</div>
|
|
</div>
|
|
</div>
|
|
);
|
|
}
|
|
|
|
const watchedCount = episodes.filter(
|
|
(ep: Episode) =>
|
|
ep.date_first_seen || (ep.episodeSeens && ep.episodeSeens.length > 0),
|
|
).length;
|
|
const totalEpisodes = episodes.length;
|
|
const progress = totalEpisodes > 0 ? (watchedCount / totalEpisodes) * 100 : 0;
|
|
|
|
return (
|
|
<div className="space-y-8 animate-page-enter mx-auto max-w-4xl">
|
|
{/* Breadcrumb: Title / Season 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>
|
|
<span className="text-foreground font-medium">
|
|
Season {season.season_number}
|
|
</span>
|
|
</div>
|
|
|
|
{/* Header */}
|
|
<div className="flex flex-col gap-4 sm:flex-row sm:gap-6">
|
|
<img
|
|
src={getTmdbPosterUrl(season.poster_path, "w185")}
|
|
alt={season.name}
|
|
className="h-40 w-28 shrink-0 rounded-lg object-cover shadow-lg self-center sm:self-start sm:h-56 sm:w-36"
|
|
/>
|
|
<div className="flex-1 flex flex-col justify-center space-y-3">
|
|
<div className="flex flex-col gap-2 sm:flex-row sm:items-start sm:justify-between sm:gap-4">
|
|
<h1 className="text-xl font-bold sm:text-2xl">{season.name}</h1>
|
|
{/* Actions dropdown — top right */}
|
|
<DropdownMenu>
|
|
<DropdownMenuTrigger asChild>
|
|
<Button
|
|
size="icon"
|
|
variant="outline"
|
|
className="shrink-0 self-end sm:self-start"
|
|
>
|
|
<Ellipsis className="h-4 w-4" />
|
|
</Button>
|
|
</DropdownMenuTrigger>
|
|
<DropdownMenuContent align="end" className="w-48">
|
|
<DropdownMenuItem onClick={() => setShowMarkAllDialog(true)}>
|
|
<ListChecks className="mr-2 h-4 w-4" />
|
|
Mark Season Watched
|
|
</DropdownMenuItem>
|
|
<DropdownMenuItem onClick={() => setShowRatingDialog(true)}>
|
|
<Star className="mr-2 h-4 w-4" />
|
|
Rate Season
|
|
</DropdownMenuItem>
|
|
<DropdownMenuItem
|
|
onClick={() => refreshSeason.mutate(season.id)}
|
|
disabled={refreshSeason.isPending}
|
|
>
|
|
<RefreshCw
|
|
className={`mr-2 h-4 w-4 ${refreshSeason.isPending ? "animate-spin" : ""}`}
|
|
/>
|
|
Refresh Season
|
|
</DropdownMenuItem>
|
|
</DropdownMenuContent>
|
|
</DropdownMenu>
|
|
</div>
|
|
<div className="flex items-center gap-2 text-sm text-muted-foreground">
|
|
<span>Season {season.season_number}</span>
|
|
{season.air_date && (
|
|
<>
|
|
<span>•</span>
|
|
<span>{getYear(season.air_date)}</span>
|
|
</>
|
|
)}
|
|
<span>•</span>
|
|
<span>{totalEpisodes} episodes</span>
|
|
</div>
|
|
|
|
{/* Season rating */}
|
|
<div className="flex items-center gap-2">
|
|
<StarRating value={season.user_rating ?? 0} readOnly size="sm" />
|
|
{season.user_rating != null && season.user_rating > 0 && (
|
|
<span className="text-sm font-medium tabular-nums">
|
|
{season.user_rating}/10
|
|
</span>
|
|
)}
|
|
{season.tmdb_rating != null && season.tmdb_rating > 0 && (
|
|
<span className="text-xs text-muted-foreground">
|
|
TMDB {season.tmdb_rating}
|
|
</span>
|
|
)}
|
|
</div>
|
|
|
|
{/* Watch progress */}
|
|
<div className="flex items-center gap-3 max-w-xs">
|
|
<Progress value={progress} className="flex-1 h-2" />
|
|
<span className="text-sm font-medium">
|
|
{watchedCount}/{totalEpisodes}
|
|
</span>
|
|
</div>
|
|
|
|
{season.overview && (
|
|
<p className="text-sm text-muted-foreground max-w-lg">
|
|
{season.overview}
|
|
</p>
|
|
)}
|
|
</div>
|
|
</div>
|
|
|
|
{/* Episodes */}
|
|
<div className="space-y-3">
|
|
<h3 className="text-lg font-semibold">Episodes</h3>
|
|
<div className="space-y-2">
|
|
{episodes.map((episode: Episode) => {
|
|
const isWatched =
|
|
!!episode.date_first_seen ||
|
|
(episode.episodeSeens && episode.episodeSeens.length > 0);
|
|
const seenDates = (episode.episodeSeens ?? []).map(
|
|
(s: EpisodeSeen) => s.date_seen,
|
|
);
|
|
|
|
return (
|
|
<Link
|
|
key={episode.id}
|
|
to={`/entry/${entryId}/season/${seasonId}/episode/${episode.id}`}
|
|
className="flex gap-3 rounded-lg border border-border p-2 transition-colors hover:bg-accent/50 sm:gap-4 sm:p-3"
|
|
>
|
|
{/* Thumbnail — click to watch */}
|
|
<div
|
|
className="relative h-14 w-24 shrink-0 overflow-hidden rounded bg-muted sm:h-16 sm:w-28 group/thumb cursor-pointer"
|
|
onClick={(ev) => {
|
|
ev.preventDefault();
|
|
ev.stopPropagation();
|
|
if (!entry?.entryIds?.tmdb_id) return;
|
|
handleWatchEpisode(
|
|
season.season_number,
|
|
episode.episode_number,
|
|
);
|
|
}}
|
|
>
|
|
{episode.still_path ? (
|
|
<img
|
|
src={getTmdbPosterUrl(episode.still_path, "w300")}
|
|
alt={episode.name}
|
|
className="h-full w-full object-cover transition-[filter] group-hover/thumb:brightness-50"
|
|
loading="lazy"
|
|
/>
|
|
) : (
|
|
<div className="flex h-full w-full items-center justify-center text-xs text-muted-foreground bg-muted group-hover/thumb:bg-muted/70 transition-colors">
|
|
No Image
|
|
</div>
|
|
)}
|
|
<div className="absolute inset-0 flex items-center justify-center opacity-0 group-hover/thumb:opacity-100 transition-opacity">
|
|
<PlayCircle className="h-7 w-7 text-white drop-shadow-lg" />
|
|
</div>
|
|
</div>
|
|
|
|
{/* Info */}
|
|
<div className="flex-1 min-w-0">
|
|
<div className="flex items-center gap-2">
|
|
<span className="text-xs text-muted-foreground">
|
|
E{episode.episode_number}
|
|
</span>
|
|
<span className="text-sm font-medium truncate">
|
|
{episode.name}
|
|
</span>
|
|
{isWatched && (
|
|
<CheckCircle2 className="h-4 w-4 shrink-0 text-green-500" />
|
|
)}
|
|
</div>
|
|
<div className="flex items-center gap-2 flex-wrap">
|
|
{episode.air_date && (
|
|
<p className="text-xs text-muted-foreground">
|
|
{formatDate(episode.air_date)}
|
|
</p>
|
|
)}
|
|
{seenDates.length > 0 && (
|
|
<>
|
|
<span className="text-xs text-muted-foreground">•</span>
|
|
{seenDates.length === 1 ? (
|
|
<span className="text-xs text-green-600 dark:text-green-400">
|
|
Seen {formatDate(seenDates[0])}
|
|
</span>
|
|
) : (
|
|
<TooltipProvider>
|
|
<Tooltip>
|
|
<TooltipTrigger>
|
|
<span className="text-xs text-green-600 dark:text-green-400 cursor-default underline decoration-dotted">
|
|
Seen {seenDates.length} times
|
|
</span>
|
|
</TooltipTrigger>
|
|
<TooltipContent>
|
|
<div className="space-y-0.5 text-xs">
|
|
{seenDates.map((d: string, i: number) => (
|
|
<p key={i}>{formatDate(d)}</p>
|
|
))}
|
|
</div>
|
|
</TooltipContent>
|
|
</Tooltip>
|
|
</TooltipProvider>
|
|
)}
|
|
</>
|
|
)}
|
|
</div>
|
|
{episode.overview && (
|
|
<p className="mt-1 text-xs text-muted-foreground line-clamp-2">
|
|
{episode.overview}
|
|
</p>
|
|
)}
|
|
</div>
|
|
|
|
{/* Watch button — always clickable to add another seen record */}
|
|
<Button
|
|
size="icon"
|
|
variant={isWatched ? "secondary" : "outline"}
|
|
className="h-8 w-8 shrink-0 self-center"
|
|
onClick={(ev) => {
|
|
ev.preventDefault();
|
|
ev.stopPropagation();
|
|
setDialogEpisode(episode);
|
|
}}
|
|
>
|
|
<Eye className="h-4 w-4" />
|
|
</Button>
|
|
</Link>
|
|
);
|
|
})}
|
|
</div>
|
|
</div>
|
|
|
|
{/* Episode Seen Dialog */}
|
|
{dialogEpisode && (
|
|
<EpisodeSeenEditDialog
|
|
open={!!dialogEpisode}
|
|
onOpenChange={(open) => {
|
|
if (!open) setDialogEpisode(null);
|
|
}}
|
|
mode="create"
|
|
episodeId={dialogEpisode.id}
|
|
entryId={parseInt(entryId!, 10)}
|
|
seasonId={parseInt(seasonId!, 10)}
|
|
/>
|
|
)}
|
|
|
|
{/* Mark Season Watched dialog */}
|
|
<MarkSeasonWatchedDialog
|
|
open={showMarkAllDialog}
|
|
onOpenChange={setShowMarkAllDialog}
|
|
entryId={parseInt(entryId!, 10)}
|
|
seasonId={parseInt(seasonId!, 10)}
|
|
episodes={episodes}
|
|
/>
|
|
|
|
{/* Season Rating dialog */}
|
|
{season && (
|
|
<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>
|
|
);
|
|
}
|