import React, { useEffect } from "react"; import { Box, Flex, HStack, Text, useColorModeValue, VStack, } from "@chakra-ui/react"; import { ReactionButton } from "../../utils/Buttons"; import { HeartIcon, RedHeart, CommentIcon, AuthorIcon, } from "../../assets/icons"; import { dateFormat, showEditedDate } from "../../helper/calcTimestamp"; import { htmlToJsx } from "../../helper/htmlToJsx"; import converter from "../../helper/converter"; import CustomAvatar from "../../utils/CustomAvatar"; import { useNavigate } from "react-router-dom"; import { useState } from "react"; import DiscussionBox from "../discussion/DiscussionBox"; import useClickLikeToComment from "../../hooks/useClickLikeToComment"; import ManageComment from "./ManageComment"; import { FiCornerLeftUp } from "react-icons/fi"; import { useDispatch } from "react-redux"; import { setLoginAlert } from "../../store/loginAlert"; const CommentItem = ({ text, createdAt, currentUserProfile, userId, postId, commentId, comments, likes, authorId, currentUserId, ps, footerPs, avatarSize, edited, editedAt, reply, repliedUserName, }) => { const navigate = useNavigate(); const dispatch = useDispatch(); const [showDiscussionBox, setShowDiscussionbox] = useState(false); const { handleClickLike, updatingLike } = useClickLikeToComment( currentUserId, postId ); const handleViewProfile = (username) => { navigate(`/${username}`); }; const totalLike = likes.length; const alreadyLiked = likes.includes(currentUserId); const handleshowDiscussionBox = () => { if (!currentUserId) { dispatch(setLoginAlert(true)); return; } setShowDiscussionbox((prev) => !prev); }; // auto focus when click reply // give commentId for each comment item and when showDiscussionBox is true , select editor inside this specific id and focus it useEffect(() => { if (showDiscussionBox) { document.querySelector(`#comment${commentId} .mde-text`).focus(); } }, [showDiscussionBox, commentId]); const reactionIconColor = useColorModeValue("#3d3d3d", "#d6d6d7"); const ghostColor = useColorModeValue("light.ghostColor", "dark.ghostColor"); const colorHover = useColorModeValue("light.colorHover", "dark.colorHover"); const colorTertiary = useColorModeValue( "light.colorTertiary", "dark.colorTertiary" ); const replyToColor = useColorModeValue("#8f8f8f", "dark.colorTertiary"); return ( handleViewProfile(currentUserProfile.username)} /> handleViewProfile(currentUserProfile.username)} > {currentUserProfile.name} {authorId === userId && } {/* show Date */} {" "} • {dateFormat(createdAt)}{" "} {edited && ( {showEditedDate(createdAt, editedAt) ? `• Edited on ${dateFormat(editedAt)}` : "• Edited"} )} {/* option menu */} {currentUserId === userId && ( )} {reply && repliedUserName !== currentUserProfile.name && ( {" "} reply to {repliedUserName} )} {htmlToJsx(converter().makeHtml(text))} {!showDiscussionBox && ( 1 ? "likes" : "like"} disabled={updatingLike} onClick={() => handleClickLike(comments, commentId)} > {alreadyLiked ? ( ) : ( )} )} {showDiscussionBox && ( )} ); }; export default CommentItem;