import React from "react"
import {
removeBookmarks,
setBookmarks,
} from "@/app/(providers)/(default)/projects/api"
import { useMutation, useQueryClient } from "@tanstack/react-query"
import { MdBookmarkBorder, MdOutlineBookmark } from "react-icons/md"
import type { Tables } from "@/types/supabase"
import { useRouter } from "next/navigation"
import { useCustomModal } from "@/hooks/useCustomModal"
interface Props {
projectId: string
currentUser: string
bookmarks: Tables<"bookmarks">[]
}
const BookmarkButton = ({ projectId, currentUser, bookmarks }: Props) => {
const queryClient = useQueryClient()
const router = useRouter()
const { openCustomModalHandler } = useCustomModal()
const { mutate: addMutate } = useMutation({
mutationFn: setBookmarks,
onSuccess: () => {
queryClient.invalidateQueries({ queryKey: ["bookmarks"] })
},
})
const handler = () => {
router.push("/signin")
}
const onClickHandler = async (e: React.MouseEvent<HTMLDivElement>) => {
e.stopPropagation()
if (!currentUser) {
openCustomModalHandler(
`로그인이 필요합니다.
로그인 페이지로 이동하시겠습니까?`,
"confirm",
handler,
)
}
if (isBookmarked) {
await removeBookmarks({ projectId, currentUser })
queryClient.invalidateQueries({ queryKey: ["bookmarks"] })
} else {
addMutate({ projectId, currentUser })
}
}
const isBookmarked =
bookmarks && bookmarks.some((bookmark) => bookmark.project_id === projectId)
return (
<div className="cursor-pointer z-10 text-gray-400" onClick={onClickHandler}>
{isBookmarked ? (
<MdOutlineBookmark size={35} />
) : (
<MdBookmarkBorder size={35} />
)}
</div>
)
}
export default BookmarkButton