import { Fragment, useCallback, useMemo, useState } from 'react'; import { useLocation, useParams } from 'react-router-dom'; import { Trans, useTranslation } from 'react-i18next'; import { Comment, useComment } from '@plebbit/plebbit-react-hooks'; import useSubplebbitsPagesStore from '@plebbit/plebbit-react-hooks/dist/stores/subplebbits-pages'; import usePostNumberStore from '../../stores/use-post-number-store'; import Plebbit from '@plebbit/plebbit-js'; import { getFormattedDate, getFormattedTimeAgo } from '../../lib/utils/time-utils'; import { isPostPageView } from '../../lib/utils/view-utils'; import useIsMobile from '../../hooks/use-is-mobile'; import useStateString from '../../hooks/use-state-string'; import LoadingEllipsis from '../../components/loading-ellipsis'; import ReplyQuotePreview from '../../components/reply-quote-preview'; import Markdown from '../../components/markdown'; import Tooltip from '../../components/tooltip'; import styles from '../../views/post/post.module.css'; import { capitalize } from 'lodash'; const QuotedCidLink = ({ cid, postCid }: { cid: string; postCid: string }) => { const commentFromStore = useSubplebbitsPagesStore((state) => state.comments[cid]); const commentFromHook = useComment({ commentCid: cid, onlyIfCached: true }); // Prefer hook version to ensure 'number' property is populated for deeper nested replies in Virtuoso const quotedComment = commentFromHook?.number !== undefined ? commentFromHook : commentFromStore; const isOP = cid === postCid; return ; }; const useScopedCidToNumber = (cids: string[]) => { const sortedUniqueCids = useMemo(() => { const uniqueCids = new Set(); for (const cid of cids) { if (cid) { uniqueCids.add(cid); } } return [...uniqueCids].sort(); }, [cids]); // Subscribe only to CIDs this comment needs so unrelated thread updates do not rerender content. const cidNumbersSignature = usePostNumberStore( useCallback((state) => sortedUniqueCids.map((cid) => `${cid}:${state.cidToNumber[cid] ?? ''}`).join('|'), [sortedUniqueCids]), ); return useMemo(() => { if (sortedUniqueCids.length === 0) { return {} as Record; } const { cidToNumber } = usePostNumberStore.getState(); const nextCidToNumber: Record = {}; for (const cid of sortedUniqueCids) { const number = cidToNumber[cid]; if (typeof number === 'number') { nextCidToNumber[cid] = number; } } return nextCidToNumber; }, [sortedUniqueCids, cidNumbersSignature]); }; const CommentContent = ({ comment: post }: { comment: Comment }) => { const { t } = useTranslation(); const params = useParams(); const location = useLocation(); const isInPostView = isPostPageView(location.pathname, params); const [showOriginal, setShowOriginal] = useState(false); const isMobile = useIsMobile(); const { cid, content, deleted, edit, original, parentCid, postCid, pendingApproval, quotedCids, reason, removed, state, subplebbitAddress } = post || {}; const banned = !!post?.author?.subplebbit?.banExpiresAt; const [showFullComment, setShowFullComment] = useState(false); const displayContent = content && (!isInPostView && content.length > 1000 && !showFullComment ? content.slice(0, 1000) : isInPostView && content.length > 2000 && !showFullComment ? content.slice(0, 2000) : content); const quotelinkReplyFromStore = useSubplebbitsPagesStore((state) => state.comments[parentCid]); const quotelinkReplyFromHook = useComment({ commentCid: parentCid, onlyIfCached: true }); // Prefer hook version to ensure 'number' property is populated for deeper nested replies in Virtuoso const quotelinkReply = quotelinkReplyFromHook?.number !== undefined ? quotelinkReplyFromHook : quotelinkReplyFromStore; const isReply = !!parentCid; const isReplyingToReply = isReply && parentCid !== postCid; const contentNumbers = useMemo(() => { if (!content) return new Set(); const matches = content.matchAll(/(?/\w])>>(\d+)(?![\d/])/g); return new Set([...matches].map((m) => parseInt(m[1], 10))); }, [content]); const relevantQuotedCids = useMemo(() => { const cids = quotedCids ? [...quotedCids] : []; if (parentCid) { cids.push(parentCid); } return cids; }, [quotedCids, parentCid]); const cidToNumber = useScopedCidToNumber(relevantQuotedCids); const filteredQuotedCids = useMemo(() => { if (!quotedCids?.length) return []; return quotedCids.filter((cid: string) => { const num = cidToNumber[cid]; return num === undefined || !contentNumbers.has(num); }); }, [quotedCids, cidToNumber, contentNumbers]); const shouldShowReplyingToReply = isReplyingToReply && (parentCid ? !contentNumbers.has(cidToNumber[parentCid] ?? -1) : true); const stateString = useStateString(post); const hasFailedState = state === 'failed'; const loadingString = (
{!hasFailedState ? : stateString || capitalize(t('failed'))}
); return (
{isReply && !hasFailedState && !(deleted || removed) && (filteredQuotedCids.length > 0 ? filteredQuotedCids.map((cid: string) => ) : shouldShowReplyingToReply && )} {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 && } {pendingApproval && ( <>
({t('pending_mod_approval')}) )} {((!isInPostView && content?.length > 1000 && !showFullComment) || (isInPostView && content?.length > 2000 && !showFullComment)) && (

setShowFullComment(true)} /> }} />
)} {edit && original?.content !== content && ( {showOriginal && }

} />, }} />{' '} {reason && <>{t('reason_reason', { reason: reason, interpolation: { escapeValue: false } })} } {showOriginal ? ( setShowOriginal(!showOriginal)} /> }} /> ) : ( setShowOriginal(!showOriginal)} /> }} /> )}
)} )} {banned && (

)} {!cid && !hasFailedState && ( <>

{loadingString} )}
); }; export default CommentContent;