import { useEffect, useRef, useState } from 'react';
import { Trans, useTranslation } from 'react-i18next';
import { Link, useLocation, useParams } from 'react-router-dom';
import { Comment, useAccount, useComment } 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, getDisplayMediaInfoType, getHasThumbnail } from '../../lib/utils/media-utils';
import { isValidURL } from '../../lib/utils/url-utils';
import { isAllView, isPendingPostView, isPostPageView, isSubscriptionsView } from '../../lib/utils/view-utils';
import useEditCommentPrivileges from '../../hooks/use-author-privileges';
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 { canEmbed } from '../embed';
import LoadingEllipsis from '../loading-ellipsis';
import Markdown from '../markdown';
import PostMenuDesktop from './post-menu-desktop';
import EditMenu from '../edit-menu/edit-menu';
import ReplyQuotePreview from '../reply-quote-preview';
import { PostProps } from '../../views/post/post';
import Timestamp from '../timestamp';
import _ from 'lodash';
const PostInfo = ({ openReplyModal, post, roles, isHidden }: PostProps) => {
const { t } = useTranslation();
const { author, cid, locked, pinned, parentCid, postCid, replyCount, shortCid, state, subplebbitAddress, timestamp, title } = post || {};
const replies = useReplies(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;
const account = useAccount();
const accountShortAddress = account?.author?.shortAddress; // if reply by account is pending, it doesn't have an author yet
const { isCommentAuthorMod, isAccountMod, isAccountCommentAuthor } = useEditCommentPrivileges({ commentAuthorAddress: address, subplebbitAddress });
return (
{!isHidden &&
}
{title &&
{displayTitle} }
{shortDisplayName || _.capitalize(t('anonymous'))}
{authorRole && ` ## Board ${authorRole}`}{' '}
{!(isDescription || isRules) && (u/{shortAddress || accountShortAddress}) }
{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 && !isHidden && (
[
{_.capitalize(t('reply'))}
]
)}
{replyCount > 0 &&
parentCid &&
replies &&
replies.map((reply: Comment, index: number) => reply?.parentCid === cid &&
)}
);
};
const PostMedia = ({ post }: PostProps) => {
const { t } = useTranslation();
const commentMediaInfo = getCommentMediaInfo(post);
const { type, url } = commentMediaInfo || {};
const embedUrl = url && new URL(url);
const hasThumbnail = getHasThumbnail(commentMediaInfo, post?.link);
const [showThumbnail, setShowThumbnail] = useState(true);
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, deleted, parentCid, postCid, reason, removed, spoiler, state, subplebbitAddress } = post || {};
const { t } = useTranslation();
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 (
{isReply && isReplyingToReply && }
{removed ? (
({t('this_post_was_removed')})
) : deleted ? (
{t('user_deleted_this_post')}
) : (
)}
{(removed || deleted) && reason && (
{t('reason')}: {reason}
)}
{!isReply && content.length > 1000 && !isInPostView && (
}} />
)}
{!cid && state === 'pending' && stateString !== 'Failed' && (
<>
{loadingString}
>
)}
);
};
const Reply = ({ openReplyModal, reply, roles }: PostProps) => {
const { cid, subplebbitAddress } = reply || {};
const isRouteLinkToReply = useLocation().pathname.startsWith(`/p/${subplebbitAddress}/c/${cid}`);
const { hidden } = useHide({ cid });
return (
{'>>'}
{reply.link && !hidden && isValidURL(reply.link) &&
}
{reply.content && !hidden &&
}
);
};
const PostDesktop = ({ openReplyModal, post, roles, showAllReplies, showReplies = true }: 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 isInPostPageView = isPostPageView(location.pathname, params);
const { hidden, unhide, hide } = useHide({ cid });
const isHidden = hidden && !isInPostPageView;
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;
// 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 (
{showReplies ? (
) : (
)}
{!isInPostPageView && !isDescription && !isRules && showReplies && (
)}
{link && !isHidden && isValidURL(link) &&
}
{!isHidden && !content &&
}
{!isHidden && content &&
}
{!isHidden && !isDescription && !isRules && !isInPendingPostView && (replies.length > 5 || (pinned && replies.length > 0)) && !isInPostPageView && (
{linksCount > 0 ? (
}}
values={{ repliesCount, linksCount }}
/>
) : (
}} values={{ repliesCount }} />
)}
)}
{!isHidden &&
!(pinned && !isInPostPageView) &&
!isInPendingPostView &&
!isDescription &&
!isRules &&
replies &&
showReplies &&
(showAllReplies ? replies : replies.slice(-5)).map((reply, index) => (
(replyRefs.current[index] = el)}>
))}
);
};
export default PostDesktop;