Files
sofa/lib/actions/watchlist.ts
T
jakeandClaude Opus 4.6 cd63d4f92a Add episode progress bar to title cards for in-progress TV shows
Shows a subtle progress bar at the bottom of title cards indicating
watched/total episodes, with a tooltip for exact counts. Uses a single
efficient SQL query with JOINs to batch-fetch progress for all visible
titles on the Explore page.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-03-05 10:22:05 -05:00

60 lines
1.8 KiB
TypeScript

"use server";
import { and, eq } from "drizzle-orm";
import { headers } from "next/headers";
import { auth } from "@/lib/auth/server";
import { db } from "@/lib/db/client";
import { userTitleStatus } from "@/lib/db/schema";
import { importTitle } from "@/lib/services/metadata";
import {
getEpisodeProgressByTmdbIds,
getUserStatusesByTmdbIds,
setTitleStatus,
} from "@/lib/services/tracking";
export async function fetchUserStatuses(
tmdbIds: { tmdbId: number; type: string }[],
): Promise<Record<string, "watchlist" | "in_progress" | "completed">> {
const session = await auth.api.getSession({ headers: await headers() });
if (!session) return {};
return getUserStatusesByTmdbIds(session.user.id, tmdbIds);
}
export async function fetchEpisodeProgress(
tmdbIds: { tmdbId: number; type: string }[],
): Promise<Record<string, { watched: number; total: number }>> {
const session = await auth.api.getSession({ headers: await headers() });
if (!session) return {};
return getEpisodeProgressByTmdbIds(session.user.id, tmdbIds);
}
export async function quickAddToWatchlist(
tmdbId: number,
type: "movie" | "tv",
) {
const session = await auth.api.getSession({ headers: await headers() });
if (!session) throw new Error("Unauthorized");
const userId = session.user.id;
const title = await importTitle(tmdbId, type);
if (!title) throw new Error("Failed to import title");
const existing = db
.select()
.from(userTitleStatus)
.where(
and(
eq(userTitleStatus.userId, userId),
eq(userTitleStatus.titleId, title.id),
),
)
.get();
if (existing) {
return { success: true, titleId: title.id, alreadyAdded: true };
}
setTitleStatus(userId, title.id, "watchlist");
return { success: true, titleId: title.id, alreadyAdded: false };
}