|
| 1 | +import React, { useCallback } from "react"; |
| 2 | +import * as S from "./styles"; |
| 3 | +import ViewMarkdown from "@components/ViewMarkdown"; |
| 4 | +import Heart from "@components/Icons/Heart"; |
| 5 | +import NewComment from "@components/CommentCreate"; |
| 6 | + |
| 7 | +export interface ICommentData { |
| 8 | + id: number; |
| 9 | + user: { |
| 10 | + name: string; |
| 11 | + avatar: string; |
| 12 | + }; |
| 13 | + comment: string; |
| 14 | + likes: number; |
| 15 | + reply?: ICommentData[]; |
| 16 | +} |
| 17 | + |
| 18 | +export interface IComment { |
| 19 | + listComment: ICommentData; |
| 20 | + children?: React.ReactNode; |
| 21 | + currentUser: string; |
| 22 | + isReplying?: boolean; |
| 23 | + handleReply?: (id: number) => void; |
| 24 | + replyComment?: { |
| 25 | + id: number; |
| 26 | + isReplying: boolean; |
| 27 | + }; |
| 28 | +} |
| 29 | + |
| 30 | +const Comment: React.FC<IComment> = ({ |
| 31 | + currentUser = "", |
| 32 | + children, |
| 33 | + listComment, |
| 34 | + isReplying = false, |
| 35 | + handleReply = () => {}, |
| 36 | + replyComment, |
| 37 | +}) => { |
| 38 | + const userMention = useCallback( |
| 39 | + (content: string) => { |
| 40 | + const newContent = content.replace( |
| 41 | + `@${currentUser}`, |
| 42 | + `<span className="mention">@${currentUser}</span>` |
| 43 | + ); |
| 44 | + |
| 45 | + const reply = newContent.replace(/@(\w+)/g, (match, username) => { |
| 46 | + if (username === currentUser) return match; |
| 47 | + return `<span className="reply">@${username}</span>`; |
| 48 | + }); |
| 49 | + |
| 50 | + return reply; |
| 51 | + |
| 52 | + /* return newContent; */ |
| 53 | + }, |
| 54 | + [currentUser] |
| 55 | + ); |
| 56 | + |
| 57 | + return ( |
| 58 | + <> |
| 59 | + <S.CommentContainer |
| 60 | + key={listComment.id} |
| 61 | + className={isReplying ? "reply" : ""} |
| 62 | + > |
| 63 | + <S.Avatar |
| 64 | + src={listComment.user.avatar} |
| 65 | + alt={`${listComment.user.avatar} avatar`} |
| 66 | + className={isReplying ? "reply" : ""} |
| 67 | + /> |
| 68 | + |
| 69 | + <S.CommentInfos> |
| 70 | + <S.CommentTitle> |
| 71 | + <S.UserName>{listComment.user.name}</S.UserName> |
| 72 | + |
| 73 | + <S.Separator /> |
| 74 | + <S.Date>22/01/2024</S.Date> |
| 75 | + </S.CommentTitle> |
| 76 | + |
| 77 | + <S.Comment> |
| 78 | + <ViewMarkdown content={userMention(listComment.comment)} /> |
| 79 | + </S.Comment> |
| 80 | + |
| 81 | + <S.ExtraInfo> |
| 82 | + <S.LikeButton> |
| 83 | + <Heart /> |
| 84 | + <span>{listComment.likes}</span> |
| 85 | + </S.LikeButton> |
| 86 | + |
| 87 | + <S.ReplyButton onClick={() => handleReply(listComment.id)}> |
| 88 | + {replyComment?.isReplying && replyComment.id === listComment.id |
| 89 | + ? "Cancelar resposta" |
| 90 | + : "Responder"} |
| 91 | + </S.ReplyButton> |
| 92 | + </S.ExtraInfo> |
| 93 | + |
| 94 | + {listComment.reply && listComment.reply.length > 0 && ( |
| 95 | + <S.ReplyCount>{listComment.reply?.length} respostas</S.ReplyCount> |
| 96 | + )} |
| 97 | + |
| 98 | + {replyComment?.isReplying && replyComment.id === listComment.id && ( |
| 99 | + <NewComment |
| 100 | + isReplying={true} |
| 101 | + replyId={listComment.id} |
| 102 | + replyUser={listComment.user.name} |
| 103 | + isReplyOverReply={isReplying} |
| 104 | + /> |
| 105 | + )} |
| 106 | + <S.ReplyContainer>{children}</S.ReplyContainer> |
| 107 | + </S.CommentInfos> |
| 108 | + </S.CommentContainer> |
| 109 | + </> |
| 110 | + ); |
| 111 | +}; |
| 112 | + |
| 113 | +export default Comment; |
0 commit comments