mirror of
https://github.com/jakejarvis/sofa.git
synced 2026-08-29 05:05:38 -04:00
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>
This commit is contained in:
@@ -1,85 +0,0 @@
|
||||
"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);
|
||||
}
|
||||
}
|
||||
@@ -3,16 +3,6 @@
|
||||
import { useStore } from "jotai";
|
||||
import { useCallback } from "react";
|
||||
import { toast } from "sonner";
|
||||
import {
|
||||
episodeWatchesAtom,
|
||||
seasonsAtom,
|
||||
titleIdAtom,
|
||||
titleNameAtom,
|
||||
userRatingAtom,
|
||||
userStatusAtom,
|
||||
watchingEpAtom,
|
||||
} from "@/lib/atoms/title";
|
||||
import type { Season } from "@/lib/types/title";
|
||||
import {
|
||||
batchWatchEpisodes,
|
||||
markAllWatchedAction,
|
||||
@@ -23,7 +13,17 @@ import {
|
||||
watchEpisode,
|
||||
watchMovie,
|
||||
watchSeason,
|
||||
} from "./actions";
|
||||
} from "@/lib/actions/titles";
|
||||
import {
|
||||
episodeWatchesAtom,
|
||||
seasonsAtom,
|
||||
titleIdAtom,
|
||||
titleNameAtom,
|
||||
userRatingAtom,
|
||||
userStatusAtom,
|
||||
watchingEpAtom,
|
||||
} from "@/lib/atoms/title";
|
||||
import type { Season } from "@/lib/types/title";
|
||||
|
||||
export function useTitleActions() {
|
||||
const store = useStore();
|
||||
|
||||
Reference in New Issue
Block a user