- Backend: /history/years and /history/:year return every movie and episode watch in a year, with entry posters, genres, networks and buddies - History tab: watches by month and day, back-to-back episodes grouped - Timeline tab: latest to oldest, month dividers, stacked binge sessions - Year in Review tab: hours, top shows and genres, streaks, busiest day, biggest binge, monthly and weekday charts, buddies and new shows - Year picker, movies/TV filter, state kept in the URL; unknown-date watches badged and excluded from the review by default - History link in the sidebar and command palette
228 lines
7.6 KiB
TypeScript
228 lines
7.6 KiB
TypeScript
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 (
|
|
<div className="flex flex-col items-center justify-center gap-3 py-24 text-center">
|
|
<HistoryIcon className="h-10 w-10 text-muted-foreground" />
|
|
<p className="text-lg font-semibold">No watch history yet</p>
|
|
<p className="text-sm text-muted-foreground">
|
|
Mark movies or episodes as watched and they'll show up here.
|
|
</p>
|
|
</div>
|
|
);
|
|
}
|
|
|
|
return (
|
|
<div className="mx-auto max-w-6xl space-y-6 animate-page-enter">
|
|
{/* Header */}
|
|
<div className="flex flex-wrap items-end justify-between gap-4">
|
|
<div className="space-y-1">
|
|
<h1 className="text-3xl font-bold tracking-tight">History</h1>
|
|
<p className="text-sm text-muted-foreground tabular-nums">
|
|
{data
|
|
? `${filtered.length.toLocaleString()} watched in ${year} · ${formatMinutes(totalMinutes)}`
|
|
: " "}
|
|
</p>
|
|
</div>
|
|
|
|
<div className="flex flex-wrap items-center gap-2">
|
|
{/* Year picker */}
|
|
<div className="flex items-center gap-1 rounded-lg border border-border/60 p-0.5">
|
|
<Button
|
|
variant="ghost"
|
|
size="icon-sm"
|
|
disabled={!olderYear}
|
|
onClick={() => olderYear && setParam("year", String(olderYear))}
|
|
aria-label="Previous year"
|
|
>
|
|
<ChevronLeft />
|
|
</Button>
|
|
<Select
|
|
value={year ? String(year) : ""}
|
|
onValueChange={(v) => v && setParam("year", String(v))}
|
|
>
|
|
<SelectTrigger
|
|
size="sm"
|
|
className="h-7 min-w-20 border-0 bg-transparent font-semibold tabular-nums shadow-none"
|
|
aria-label="Year"
|
|
>
|
|
<SelectValue placeholder="Year" />
|
|
</SelectTrigger>
|
|
<SelectContent>
|
|
{years.map((y) => (
|
|
<SelectItem key={y.year} value={String(y.year)}>
|
|
<span className="tabular-nums">{y.year}</span>
|
|
<span className="ml-2 text-xs text-muted-foreground">
|
|
{y.movies + y.episodes} watched
|
|
</span>
|
|
</SelectItem>
|
|
))}
|
|
</SelectContent>
|
|
</Select>
|
|
<Button
|
|
variant="ghost"
|
|
size="icon-sm"
|
|
disabled={!newerYear}
|
|
onClick={() => newerYear && setParam("year", String(newerYear))}
|
|
aria-label="Next year"
|
|
>
|
|
<ChevronRight />
|
|
</Button>
|
|
</div>
|
|
|
|
{/* Type filter */}
|
|
<Tabs
|
|
value={type}
|
|
onValueChange={(v) => setParam("type", v === "all" ? null : v)}
|
|
>
|
|
<TabsList>
|
|
{TYPE_FILTERS.map((f) => (
|
|
<TabsTrigger key={f.value} value={f.value}>
|
|
{f.label}
|
|
</TabsTrigger>
|
|
))}
|
|
</TabsList>
|
|
</Tabs>
|
|
</div>
|
|
</div>
|
|
|
|
<Tabs
|
|
value={tab}
|
|
onValueChange={(v) => setParam("tab", v === "history" ? null : v)}
|
|
>
|
|
<TabsList>
|
|
{TABS.map((t) => (
|
|
<TabsTrigger key={t.value} value={t.value}>
|
|
{t.label}
|
|
</TabsTrigger>
|
|
))}
|
|
</TabsList>
|
|
</Tabs>
|
|
|
|
{isLoading || yearsLoading || !data ? (
|
|
<div className="space-y-3">
|
|
{Array.from({ length: 6 }).map((_, i) => (
|
|
<Skeleton key={i} className="h-20 w-full rounded-lg" />
|
|
))}
|
|
</div>
|
|
) : filtered.length === 0 ? (
|
|
<p className="py-16 text-center text-sm text-muted-foreground">
|
|
Nothing watched in {year}
|
|
{type !== "all" && ` (${type === "movies" ? "movies" : "TV"})`}.
|
|
</p>
|
|
) : tab === "timeline" ? (
|
|
<HistoryTimeline months={months} />
|
|
) : tab === "review" && review ? (
|
|
<div className="space-y-4">
|
|
{unknownCount > 0 && (
|
|
<label className="flex items-center gap-2 text-xs text-muted-foreground">
|
|
<input
|
|
type="checkbox"
|
|
checked={includeUnknown}
|
|
onChange={(e) =>
|
|
setParam("unknown", e.target.checked ? "1" : null)
|
|
}
|
|
/>
|
|
Include {unknownCount} watches with unknown dates (imported) —
|
|
they can skew streaks, months and busiest days
|
|
</label>
|
|
)}
|
|
<YearInReview year={year} review={review} entries={data.entries} />
|
|
</div>
|
|
) : (
|
|
<HistoryList months={months} />
|
|
)}
|
|
</div>
|
|
);
|
|
}
|