"use client"; import { useState, useEffect } from "react"; import { Button } from "@/components/ui/button"; import { Textarea } from "@/components/ui/textarea"; import { MessageCircle, Share2, Trash2 } from "lucide-react"; import { useRouter, useParams } from "next/navigation"; import { getArticleDetail, createArticleComment, getArticleComments, deleteArticleComment, } from "@/service/content/content"; import { getCookiesDecrypt } from "@/lib/utils"; import Swal from "sweetalert2"; import withReactContent from "sweetalert2-react-content"; function getAvatarColor(name: string) { const colors = [ "#F87171", "#FB923C", "#FACC15", "#4ADE80", "#60A5FA", "#A78BFA", "#F472B6", ]; const index = name.charCodeAt(0) % colors.length; return colors[index]; } export default function DetailCommentImage() { const [isLoggedIn, setIsLoggedIn] = useState(false); const [currentUserId, setCurrentUserId] = useState(null); const [article, setArticle] = useState(null); const [comments, setComments] = useState([]); const [newComment, setNewComment] = useState(""); const [replyParentId, setReplyParentId] = useState(null); const [replyMessage, setReplyMessage] = useState(""); const router = useRouter(); const params = useParams(); const MySwal = withReactContent(Swal); const id = Number(params?.id); useEffect(() => { checkLoginStatus(); if (id) { fetchArticleDetail(id); fetchComments(id); } }, [id]); const checkLoginStatus = () => { const userId = getCookiesDecrypt("urie"); if (userId) { setIsLoggedIn(true); setCurrentUserId(Number(userId)); } else { setIsLoggedIn(false); setCurrentUserId(null); } }; const fetchArticleDetail = async (articleId: number) => { try { const res = await getArticleDetail(articleId); if (res?.data?.data) setArticle(res.data.data); } catch (error) { console.error("Gagal memuat artikel:", error); } }; const fetchComments = async (articleId: number) => { try { const res = await getArticleComments(articleId); if (res?.data?.data) { const all = res.data.data.map((c: any) => ({ ...c, parentId: c.parentId ?? 0, })); const structured = buildCommentTree(all); setComments(structured); } } catch (error) { console.error("Gagal memuat komentar:", error); } }; const buildCommentTree: any = (comments: any[], parentId = 0) => comments .filter((c) => c.parentId === parentId) .map((c) => ({ ...c, replies: buildCommentTree(comments, c.id), })); const handlePostComment = async () => { if (!newComment.trim()) { MySwal.fire("Oops!", "Komentar tidak boleh kosong.", "warning"); return; } await sendComment({ articleId: id, message: newComment, isPublic: true, parentId: 0, }); setNewComment(""); }; const handleReplySubmit = async (parentId: number) => { if (!replyMessage.trim()) { MySwal.fire("Oops!", "Balasan tidak boleh kosong.", "warning"); return; } await sendComment({ articleId: id, message: replyMessage, isPublic: true, parentId, }); setReplyMessage(""); setReplyParentId(null); }; const sendComment = async (payload: any) => { MySwal.fire({ title: "Mengirim komentar...", didOpen: () => MySwal.showLoading(), allowOutsideClick: false, showConfirmButton: false, }); try { const res = await createArticleComment(payload); if (res?.data?.success || !res?.error) { MySwal.fire({ icon: "success", title: "Komentar terkirim!", timer: 1000, showConfirmButton: false, }); fetchComments(id); } else { MySwal.fire( "Gagal", res.message || "Tidak dapat mengirim komentar.", "error" ); } } catch (error) { console.error(error); MySwal.fire( "Error", "Terjadi kesalahan saat mengirim komentar.", "error" ); } }; const handleDeleteComment = async (commentId: number) => { const confirm = await MySwal.fire({ title: "Hapus komentar ini?", text: "Tindakan ini tidak dapat dibatalkan!", icon: "warning", showCancelButton: true, confirmButtonColor: "#d33", cancelButtonColor: "#6b7280", confirmButtonText: "Ya, hapus", cancelButtonText: "Batal", }); if (!confirm.isConfirmed) return; MySwal.fire({ title: "Menghapus komentar...", didOpen: () => MySwal.showLoading(), allowOutsideClick: false, showConfirmButton: false, }); try { const res = await deleteArticleComment(commentId); if (res?.data?.success || !res?.error) { MySwal.fire({ icon: "success", title: "Komentar dihapus!", timer: 1000, showConfirmButton: false, }); fetchComments(id); } else { MySwal.fire( "Gagal", res.message || "Tidak dapat menghapus komentar.", "error" ); } } catch (error) { console.error("Gagal menghapus komentar:", error); MySwal.fire( "Error", "Terjadi kesalahan saat menghapus komentar.", "error" ); } }; return (

Comments on:

{article?.title || "Memuat judul..."}

{isLoggedIn ? ( <>