({ className, blockId }: CommentableProps)
| 54 | } |
| 55 | |
| 56 | function Root({ className, blockId }: CommentableProps) { |
| 57 | const pathname = usePathname() |
| 58 | const { postId, comments, currentBlockId } = useSnapshot(blogPostState) |
| 59 | const { user: me } = useUser() |
| 60 | const [isCommenting, setIsCommenting] = React.useState(false) |
| 61 | const currentComments = React.useMemo( |
| 62 | () => comments.filter((c) => c.body.blockId === blockId), |
| 63 | [comments, blockId] |
| 64 | ) |
| 65 | const top3Users = React.useMemo(() => { |
| 66 | if (currentComments.length === 0) { |
| 67 | return [] |
| 68 | } |
| 69 | |
| 70 | const users = new Set<PostIDLessCommentDto['userId']>() |
| 71 | const top3: PostIDLessCommentDto['userInfo'][] = [] |
| 72 | for (const comment of currentComments) { |
| 73 | if (users.has(comment.userId)) { |
| 74 | continue |
| 75 | } |
| 76 | |
| 77 | if (comment.userInfo.imageUrl) { |
| 78 | top3.push(comment.userInfo) |
| 79 | users.add(comment.userId) |
| 80 | } |
| 81 | |
| 82 | if (top3.length >= 3) { |
| 83 | break |
| 84 | } |
| 85 | } |
| 86 | |
| 87 | return top3 |
| 88 | }, [currentComments]) |
| 89 | |
| 90 | const formRef = React.useRef<HTMLFormElement>(null) |
| 91 | const commentsRef = React.useRef<HTMLUListElement>(null) |
| 92 | const scrollToComment = React.useCallback((id: string) => { |
| 93 | setTimeout(() => { |
| 94 | if (commentsRef.current) { |
| 95 | commentsRef.current |
| 96 | .querySelector(`[data-commentid="${id}"]`) |
| 97 | ?.scrollIntoView({ |
| 98 | behavior: 'smooth', |
| 99 | }) |
| 100 | } |
| 101 | }, 300) |
| 102 | }, []) |
| 103 | |
| 104 | const { mutate: createComment, isLoading } = useMutation( |
| 105 | ['comment', postId], |
| 106 | async (comment: string) => { |
| 107 | const res = await fetch(`/api/comments/${postId}`, { |
| 108 | method: 'POST', |
| 109 | headers: { |
| 110 | 'Content-Type': 'application/json', |
| 111 | }, |
| 112 | body: JSON.stringify({ |
| 113 | body: { |
nothing calls this directly
no test coverage detected