import React, { useState, useRef } from "react"; import { useParams } from "react-router-dom"; import { Divider, Paper } from "@mantine/core"; import CommentListItem from "@/features/comment/components/comment-list-item"; import { useCommentsQuery, useCreateCommentMutation, } from "@/features/comment/queries/comment-query"; import CommentEditor from "@/features/comment/components/comment-editor"; import CommentActions from "@/features/comment/components/comment-actions"; import { useFocusWithin } from "@mantine/hooks"; import { IComment } from "@/features/comment/types/comment.types.ts"; function CommentList() { const { pageId } = useParams(); const { data: comments, isLoading: isCommentsLoading, isError, } = useCommentsQuery(pageId); const [isLoading, setIsLoading] = useState(false); const createCommentMutation = useCreateCommentMutation(); if (isCommentsLoading) { return <>; } if (isError) { return
Error loading comments.
; } if (!comments || comments.items.length === 0) { return <>No comments yet.; } const renderComments = (comment: IComment) => { const handleAddReply = async (commentId: string, content: string) => { try { setIsLoading(true); const commentData = { pageId: comment.pageId, parentCommentId: comment.id, content: JSON.stringify(content), }; await createCommentMutation.mutateAsync(commentData); } catch (error) { console.error("Failed to post comment:", error); } finally { setIsLoading(false); } }; return (
); }; return ( <> {comments.items .filter((comment) => comment.parentCommentId === null) .map(renderComments)} ); } const ChildComments = ({ comments, parentId }) => { const getChildComments = (parentId: string) => { return comments.items.filter( (comment: IComment) => comment.parentCommentId === parentId, ); }; return (
{getChildComments(parentId).map((childComment) => (
))}
); }; const CommentEditorWithActions = ({ commentId, onSave, isLoading }) => { const [content, setContent] = useState(""); const { ref, focused } = useFocusWithin(); const commentEditorRef = useRef(null); const handleSave = () => { onSave(commentId, content); setContent(""); commentEditorRef.current?.clearContent(); }; return (
{focused && }
); }; export default CommentList;