import { Divider, Paper, ScrollArea } from '@mantine/core'; import CommentEditor from '@/features/comment/components/comment-editor'; import CommentActions from '@/features/comment/components/comment-actions'; import React, { useState } from 'react'; import CommentListItem from '@/features/comment/components/comment-list-item'; import { IComment } from '@/features/comment/types/comment.types'; import useComment from '@/features/comment/hooks/use-comment'; import { useAtom } from 'jotai'; import { commentsAtom } from '@/features/comment/atoms/comment-atom'; import { useParams } from 'react-router-dom'; import { useFocusWithin } from '@mantine/hooks'; function CommentList({ comments }: IComment[]) { const { createCommentMutation } = useComment(); const { isLoading } = createCommentMutation; const { pageId } = useParams(); const [, setCommentsAtom] = useAtom(commentsAtom(pageId)); const getChildComments = (parentId) => { return comments.filter(comment => comment.parentCommentId === parentId); }; const renderChildComments = (parentId) => { const children = getChildComments(parentId); return (
{children.map(childComment => (
{renderChildComments(childComment.id)}
))}
); }; const CommentEditorWithActions = ({ commentId, onSave, isLoading }) => { const [content, setContent] = useState(''); const { ref, focused } = useFocusWithin(); const handleSave = () => { onSave(commentId, content); setContent(''); }; return (
{focused && }
); }; const renderComments = (comment) => { const handleAddReply = async (commentId, content) => { const commentData = { pageId: comment.pageId, parentCommentId: comment.id, content: JSON.stringify(content), }; const createdComment = await createCommentMutation.mutateAsync(commentData); setCommentsAtom(prevComments => [...prevComments, createdComment]); }; return (
{renderChildComments(comment.id)}
); }; return (
{comments .filter(comment => comment.parentCommentId === null) .map(comment => renderComments(comment)) }
); } export default CommentList;