import React, { useState } from 'react'; import LangTag from '../../utils/LangTag'; import { Box, HStack, Image, Text, VStack, Heading, Wrap, WrapItem, Flex, useColorModeValue, } from '@chakra-ui/react'; import { useNavigate } from 'react-router-dom'; import { ReactionButton, SecondaryBtn } from '../../utils/Buttons'; import CustomAvatar from '../../utils/CustomAvatar'; import { nanoid } from '@reduxjs/toolkit'; import ManangePost from './ManangePost'; import UserProfilePopup from '../profile/UserProfilePopup'; import DisplayDate from './DisplayDate'; import { useDispatch } from 'react-redux'; import { setClickComment } from '../../store/scrollDiscussion'; import { RiBookmarkFill, RiBookmarkLine } from 'react-icons/ri'; import useClickReactToPost from '../../hooks/useClickReactToPost'; import { titleRoute } from '../../helper/titleRoute'; import useClickTag from '../../hooks/useClickTag'; import useClickSameRoute from '../../hooks/useClickSameRoute'; import { CommentIcon, HeartIcon } from '../../assets/icons'; const PostItem = ({ name, username, profile, coverImg, createdAt, title, tags, id, readTime, isUpdated, fromDashboard, showHover, userId, currentUserId, currentUserProfile, totalDiscussion, totalReaction, saved, alreadySaved, isFirstItem, baseRadius, }) => { const [showProfilePopup, setShowProfilePopup] = useState(false); const navigate = useNavigate(); const dispatch = useDispatch(); const { clickReactHandler: clickSave, updatingReact: updatingSave } = useClickReactToPost(saved, id, 'saved'); const handleClickComment = (e) => { e.stopPropagation(); dispatch(setClickComment(true)); // if user click comment , it will start on where discussions exist 😉 navigate(`/${titleRoute(username, title, id)}`); }; const handleSameRoute = useClickSameRoute(); const handleClickTag = useClickTag(handleSameRoute); const handleNavigate = () => { dispatch(setClickComment(false)); navigate(`/${titleRoute(username, title, id)}`); }; const handleViewProfile = (e) => { e.stopPropagation(); navigate(`/${username}`); handleSameRoute(); }; const handleClickSave = (e) => { e.stopPropagation(); clickSave(); }; /// showPopup logic start let INTERVAl; const handleMouseEnter = () => { if (window.innerWidth < 768 || !showHover) return; INTERVAl = setTimeout(() => setShowProfilePopup(true), 300); }; const handleMouseLeave = () => { if (window.innerWidth < 768 || !showHover) return; setShowProfilePopup(false); clearTimeout(INTERVAl); }; /// showPopup logic end const reactionIconColor = useColorModeValue('#3d3d3d', '#d6d6d7'); const colorHover = useColorModeValue('light.colorHover', 'dark.colorHover'); const ghostColor = useColorModeValue('light.ghostColor', 'dark.ghostColor'); const headingHover = useColorModeValue( 'light.headingHover', 'dark.headingHover' ); const colorTertiary = useColorModeValue( 'light.colorTertiary', 'dark.colorTertiary' ); return ( {coverImg && isFirstItem && ( cover_img )} {/* avatar */} {/* name and date */} {name} {title} {tags.length !== 0 && ( {tags?.map((tag) => ( handleClickTag(e, tag.tagName)} > ))} )} {/* reaction buttons */} {totalReaction && ( 1 ? 'Reactions' : 'Reaction' } > )} 0 ? 'Comments' : 'Add comment' } > {readTime} min read {fromDashboard && ( )} {userId !== currentUserId && ( {alreadySaved ? ( ) : ( )} )} ); }; export default React.memo(PostItem);