Files
sofa/lib/actions/titles.ts
T
jakeandClaude Opus 4.6 bcf9a3c751 Add quick-add watchlist button and consolidate server actions into lib/actions
Move all server actions from scattered page-level files into shared lib/actions/
directory (settings.ts, titles.ts, watchlist.ts) so they can be reused across
the app. Add a hover-triggered plus button on explore page title cards that lets
users add titles to their watchlist without navigating away.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-03-04 21:44:23 -05:00

86 lines
2.2 KiB
TypeScript

"use server";
import { eq } from "drizzle-orm";
import { headers } from "next/headers";
import { auth } from "@/lib/auth/server";
import { db } from "@/lib/db/client";
import { episodes } from "@/lib/db/schema";
import {
logEpisodeWatch,
logMovieWatch,
markAllEpisodesWatched,
rateTitleStars,
removeTitleStatus,
setTitleStatus,
unwatchEpisode,
unwatchSeason,
} from "@/lib/services/tracking";
async function getSessionUserId() {
const session = await auth.api.getSession({ headers: await headers() });
if (!session) throw new Error("Unauthorized");
return session.user.id;
}
export async function updateTitleStatus(
titleId: string,
status: "in_progress" | null,
) {
const userId = await getSessionUserId();
if (status === null) {
removeTitleStatus(userId, titleId);
} else {
setTitleStatus(userId, titleId, status);
}
}
export async function markAllWatchedAction(titleId: string) {
const userId = await getSessionUserId();
markAllEpisodesWatched(userId, titleId);
}
export async function updateTitleRating(titleId: string, ratingStars: number) {
const userId = await getSessionUserId();
if (ratingStars < 0 || ratingStars > 5) throw new Error("Invalid rating");
rateTitleStars(userId, titleId, ratingStars);
}
export async function watchMovie(titleId: string) {
const userId = await getSessionUserId();
logMovieWatch(userId, titleId);
}
export async function watchEpisode(episodeId: string) {
const userId = await getSessionUserId();
logEpisodeWatch(userId, episodeId);
}
export async function unwatchEpisodeAction(episodeId: string) {
const userId = await getSessionUserId();
unwatchEpisode(userId, episodeId);
}
export async function watchSeason(seasonId: string) {
const userId = await getSessionUserId();
const seasonEps = db
.select()
.from(episodes)
.where(eq(episodes.seasonId, seasonId))
.all();
for (const ep of seasonEps) {
logEpisodeWatch(userId, ep.id);
}
}
export async function unwatchSeasonAction(seasonId: string) {
const userId = await getSessionUserId();
unwatchSeason(userId, seasonId);
}
export async function batchWatchEpisodes(episodeIds: string[]) {
const userId = await getSessionUserId();
for (const id of episodeIds) {
logEpisodeWatch(userId, id);
}
}