import { Group, Text, Box } from '@mantine/core'; import React, { useState } from 'react'; import classes from './comment.module.css'; import { useAtomValue } from 'jotai'; import { timeAgo } from '@/lib/time'; import CommentEditor from '@/features/comment/components/comment-editor'; import { pageEditorAtom } from '@/features/editor/atoms/editor-atoms'; import CommentActions from '@/features/comment/components/comment-actions'; import CommentMenu from '@/features/comment/components/comment-menu'; import { useHover } from '@mantine/hooks'; import { useDeleteCommentMutation, useUpdateCommentMutation } from '@/features/comment/queries/comment-query'; import { IComment } from '@/features/comment/types/comment.types'; import { UserAvatar } from '@/components/ui/user-avatar'; interface CommentListItemProps { comment: IComment; } function CommentListItem({ comment }: CommentListItemProps) { const { hovered, ref } = useHover(); const [isEditing, setIsEditing] = useState(false); const [isLoading, setIsLoading] = useState(false); const editor = useAtomValue(pageEditorAtom); const [content, setContent] = useState(comment.content); const updateCommentMutation = useUpdateCommentMutation(); const deleteCommentMutation = useDeleteCommentMutation(comment.pageId); async function handleUpdateComment() { try { setIsLoading(true); const commentToUpdate = { id: comment.id, content: JSON.stringify(content), }; await updateCommentMutation.mutateAsync(commentToUpdate); setIsEditing(false); } catch (error) { console.error('Failed to update comment:', error); } finally { setIsLoading(false); } } async function handleDeleteComment() { try { await deleteCommentMutation.mutateAsync(comment.id); editor?.commands.unsetComment(comment.id); } catch (error) { console.error('Failed to delete comment:', error); } } function handleEditToggle() { setIsEditing(true); } return (
{comment.creator.name}
{/*!comment.parentCommentId && ( )*/}
{timeAgo(comment.createdAt)}
{!comment.parentCommentId && comment?.selection && {comment?.selection} } { !isEditing ? () : (<> setContent(newContent)} autofocus={true} /> ) }
); } export default CommentListItem;