import { useEffect, useMemo, useState } from 'react'; import { Trans, useTranslation } from 'react-i18next'; import { Link, useLocation, useParams } from 'react-router-dom'; import { Comment, useAuthorAvatar, useComment, useEditedComment, useSubplebbit } from '@plebbit/plebbit-react-hooks'; import Plebbit from '@plebbit/plebbit-js/dist/browser/index.js'; import styles from '../../views/post/post.module.css'; import { fetchWebpageThumbnailIfNeeded, 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 useAvatarVisibilityStore from '../../stores/use-avatar-visibility-store'; 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, reason, 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 { hideAvatars } = useAvatarVisibilityStore(); const params = useParams(); const location = useLocation(); const isInAllView = isAllView(location.pathname, params); const isInPostPageView = isPostPageView(location.pathname, params); const isInSubscriptionsView = isSubscriptionsView(location.pathname, params); // some sites have CORS access, so the thumbnail can be fetched client-side, which is helpful if subplebbit.settings.fetchThumbnailUrls is false const initialCommentMediaInfo = useMemo(() => getCommentMediaInfo(post), [post]); const [commentMediaInfo, setCommentMediaInfo] = useState(initialCommentMediaInfo); useEffect(() => { if (initialCommentMediaInfo?.type === 'webpage' && !initialCommentMediaInfo.thumbnail) { fetchWebpageThumbnailIfNeeded(initialCommentMediaInfo).then(setCommentMediaInfo); } else { setCommentMediaInfo(initialCommentMediaInfo); } }, [initialCommentMediaInfo]); const hasThumbnail = getHasThumbnail(commentMediaInfo, link); const [showThumbnail, setShowThumbnail] = useState(true); const isReply = parentCid; const stateString = useStateString(post); const handleUserAddressClick = useAuthorAddressClick(); const numberOfPostsByAuthor = document.querySelectorAll(`[data-author-address="${shortAddress}"][data-post-cid="${postCid}"]`).length; const userID = address && Plebbit.getShortAddress(address); const userIDBackgroundColor = hashStringToColor(userID); const userIDTextColor = getTextColorForBackground(userIDBackgroundColor); const { hidden } = useHide(post); return ( <>
{removed ? ( _.capitalize(t('removed')) ) : deleted ? ( _.capitalize(t('deleted')) ) : displayName ? ( displayName.length <= 20 ? ( displayName ) : ( ) ) : ( _.capitalize(t('anonymous')) )} {!(deleted || removed) && {authorRole && ` ## Board ${authorRole}`} } {!(isDescription || isRules) && ( <> {author?.avatar && !(deleted || removed) && !hideAvatars ? ( ) : ( ' ' )} (ID: {''} {removed ? ( _.lowerCase(t('removed')) ) : deleted ? ( _.lowerCase(t('deleted')) ) : ( handleUserAddressClick(userID, postCid)} style={{ backgroundColor: userIDBackgroundColor, color: userIDTextColor }} > {userID} } content={`${numberOfPostsByAuthor === 1 ? t('1_post_by_this_id') : t('x_posts_by_this_id', { number: numberOfPostsByAuthor })}`} showTooltip={isInPostPageView || postReplyCount < 6} /> )} ){' '} )} {pinned && ( )} {locked && ( )} {title && ( {title.length <= 30 ? ( {title} ) : ( {title.slice(0, 30) + '(...)'}} content={title.length < 1000 ? title : title.slice(0, 1000) + `... ${t('title_too_long')}`} /> )} )} {subplebbitAddress && (isInAllView || isInSubscriptionsView) && !isReply && (
{' '} p/{subplebbitAddress && Plebbit.getShortAddress(subplebbitAddress)}
)} {getFormattedDate(timestamp)}
} content={getFormattedTimeAgo(timestamp)} />{' '} {!(isDescription || isRules) && (cid ? ( !cid && e.preventDefault()}> c/ openReplyModal && openReplyModal(cid, postCid, subplebbitAddress)}> {shortCid.slice(0, -4)} ) : ( <> c/ {state === 'failed' || stateString === 'Failed' ? _.capitalize(t('failed')) : state === 'pending' ? _.capitalize(t('pending')) : ''} ))}
{(hasThumbnail || link) && !(deleted || removed) && ( )} ); }; const ReplyBacklinks = ({ post }: PostProps) => { const { cid, parentCid } = post || {}; const replies = useReplies(post); return ( cid && parentCid && replies.length > 0 && (
{replies.map( (reply: Comment, index: number) => reply?.parentCid === cid && reply?.cid && , )}
) ); }; const PostMessageMobile = ({ post }: PostProps) => { const { t } = useTranslation(); const { cid, content, deleted, edit, isRules, original, parentCid, postCid, reason, removed, state } = post || {}; // TODO: commentAuthor is not available outside of editedComment, update when available // const banned = !!post?.commentAuthor?.banExpiresAt; const [showOriginal, setShowOriginal] = useState(false); const params = useParams(); const location = useLocation(); const isInPostView = isPostPageView(location.pathname, params); const [showFullComment, setShowFullComment] = useState(false); const displayContent = content && !isInPostView && content.length > 1000 && !showFullComment ? content.slice(0, 1000) : content; const quotelinkReply = useComment({ commentCid: parentCid }); const isReply = parentCid; const isReplyingToReply = (postCid && postCid !== parentCid) || quotelinkReply?.postCid !== parentCid; const stateString = useStateString(post); const loadingString = (
{stateString !== 'Failed' ? : stateString}
); return (
{isReply && !(removed || deleted) && state !== 'failed' && isReplyingToReply && } {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 ? ( setShowOriginal(!showOriginal)} /> }} /> ) : ( setShowOriginal(!showOriginal)} /> }} /> )}
)} )} {/* TODO: commentAuthor is not available outside of editedComment, update when available */} {/* {banned && (

)} */} {content?.length > 1000 && !isInPostView && !showFullComment && (
setShowFullComment(true)} /> }} />
)} {!cid && state === 'pending' && stateString !== 'Failed' && ( <>
{loadingString} )}
); }; 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, deleted, postCid, reason, removed, subplebbitAddress } = post || {}; const isRouteLinkToReply = useLocation().pathname.startsWith(`/p/${subplebbitAddress}/c/${cid}`); const { hidden } = useHide({ cid }); return (
{!hidden && (!(removed || deleted) || ((removed || deleted) && reason)) && }
); }; const PostMobile = ({ openReplyModal, post, roles, showAllReplies, showReplies = true }: PostProps) => { const { t } = useTranslation(); const { author, cid, pinned, postCid, replyCount, state, subplebbitAddress } = post || {}; const { isDescription, isRules } = post || {}; // custom properties, not from api const params = useParams(); const location = useLocation(); const isInAllView = isAllView(location.pathname, params); const isInPendingPostView = isPendingPostView(location.pathname, params); const isInPostView = isPostPageView(location.pathname, params); const linksCount = useCountLinksInReplies(post); const replies = useReplies(post); const isInPostPageView = isPostPageView(location.pathname, params); const { hidden, unhide } = useHide({ cid }); const stateString = useStateString(post); const subplebbit = useSubplebbit({ subplebbitAddress }); const showRules = isDescription && subplebbit?.rules && subplebbit?.rules.length > 0; const subplebbitRulesReply = { isRules: true, subplebbitAddress, timestamp: subplebbit?.createdAt, author: { displayName: `## ${t('board_mods')}` }, content: `${subplebbit?.rules?.map((rule: string, index: number) => `${index + 1}. ${rule}`).join('\n')}`, replyCount: 0, }; return ( <> {hidden && !isInPostPageView ? ( <>
Show Hidden Thread ) : (
{showReplies && (

)}
{!isInPostView && !isInPendingPostView && showReplies && (
{replyCount > 0 && `${replyCount} Replies`} {linksCount > 0 && ` / ${linksCount} Links`} {t('view_thread')}
)}
{!(pinned && !isInPostView) && !isInPendingPostView && replies && showReplies && (showAllReplies ? replies : replies.slice(-5)).map((reply, index) => (
))} {showRules && (
)}
{!isInPendingPostView && (stateString && stateString !== 'Failed' && state !== 'succeeded' ? (
) : ( state === 'failed' && {t('failed')} ))}
)} ); }; export default PostMobile;