diff --git a/components/saved-games/bookmark-button.tsx b/components/saved-games/bookmark-button.tsx new file mode 100644 index 0000000..3f9aebb --- /dev/null +++ b/components/saved-games/bookmark-button.tsx @@ -0,0 +1,107 @@ +"use client" + +import { useState, useEffect } from "react" +import { Bookmark, Loader2 } from "lucide-react" +import { motion, AnimatePresence } from "motion/react" +import { useSession } from "@/lib/auth-client" + +interface BookmarkButtonProps { + gameId: string + className?: string +} + +export function BookmarkButton({ gameId, className = "" }: BookmarkButtonProps) { + const { data: session } = useSession() + const [isSaved, setIsSaved] = useState(false) + const [isLoading, setIsLoading] = useState(true) + const [isToggling, setIsToggling] = useState(false) + + useEffect(() => { + if (!session) { + setIsLoading(false) + return + } + + async function checkSaved() { + try { + const res = await fetch(`/api/user/me/saved-games/check/${gameId}`) + if (res.ok) { + const data = await res.json() + setIsSaved(data.saved) + } + } catch (err) { + console.error("Failed to check saved status:", err) + } finally { + setIsLoading(false) + } + } + + checkSaved() + }, [session, gameId]) + + const toggleSave = async () => { + if (!session || isToggling) return + + setIsToggling(true) + try { + if (isSaved) { + const res = await fetch(`/api/user/me/saved-games/${gameId}`, { + method: "DELETE", + }) + if (res.ok) { + setIsSaved(false) + } + } else { + const res = await fetch("/api/user/me/saved-games", { + method: "POST", + headers: { "Content-Type": "application/json" }, + body: JSON.stringify({ gameId }), + }) + if (res.ok) { + setIsSaved(true) + } + } + } catch (err) { + console.error("Failed to toggle saved game:", err) + } finally { + setIsToggling(false) + } + } + + if (!session || isLoading) { + return null + } + + return ( + + + {isToggling ? ( + + ) : ( + + + + )} + + + {isSaved ? "Saved" : "Save"} + + + ) +}