Add history page with list, timeline and year in review
- 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
This commit is contained in:
1 parent
c4aa0488d4
commit
760afd8e84
13 files changed
+1724
No files matched your search
@@ -0,0 +1,24 @@
|
|||||||
|
const express = require("express");
|
||||||
|
const historyRouter = express.Router();
|
||||||
|
|
||||||
|
const historyService = require("../services/historyService");
|
||||||
|
const asyncWrapper = require("../utils/asyncWrapper");
|
||||||
|
|
||||||
|
// ! /history
|
||||||
|
// * GET - Years that have watches, with movie/episode counts
|
||||||
|
historyRouter.get(
|
||||||
|
"/years",
|
||||||
|
asyncWrapper(async (req, res) => {
|
||||||
|
res.status(200).json(await historyService.getYears());
|
||||||
|
}),
|
||||||
|
);
|
||||||
|
|
||||||
|
// * GET - Every movie/episode watch in a year (newest first)
|
||||||
|
historyRouter.get(
|
||||||
|
"/:year",
|
||||||
|
asyncWrapper(async (req, res) => {
|
||||||
|
res.status(200).json(await historyService.getYear(req.params.year));
|
||||||
|
}),
|
||||||
|
);
|
||||||
|
|
||||||
|
module.exports = historyRouter;
|
||||||
@@ -17,6 +17,7 @@ const authRouter = require("./auth.routes");
|
|||||||
const watchBuddyRouter = require("./watch-buddy.routes");
|
const watchBuddyRouter = require("./watch-buddy.routes");
|
||||||
const homepageIntegrationRouter = require("./homepage-integration.routes");
|
const homepageIntegrationRouter = require("./homepage-integration.routes");
|
||||||
const videoRouter = require("./video.routes");
|
const videoRouter = require("./video.routes");
|
||||||
|
const historyRouter = require("./history.routes");
|
||||||
|
|
||||||
const router = express.Router();
|
const router = express.Router();
|
||||||
router.use("/auth", authRouter);
|
router.use("/auth", authRouter);
|
||||||
@@ -32,6 +33,7 @@ router.use("/episode-seen", episodeSeenRouter);
|
|||||||
|
|
||||||
router.use("/import", importRouter);
|
router.use("/import", importRouter);
|
||||||
router.use("/statistics", statisticsRouter);
|
router.use("/statistics", statisticsRouter);
|
||||||
|
router.use("/history", historyRouter);
|
||||||
router.use("/settings", settingsRouter);
|
router.use("/settings", settingsRouter);
|
||||||
router.use("/maintenance", maintenanceRouter);
|
router.use("/maintenance", maintenanceRouter);
|
||||||
router.use("/ping", pingRouter);
|
router.use("/ping", pingRouter);
|
||||||
|
|||||||
@@ -0,0 +1,208 @@
|
|||||||
|
const { Op } = require("sequelize");
|
||||||
|
const sequelize = require("../config/database");
|
||||||
|
|
||||||
|
const Entry = require("../models/Entry");
|
||||||
|
const EntryRating = require("../models/EntryRating");
|
||||||
|
const EntrySeen = require("../models/EntrySeen");
|
||||||
|
const Episode = require("../models/Episode");
|
||||||
|
const EpisodeSeen = require("../models/EpisodeSeen");
|
||||||
|
const Genre = require("../models/Genre");
|
||||||
|
const Media = require("../models/Media");
|
||||||
|
const Network = require("../models/Network");
|
||||||
|
const Season = require("../models/Season");
|
||||||
|
const WatchBuddy = require("../models/WatchBuddy");
|
||||||
|
|
||||||
|
const BadRequestError = require("../errors/BadRequestError");
|
||||||
|
|
||||||
|
const ENTRY_ATTRIBUTES = [
|
||||||
|
"id",
|
||||||
|
"title",
|
||||||
|
"is_tvseries",
|
||||||
|
"poster_path",
|
||||||
|
"banner_path",
|
||||||
|
"runtime",
|
||||||
|
"release_date",
|
||||||
|
"original_language",
|
||||||
|
];
|
||||||
|
|
||||||
|
const BUDDY_INCLUDE = {
|
||||||
|
model: WatchBuddy,
|
||||||
|
as: "buddies",
|
||||||
|
attributes: ["id", "name"],
|
||||||
|
through: { attributes: [] },
|
||||||
|
};
|
||||||
|
|
||||||
|
class HistoryService {
|
||||||
|
constructor() {
|
||||||
|
if (!HistoryService.instance) {
|
||||||
|
HistoryService.instance = this;
|
||||||
|
}
|
||||||
|
return HistoryService.instance;
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Years that have watches, newest first, with movie/episode counts. */
|
||||||
|
async getYears() {
|
||||||
|
const [rows] = await sequelize.query(`
|
||||||
|
SELECT y AS year, SUM(movies) AS movies, SUM(episodes) AS episodes FROM (
|
||||||
|
SELECT YEAR(date_seen) AS y, COUNT(*) AS movies, 0 AS episodes
|
||||||
|
FROM entrySeens WHERE date_seen IS NOT NULL GROUP BY y
|
||||||
|
UNION ALL
|
||||||
|
SELECT YEAR(date_seen) AS y, 0 AS movies, COUNT(*) AS episodes
|
||||||
|
FROM episodeSeens WHERE date_seen IS NOT NULL GROUP BY y
|
||||||
|
) t GROUP BY y ORDER BY y DESC
|
||||||
|
`);
|
||||||
|
return rows.map((r) => ({
|
||||||
|
year: Number(r.year),
|
||||||
|
movies: Number(r.movies),
|
||||||
|
episodes: Number(r.episodes),
|
||||||
|
}));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Every watch in a year, newest first. Entries are returned once in
|
||||||
|
* `entries` (keyed by id) and referenced by `entry_id` from each event.
|
||||||
|
* @param {number} year
|
||||||
|
*/
|
||||||
|
async getYear(year) {
|
||||||
|
year = Number(year);
|
||||||
|
if (!Number.isInteger(year) || year < 1900 || year > 3000) {
|
||||||
|
throw new BadRequestError("Invalid year");
|
||||||
|
}
|
||||||
|
const range = {
|
||||||
|
[Op.gte]: new Date(year, 0, 1),
|
||||||
|
[Op.lt]: new Date(year + 1, 0, 1),
|
||||||
|
};
|
||||||
|
|
||||||
|
const [movieSeens, episodeSeens] = await Promise.all([
|
||||||
|
EntrySeen.findAll({
|
||||||
|
where: { date_seen: range },
|
||||||
|
attributes: ["id", "entry_id", "date_seen", "accuracy"],
|
||||||
|
include: [BUDDY_INCLUDE],
|
||||||
|
}),
|
||||||
|
EpisodeSeen.findAll({
|
||||||
|
where: { date_seen: range },
|
||||||
|
attributes: ["id", "entry_id", "episode_id", "date_seen", "accuracy"],
|
||||||
|
include: [
|
||||||
|
BUDDY_INCLUDE,
|
||||||
|
{
|
||||||
|
model: Episode,
|
||||||
|
attributes: [
|
||||||
|
"id",
|
||||||
|
"name",
|
||||||
|
"episode_number",
|
||||||
|
"runtime",
|
||||||
|
"still_path",
|
||||||
|
"season_id",
|
||||||
|
],
|
||||||
|
include: [
|
||||||
|
{ model: Season, as: "season", attributes: ["season_number"] },
|
||||||
|
],
|
||||||
|
},
|
||||||
|
],
|
||||||
|
}),
|
||||||
|
]);
|
||||||
|
|
||||||
|
const entryIds = [
|
||||||
|
...new Set([
|
||||||
|
...movieSeens.map((s) => s.entry_id),
|
||||||
|
...episodeSeens.map((s) => s.entry_id),
|
||||||
|
]),
|
||||||
|
].filter(Boolean);
|
||||||
|
|
||||||
|
const entries = entryIds.length
|
||||||
|
? await Entry.findAll({
|
||||||
|
where: { id: entryIds },
|
||||||
|
attributes: ENTRY_ATTRIBUTES,
|
||||||
|
include: [
|
||||||
|
{
|
||||||
|
model: Media,
|
||||||
|
as: "medias",
|
||||||
|
attributes: ["id", "type", "path", "isSelected"],
|
||||||
|
where: { type: "poster", isSelected: true },
|
||||||
|
required: false,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
model: Genre,
|
||||||
|
as: "genres",
|
||||||
|
attributes: ["id", "name"],
|
||||||
|
through: { attributes: [] },
|
||||||
|
},
|
||||||
|
{
|
||||||
|
model: Network,
|
||||||
|
as: "networks",
|
||||||
|
attributes: ["id", "name", "logoPath"],
|
||||||
|
through: { attributes: [] },
|
||||||
|
},
|
||||||
|
{
|
||||||
|
model: EntryRating,
|
||||||
|
as: "entryRating",
|
||||||
|
attributes: ["user_rating", "tmdb_rating"],
|
||||||
|
},
|
||||||
|
],
|
||||||
|
})
|
||||||
|
: [];
|
||||||
|
|
||||||
|
const buddies = (seen) =>
|
||||||
|
(seen.buddies ?? []).map((b) => ({ id: b.id, name: b.name }));
|
||||||
|
|
||||||
|
const events = [
|
||||||
|
...movieSeens.map((s) => ({
|
||||||
|
id: `m-${s.id}`,
|
||||||
|
seen_id: s.id,
|
||||||
|
kind: "movie",
|
||||||
|
entry_id: s.entry_id,
|
||||||
|
date_seen: s.date_seen,
|
||||||
|
accuracy: s.accuracy,
|
||||||
|
buddies: buddies(s),
|
||||||
|
})),
|
||||||
|
...episodeSeens.map((s) => {
|
||||||
|
const ep = s.episode;
|
||||||
|
return {
|
||||||
|
id: `e-${s.id}`,
|
||||||
|
seen_id: s.id,
|
||||||
|
kind: "episode",
|
||||||
|
entry_id: s.entry_id,
|
||||||
|
date_seen: s.date_seen,
|
||||||
|
accuracy: s.accuracy,
|
||||||
|
buddies: buddies(s),
|
||||||
|
episode: ep
|
||||||
|
? {
|
||||||
|
id: ep.id,
|
||||||
|
name: ep.name,
|
||||||
|
season_id: ep.season_id,
|
||||||
|
season_number: ep.season?.season_number ?? null,
|
||||||
|
episode_number: ep.episode_number,
|
||||||
|
runtime: ep.runtime,
|
||||||
|
still_path: ep.still_path,
|
||||||
|
}
|
||||||
|
: null,
|
||||||
|
};
|
||||||
|
}),
|
||||||
|
].sort((a, b) => new Date(b.date_seen) - new Date(a.date_seen));
|
||||||
|
|
||||||
|
return {
|
||||||
|
year,
|
||||||
|
entries: Object.fromEntries(
|
||||||
|
entries.map((e) => {
|
||||||
|
const plain = e.get({ plain: true });
|
||||||
|
return [
|
||||||
|
plain.id,
|
||||||
|
{
|
||||||
|
...plain,
|
||||||
|
genres: plain.genres?.map((g) => g.name) ?? [],
|
||||||
|
networks:
|
||||||
|
plain.networks?.map((n) => ({
|
||||||
|
id: n.id,
|
||||||
|
name: n.name,
|
||||||
|
logo_path: n.logoPath,
|
||||||
|
})) ?? [],
|
||||||
|
},
|
||||||
|
];
|
||||||
|
}),
|
||||||
|
),
|
||||||
|
events,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
module.exports = new HistoryService();
|
||||||
@@ -0,0 +1,61 @@
|
|||||||
|
import { api } from "@/lib/api";
|
||||||
|
|
||||||
|
export type WatchAccuracy = "EXACT" | "ESTIMATED" | "UNKNOWN";
|
||||||
|
|
||||||
|
export interface HistoryYear {
|
||||||
|
year: number;
|
||||||
|
movies: number;
|
||||||
|
episodes: number;
|
||||||
|
}
|
||||||
|
|
||||||
|
export interface HistoryEntry {
|
||||||
|
id: number;
|
||||||
|
title: string;
|
||||||
|
is_tvseries: boolean;
|
||||||
|
poster_path: string | null;
|
||||||
|
banner_path: string | null;
|
||||||
|
runtime: number | null;
|
||||||
|
release_date: string | null;
|
||||||
|
original_language: string | null;
|
||||||
|
medias: { id: number; type: string; path: string; isSelected: boolean }[];
|
||||||
|
genres: string[];
|
||||||
|
networks: { id: number; name: string; logo_path: string | null }[];
|
||||||
|
entryRating: {
|
||||||
|
user_rating: number | null;
|
||||||
|
tmdb_rating: number | null;
|
||||||
|
} | null;
|
||||||
|
}
|
||||||
|
|
||||||
|
export interface HistoryEpisode {
|
||||||
|
id: number;
|
||||||
|
name: string;
|
||||||
|
season_id: number;
|
||||||
|
season_number: number | null;
|
||||||
|
episode_number: number;
|
||||||
|
runtime: number | null;
|
||||||
|
still_path: string | null;
|
||||||
|
}
|
||||||
|
|
||||||
|
export interface HistoryEvent {
|
||||||
|
id: string;
|
||||||
|
seen_id: number;
|
||||||
|
kind: "movie" | "episode";
|
||||||
|
entry_id: number;
|
||||||
|
date_seen: string;
|
||||||
|
accuracy: WatchAccuracy;
|
||||||
|
buddies: { id: number; name: string }[];
|
||||||
|
episode?: HistoryEpisode | null;
|
||||||
|
}
|
||||||
|
|
||||||
|
export interface HistoryYearData {
|
||||||
|
year: number;
|
||||||
|
entries: Record<number, HistoryEntry>;
|
||||||
|
events: HistoryEvent[];
|
||||||
|
}
|
||||||
|
|
||||||
|
export const historyApi = {
|
||||||
|
getYears: () => api.get<HistoryYear[]>("/history/years").then((r) => r.data),
|
||||||
|
|
||||||
|
getYear: (year: number) =>
|
||||||
|
api.get<HistoryYearData>(`/history/${year}`).then((r) => r.data),
|
||||||
|
};
|
||||||
@@ -16,6 +16,7 @@ import {
|
|||||||
ArrowDown,
|
ArrowDown,
|
||||||
CornerDownLeft,
|
CornerDownLeft,
|
||||||
ArrowRight,
|
ArrowRight,
|
||||||
|
History,
|
||||||
} from "lucide-react";
|
} from "lucide-react";
|
||||||
import { tmdbApi } from "@/api/tmdb";
|
import { tmdbApi } from "@/api/tmdb";
|
||||||
import { useLibraryStore } from "@/stores/useLibraryStore";
|
import { useLibraryStore } from "@/stores/useLibraryStore";
|
||||||
@@ -53,6 +54,12 @@ const NAVIGATION_COMMANDS: CommandItem[] = [
|
|||||||
title: "Go to Watchlist",
|
title: "Go to Watchlist",
|
||||||
icon: <BookOpen className="h-4 w-4" />,
|
icon: <BookOpen className="h-4 w-4" />,
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
id: "cmd-history",
|
||||||
|
type: "command",
|
||||||
|
title: "Go to History",
|
||||||
|
icon: <History className="h-4 w-4" />,
|
||||||
|
},
|
||||||
{
|
{
|
||||||
id: "cmd-stats",
|
id: "cmd-stats",
|
||||||
type: "command",
|
type: "command",
|
||||||
@@ -88,6 +95,7 @@ const NAVIGATION_COMMANDS: CommandItem[] = [
|
|||||||
const COMMAND_ROUTES: Record<string, string> = {
|
const COMMAND_ROUTES: Record<string, string> = {
|
||||||
"cmd-library": "/library",
|
"cmd-library": "/library",
|
||||||
"cmd-watchlist": "/watchlist",
|
"cmd-watchlist": "/watchlist",
|
||||||
|
"cmd-history": "/history",
|
||||||
"cmd-stats": "/dashboard",
|
"cmd-stats": "/dashboard",
|
||||||
"cmd-calendar": "/calendar",
|
"cmd-calendar": "/calendar",
|
||||||
"cmd-people": "/people",
|
"cmd-people": "/people",
|
||||||
|
|||||||
@@ -0,0 +1,136 @@
|
|||||||
|
import { Link } from "react-router-dom";
|
||||||
|
import { Film, Tv, Users } from "lucide-react";
|
||||||
|
import {
|
||||||
|
MONTH_NAMES,
|
||||||
|
formatMinutes,
|
||||||
|
posterUrl,
|
||||||
|
sessionEpisodeLabel,
|
||||||
|
sessionLink,
|
||||||
|
type HistorySession,
|
||||||
|
type MonthGroup,
|
||||||
|
} from "@/lib/history";
|
||||||
|
|
||||||
|
/** Month sections (newest first), each split into days. */
|
||||||
|
export function HistoryList({ months }: { months: MonthGroup[] }) {
|
||||||
|
return (
|
||||||
|
<div className="space-y-10">
|
||||||
|
{months.map((m) => (
|
||||||
|
<section key={m.month} className="space-y-3">
|
||||||
|
<header className="sticky top-0 z-10 -mx-1 flex flex-wrap items-baseline justify-between gap-2 border-b border-border/60 bg-background/90 px-1 py-2 backdrop-blur">
|
||||||
|
<h2 className="text-xl font-bold tracking-tight">
|
||||||
|
{MONTH_NAMES[m.month]}
|
||||||
|
</h2>
|
||||||
|
<p className="text-xs text-muted-foreground tabular-nums">
|
||||||
|
{[
|
||||||
|
m.movies > 0 &&
|
||||||
|
`${m.movies} ${m.movies === 1 ? "movie" : "movies"}`,
|
||||||
|
m.episodes > 0 &&
|
||||||
|
`${m.episodes} ${m.episodes === 1 ? "episode" : "episodes"}`,
|
||||||
|
m.minutes > 0 && formatMinutes(m.minutes),
|
||||||
|
]
|
||||||
|
.filter(Boolean)
|
||||||
|
.join(" · ")}
|
||||||
|
</p>
|
||||||
|
</header>
|
||||||
|
|
||||||
|
<div className="space-y-5">
|
||||||
|
{groupByDay(m.sessions).map(([day, sessions]) => (
|
||||||
|
<div key={day} className="grid gap-2 sm:grid-cols-[7rem_1fr]">
|
||||||
|
<p className="pt-2 text-sm font-medium text-muted-foreground">
|
||||||
|
{new Date(sessions[0].date).toLocaleDateString("en-US", {
|
||||||
|
weekday: "short",
|
||||||
|
month: "short",
|
||||||
|
day: "numeric",
|
||||||
|
})}
|
||||||
|
</p>
|
||||||
|
<div className="space-y-1.5">
|
||||||
|
{sessions.map((s) => (
|
||||||
|
<HistoryRow key={s.key} session={s} />
|
||||||
|
))}
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
))}
|
||||||
|
</div>
|
||||||
|
</section>
|
||||||
|
))}
|
||||||
|
</div>
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
function groupByDay(sessions: HistorySession[]) {
|
||||||
|
const days = new Map<string, HistorySession[]>();
|
||||||
|
for (const s of sessions) {
|
||||||
|
const list = days.get(s.day) ?? [];
|
||||||
|
list.push(s);
|
||||||
|
days.set(s.day, list);
|
||||||
|
}
|
||||||
|
return [...days.entries()];
|
||||||
|
}
|
||||||
|
|
||||||
|
function HistoryRow({ session }: { session: HistorySession }) {
|
||||||
|
const entry = session.entry;
|
||||||
|
const isMovie = session.kind === "movie";
|
||||||
|
const single = session.events.length === 1 ? session.events[0] : null;
|
||||||
|
const buddies = [
|
||||||
|
...new Map(
|
||||||
|
session.events.flatMap((e) => e.buddies).map((b) => [b.id, b]),
|
||||||
|
).values(),
|
||||||
|
];
|
||||||
|
|
||||||
|
return (
|
||||||
|
<Link
|
||||||
|
to={sessionLink(session)}
|
||||||
|
className="group flex items-center gap-3 rounded-lg border border-transparent p-1.5 transition-colors hover:border-border/60 hover:bg-accent/40"
|
||||||
|
>
|
||||||
|
<img
|
||||||
|
src={posterUrl(entry)}
|
||||||
|
alt=""
|
||||||
|
loading="lazy"
|
||||||
|
className="h-16 w-11 shrink-0 rounded-md object-cover ring-1 ring-border/60"
|
||||||
|
/>
|
||||||
|
<div className="min-w-0 flex-1">
|
||||||
|
<p className="line-clamp-1 font-medium">
|
||||||
|
{entry?.title ?? "Unknown title"}
|
||||||
|
</p>
|
||||||
|
<p className="line-clamp-1 text-xs text-muted-foreground">
|
||||||
|
{isMovie ? (
|
||||||
|
<span className="inline-flex items-center gap-1">
|
||||||
|
<Film className="h-3 w-3" /> Movie
|
||||||
|
{entry?.release_date &&
|
||||||
|
` · ${new Date(entry.release_date).getFullYear()}`}
|
||||||
|
</span>
|
||||||
|
) : (
|
||||||
|
<span className="inline-flex items-center gap-1">
|
||||||
|
<Tv className="h-3 w-3" />
|
||||||
|
{sessionEpisodeLabel(session)}
|
||||||
|
{single?.episode?.name
|
||||||
|
? ` · ${single.episode.name}`
|
||||||
|
: ` · ${session.events.length} episodes`}
|
||||||
|
</span>
|
||||||
|
)}
|
||||||
|
</p>
|
||||||
|
</div>
|
||||||
|
<div className="flex shrink-0 flex-col items-end gap-1 text-xs text-muted-foreground">
|
||||||
|
{session.minutes > 0 && (
|
||||||
|
<span className="tabular-nums">{formatMinutes(session.minutes)}</span>
|
||||||
|
)}
|
||||||
|
{session.unknownDate && (
|
||||||
|
<span className="rounded bg-amber-500/15 px-1.5 py-px text-[10px] font-medium text-amber-600 dark:text-amber-400">
|
||||||
|
Date unknown
|
||||||
|
</span>
|
||||||
|
)}
|
||||||
|
{buddies.length > 0 && (
|
||||||
|
<span
|
||||||
|
className="inline-flex items-center gap-1"
|
||||||
|
title={buddies.map((b) => b.name).join(", ")}
|
||||||
|
>
|
||||||
|
<Users className="h-3 w-3" />
|
||||||
|
<span className="max-w-28 truncate">
|
||||||
|
{buddies.map((b) => b.name).join(", ")}
|
||||||
|
</span>
|
||||||
|
</span>
|
||||||
|
)}
|
||||||
|
</div>
|
||||||
|
</Link>
|
||||||
|
);
|
||||||
|
}
|
||||||
@@ -0,0 +1,152 @@
|
|||||||
|
import { Link } from "react-router-dom";
|
||||||
|
import { Film, Tv } from "lucide-react";
|
||||||
|
import {
|
||||||
|
MONTH_NAMES,
|
||||||
|
formatMinutes,
|
||||||
|
posterUrl,
|
||||||
|
sessionEpisodeLabel,
|
||||||
|
sessionLink,
|
||||||
|
type HistorySession,
|
||||||
|
type MonthGroup,
|
||||||
|
} from "@/lib/history";
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Vertical timeline, latest watch at the top down to January. Episodes of a
|
||||||
|
* show watched back to back on the same day are stacked into one card.
|
||||||
|
*/
|
||||||
|
export function HistoryTimeline({ months }: { months: MonthGroup[] }) {
|
||||||
|
let index = 0; // alternates sides across the whole year
|
||||||
|
|
||||||
|
return (
|
||||||
|
<div className="relative mx-auto max-w-4xl pb-6">
|
||||||
|
{/* The line */}
|
||||||
|
<div className="absolute bottom-0 left-4 top-0 w-px bg-border md:left-1/2 md:-translate-x-1/2" />
|
||||||
|
|
||||||
|
{months.map((m) => (
|
||||||
|
<section key={m.month} className="relative">
|
||||||
|
{/* Month divider */}
|
||||||
|
<div className="relative flex items-center py-6 md:justify-center">
|
||||||
|
<div className="absolute inset-x-0 top-1/2 h-px bg-linear-to-r from-transparent via-border to-transparent" />
|
||||||
|
<span className="relative ml-8 rounded-full border border-border bg-background px-4 py-1 text-sm font-semibold shadow-sm md:ml-0">
|
||||||
|
{MONTH_NAMES[m.month]}
|
||||||
|
<span className="ml-2 text-xs font-normal text-muted-foreground tabular-nums">
|
||||||
|
{m.movies + m.episodes} watched
|
||||||
|
</span>
|
||||||
|
</span>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<ol className="space-y-6">
|
||||||
|
{m.sessions.map((s) => {
|
||||||
|
const side = index++ % 2 === 0 ? "left" : "right";
|
||||||
|
return <TimelineItem key={s.key} session={s} side={side} />;
|
||||||
|
})}
|
||||||
|
</ol>
|
||||||
|
</section>
|
||||||
|
))}
|
||||||
|
</div>
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
function TimelineItem({
|
||||||
|
session,
|
||||||
|
side,
|
||||||
|
}: {
|
||||||
|
session: HistorySession;
|
||||||
|
side: "left" | "right";
|
||||||
|
}) {
|
||||||
|
const entry = session.entry;
|
||||||
|
const count = session.events.length;
|
||||||
|
const isMovie = session.kind === "movie";
|
||||||
|
const date = new Date(session.date).toLocaleDateString("en-US", {
|
||||||
|
month: "short",
|
||||||
|
day: "numeric",
|
||||||
|
});
|
||||||
|
|
||||||
|
return (
|
||||||
|
<li
|
||||||
|
className={`relative flex pl-12 md:w-1/2 md:pl-0 ${
|
||||||
|
side === "left"
|
||||||
|
? "md:mr-auto md:justify-end md:pr-10"
|
||||||
|
: "md:ml-auto md:pl-10"
|
||||||
|
}`}
|
||||||
|
>
|
||||||
|
{/* Dot on the line */}
|
||||||
|
<span
|
||||||
|
className={`absolute top-6 left-4 h-3 w-3 -translate-x-1/2 rounded-full ring-4 ring-background ${
|
||||||
|
isMovie ? "bg-amber-500" : "bg-sky-500"
|
||||||
|
} ${side === "left" ? "md:right-0 md:left-auto md:translate-x-1/2" : "md:left-0"}`}
|
||||||
|
/>
|
||||||
|
|
||||||
|
<Link
|
||||||
|
to={sessionLink(session)}
|
||||||
|
className={`group flex w-full max-w-sm items-start gap-3 rounded-xl border border-border/60 bg-card/60 p-2.5 transition-colors hover:bg-accent/40 ${
|
||||||
|
side === "left" ? "md:flex-row-reverse md:text-right" : ""
|
||||||
|
}`}
|
||||||
|
>
|
||||||
|
{/* Poster, stacked for multi-episode sessions */}
|
||||||
|
<div className="relative shrink-0">
|
||||||
|
{count > 1 && (
|
||||||
|
<>
|
||||||
|
<img
|
||||||
|
src={posterUrl(entry)}
|
||||||
|
alt=""
|
||||||
|
className="absolute inset-0 h-full w-full translate-x-2 -translate-y-1 rotate-6 rounded-lg object-cover opacity-40"
|
||||||
|
/>
|
||||||
|
<img
|
||||||
|
src={posterUrl(entry)}
|
||||||
|
alt=""
|
||||||
|
className="absolute inset-0 h-full w-full translate-x-1 rotate-3 rounded-lg object-cover opacity-70"
|
||||||
|
/>
|
||||||
|
</>
|
||||||
|
)}
|
||||||
|
<img
|
||||||
|
src={posterUrl(entry, "w342")}
|
||||||
|
alt=""
|
||||||
|
loading="lazy"
|
||||||
|
className="relative h-28 w-[4.7rem] rounded-lg object-cover shadow-md ring-1 ring-border/60 transition-transform group-hover:scale-[1.03]"
|
||||||
|
/>
|
||||||
|
{count > 1 && (
|
||||||
|
<span className="absolute -bottom-1.5 -right-1.5 rounded-full bg-sky-500 px-1.5 py-0.5 text-[10px] font-bold text-white shadow">
|
||||||
|
×{count}
|
||||||
|
</span>
|
||||||
|
)}
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div className="min-w-0 flex-1 space-y-1 py-0.5">
|
||||||
|
<p className="text-xs font-medium text-muted-foreground">
|
||||||
|
{date}
|
||||||
|
{session.unknownDate && (
|
||||||
|
<span className="ml-1.5 rounded bg-amber-500/15 px-1 py-px text-[10px] text-amber-600 dark:text-amber-400">
|
||||||
|
date unknown
|
||||||
|
</span>
|
||||||
|
)}
|
||||||
|
</p>
|
||||||
|
<p className="line-clamp-2 font-semibold leading-tight">
|
||||||
|
{entry?.title ?? "Unknown title"}
|
||||||
|
</p>
|
||||||
|
<p
|
||||||
|
className={`flex items-center gap-1 text-xs text-muted-foreground ${
|
||||||
|
side === "left" ? "md:justify-end" : ""
|
||||||
|
}`}
|
||||||
|
>
|
||||||
|
{isMovie ? (
|
||||||
|
<Film className="h-3 w-3" />
|
||||||
|
) : (
|
||||||
|
<Tv className="h-3 w-3" />
|
||||||
|
)}
|
||||||
|
{isMovie
|
||||||
|
? "Movie"
|
||||||
|
: count > 1
|
||||||
|
? `${sessionEpisodeLabel(session)} · ${count} episodes`
|
||||||
|
: `${sessionEpisodeLabel(session)} · ${session.events[0].episode?.name ?? ""}`}
|
||||||
|
</p>
|
||||||
|
{session.minutes > 0 && (
|
||||||
|
<p className="text-[11px] text-muted-foreground/80 tabular-nums">
|
||||||
|
{formatMinutes(session.minutes)}
|
||||||
|
</p>
|
||||||
|
)}
|
||||||
|
</div>
|
||||||
|
</Link>
|
||||||
|
</li>
|
||||||
|
);
|
||||||
|
}
|
||||||
@@ -0,0 +1,496 @@
|
|||||||
|
import type { ReactNode } from "react";
|
||||||
|
import { Link } from "react-router-dom";
|
||||||
|
import {
|
||||||
|
CalendarDays,
|
||||||
|
Clapperboard,
|
||||||
|
Flame,
|
||||||
|
Popcorn,
|
||||||
|
Repeat,
|
||||||
|
Sparkles,
|
||||||
|
Star,
|
||||||
|
Tv,
|
||||||
|
Users,
|
||||||
|
Zap,
|
||||||
|
} from "lucide-react";
|
||||||
|
import { getTmdbImageUrl } from "@/lib/api";
|
||||||
|
import {
|
||||||
|
MONTH_NAMES,
|
||||||
|
WEEKDAY_NAMES,
|
||||||
|
episodeCode,
|
||||||
|
formatMinutes,
|
||||||
|
posterUrl,
|
||||||
|
type YearReview,
|
||||||
|
} from "@/lib/history";
|
||||||
|
import type { HistoryEntry, HistoryEvent } from "@/api/history";
|
||||||
|
|
||||||
|
const formatDay = (day: string) =>
|
||||||
|
new Date(`${day}T12:00:00`).toLocaleDateString("en-US", {
|
||||||
|
weekday: "long",
|
||||||
|
month: "long",
|
||||||
|
day: "numeric",
|
||||||
|
});
|
||||||
|
|
||||||
|
export function YearInReview({
|
||||||
|
year,
|
||||||
|
review,
|
||||||
|
entries,
|
||||||
|
}: {
|
||||||
|
year: number;
|
||||||
|
review: YearReview;
|
||||||
|
entries: Record<number, HistoryEntry>;
|
||||||
|
}) {
|
||||||
|
const hours = Math.round(review.totalMinutes / 60);
|
||||||
|
const maxMonth = Math.max(1, ...review.monthly.map((m) => m.minutes));
|
||||||
|
const maxWeekday = Math.max(1, ...review.weekdays);
|
||||||
|
const topWeekday = review.weekdays.indexOf(Math.max(...review.weekdays));
|
||||||
|
const topMonth = review.monthly.findIndex((m) => m.minutes === maxMonth);
|
||||||
|
|
||||||
|
return (
|
||||||
|
<div className="space-y-6">
|
||||||
|
{/* Hero */}
|
||||||
|
<section className="relative overflow-hidden rounded-3xl bg-linear-to-br from-fuchsia-600 via-violet-600 to-indigo-700 p-8 text-white shadow-xl sm:p-10">
|
||||||
|
<div className="absolute -right-16 -top-16 h-64 w-64 rounded-full bg-white/10 blur-2xl" />
|
||||||
|
<div className="absolute -bottom-20 left-1/3 h-56 w-56 rounded-full bg-amber-300/20 blur-3xl" />
|
||||||
|
<div className="relative space-y-6">
|
||||||
|
<p className="inline-flex items-center gap-2 text-sm font-medium uppercase tracking-[0.2em] text-white/80">
|
||||||
|
<Sparkles className="h-4 w-4" /> Your {year} in review
|
||||||
|
</p>
|
||||||
|
<div>
|
||||||
|
<p className="text-6xl font-black tracking-tight tabular-nums sm:text-8xl">
|
||||||
|
{hours.toLocaleString()}
|
||||||
|
</p>
|
||||||
|
<p className="text-lg font-medium text-white/85">
|
||||||
|
hours of movies and TV
|
||||||
|
{hours >= 24 && (
|
||||||
|
<span className="text-white/70">
|
||||||
|
{" "}
|
||||||
|
— that's {Math.round(hours / 24)} full days
|
||||||
|
</span>
|
||||||
|
)}
|
||||||
|
</p>
|
||||||
|
</div>
|
||||||
|
<div className="grid grid-cols-2 gap-3 sm:grid-cols-4">
|
||||||
|
<HeroStat label="Movies" value={review.uniqueMovies} />
|
||||||
|
<HeroStat label="Episodes" value={review.episodeWatches} />
|
||||||
|
<HeroStat label="Shows" value={review.uniqueShows} />
|
||||||
|
<HeroStat label="Days watching" value={review.activeDays} />
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</section>
|
||||||
|
|
||||||
|
{/* Highlights */}
|
||||||
|
<div className="grid gap-4 sm:grid-cols-2 lg:grid-cols-4">
|
||||||
|
{review.longestStreak && review.longestStreak.days > 1 && (
|
||||||
|
<HighlightCard
|
||||||
|
icon={<Flame />}
|
||||||
|
tone="from-orange-500/20 to-red-500/10"
|
||||||
|
label="Longest streak"
|
||||||
|
value={`${review.longestStreak.days} days`}
|
||||||
|
detail={`${shortDay(review.longestStreak.start)} – ${shortDay(review.longestStreak.end)}`}
|
||||||
|
/>
|
||||||
|
)}
|
||||||
|
{review.busiestDay && (
|
||||||
|
<HighlightCard
|
||||||
|
icon={<CalendarDays />}
|
||||||
|
tone="from-sky-500/20 to-cyan-500/10"
|
||||||
|
label="Busiest day"
|
||||||
|
value={formatMinutes(review.busiestDay.minutes)}
|
||||||
|
detail={`${formatDay(review.busiestDay.day)} · ${review.busiestDay.watches} watched`}
|
||||||
|
/>
|
||||||
|
)}
|
||||||
|
{review.biggestBinge && (
|
||||||
|
<HighlightCard
|
||||||
|
icon={<Zap />}
|
||||||
|
tone="from-violet-500/20 to-fuchsia-500/10"
|
||||||
|
label="Biggest binge"
|
||||||
|
value={`${review.biggestBinge.episodes} episodes`}
|
||||||
|
detail={`${review.biggestBinge.entry.title} · ${shortDay(review.biggestBinge.day)}`}
|
||||||
|
poster={posterUrl(review.biggestBinge.entry)}
|
||||||
|
/>
|
||||||
|
)}
|
||||||
|
<HighlightCard
|
||||||
|
icon={<Star />}
|
||||||
|
tone="from-amber-500/20 to-yellow-500/10"
|
||||||
|
label="Favorite day"
|
||||||
|
value={WEEKDAY_NAMES[topWeekday]}
|
||||||
|
detail={`and ${MONTH_NAMES[topMonth]} was your biggest month`}
|
||||||
|
/>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div className="grid gap-6 lg:grid-cols-2">
|
||||||
|
{/* Top shows */}
|
||||||
|
{review.topShows.length > 0 && (
|
||||||
|
<Card title="Top shows" icon={<Tv />}>
|
||||||
|
<ol className="space-y-2.5">
|
||||||
|
{review.topShows.map((s, i) => (
|
||||||
|
<li key={s.entry.id}>
|
||||||
|
<Link
|
||||||
|
to={`/entry/${s.entry.id}`}
|
||||||
|
className="flex items-center gap-3 rounded-lg p-1 transition-colors hover:bg-accent/50"
|
||||||
|
>
|
||||||
|
<span className="w-7 text-center text-2xl font-black text-muted-foreground/60 tabular-nums">
|
||||||
|
{i + 1}
|
||||||
|
</span>
|
||||||
|
<img
|
||||||
|
src={posterUrl(s.entry)}
|
||||||
|
alt=""
|
||||||
|
className="h-14 w-10 rounded object-cover ring-1 ring-border/60"
|
||||||
|
/>
|
||||||
|
<div className="min-w-0 flex-1">
|
||||||
|
<p className="line-clamp-1 font-semibold">
|
||||||
|
{s.entry.title}
|
||||||
|
</p>
|
||||||
|
<p className="text-xs text-muted-foreground">
|
||||||
|
{s.episodes} episodes · {formatMinutes(s.minutes)}
|
||||||
|
</p>
|
||||||
|
</div>
|
||||||
|
</Link>
|
||||||
|
</li>
|
||||||
|
))}
|
||||||
|
</ol>
|
||||||
|
</Card>
|
||||||
|
)}
|
||||||
|
|
||||||
|
{/* Genres */}
|
||||||
|
{review.topGenres.length > 0 && (
|
||||||
|
<Card title="Top genres" icon={<Clapperboard />}>
|
||||||
|
<div className="space-y-3">
|
||||||
|
{review.topGenres.map((g, i) => (
|
||||||
|
<div key={g.name} className="space-y-1">
|
||||||
|
<div className="flex justify-between text-sm">
|
||||||
|
<span className="font-medium">{g.name}</span>
|
||||||
|
<span className="text-muted-foreground tabular-nums">
|
||||||
|
{Math.round(g.share * 100)}%
|
||||||
|
</span>
|
||||||
|
</div>
|
||||||
|
<div className="h-2 overflow-hidden rounded-full bg-muted">
|
||||||
|
<div
|
||||||
|
className="h-full rounded-full bg-linear-to-r from-violet-500 to-fuchsia-500"
|
||||||
|
style={{
|
||||||
|
width: `${(g.minutes / review.topGenres[0].minutes) * 100}%`,
|
||||||
|
opacity: 1 - i * 0.1,
|
||||||
|
}}
|
||||||
|
/>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
))}
|
||||||
|
</div>
|
||||||
|
</Card>
|
||||||
|
)}
|
||||||
|
</div>
|
||||||
|
|
||||||
|
{/* Monthly activity */}
|
||||||
|
<Card title="Month by month" icon={<CalendarDays />}>
|
||||||
|
<div className="flex h-44 items-end gap-1.5 sm:gap-3">
|
||||||
|
{review.monthly.map((m, i) => (
|
||||||
|
<div key={i} className="flex flex-1 flex-col items-center gap-1.5">
|
||||||
|
<div className="flex h-36 w-full items-end">
|
||||||
|
<div
|
||||||
|
title={`${MONTH_NAMES[i]}: ${formatMinutes(m.minutes)} · ${m.movies} movies · ${m.episodes} episodes`}
|
||||||
|
className={`w-full rounded-t-md transition-all ${
|
||||||
|
i === topMonth
|
||||||
|
? "bg-linear-to-t from-fuchsia-600 to-violet-400"
|
||||||
|
: "bg-linear-to-t from-violet-500/50 to-violet-400/30"
|
||||||
|
}`}
|
||||||
|
style={{
|
||||||
|
height: `${Math.max(2, (m.minutes / maxMonth) * 100)}%`,
|
||||||
|
}}
|
||||||
|
/>
|
||||||
|
</div>
|
||||||
|
<span className="text-[10px] font-medium text-muted-foreground">
|
||||||
|
{MONTH_NAMES[i].slice(0, 3)}
|
||||||
|
</span>
|
||||||
|
</div>
|
||||||
|
))}
|
||||||
|
</div>
|
||||||
|
</Card>
|
||||||
|
|
||||||
|
<div className="grid gap-6 lg:grid-cols-3">
|
||||||
|
{/* Top rated movies */}
|
||||||
|
{review.topMovies.length > 0 && (
|
||||||
|
<Card
|
||||||
|
title="Your top-rated movies"
|
||||||
|
icon={<Popcorn />}
|
||||||
|
className="lg:col-span-2"
|
||||||
|
>
|
||||||
|
<div className="grid grid-cols-3 gap-3 sm:grid-cols-5">
|
||||||
|
{review.topMovies.map((m) => (
|
||||||
|
<Link
|
||||||
|
key={m.entry.id}
|
||||||
|
to={`/entry/${m.entry.id}`}
|
||||||
|
className="group space-y-1.5"
|
||||||
|
>
|
||||||
|
<img
|
||||||
|
src={posterUrl(m.entry, "w342")}
|
||||||
|
alt={m.entry.title}
|
||||||
|
className="aspect-2/3 w-full rounded-lg object-cover ring-1 ring-border/60 transition-transform group-hover:scale-[1.03]"
|
||||||
|
/>
|
||||||
|
<p className="line-clamp-1 text-xs font-medium">
|
||||||
|
{m.entry.title}
|
||||||
|
</p>
|
||||||
|
<p className="inline-flex items-center gap-0.5 text-xs text-muted-foreground tabular-nums">
|
||||||
|
<Star className="h-3 w-3 fill-yellow-400 text-yellow-400" />
|
||||||
|
{m.rating}/10
|
||||||
|
</p>
|
||||||
|
</Link>
|
||||||
|
))}
|
||||||
|
</div>
|
||||||
|
</Card>
|
||||||
|
)}
|
||||||
|
|
||||||
|
{/* Weekdays */}
|
||||||
|
<Card title="When you watch" icon={<CalendarDays />}>
|
||||||
|
<div className="space-y-2">
|
||||||
|
{review.weekdays.map((n, d) => (
|
||||||
|
<div key={d} className="flex items-center gap-2 text-xs">
|
||||||
|
<span className="w-8 text-muted-foreground">
|
||||||
|
{WEEKDAY_NAMES[d]}
|
||||||
|
</span>
|
||||||
|
<div className="h-2 flex-1 overflow-hidden rounded-full bg-muted">
|
||||||
|
<div
|
||||||
|
className={`h-full rounded-full ${d === topWeekday ? "bg-amber-500" : "bg-amber-500/40"}`}
|
||||||
|
style={{ width: `${(n / maxWeekday) * 100}%` }}
|
||||||
|
/>
|
||||||
|
</div>
|
||||||
|
<span className="w-8 text-right tabular-nums text-muted-foreground">
|
||||||
|
{n}
|
||||||
|
</span>
|
||||||
|
</div>
|
||||||
|
))}
|
||||||
|
</div>
|
||||||
|
</Card>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div className="grid gap-6 sm:grid-cols-2 lg:grid-cols-3">
|
||||||
|
{review.rewatches.length > 0 && (
|
||||||
|
<Card title="On repeat" icon={<Repeat />}>
|
||||||
|
<ul className="space-y-2">
|
||||||
|
{review.rewatches.map((r) => (
|
||||||
|
<li
|
||||||
|
key={r.entry.id}
|
||||||
|
className="flex items-center justify-between gap-2 text-sm"
|
||||||
|
>
|
||||||
|
<Link
|
||||||
|
to={`/entry/${r.entry.id}`}
|
||||||
|
className="line-clamp-1 hover:underline"
|
||||||
|
>
|
||||||
|
{r.entry.title}
|
||||||
|
</Link>
|
||||||
|
<span className="shrink-0 rounded-full bg-muted px-2 py-0.5 text-xs tabular-nums">
|
||||||
|
×{r.watches}
|
||||||
|
</span>
|
||||||
|
</li>
|
||||||
|
))}
|
||||||
|
</ul>
|
||||||
|
</Card>
|
||||||
|
)}
|
||||||
|
|
||||||
|
{review.topNetworks.length > 0 && (
|
||||||
|
<Card title="Top networks" icon={<Tv />}>
|
||||||
|
<ul className="space-y-2.5">
|
||||||
|
{review.topNetworks.map((n) => (
|
||||||
|
<li
|
||||||
|
key={n.name}
|
||||||
|
className="flex items-center justify-between gap-2 text-sm"
|
||||||
|
>
|
||||||
|
{n.logo_path ? (
|
||||||
|
<img
|
||||||
|
src={getTmdbImageUrl(n.logo_path, "w92")}
|
||||||
|
alt={n.name}
|
||||||
|
title={n.name}
|
||||||
|
className="h-5 max-w-24 object-contain dark:invert dark:brightness-200"
|
||||||
|
/>
|
||||||
|
) : (
|
||||||
|
<span>{n.name}</span>
|
||||||
|
)}
|
||||||
|
<span className="text-xs text-muted-foreground tabular-nums">
|
||||||
|
{n.episodes} eps
|
||||||
|
</span>
|
||||||
|
</li>
|
||||||
|
))}
|
||||||
|
</ul>
|
||||||
|
</Card>
|
||||||
|
)}
|
||||||
|
|
||||||
|
{review.topBuddies.length > 0 && (
|
||||||
|
<Card title="Watched together" icon={<Users />}>
|
||||||
|
<ul className="space-y-2">
|
||||||
|
{review.topBuddies.map((b) => (
|
||||||
|
<li
|
||||||
|
key={b.id}
|
||||||
|
className="flex items-center justify-between gap-2 text-sm"
|
||||||
|
>
|
||||||
|
<Link
|
||||||
|
to={`/watch-buddies/${b.id}`}
|
||||||
|
className="hover:underline"
|
||||||
|
>
|
||||||
|
{b.name}
|
||||||
|
</Link>
|
||||||
|
<span className="text-xs text-muted-foreground tabular-nums">
|
||||||
|
{b.watches} watches
|
||||||
|
</span>
|
||||||
|
</li>
|
||||||
|
))}
|
||||||
|
</ul>
|
||||||
|
</Card>
|
||||||
|
)}
|
||||||
|
</div>
|
||||||
|
|
||||||
|
{/* Started shows */}
|
||||||
|
{review.startedShows.length > 0 && (
|
||||||
|
<Card
|
||||||
|
title={`New shows you started (${review.startedShows.length})`}
|
||||||
|
icon={<Sparkles />}
|
||||||
|
>
|
||||||
|
<div className="flex gap-3 overflow-x-auto pb-2">
|
||||||
|
{review.startedShows.map((e) => (
|
||||||
|
<Link
|
||||||
|
key={e.id}
|
||||||
|
to={`/entry/${e.id}`}
|
||||||
|
className="w-24 shrink-0 space-y-1"
|
||||||
|
>
|
||||||
|
<img
|
||||||
|
src={posterUrl(e, "w342")}
|
||||||
|
alt={e.title}
|
||||||
|
className="aspect-2/3 w-full rounded-lg object-cover ring-1 ring-border/60"
|
||||||
|
/>
|
||||||
|
<p className="line-clamp-1 text-xs">{e.title}</p>
|
||||||
|
</Link>
|
||||||
|
))}
|
||||||
|
</div>
|
||||||
|
</Card>
|
||||||
|
)}
|
||||||
|
|
||||||
|
{/* Bookends */}
|
||||||
|
{review.first && review.last && (
|
||||||
|
<div className="grid gap-4 sm:grid-cols-2">
|
||||||
|
<BookendCard
|
||||||
|
label={`First watch of ${year}`}
|
||||||
|
event={review.first}
|
||||||
|
entries={entries}
|
||||||
|
/>
|
||||||
|
<BookendCard
|
||||||
|
label={`Last watch of ${year}`}
|
||||||
|
event={review.last}
|
||||||
|
entries={entries}
|
||||||
|
/>
|
||||||
|
</div>
|
||||||
|
)}
|
||||||
|
</div>
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
const shortDay = (day: string) =>
|
||||||
|
new Date(`${day}T12:00:00`).toLocaleDateString("en-US", {
|
||||||
|
month: "short",
|
||||||
|
day: "numeric",
|
||||||
|
});
|
||||||
|
|
||||||
|
function HeroStat({ label, value }: { label: string; value: number }) {
|
||||||
|
return (
|
||||||
|
<div className="rounded-2xl bg-white/10 px-4 py-3 backdrop-blur">
|
||||||
|
<p className="text-3xl font-bold tabular-nums">
|
||||||
|
{value.toLocaleString()}
|
||||||
|
</p>
|
||||||
|
<p className="text-xs font-medium uppercase tracking-wider text-white/75">
|
||||||
|
{label}
|
||||||
|
</p>
|
||||||
|
</div>
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
function HighlightCard({
|
||||||
|
icon,
|
||||||
|
tone,
|
||||||
|
label,
|
||||||
|
value,
|
||||||
|
detail,
|
||||||
|
poster,
|
||||||
|
}: {
|
||||||
|
icon: ReactNode;
|
||||||
|
tone: string;
|
||||||
|
label: string;
|
||||||
|
value: string;
|
||||||
|
detail: string;
|
||||||
|
poster?: string;
|
||||||
|
}) {
|
||||||
|
return (
|
||||||
|
<div
|
||||||
|
className={`relative overflow-hidden rounded-2xl border border-border/60 bg-linear-to-br ${tone} p-5`}
|
||||||
|
>
|
||||||
|
{poster && (
|
||||||
|
<img
|
||||||
|
src={poster}
|
||||||
|
alt=""
|
||||||
|
className="absolute -right-4 -bottom-6 h-32 w-24 rotate-12 rounded-lg object-cover opacity-30"
|
||||||
|
/>
|
||||||
|
)}
|
||||||
|
<div className="relative space-y-1">
|
||||||
|
<p className="flex items-center gap-1.5 text-xs font-semibold uppercase tracking-wider text-muted-foreground [&_svg]:h-3.5 [&_svg]:w-3.5">
|
||||||
|
{icon}
|
||||||
|
{label}
|
||||||
|
</p>
|
||||||
|
<p className="text-3xl font-black tracking-tight">{value}</p>
|
||||||
|
<p className="line-clamp-2 text-xs text-muted-foreground">{detail}</p>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
function Card({
|
||||||
|
title,
|
||||||
|
icon,
|
||||||
|
children,
|
||||||
|
className = "",
|
||||||
|
}: {
|
||||||
|
title: string;
|
||||||
|
icon: ReactNode;
|
||||||
|
children: ReactNode;
|
||||||
|
className?: string;
|
||||||
|
}) {
|
||||||
|
return (
|
||||||
|
<section
|
||||||
|
className={`rounded-2xl border border-border/60 bg-card/60 p-5 ${className}`}
|
||||||
|
>
|
||||||
|
<h3 className="mb-4 flex items-center gap-2 text-sm font-semibold [&_svg]:h-4 [&_svg]:w-4 [&_svg]:text-muted-foreground">
|
||||||
|
{icon}
|
||||||
|
{title}
|
||||||
|
</h3>
|
||||||
|
{children}
|
||||||
|
</section>
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
function BookendCard({
|
||||||
|
label,
|
||||||
|
event,
|
||||||
|
entries,
|
||||||
|
}: {
|
||||||
|
label: string;
|
||||||
|
event: HistoryEvent;
|
||||||
|
entries: Record<number, HistoryEntry>;
|
||||||
|
}) {
|
||||||
|
const entry = entries[event.entry_id];
|
||||||
|
return (
|
||||||
|
<div className="flex items-center gap-4 rounded-2xl border border-border/60 bg-card/60 p-4">
|
||||||
|
<img
|
||||||
|
src={posterUrl(entry, "w342")}
|
||||||
|
alt=""
|
||||||
|
className="h-24 w-16 rounded-lg object-cover ring-1 ring-border/60"
|
||||||
|
/>
|
||||||
|
<div className="min-w-0 space-y-0.5">
|
||||||
|
<p className="text-xs font-semibold uppercase tracking-wider text-muted-foreground">
|
||||||
|
{label}
|
||||||
|
</p>
|
||||||
|
<p className="line-clamp-1 text-lg font-bold">{entry?.title}</p>
|
||||||
|
<p className="text-xs text-muted-foreground">
|
||||||
|
{event.kind === "episode" && `${episodeCode(event.episode)} · `}
|
||||||
|
{new Date(event.date_seen).toLocaleDateString("en-US", {
|
||||||
|
month: "long",
|
||||||
|
day: "numeric",
|
||||||
|
})}
|
||||||
|
</p>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
);
|
||||||
|
}
|
||||||
@@ -14,6 +14,7 @@ import {
|
|||||||
Sun,
|
Sun,
|
||||||
Film,
|
Film,
|
||||||
Video,
|
Video,
|
||||||
|
History,
|
||||||
} from "lucide-react";
|
} from "lucide-react";
|
||||||
import { useThemeStore } from "@/stores/useThemeStore";
|
import { useThemeStore } from "@/stores/useThemeStore";
|
||||||
import { Separator } from "@/components/ui/separator";
|
import { Separator } from "@/components/ui/separator";
|
||||||
@@ -26,6 +27,7 @@ const mainNav = [
|
|||||||
const libraryNav = [
|
const libraryNav = [
|
||||||
{ to: "/library", icon: Library, label: "Library" },
|
{ to: "/library", icon: Library, label: "Library" },
|
||||||
{ to: "/watchlist", icon: ListVideo, label: "Watchlist" },
|
{ to: "/watchlist", icon: ListVideo, label: "Watchlist" },
|
||||||
|
{ to: "/history", icon: History, label: "History" },
|
||||||
{ to: "/collections", icon: FolderOpen, label: "Collections" },
|
{ to: "/collections", icon: FolderOpen, label: "Collections" },
|
||||||
{ to: "/media", icon: Image, label: "Media" },
|
{ to: "/media", icon: Image, label: "Media" },
|
||||||
{ to: "/discovery-videos", icon: Video, label: "Discover" },
|
{ to: "/discovery-videos", icon: Video, label: "Discover" },
|
||||||
|
|||||||
@@ -0,0 +1,24 @@
|
|||||||
|
import { useQuery } from "@tanstack/react-query";
|
||||||
|
import { historyApi } from "@/api/history";
|
||||||
|
|
||||||
|
export const historyKeys = {
|
||||||
|
years: ["history", "years"] as const,
|
||||||
|
year: (year: number) => ["history", year] as const,
|
||||||
|
};
|
||||||
|
|
||||||
|
export function useHistoryYears() {
|
||||||
|
return useQuery({
|
||||||
|
queryKey: historyKeys.years,
|
||||||
|
queryFn: historyApi.getYears,
|
||||||
|
staleTime: 5 * 60 * 1000,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
export function useHistoryYear(year: number) {
|
||||||
|
return useQuery({
|
||||||
|
queryKey: historyKeys.year(year),
|
||||||
|
queryFn: () => historyApi.getYear(year),
|
||||||
|
enabled: !!year,
|
||||||
|
staleTime: 5 * 60 * 1000,
|
||||||
|
});
|
||||||
|
}
|
||||||
@@ -0,0 +1,373 @@
|
|||||||
|
import { getMediaUrl, getTmdbImageUrl } from "@/lib/api";
|
||||||
|
import type { HistoryEntry, HistoryEpisode, HistoryEvent } from "@/api/history";
|
||||||
|
|
||||||
|
export type HistoryTypeFilter = "all" | "movies" | "tv";
|
||||||
|
|
||||||
|
export const MONTH_NAMES = Array.from({ length: 12 }, (_, m) =>
|
||||||
|
new Date(2000, m, 1).toLocaleDateString("en-US", { month: "long" }),
|
||||||
|
);
|
||||||
|
export const WEEKDAY_NAMES = Array.from({ length: 7 }, (_, d) =>
|
||||||
|
// 2000-01-02 was a Sunday
|
||||||
|
new Date(2000, 0, 2 + d).toLocaleDateString("en-US", { weekday: "short" }),
|
||||||
|
);
|
||||||
|
|
||||||
|
const DEFAULT_POSTER = "/images/default-poster.png";
|
||||||
|
|
||||||
|
/** Selected local poster first, then the TMDB poster. */
|
||||||
|
export function posterUrl(entry: HistoryEntry | undefined, size = "w185") {
|
||||||
|
if (!entry) return DEFAULT_POSTER;
|
||||||
|
const local = entry.medias?.find((m) => m.type === "poster" && m.isSelected);
|
||||||
|
if (local) return getMediaUrl("entries", "posters", "preview", local.path);
|
||||||
|
return entry.poster_path
|
||||||
|
? getTmdbImageUrl(entry.poster_path, size)
|
||||||
|
: DEFAULT_POSTER;
|
||||||
|
}
|
||||||
|
|
||||||
|
export function episodeCode(ep: HistoryEpisode | null | undefined) {
|
||||||
|
if (!ep) return "";
|
||||||
|
const s = ep.season_number ?? "?";
|
||||||
|
return `S${s}E${ep.episode_number}`;
|
||||||
|
}
|
||||||
|
|
||||||
|
export function eventRuntime(
|
||||||
|
event: HistoryEvent,
|
||||||
|
entries: Record<number, HistoryEntry>,
|
||||||
|
) {
|
||||||
|
if (event.kind === "episode") {
|
||||||
|
return event.episode?.runtime ?? entries[event.entry_id]?.runtime ?? 0;
|
||||||
|
}
|
||||||
|
return entries[event.entry_id]?.runtime ?? 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
export function filterEvents(events: HistoryEvent[], type: HistoryTypeFilter) {
|
||||||
|
if (type === "movies") return events.filter((e) => e.kind === "movie");
|
||||||
|
if (type === "tv") return events.filter((e) => e.kind === "episode");
|
||||||
|
return events;
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Local calendar day, e.g. "2024-12-31". */
|
||||||
|
export function dayKey(date: string | Date) {
|
||||||
|
const d = new Date(date);
|
||||||
|
const mm = String(d.getMonth() + 1).padStart(2, "0");
|
||||||
|
const dd = String(d.getDate()).padStart(2, "0");
|
||||||
|
return `${d.getFullYear()}-${mm}-${dd}`;
|
||||||
|
}
|
||||||
|
|
||||||
|
export function formatMinutes(minutes: number) {
|
||||||
|
if (minutes < 60) return `${minutes}m`;
|
||||||
|
const h = Math.floor(minutes / 60);
|
||||||
|
const m = minutes % 60;
|
||||||
|
return m ? `${h}h ${m}m` : `${h}h`;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* ---------- Sessions ---------- */
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A movie watch, or a run of episodes of the same show watched one after the
|
||||||
|
* other on the same day.
|
||||||
|
*/
|
||||||
|
export interface HistorySession {
|
||||||
|
key: string;
|
||||||
|
kind: "movie" | "episode";
|
||||||
|
entry: HistoryEntry | undefined;
|
||||||
|
entryId: number;
|
||||||
|
date: string; // latest watch in the session
|
||||||
|
day: string;
|
||||||
|
events: HistoryEvent[]; // oldest → newest
|
||||||
|
minutes: number;
|
||||||
|
unknownDate: boolean;
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Groups newest-first events into sessions (also newest-first). */
|
||||||
|
export function buildSessions(
|
||||||
|
events: HistoryEvent[],
|
||||||
|
entries: Record<number, HistoryEntry>,
|
||||||
|
): HistorySession[] {
|
||||||
|
const sessions: HistorySession[] = [];
|
||||||
|
for (const event of events) {
|
||||||
|
const day = dayKey(event.date_seen);
|
||||||
|
const last = sessions.at(-1);
|
||||||
|
if (
|
||||||
|
last &&
|
||||||
|
event.kind === "episode" &&
|
||||||
|
last.kind === "episode" &&
|
||||||
|
last.entryId === event.entry_id &&
|
||||||
|
last.day === day
|
||||||
|
) {
|
||||||
|
last.events.unshift(event);
|
||||||
|
last.minutes += eventRuntime(event, entries);
|
||||||
|
last.unknownDate ||= event.accuracy === "UNKNOWN";
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
sessions.push({
|
||||||
|
key: event.id,
|
||||||
|
kind: event.kind,
|
||||||
|
entry: entries[event.entry_id],
|
||||||
|
entryId: event.entry_id,
|
||||||
|
date: event.date_seen,
|
||||||
|
day,
|
||||||
|
events: [event],
|
||||||
|
minutes: eventRuntime(event, entries),
|
||||||
|
unknownDate: event.accuracy === "UNKNOWN",
|
||||||
|
});
|
||||||
|
}
|
||||||
|
// Episodes within a session in watch order (season, episode)
|
||||||
|
for (const s of sessions) {
|
||||||
|
if (s.kind === "episode" && s.events.length > 1) {
|
||||||
|
s.events.sort(
|
||||||
|
(a, b) =>
|
||||||
|
(a.episode?.season_number ?? 0) - (b.episode?.season_number ?? 0) ||
|
||||||
|
(a.episode?.episode_number ?? 0) - (b.episode?.episode_number ?? 0),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return sessions;
|
||||||
|
}
|
||||||
|
|
||||||
|
/** "S1 E1–E5" for a session's episodes, or "S1 E3" for one. */
|
||||||
|
export function sessionEpisodeLabel(session: HistorySession) {
|
||||||
|
const eps = session.events.map((e) => e.episode).filter(Boolean);
|
||||||
|
if (eps.length === 0) return "";
|
||||||
|
if (eps.length === 1) return episodeCode(eps[0]);
|
||||||
|
const first = eps[0]!;
|
||||||
|
const last = eps.at(-1)!;
|
||||||
|
if (first.season_number === last.season_number) {
|
||||||
|
return `S${first.season_number} E${first.episode_number}–E${last.episode_number}`;
|
||||||
|
}
|
||||||
|
return `${episodeCode(first)} – ${episodeCode(last)}`;
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Episode page for a single episode, otherwise the entry page. */
|
||||||
|
export function sessionLink(session: HistorySession) {
|
||||||
|
if (session.kind === "episode" && session.events.length === 1) {
|
||||||
|
const ep = session.events[0].episode;
|
||||||
|
if (ep) {
|
||||||
|
return `/entry/${session.entryId}/season/${ep.season_id}/episode/${ep.id}`;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return `/entry/${session.entryId}`;
|
||||||
|
}
|
||||||
|
|
||||||
|
export interface MonthGroup {
|
||||||
|
month: number; // 0-11
|
||||||
|
sessions: HistorySession[];
|
||||||
|
movies: number;
|
||||||
|
episodes: number;
|
||||||
|
minutes: number;
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Sessions bucketed by month, December first. Empty months are skipped. */
|
||||||
|
export function groupByMonth(sessions: HistorySession[]): MonthGroup[] {
|
||||||
|
const months = new Map<number, MonthGroup>();
|
||||||
|
for (const s of sessions) {
|
||||||
|
const month = new Date(s.date).getMonth();
|
||||||
|
let group = months.get(month);
|
||||||
|
if (!group) {
|
||||||
|
group = { month, sessions: [], movies: 0, episodes: 0, minutes: 0 };
|
||||||
|
months.set(month, group);
|
||||||
|
}
|
||||||
|
group.sessions.push(s);
|
||||||
|
group.minutes += s.minutes;
|
||||||
|
if (s.kind === "movie") group.movies++;
|
||||||
|
else group.episodes += s.events.length;
|
||||||
|
}
|
||||||
|
return [...months.values()].sort((a, b) => b.month - a.month);
|
||||||
|
}
|
||||||
|
|
||||||
|
/* ---------- Year in review ---------- */
|
||||||
|
|
||||||
|
export interface YearReview {
|
||||||
|
totalMinutes: number;
|
||||||
|
movieWatches: number;
|
||||||
|
uniqueMovies: number;
|
||||||
|
episodeWatches: number;
|
||||||
|
uniqueShows: number;
|
||||||
|
activeDays: number;
|
||||||
|
topShows: { entry: HistoryEntry; episodes: number; minutes: number }[];
|
||||||
|
topMovies: { entry: HistoryEntry; rating: number; watches: number }[];
|
||||||
|
rewatches: { entry: HistoryEntry; watches: number }[];
|
||||||
|
topGenres: { name: string; minutes: number; share: number }[];
|
||||||
|
topNetworks: { name: string; logo_path: string | null; episodes: number }[];
|
||||||
|
monthly: { movies: number; episodes: number; minutes: number }[];
|
||||||
|
weekdays: number[];
|
||||||
|
busiestDay: { day: string; watches: number; minutes: number } | null;
|
||||||
|
longestStreak: { days: number; start: string; end: string } | null;
|
||||||
|
biggestBinge: { entry: HistoryEntry; day: string; episodes: number } | null;
|
||||||
|
startedShows: HistoryEntry[];
|
||||||
|
topBuddies: { id: number; name: string; watches: number }[];
|
||||||
|
first: HistoryEvent | null;
|
||||||
|
last: HistoryEvent | null;
|
||||||
|
}
|
||||||
|
|
||||||
|
const top = <T>(items: T[], score: (t: T) => number, n: number) =>
|
||||||
|
[...items].sort((a, b) => score(b) - score(a)).slice(0, n);
|
||||||
|
|
||||||
|
/** Year-in-review stats from a year's events (any order). */
|
||||||
|
export function computeReview(
|
||||||
|
events: HistoryEvent[],
|
||||||
|
entries: Record<number, HistoryEntry>,
|
||||||
|
): YearReview {
|
||||||
|
const sorted = [...events].sort(
|
||||||
|
(a, b) => new Date(a.date_seen).getTime() - new Date(b.date_seen).getTime(),
|
||||||
|
);
|
||||||
|
|
||||||
|
let totalMinutes = 0;
|
||||||
|
const movieCounts = new Map<number, number>();
|
||||||
|
const showStats = new Map<number, { episodes: number; minutes: number }>();
|
||||||
|
const genreMinutes = new Map<string, number>();
|
||||||
|
const networkEpisodes = new Map<string, { logo: string | null; n: number }>();
|
||||||
|
const monthly = Array.from({ length: 12 }, () => ({
|
||||||
|
movies: 0,
|
||||||
|
episodes: 0,
|
||||||
|
minutes: 0,
|
||||||
|
}));
|
||||||
|
const weekdays = Array(7).fill(0) as number[];
|
||||||
|
const days = new Map<string, { watches: number; minutes: number }>();
|
||||||
|
const showDays = new Map<string, number>(); // `${entryId}|${day}` → episodes
|
||||||
|
const firstEpisodeOfShow = new Map<number, HistoryEvent>();
|
||||||
|
const buddies = new Map<number, { name: string; watches: number }>();
|
||||||
|
|
||||||
|
for (const event of sorted) {
|
||||||
|
const entry = entries[event.entry_id];
|
||||||
|
const minutes = eventRuntime(event, entries);
|
||||||
|
const date = new Date(event.date_seen);
|
||||||
|
const day = dayKey(date);
|
||||||
|
totalMinutes += minutes;
|
||||||
|
|
||||||
|
if (event.kind === "movie") {
|
||||||
|
movieCounts.set(
|
||||||
|
event.entry_id,
|
||||||
|
(movieCounts.get(event.entry_id) ?? 0) + 1,
|
||||||
|
);
|
||||||
|
monthly[date.getMonth()].movies++;
|
||||||
|
} else {
|
||||||
|
const s = showStats.get(event.entry_id) ?? { episodes: 0, minutes: 0 };
|
||||||
|
s.episodes++;
|
||||||
|
s.minutes += minutes;
|
||||||
|
showStats.set(event.entry_id, s);
|
||||||
|
monthly[date.getMonth()].episodes++;
|
||||||
|
const k = `${event.entry_id}|${day}`;
|
||||||
|
showDays.set(k, (showDays.get(k) ?? 0) + 1);
|
||||||
|
if (!firstEpisodeOfShow.has(event.entry_id)) {
|
||||||
|
firstEpisodeOfShow.set(event.entry_id, event);
|
||||||
|
}
|
||||||
|
for (const n of entry?.networks ?? []) {
|
||||||
|
const cur = networkEpisodes.get(n.name) ?? { logo: n.logo_path, n: 0 };
|
||||||
|
cur.n++;
|
||||||
|
networkEpisodes.set(n.name, cur);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
monthly[date.getMonth()].minutes += minutes;
|
||||||
|
weekdays[date.getDay()]++;
|
||||||
|
|
||||||
|
const d = days.get(day) ?? { watches: 0, minutes: 0 };
|
||||||
|
d.watches++;
|
||||||
|
d.minutes += minutes;
|
||||||
|
days.set(day, d);
|
||||||
|
|
||||||
|
for (const g of entry?.genres ?? []) {
|
||||||
|
genreMinutes.set(g, (genreMinutes.get(g) ?? 0) + minutes);
|
||||||
|
}
|
||||||
|
for (const b of event.buddies) {
|
||||||
|
const cur = buddies.get(b.id) ?? { name: b.name, watches: 0 };
|
||||||
|
cur.watches++;
|
||||||
|
buddies.set(b.id, cur);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Longest run of consecutive days with at least one watch
|
||||||
|
let longestStreak: YearReview["longestStreak"] = null;
|
||||||
|
const dayList = [...days.keys()].sort();
|
||||||
|
let runStart = 0;
|
||||||
|
for (let i = 1; i <= dayList.length; i++) {
|
||||||
|
const prev = new Date(dayList[i - 1]);
|
||||||
|
const isNext =
|
||||||
|
i < dayList.length &&
|
||||||
|
new Date(dayList[i]).getTime() - prev.getTime() <= 36 * 60 * 60 * 1000;
|
||||||
|
if (!isNext) {
|
||||||
|
const length = i - runStart;
|
||||||
|
if (!longestStreak || length > longestStreak.days) {
|
||||||
|
longestStreak = {
|
||||||
|
days: length,
|
||||||
|
start: dayList[runStart],
|
||||||
|
end: dayList[i - 1],
|
||||||
|
};
|
||||||
|
}
|
||||||
|
runStart = i;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
const busiest = top([...days.entries()], ([, d]) => d.minutes, 1)[0];
|
||||||
|
const binge = top([...showDays.entries()], ([, n]) => n, 1)[0];
|
||||||
|
const totalGenreMinutes =
|
||||||
|
[...genreMinutes.values()].reduce((a, b) => a + b, 0) || 1;
|
||||||
|
|
||||||
|
const movieEntries = [...movieCounts.keys()]
|
||||||
|
.map((id) => entries[id])
|
||||||
|
.filter(Boolean);
|
||||||
|
|
||||||
|
return {
|
||||||
|
totalMinutes,
|
||||||
|
movieWatches: sorted.filter((e) => e.kind === "movie").length,
|
||||||
|
uniqueMovies: movieCounts.size,
|
||||||
|
episodeWatches: sorted.filter((e) => e.kind === "episode").length,
|
||||||
|
uniqueShows: showStats.size,
|
||||||
|
activeDays: days.size,
|
||||||
|
topShows: top([...showStats.entries()], ([, s]) => s.episodes, 5)
|
||||||
|
.filter(([id]) => entries[id])
|
||||||
|
.map(([id, s]) => ({ entry: entries[id], ...s })),
|
||||||
|
topMovies: top(movieEntries, (e) => e.entryRating?.user_rating || 0, 5)
|
||||||
|
.filter((e) => (e.entryRating?.user_rating ?? 0) > 0)
|
||||||
|
.map((e) => ({
|
||||||
|
entry: e,
|
||||||
|
rating: e.entryRating!.user_rating!,
|
||||||
|
watches: movieCounts.get(e.id) ?? 1,
|
||||||
|
})),
|
||||||
|
rewatches: top(
|
||||||
|
movieEntries.filter((e) => (movieCounts.get(e.id) ?? 0) > 1),
|
||||||
|
(e) => movieCounts.get(e.id) ?? 0,
|
||||||
|
5,
|
||||||
|
).map((e) => ({ entry: e, watches: movieCounts.get(e.id)! })),
|
||||||
|
topGenres: top([...genreMinutes.entries()], ([, m]) => m, 6).map(
|
||||||
|
([name, minutes]) => ({
|
||||||
|
name,
|
||||||
|
minutes,
|
||||||
|
share: minutes / totalGenreMinutes,
|
||||||
|
}),
|
||||||
|
),
|
||||||
|
topNetworks: top([...networkEpisodes.entries()], ([, v]) => v.n, 5).map(
|
||||||
|
([name, v]) => ({ name, logo_path: v.logo, episodes: v.n }),
|
||||||
|
),
|
||||||
|
monthly,
|
||||||
|
weekdays,
|
||||||
|
busiestDay: busiest
|
||||||
|
? {
|
||||||
|
day: busiest[0],
|
||||||
|
watches: busiest[1].watches,
|
||||||
|
minutes: busiest[1].minutes,
|
||||||
|
}
|
||||||
|
: null,
|
||||||
|
longestStreak,
|
||||||
|
biggestBinge:
|
||||||
|
binge && binge[1] > 1 && entries[Number(binge[0].split("|")[0])]
|
||||||
|
? {
|
||||||
|
entry: entries[Number(binge[0].split("|")[0])],
|
||||||
|
day: binge[0].split("|")[1],
|
||||||
|
episodes: binge[1],
|
||||||
|
}
|
||||||
|
: null,
|
||||||
|
// Shows whose first episode watched this year was S1E1
|
||||||
|
startedShows: [...firstEpisodeOfShow.values()]
|
||||||
|
.filter(
|
||||||
|
(e) =>
|
||||||
|
e.episode?.season_number === 1 && e.episode?.episode_number === 1,
|
||||||
|
)
|
||||||
|
.map((e) => entries[e.entry_id])
|
||||||
|
.filter(Boolean),
|
||||||
|
topBuddies: top([...buddies.entries()], ([, b]) => b.watches, 5).map(
|
||||||
|
([id, b]) => ({ id, ...b }),
|
||||||
|
),
|
||||||
|
first: sorted[0] ?? null,
|
||||||
|
last: sorted.at(-1) ?? null,
|
||||||
|
};
|
||||||
|
}
|
||||||
@@ -0,0 +1,227 @@
|
|||||||
|
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>
|
||||||
|
);
|
||||||
|
}
|
||||||
@@ -26,6 +26,9 @@ const HomePage = lazy(() =>
|
|||||||
const DashboardPage = lazy(() =>
|
const DashboardPage = lazy(() =>
|
||||||
import("@/pages/DashboardPage").then((m) => ({ default: m.DashboardPage })),
|
import("@/pages/DashboardPage").then((m) => ({ default: m.DashboardPage })),
|
||||||
);
|
);
|
||||||
|
const HistoryPage = lazy(() =>
|
||||||
|
import("@/pages/HistoryPage").then((m) => ({ default: m.HistoryPage })),
|
||||||
|
);
|
||||||
const LibraryPage = lazy(() =>
|
const LibraryPage = lazy(() =>
|
||||||
import("@/pages/LibraryPage").then((m) => ({ default: m.LibraryPage })),
|
import("@/pages/LibraryPage").then((m) => ({ default: m.LibraryPage })),
|
||||||
);
|
);
|
||||||
@@ -161,6 +164,14 @@ export const router = createRouter([
|
|||||||
</SuspensePage>
|
</SuspensePage>
|
||||||
),
|
),
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
path: "/history",
|
||||||
|
element: (
|
||||||
|
<SuspensePage fallback={<WatchlistSkeleton />}>
|
||||||
|
<HistoryPage />
|
||||||
|
</SuspensePage>
|
||||||
|
),
|
||||||
|
},
|
||||||
{
|
{
|
||||||
path: "/watchlist",
|
path: "/watchlist",
|
||||||
element: (
|
element: (
|
||||||
|
|||||||
Reference in new issue
Block a user