import { useMemo } from "react"; import { useSearchParams } from "react-router-dom"; import { ChevronLeft, ChevronRight, History as HistoryIcon, } from "lucide-react"; import { useHistoryYear, useHistoryYears } from "@/hooks/useHistory"; import { Button } from "@/components/ui/button"; import { Skeleton } from "@/components/ui/skeleton"; import { Tabs, TabsList, TabsTrigger } from "@/components/ui/tabs"; import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue, } from "@/components/ui/select"; import { HistoryList } from "@/components/history/HistoryList"; import { HistoryTimeline } from "@/components/history/HistoryTimeline"; import { YearInReview } from "@/components/history/YearInReview"; import { buildSessions, computeReview, filterEvents, formatMinutes, groupByMonth, eventRuntime, type HistoryTypeFilter, } from "@/lib/history"; type HistoryTab = "history" | "timeline" | "review"; const TABS: { value: HistoryTab; label: string }[] = [ { value: "history", label: "History" }, { value: "timeline", label: "Timeline" }, { value: "review", label: "Year in Review" }, ]; const TYPE_FILTERS: { value: HistoryTypeFilter; label: string }[] = [ { value: "all", label: "All" }, { value: "movies", label: "Movies" }, { value: "tv", label: "TV" }, ]; export function HistoryPage() { // Tab, year and filters live in the URL so a view can be linked/reloaded const [params, setParams] = useSearchParams(); const tab = (params.get("tab") as HistoryTab) || "history"; const type = (params.get("type") as HistoryTypeFilter) || "all"; const includeUnknown = params.get("unknown") === "1"; const { data: years = [], isLoading: yearsLoading } = useHistoryYears(); const year = Number(params.get("year")) || years[0]?.year || 0; const { data, isLoading } = useHistoryYear(year); const setParam = (key: string, value: string | null) => setParams( (prev) => { const next = new URLSearchParams(prev); if (value === null) next.delete(key); else next.set(key, value); return next; }, { replace: true }, ); const yearIndex = years.findIndex((y) => y.year === year); const olderYear = years[yearIndex + 1]?.year; const newerYear = yearIndex > 0 ? years[yearIndex - 1]?.year : undefined; const filtered = useMemo( () => (data ? filterEvents(data.events, type) : []), [data, type], ); const months = useMemo( () => (data ? groupByMonth(buildSessions(filtered, data.entries)) : []), [data, filtered], ); const unknownCount = filtered.filter((e) => e.accuracy === "UNKNOWN").length; const review = useMemo(() => { if (!data) return null; const events = includeUnknown ? filtered : filtered.filter((e) => e.accuracy !== "UNKNOWN"); return computeReview(events, data.entries); }, [data, filtered, includeUnknown]); const totalMinutes = data ? filtered.reduce((sum, e) => sum + eventRuntime(e, data.entries), 0) : 0; if (!yearsLoading && years.length === 0) { return (

No watch history yet

Mark movies or episodes as watched and they'll show up here.

); } return (
{/* Header */}

History

{data ? `${filtered.length.toLocaleString()} watched in ${year} · ${formatMinutes(totalMinutes)}` : " "}

{/* Year picker */}
{/* Type filter */} setParam("type", v === "all" ? null : v)} > {TYPE_FILTERS.map((f) => ( {f.label} ))}
setParam("tab", v === "history" ? null : v)} > {TABS.map((t) => ( {t.label} ))} {isLoading || yearsLoading || !data ? (
{Array.from({ length: 6 }).map((_, i) => ( ))}
) : filtered.length === 0 ? (

Nothing watched in {year} {type !== "all" && ` (${type === "movies" ? "movies" : "TV"})`}.

) : tab === "timeline" ? ( ) : tab === "review" && review ? (
{unknownCount > 0 && ( )}
) : ( )}
); }