import { useEffect, useRef, useState } from 'react'; import { Trans, useTranslation } from 'react-i18next'; import { Link, useLocation, useParams } from 'react-router-dom'; import { Comment, useAccount, useAuthorAvatar, useComment, useEditedComment } from '@plebbit/plebbit-react-hooks'; import Plebbit from '@plebbit/plebbit-js/dist/browser/index.js'; import styles from '../../views/post/post.module.css'; import { getCommentMediaInfo, getHasThumbnail } from '../../lib/utils/media-utils'; import { getTextColorForBackground, hashStringToColor } from '../../lib/utils/post-utils'; import { getFormattedDate, getFormattedTimeAgo } from '../../lib/utils/time-utils'; import { isAllView, isPendingPostView, isPostPageView, isSubscriptionsView } from '../../lib/utils/view-utils'; import useAnonModeStore from '../../stores/use-anon-mode-store'; import useAnonMode from '../../hooks/use-anon-mode'; import useAuthorAddressClick from '../../hooks/use-author-address-click'; import useCountLinksInReplies from '../../hooks/use-count-links-in-replies'; import useHide from '../../hooks/use-hide'; import useReplies from '../../hooks/use-replies'; import useStateString from '../../hooks/use-state-string'; import CommentMedia from '../comment-media'; import LoadingEllipsis from '../loading-ellipsis'; import Markdown from '../markdown'; import PostMenuMobile from './post-menu-mobile'; import ReplyQuotePreview from '../reply-quote-preview'; import Tooltip from '../tooltip'; import { PostProps } from '../../views/post/post'; import _ from 'lodash'; const PostInfoAndMedia = ({ openReplyModal, post, postReplyCount = 0, roles }: PostProps) => { const { t } = useTranslation(); const { author, cid, deleted, link, locked, parentCid, pinned, postCid, removed, shortCid, state, subplebbitAddress, timestamp } = post || {}; const title = post?.title?.trim(); const { isDescription, isRules } = post || {}; // custom properties, not from api const { address, shortAddress } = author || {}; const displayName = author?.displayName?.trim(); const authorRole = roles?.[address]?.role; const { imageUrl: avatarImageUrl } = useAuthorAvatar({ author }); const params = useParams(); const location = useLocation(); const isInAllView = isAllView(location.pathname, params); const isInPostPageView = isPostPageView(location.pathname, params); const isInSubscriptionsView = isSubscriptionsView(location.pathname, params); const commentMediaInfo = getCommentMediaInfo(post); const hasThumbnail = getHasThumbnail(commentMediaInfo, link); const [showThumbnail, setShowThumbnail] = useState(true); const isReply = parentCid; // comment.author.shortAddress is undefined while the comment publishing state is pending, use account instead // in anon mode, use the newly generated signer.address instead, which will be comment.author.address const { anonMode } = useAnonMode(); const { currentAnonSignerAddress } = useAnonModeStore(); const account = useAccount(); const pendingShortAddress = anonMode ? currentAnonSignerAddress && Plebbit.getShortAddress(currentAnonSignerAddress) : account?.author?.shortAddress; const stateString = useStateString(post); const handleUserAddressClick = useAuthorAddressClick(); const numberOfPostsByAuthor = document.querySelectorAll(`[data-author-address="${shortAddress}"][data-post-cid="${postCid}"]`).length; const userIDBackgroundColor = hashStringToColor(shortAddress || pendingShortAddress); const userIDTextColor = getTextColorForBackground(userIDBackgroundColor); return ( <>
{isReply && !(removed || deleted) && state !== 'failed' && isReplyingToReply &&); }; const Reply = ({ openReplyModal, postReplyCount, reply, roles }: PostProps) => { let post = reply; // handle pending mod or author edit const { editedComment } = useEditedComment({ comment: reply }); if (editedComment) { post = editedComment; } const { author, cid, postCid, subplebbitAddress } = post || {}; const isRouteLinkToReply = useLocation().pathname.startsWith(`/p/${subplebbitAddress}/c/${cid}`); const { hidden } = useHide({ cid }); return (} {removed ? ( reason ? ( <> ({t('this_post_was_removed')})
{`${_.capitalize(t('reason'))}: "${reason}"`}. > ) : ( {_.capitalize(t('this_post_was_removed'))}. ) ) : deleted ? ( reason ? ( <> {t('user_deleted_this_post')}{' '} {`${_.capitalize(t('reason'))}: "${reason}"`}. > ) : ( {t('user_deleted_this_post')} ) ) : ( <> {!showOriginal &&} {edit && original?.content !== post?.content && ( {showOriginal && }
>} /> }} />{' '} {reason && <>{t('reason_reason', { reason: reason, interpolation: { escapeValue: false } })} >} {showOriginal ? ( )} > )} {/* TODO: commentAuthor is not available outside of editedComment, update when available */} {/* {banned && (setShowOriginal(!showOriginal)} /> }} /> ) : ( setShowOriginal(!showOriginal)} /> }} /> )}
)} */} {content?.length > 1000 && !isInPostView && !showFullComment && (
setShowFullComment(true)} /> }} /> )} {!cid && state === 'pending' && stateString !== 'Failed' && ( <>
{loadingString} > )}