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(loadSeasonView); const [markingSeason, setMarkingSeason] = useState(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 (

Seasons

{view === "cards" ? (
{display.map((season) => ( setMarkingSeason(season)} /> ))}
) : (
{display.map((season) => ( setMarkingSeason(season)} /> ))}
)} {sorted.length > limit && ( )} {/* Marks the season's next unwatched episode */} {nextToMark && ( { if (!open) setMarkingSeason(null); }} mode="create" episodeId={nextToMark.id} entryId={entryId} /> )}
); } 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 (
{season.name}
{season.name} {getYear(season.air_date)}
{watched}/{total}
{canMark && ( )} ); } function SeasonCard({ season, entryId, onMarkWatched, }: { season: Season; entryId: number; onMarkWatched: () => void; }) { const { watched, total, percent } = seasonProgress(season); const canMark = !!nextUnwatched(season); return (
{season.name}

{season.name}

{[getYear(season.air_date), `${watched}/${total} eps`] .filter(Boolean) .join(" ยท ")}

0 && watched >= total} onClick={canMark ? (e) => stop(e, onMarkWatched) : undefined} />
); } /** 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 (
{done ? ( ) : ( )}
); }