import { useEffect, useRef, useState } from 'react';
import { Trans, useTranslation } from 'react-i18next';
import { Link, useLocation, useParams } from 'react-router-dom';
import { Comment, useAccount, 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 { getFormattedDate, getFormattedTimeAgo } from '../../lib/utils/time-utils';
import { isAllView, isPendingPostView, isPostPageView, isSubscriptionsView } from '../../lib/utils/view-utils';
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, link, locked, parentCid, pinned, postCid, 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 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;
// pending reply by account is not yet published
const account = useAccount();
const accountShortAddress = account?.author?.shortAddress;
const stateString = useStateString(post);
const handleUserAddressClick = useAuthorAddressClick();
const numberOfPostsByAuthor = document.querySelectorAll(`[data-author-address="${shortAddress}"][data-post-cid="${postCid}"]`).length;
return (
<>
{displayName ? (
displayName.length <= 20 ? (
{displayName}
) : (
{displayName.slice(0, 20) + '(...)'}}
content={displayName.length < 1000 ? displayName : displayName.slice(0, 1000) + '... display name too long'}
/>
)
) : (
_.capitalize(t('anonymous'))
)}
{authorRole && ` ## Board ${authorRole}`}{' '}
{!(isDescription || isRules) && (
<>
(u/
handleUserAddressClick(shortAddress || accountShortAddress, postCid)}
>
{shortAddress || accountShortAddress}
}
content={`${numberOfPostsByAuthor} ${numberOfPostsByAuthor === 1 ? 'post' : 'posts'} by this user address`}
showTooltip={isInPostPageView || postReplyCount < 6}
/>
){' '}
>
)}
{pinned && (
)}
{locked && (
)}
{title &&
(title.length <= 30 ? (
{title}
) : (
{title.slice(0, 30) + '(...)'}}
content={title.length < 1000 ? title : title.slice(0, 1000) + '... title too long'}
/>
))}
{subplebbitAddress && (isInAllView || isInSubscriptionsView) && !isReply && (
{' '}
p/{subplebbitAddress && Plebbit.getShortAddress(subplebbitAddress)}
)}
{getFormattedDate(timestamp)}} content={getFormattedTimeAgo(timestamp)} />{' '}
{!(isDescription || isRules) && (
!cid && e.preventDefault()}>
c/
{!cid ? (
{state === 'failed' || stateString === 'Failed' ? 'Failed' : 'Pending'}
) : (
openReplyModal && openReplyModal(cid)}>
{shortCid}
)}
)}
{(hasThumbnail || link) && }
>
);
};
const ReplyBacklinks = ({ post }: PostProps) => {
const { cid, parentCid, replyCount } = post || {};
const replies = useReplies(post);
return (
replyCount > 0 &&
parentCid &&
replies && (
{replies.map((reply: Comment, index: number) => reply?.parentCid === cid && )}
)
);
};
const PostMessageMobile = ({ post }: PostProps) => {
const { t } = useTranslation();
const { cid, content, deleted, edit, original, parentCid, postCid, reason, removed, spoiler, state, subplebbitAddress } = post || {};
const [showOriginal, setShowOriginal] = useState(false);
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}
);
const quotelinkReply = useComment({ commentCid: parentCid });
return (
content && (
{isReply && !(removed || deleted) && isReplyingToReply && }
{removed ? (
({t('this_post_was_removed')})
) : deleted ? (
{t('user_deleted_this_post')}
) : (
<>
{!showOriginal && }
{edit && original?.content !== post?.content && (
{showOriginal && }
{t('comment_edited_at_timestamp', { timestamp: getFormattedDate(edit?.timestamp), interpolation: { escapeValue: false } })}{' '}
{reason && <>{t('reason_reason', { reason: reason, interpolation: { escapeValue: false } })} >}
{showOriginal ? (
setShowOriginal(!showOriginal)} /> }}
/>
) : (
setShowOriginal(!showOriginal)} /> }}
/>
)}
)}
>
)}
{(removed || deleted) && reason && (
reason: {reason}
)}
{!isReply && content.length > 1000 && !isInPostView && (
}} />
)}
{!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, content, postCid, subplebbitAddress } = post || {};
const isRouteLinkToReply = useLocation().pathname.startsWith(`/p/${subplebbitAddress}/c/${cid}`);
const { hidden } = useHide({ cid });
return (
);
};
const PostMobile = ({ openReplyModal, post, roles, showAllReplies, showReplies = true }: PostProps) => {
const { t } = useTranslation();
const { author, cid, content, pinned, postCid, replyCount, 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 });
// scroll to reply if pathname is reply permalink (backlink)
const replyRefs = useRef<(HTMLDivElement | null)[]>([]);
useEffect(() => {
const replyIndex = replies.findIndex((reply) => location.pathname === `/p/${subplebbitAddress}/c/${reply?.cid}`);
if (replyIndex !== -1 && replyRefs.current[replyIndex]) {
replyRefs.current[replyIndex]?.scrollIntoView();
}
}, [location.pathname, replies, subplebbitAddress]);
return (
<>
{hidden && !isInPostPageView ? (
<>
Show Hidden Thread
>
) : (
{showReplies && (
)}
{!isInPostView && !isInPendingPostView && showReplies && (
{replyCount > 0 && `${replyCount} Replies`}
{linksCount > 0 && ` / ${linksCount} Links`}
{t('view_thread')}
)}
{!(pinned && !isInPostView) &&
!isInPendingPostView &&
!isDescription &&
!isRules &&
replies &&
showReplies &&
(showAllReplies ? replies : replies.slice(-5)).map((reply, index) => (
(replyRefs.current[index] = el)}>
))}
)}
>
);
};
export default PostMobile;