import { unixToDate, sinceUnixDate } from "#/api/utils"; import { useEffect, useState } from "react"; import { ENDPOINTS } from "#/api/config"; import { Button, Dropdown, DropdownItem } from "flowbite-react"; import Link from "next/link"; import { CommentsAddModal } from "./Comments.Add"; import { CommentsEditModal } from "./Comments.Edit"; import { useUserStore } from "#/store/auth"; import Image from "next/image"; export const CommentsComment = (props: { release_id: number; profile: { login: string; avatar: string; id: number }; comment: { id: number; timestamp: number; message: string; reply_count: number; likes_count: number; vote: number; isSpoiler: boolean; isEdited: boolean; isDeleted: boolean; }; isSubComment?: boolean; token: string | null; isReplying?: boolean; parentComment?: any; setShouldRender?: (shouldRender: boolean) => void; setCommentSend?: (commentSend: boolean) => void; type?: "release" | "collection"; }) => { const [replies, setReplies] = useState([]); const [likes, setLikes] = useState(props.comment.likes_count); const [vote, setVote] = useState(props.comment.vote); const [isAddCommentsOpen, setIsAddCommentsOpen] = useState(false); const [isEditCommentsOpen, setIsEditCommentsOpen] = useState(false); const [isHidden, setIsHidden] = useState( !props.isReplying && (props.comment.isSpoiler || props.comment.likes_count < -5) ); const user: any = useUserStore((state) => state.user); const [shouldRender, setShouldRender] = useState(true); const [commentSend, setCommentSend] = useState(false); let parentCommentId: number | null = null; if (props.parentComment) { parentCommentId = props.parentComment.id; } async function _deleteComment() { if (props.token) { let url; if (props.type == "collection") { url = `${ENDPOINTS.collection.base}/comment/delete/${props.comment.id}?token=${props.token}`; } else { url = `${ENDPOINTS.release.info}/comment/delete/${props.comment.id}?token=${props.token}`; } await fetch(url); if (props.setShouldRender && props.setCommentSend) { props.setShouldRender(true); props.setCommentSend(true); } } } useEffect(() => { async function _fetchReplies() { setReplies([]); let url; if (props.type == "collection") { url = `${ENDPOINTS.collection.base}/comment/replies/${ parentCommentId || props.comment.id }/0?sort=2`; } else { url = `${ENDPOINTS.release.info}/comment/replies/${ parentCommentId || props.comment.id }/0?sort=2`; } if (props.token) { url += `&token=${props.token}`; } await fetch(url) .then((res) => res.json()) .then((data) => { setReplies(data.content); }); } if ( !props.isSubComment && !props.isReplying && shouldRender && (commentSend || props.comment.reply_count > 0) ) { _fetchReplies(); setShouldRender(false); setCommentSend(false); } // eslint-disable-next-line react-hooks/exhaustive-deps }, [commentSend]); async function _sendVote(action: number) { if (props.token) { let url; if (props.type == "collection") { url = `${ENDPOINTS.collection.base}/comment/vote/${props.comment.id}/${action}?token=${props.token}`; } else { url = `${ENDPOINTS.release.info}/comment/vote/${props.comment.id}/${action}?token=${props.token}`; } fetch(url); } } // TODO: make it readable function _updateVote(action: string) { if (action === "like" && vote == 0) { setVote(2); setLikes(likes + 1); _sendVote(2); } else if (action === "dislike" && vote == 0) { setVote(1); setLikes(likes - 1); _sendVote(1); } else if (action === "like" && vote == 1) { setVote(2); setLikes(likes + 2); _sendVote(2); } else if (action === "dislike" && vote == 2) { setVote(1); setLikes(likes - 2); _sendVote(1); } else { _sendVote(vote); setVote(0); if (action === "dislike" && vote == 1) { setLikes(likes + 1); } else if (action === "like" && vote == 2) { setLikes(likes - 1); } } } return ( <>

{!props.comment.isDeleted ? props.comment.message : "Комментарий был удалён."}

{isHidden && ( )}
{!props.isReplying && !props.comment.isDeleted && (
{props.token ? ( ) : ( )}

0 ? "text-green-500 dark:text-green-400" : likes < 0 ? "text-red-500 dark:text-red-400" : "text-gray-500 dark:text-gray-400" }`} > {likes}

)} {replies.length > 0 && replies.map((comment: any) => ( ))}
{props.token && ( )} ); };