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 { useFocusWithin } from '@mantine/hooks'; import { useCreateCommentMutation } from '@/features/comment/queries/comment'; interface CommentListProps { comments: IComment[]; } function CommentList({ comments }: CommentListProps) { const createCommentMutation = useCreateCommentMutation(); const [isLoading, setIsLoading] = useState(false); 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) => { 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 add reply:', error); } finally { setIsLoading(false); } //setCommentsAtom(prevComments => [...prevComments, createdComment]); }; return (
{renderChildComments(comment.id)}
); }; return (
{comments .filter(comment => comment.parentCommentId === null) .map(comment => renderComments(comment)) }
); } export default CommentList;