301 lines
8.7 KiB
TypeScript
301 lines
8.7 KiB
TypeScript
import { useState } from "react";
|
|
import { Link } from "react-router-dom";
|
|
import { Check, ChevronDown, ChevronUp, Eye } from "lucide-react";
|
|
import { Button } from "@/components/ui/button";
|
|
import { Progress } from "@/components/ui/progress";
|
|
import { EpisodeSeenEditDialog } from "@/components/dialogs/EpisodeSeenEditDialog";
|
|
import { EpisodeViewToggle } from "@/components/entries/SeasonEpisodes";
|
|
import {
|
|
isEpisodeWatched,
|
|
loadSeasonView,
|
|
saveSeasonView,
|
|
type EpisodeView,
|
|
} from "@/lib/episodes";
|
|
import { getYear, getTmdbPosterUrl } from "@/lib/media-utils";
|
|
import type { Season } from "@/types";
|
|
|
|
interface SeasonsContainerProps {
|
|
seasons: Season[];
|
|
entryId: number;
|
|
}
|
|
|
|
// How many seasons show before "Show all", per view
|
|
const COLLAPSED = { list: 2, cards: 4 } as const;
|
|
|
|
export function SeasonsContainer({ seasons, entryId }: SeasonsContainerProps) {
|
|
const [expanded, setExpanded] = useState(false);
|
|
const [view, setView] = useState<EpisodeView>(loadSeasonView);
|
|
const [markingSeason, setMarkingSeason] = useState<Season | null>(null);
|
|
|
|
const changeView = (next: EpisodeView) => {
|
|
setView(next);
|
|
saveSeasonView(next);
|
|
};
|
|
|
|
// Sort: specials (season 0) at bottom, rest latest to oldest
|
|
const sorted = [...seasons].sort((a, b) => {
|
|
if (a.season_number === 0) return 1;
|
|
if (b.season_number === 0) return -1;
|
|
return b.season_number - a.season_number;
|
|
});
|
|
|
|
const limit = COLLAPSED[view];
|
|
const display = expanded ? sorted : sorted.slice(0, limit);
|
|
const nextToMark = markingSeason ? nextUnwatched(markingSeason) : undefined;
|
|
|
|
return (
|
|
<div className="space-y-3">
|
|
<div className="flex items-center justify-between gap-3">
|
|
<h3 className="text-lg font-semibold">Seasons</h3>
|
|
<EpisodeViewToggle view={view} onChange={changeView} />
|
|
</div>
|
|
|
|
{view === "cards" ? (
|
|
<div className="grid grid-cols-2 gap-3 sm:grid-cols-3 xl:grid-cols-4">
|
|
{display.map((season) => (
|
|
<SeasonCard
|
|
key={season.id}
|
|
season={season}
|
|
entryId={entryId}
|
|
onMarkWatched={() => setMarkingSeason(season)}
|
|
/>
|
|
))}
|
|
</div>
|
|
) : (
|
|
<div className="space-y-2">
|
|
{display.map((season) => (
|
|
<SeasonRow
|
|
key={season.id}
|
|
season={season}
|
|
entryId={entryId}
|
|
onMarkWatched={() => setMarkingSeason(season)}
|
|
/>
|
|
))}
|
|
</div>
|
|
)}
|
|
|
|
{sorted.length > limit && (
|
|
<Button
|
|
variant="ghost"
|
|
size="sm"
|
|
onClick={() => setExpanded(!expanded)}
|
|
className="w-full"
|
|
>
|
|
{expanded ? (
|
|
<>
|
|
<ChevronUp className="mr-1 h-4 w-4" /> Show Less
|
|
</>
|
|
) : (
|
|
<>
|
|
<ChevronDown className="mr-1 h-4 w-4" /> Show All ({sorted.length}
|
|
)
|
|
</>
|
|
)}
|
|
</Button>
|
|
)}
|
|
|
|
{/* Marks the season's next unwatched episode */}
|
|
{nextToMark && (
|
|
<EpisodeSeenEditDialog
|
|
open={!!markingSeason}
|
|
onOpenChange={(open) => {
|
|
if (!open) setMarkingSeason(null);
|
|
}}
|
|
mode="create"
|
|
episodeId={nextToMark.id}
|
|
entryId={entryId}
|
|
/>
|
|
)}
|
|
</div>
|
|
);
|
|
}
|
|
|
|
function seasonProgress(season: Season) {
|
|
const watched = season.episodes?.filter(isEpisodeWatched).length ?? 0;
|
|
const total = season.episode_count || season.episodes?.length || 0;
|
|
return {
|
|
watched,
|
|
total,
|
|
percent: total > 0 ? (watched / total) * 100 : 0,
|
|
};
|
|
}
|
|
|
|
function nextUnwatched(season: Season) {
|
|
return season.episodes
|
|
?.slice()
|
|
.sort((a, b) => a.episode_number - b.episode_number)
|
|
.find((ep) => !isEpisodeWatched(ep));
|
|
}
|
|
|
|
function stop(ev: React.MouseEvent, fn: () => void) {
|
|
ev.preventDefault();
|
|
ev.stopPropagation();
|
|
fn();
|
|
}
|
|
|
|
function SeasonRow({
|
|
season,
|
|
entryId,
|
|
onMarkWatched,
|
|
}: {
|
|
season: Season;
|
|
entryId: number;
|
|
onMarkWatched: () => void;
|
|
}) {
|
|
const { watched, total, percent } = seasonProgress(season);
|
|
const canMark = !!nextUnwatched(season);
|
|
|
|
return (
|
|
<Link
|
|
to={`/entry/${entryId}/season/${season.id}`}
|
|
className="flex items-center gap-4 rounded-lg border border-border p-3 transition-colors hover:bg-accent/50"
|
|
>
|
|
<div className="h-16 w-11 shrink-0 overflow-hidden rounded bg-muted">
|
|
<img
|
|
src={getTmdbPosterUrl(season.poster_path, "w92")}
|
|
alt={season.name}
|
|
className="h-full w-full object-cover"
|
|
loading="lazy"
|
|
/>
|
|
</div>
|
|
|
|
<div className="min-w-0 flex-1">
|
|
<div className="flex items-center gap-2">
|
|
<span className="truncate text-sm font-medium">{season.name}</span>
|
|
<span className="text-xs text-muted-foreground">
|
|
{getYear(season.air_date)}
|
|
</span>
|
|
</div>
|
|
<div className="mt-1 flex items-center gap-2">
|
|
<Progress value={percent} className="h-1.5 flex-1" />
|
|
<span className="shrink-0 text-xs text-muted-foreground">
|
|
{watched}/{total}
|
|
</span>
|
|
</div>
|
|
</div>
|
|
|
|
{canMark && (
|
|
<Button
|
|
size="icon"
|
|
variant="ghost"
|
|
className="h-8 w-8 shrink-0"
|
|
aria-label="Mark next episode as watched"
|
|
title="Mark next episode as watched"
|
|
onClick={(e) => stop(e, onMarkWatched)}
|
|
>
|
|
<Eye className="h-4 w-4" />
|
|
</Button>
|
|
)}
|
|
</Link>
|
|
);
|
|
}
|
|
|
|
function SeasonCard({
|
|
season,
|
|
entryId,
|
|
onMarkWatched,
|
|
}: {
|
|
season: Season;
|
|
entryId: number;
|
|
onMarkWatched: () => void;
|
|
}) {
|
|
const { watched, total, percent } = seasonProgress(season);
|
|
const canMark = !!nextUnwatched(season);
|
|
|
|
return (
|
|
<Link
|
|
to={`/entry/${entryId}/season/${season.id}`}
|
|
className="group flex flex-col overflow-hidden rounded-xl border border-border/60 bg-card/50 transition-colors hover:bg-accent/40"
|
|
>
|
|
<div className="aspect-2/3 overflow-hidden bg-muted">
|
|
<img
|
|
src={getTmdbPosterUrl(season.poster_path, "w342")}
|
|
alt={season.name}
|
|
loading="lazy"
|
|
className="h-full w-full object-cover transition-transform duration-300 group-hover:scale-[1.03]"
|
|
/>
|
|
</div>
|
|
<div className="flex items-center gap-2 p-2.5">
|
|
<div className="min-w-0 flex-1">
|
|
<p className="line-clamp-1 text-sm font-semibold">{season.name}</p>
|
|
<p className="text-xs text-muted-foreground tabular-nums">
|
|
{[getYear(season.air_date), `${watched}/${total} eps`]
|
|
.filter(Boolean)
|
|
.join(" · ")}
|
|
</p>
|
|
</div>
|
|
<ProgressRing
|
|
percent={percent}
|
|
done={total > 0 && watched >= total}
|
|
onClick={canMark ? (e) => stop(e, onMarkWatched) : undefined}
|
|
/>
|
|
</div>
|
|
</Link>
|
|
);
|
|
}
|
|
|
|
/** Circular progress with the mark-as-watched button in the middle. */
|
|
function ProgressRing({
|
|
percent,
|
|
done,
|
|
onClick,
|
|
}: {
|
|
percent: number;
|
|
done: boolean;
|
|
onClick?: (e: React.MouseEvent) => void;
|
|
}) {
|
|
const size = 44;
|
|
const stroke = 3.5;
|
|
const radius = (size - stroke) / 2;
|
|
const circumference = 2 * Math.PI * radius;
|
|
const offset = circumference * (1 - Math.min(100, percent) / 100);
|
|
|
|
return (
|
|
<div
|
|
className="relative shrink-0"
|
|
style={{ width: size, height: size }}
|
|
title={`${Math.round(percent)}% watched`}
|
|
>
|
|
<svg width={size} height={size} className="-rotate-90" aria-hidden="true">
|
|
<circle
|
|
cx={size / 2}
|
|
cy={size / 2}
|
|
r={radius}
|
|
fill="none"
|
|
strokeWidth={stroke}
|
|
className="stroke-muted"
|
|
/>
|
|
<circle
|
|
cx={size / 2}
|
|
cy={size / 2}
|
|
r={radius}
|
|
fill="none"
|
|
strokeWidth={stroke}
|
|
strokeLinecap="round"
|
|
strokeDasharray={circumference}
|
|
strokeDashoffset={offset}
|
|
className={`transition-[stroke-dashoffset] duration-500 ${
|
|
done ? "stroke-green-500" : "stroke-primary"
|
|
}`}
|
|
/>
|
|
</svg>
|
|
{done ? (
|
|
<span className="absolute inset-0 flex items-center justify-center text-green-500">
|
|
<Check className="h-4 w-4" />
|
|
</span>
|
|
) : (
|
|
<button
|
|
type="button"
|
|
onClick={onClick}
|
|
disabled={!onClick}
|
|
aria-label="Mark next episode as watched"
|
|
title="Mark next episode as watched"
|
|
className="absolute inset-1.5 flex items-center justify-center rounded-full text-muted-foreground transition-colors hover:bg-accent hover:text-foreground disabled:pointer-events-none"
|
|
>
|
|
<Eye className="h-4 w-4" />
|
|
</button>
|
|
)}
|
|
</div>
|
|
);
|
|
}
|