import { useState } from 'react'; import { Trans, useTranslation } from 'react-i18next'; import { useLocation, useParams } from 'react-router-dom'; import { Link } from 'react-router-dom'; import { useAccount } from '@plebbit/plebbit-react-hooks'; import Plebbit from '@plebbit/plebbit-js/dist/browser/index.js'; import styles from '../post.module.css'; import { getCommentMediaInfo, getDisplayMediaInfoType, getHasThumbnail } from '../../../lib/utils/media-utils'; import { getFormattedDate } from '../../../lib/utils/time-utils'; import { isValidURL } from '../../../lib/utils/url-utils'; import { isAllView, isPendingPostView, isPostPageView, isSubscriptionsView } from '../../../lib/utils/view-utils'; import useCountLinksInReplies from '../../../hooks/use-count-links-in-replies'; import useReplies from '../../../hooks/use-replies'; import useStateString from '../../../hooks/use-state-string'; import CommentMedia from '../../comment-media'; import { canEmbed } from '../../embed'; import LoadingEllipsis from '../../loading-ellipsis'; import Markdown from '../../markdown'; import PostMenuDesktop from './post-menu-desktop/'; import { PostProps } from '../post'; import _ from 'lodash'; const PostInfo = ({ openReplyModal, post, roles }: PostProps) => { const { t } = useTranslation(); const { author, cid, locked, pinned, parentCid, postCid, shortCid, state, subplebbitAddress, timestamp, title } = post || {}; const { address, displayName, shortAddress } = author || {}; const { isDescription, isRules } = post || {}; // custom properties, not from api const stateString = useStateString(post); const isReply = parentCid; const params = useParams(); const location = useLocation(); const isInAllView = isAllView(location.pathname); const isInPostView = isPostPageView(location.pathname, params); const isInSubscriptionsView = isSubscriptionsView(location.pathname); const shortDisplayName = displayName?.trim().length > 20 ? displayName?.trim().slice(0, 20).trim() + '...' : displayName?.trim(); const authorRole = roles?.[address]?.role; const displayTitle = title && title.length > 75 ? title?.slice(0, 75) + '...' : title; // pending reply by account is not yet published const account = useAccount(); const accountShortAddress = account?.author?.shortAddress; return (
{title && {displayTitle} } {shortDisplayName || _.capitalize(t('anonymous'))} {authorRole && ` ## Board ${authorRole}`}{' '} {!(isDescription || isRules) && (u/{shortAddress || accountShortAddress}) } {getFormattedDate(timestamp)} {isDescription || isRules ? '' : ' '} {subplebbitAddress && (isInAllView || isInSubscriptionsView) && !isReply && ( {' '} p/{subplebbitAddress && Plebbit.getShortAddress(subplebbitAddress)}{' '} )} {!(isDescription || isRules) && ( !cid && e.preventDefault()}> c/ {!cid ? ( {state === 'failed' || stateString === 'Failed' ? 'Failed' : 'Pending'} ) : ( openReplyModal && openReplyModal(cid)}> {shortCid} )} )} {pinned && ( )} {locked && ( )} {!isInPostView && !isReply && ( [ {_.capitalize(t('reply'))} ] )}
); }; const PostMedia = ({ post }: PostProps) => { const { t } = useTranslation(); const { link, linkHeight, linkWidth, parentCid } = post || {}; const { isDescription, isRules } = post || {}; // custom properties, not from api const commentMediaInfo = getCommentMediaInfo(post); const { type, url } = commentMediaInfo || {}; const embedUrl = url && new URL(url); const hasThumbnail = getHasThumbnail(commentMediaInfo, link); const [showThumbnail, setShowThumbnail] = useState(true); const isReply = parentCid; return (
{t('link')}:{' '} {url && url.length > 30 ? url.slice(0, 30) + '...' : url} {' '} ({type && _.lowerCase(getDisplayMediaInfoType(type, t))}) {!showThumbnail && (type === 'iframe' || type === 'video' || type === 'audio') && ( {' '} [ setShowThumbnail(true)}> {t('close')} ] )} {showThumbnail && !hasThumbnail && embedUrl && canEmbed(embedUrl) && ( {' '} [ setShowThumbnail(false)}> {t('open')} ] )}
{(hasThumbnail || (!hasThumbnail && !showThumbnail)) && ( )}
); }; const PostMessage = ({ post }: PostProps) => { const { cid, content, parentCid, postCid, state, subplebbitAddress } = post || {}; const params = useParams(); const location = useLocation(); const isInPostView = isPostPageView(location.pathname, params); const displayContent = content && !isInPostView && content.length > 1000 ? content?.slice(0, 1000) + '(...)' : content; const isReply = parentCid; const isReplyingToReply = postCid !== parentCid; const stateString = useStateString(post); const loadingString = stateString && (
{stateString !== 'Failed' ? : stateString}
); return (
{isReply && isReplyingToReply && ( <> {`c/${parentCid && Plebbit.getShortCid(parentCid)}`}
)} {!isReply && content.length > 1000 && !isInPostView && (
}} />
)} {!cid && state === 'pending' && stateString !== 'Failed' && ( <>
{loadingString} )}
); }; const PostDesktop = ({ openReplyModal, post, roles, showAllReplies }: PostProps) => { const { cid, content, link, pinned, replyCount, subplebbitAddress } = post || {}; const { isDescription, isRules } = post || {}; // custom properties, not from api const params = useParams(); const location = useLocation(); const isInPendingPostView = isPendingPostView(location.pathname, params); const isInPostView = isPostPageView(location.pathname, params); const replies = useReplies(post); const visiblelinksCount = useCountLinksInReplies(post, 5); const totallinksCount = useCountLinksInReplies(post); const repliesCount = pinned ? replyCount : replyCount - 5; const linksCount = pinned ? totallinksCount : totallinksCount - visiblelinksCount; return (

{!isInPostView && ( )} {link && isValidURL(link) && } {!content &&
} {content && } {!isDescription && !isRules && !isInPendingPostView && (replies.length > 5 || (pinned && replies.length > 0)) && !isInPostView && ( {linksCount > 0 ? ( }} values={{ repliesCount, linksCount }} /> ) : ( }} values={{ repliesCount }} /> )} )} {!(pinned && !isInPostView) && !isInPendingPostView && !isDescription && !isRules && replies && (showAllReplies ? replies : replies.slice(-5)).map((reply, index) => (
{'>>'}
{reply.link && isValidURL(reply.link) && } {reply.content && }
))}
); }; export default PostDesktop;