Files
5chan/src/components/post-mobile/post-mobile.tsx
T

501 lines
22 KiB
TypeScript
Raw Normal View History

2025-12-27 18:07:23 +01:00
import { useEffect, useMemo, useRef, useState } from 'react';
2025-02-22 22:22:42 +01:00
import { useTranslation } from 'react-i18next';
2025-12-27 18:07:23 +01:00
import { Link, useLocation, useNavigationType, useParams } from 'react-router-dom';
import { Virtuoso, VirtuosoHandle, StateSnapshot } from 'react-virtuoso';
2025-12-27 17:47:17 +01:00
import { Comment, useAuthorAvatar, useEditedComment, useReplies } from '@plebbit/plebbit-react-hooks';
import Plebbit from '@plebbit/plebbit-js';
import styles from '../../views/post/post.module.css';
2025-02-22 22:22:42 +01:00
import { shouldShowSnow } from '../../lib/snow';
import { getHasThumbnail } from '../../lib/utils/media-utils';
import { getTextColorForBackground, hashStringToColor } from '../../lib/utils/post-utils';
import { getFormattedDate, getFormattedTimeAgo } from '../../lib/utils/time-utils';
import { isAllView, isModQueueView, isPendingPostView, isPostPageView, isSubscriptionsView } from '../../lib/utils/view-utils';
import useModQueueStore from '../../stores/use-mod-queue-store';
import { useDefaultSubplebbits } from '../../hooks/use-default-subplebbits';
import { getBoardPath } from '../../lib/utils/route-utils';
2024-09-03 10:14:15 +02:00
import useAvatarVisibilityStore from '../../stores/use-avatar-visibility-store';
import useAuthorAddressClick from '../../hooks/use-author-address-click';
import { useCommentMediaInfo } from '../../hooks/use-comment-media-info';
import useCountLinksInReplies from '../../hooks/use-count-links-in-replies';
import useHide from '../../hooks/use-hide';
import useStateString from '../../hooks/use-state-string';
import useScrollToReply from '../../hooks/use-scroll-to-reply';
2025-02-22 22:22:42 +01:00
import CommentContent from '../comment-content';
import CommentMedia from '../comment-media';
import LoadingEllipsis from '../loading-ellipsis';
2024-05-28 19:03:43 +02:00
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';
2025-03-05 12:01:48 +01:00
import useReplyModalStore from '../../stores/use-reply-modal-store';
import { selectPostMenuProps } from '../../lib/utils/post-menu-props';
2025-12-27 18:07:23 +01:00
// Store scroll position for replies virtuoso across navigations
const lastVirtuosoStates: { [key: string]: StateSnapshot } = {};
2025-12-28 22:16:38 +01:00
const PostInfoAndMedia = ({ post, postReplyCount = 0, roles, threadNumber }: PostProps) => {
const { t } = useTranslation();
const defaultSubplebbits = useDefaultSubplebbits();
2025-12-24 18:07:14 +01:00
const { author, cid, deleted, link, linkHeight, linkWidth, locked, parentCid, pinned, postCid, reason, removed, state, subplebbitAddress, timestamp, thumbnailUrl } =
post || {};
const boardPath = subplebbitAddress ? getBoardPath(subplebbitAddress, defaultSubplebbits) : undefined;
const isReply = parentCid;
const title = post?.title?.trim();
const { address, shortAddress } = author || {};
const displayName = author?.displayName?.trim();
2025-12-23 19:33:46 +01:00
const authorRole = roles?.[address]?.role?.replace('moderator', 'mod');
2024-07-12 22:06:14 +02:00
const { imageUrl: avatarImageUrl } = useAuthorAvatar({ author });
2024-09-03 10:14:15 +02:00
const { hideAvatars } = useAvatarVisibilityStore();
const params = useParams();
const location = useLocation();
2024-10-29 17:40:09 +01:00
const isInAllView = isAllView(location.pathname);
const isInPostPageView = isPostPageView(location.pathname, params);
const isInSubscriptionsView = isSubscriptionsView(location.pathname, params);
const isInModQueueView = isModQueueView(location.pathname);
const { getAlertThresholdSeconds } = useModQueueStore();
const commentMediaInfo = useCommentMediaInfo(link, thumbnailUrl, linkWidth, linkHeight);
const hasThumbnail = getHasThumbnail(commentMediaInfo, link);
// Check if post is awaiting approval and over threshold (for mod queue view)
const approved = post?.approved;
const alreadyApproved = approved === true;
const alreadyRejected = removed === true;
const isAwaitingApproval = isInModQueueView && !alreadyApproved && !alreadyRejected;
const timeWaiting = timestamp ? Date.now() / 1000 - timestamp : 0;
const alertThresholdSeconds = getAlertThresholdSeconds();
const isOverThreshold = isAwaitingApproval && timeWaiting > alertThresholdSeconds;
const stateString = useStateString(post);
const postMenuProps = useMemo(() => selectPostMenuProps(post), [post]);
const handleUserAddressClick = useAuthorAddressClick();
const numberOfPostsByAuthor = document.querySelectorAll(`[data-author-address="${shortAddress}"][data-post-cid="${postCid}"]`).length;
2025-12-24 18:06:56 +01:00
const userID = address && Plebbit.getShortAddress({ address }); // should not be shortened to less than 12 characters, because users can create unlimited addresses/IDs before authenticating or passing challenges, so if the ID is short enough they can spoof it to troll users with the same ID
const userIDBackgroundColor = hashStringToColor(userID);
const userIDTextColor = getTextColorForBackground(userIDBackgroundColor);
2024-09-18 17:08:42 +02:00
const { hidden } = useHide(post);
2025-03-05 12:01:48 +01:00
const { openReplyModal } = useReplyModalStore();
const onReplyModalClick = () => {
deleted
? isReply
? alert(t('this_reply_was_deleted'))
: alert(t('this_thread_was_deleted'))
: removed
2025-12-27 17:47:17 +01:00
? isReply
? alert(t('this_reply_was_removed'))
: alert(t('this_thread_was_removed'))
2025-12-28 22:16:38 +01:00
: openReplyModal && openReplyModal(cid, post?.number, postCid, threadNumber, subplebbitAddress);
};
return (
<>
<div className={styles.postInfo}>
<PostMenuMobile postMenu={postMenuProps} editMenuPost={post} />
2024-09-21 13:01:23 +02:00
<span className={(hidden || ((removed || deleted) && !reason)) && parentCid ? styles.postDesktopHidden : ''}>
2024-09-18 17:08:42 +02:00
<span className={styles.nameBlock}>
<span className={`${styles.name} ${authorRole && !(deleted || removed) && (authorRole === 'mod' ? styles.capcodeMod : styles.capcodeAdmin)}`}>
{removed ? (
2024-09-18 17:08:42 +02:00
_.capitalize(t('removed'))
) : deleted ? (
2024-09-18 17:08:42 +02:00
_.capitalize(t('deleted'))
) : displayName ? (
displayName.length <= 20 ? (
displayName
) : (
<Tooltip
children={displayName.slice(0, 20) + '(...)'}
content={displayName.length < 1000 ? displayName : displayName.slice(0, 1000) + `... ${t('display_name_too_long')}`}
/>
)
) : (
2024-09-18 17:08:42 +02:00
_.capitalize(t('anonymous'))
2025-12-24 18:07:14 +01:00
)}{' '}
{!(deleted || removed) && authorRole && (
<span className='capitalize'>
{' '}
## Board {authorRole}{' '}
<span className={styles.capcodeIconMobileWrapper}>
<span
className={`${styles.capcodeIconMobile} ${authorRole === 'mod' ? styles.capcodeModIcon : styles.capcodeAdminIcon}`}
title={authorRole === 'mod' ? t('moderator_of_this_board') : t('administrator_of_this_board')}
/>
</span>
&nbsp;
</span>
)}
</span>
{author?.avatar && !(deleted || removed) && !hideAvatars && avatarImageUrl ? (
<span className={styles.authorAvatar}>
<img src={avatarImageUrl} alt='' />
</span>
) : null}
(ID: {''}
{removed ? (
_.lowerCase(t('removed'))
) : deleted ? (
_.lowerCase(t('deleted'))
) : (
<Tooltip
children={
<span
title={t('highlight_posts')}
className={styles.userAddress}
onClick={() => handleUserAddressClick(userID, postCid)}
style={{ backgroundColor: userIDBackgroundColor, color: userIDTextColor }}
>
{userID}
2024-09-18 17:08:42 +02:00
</span>
}
content={`${numberOfPostsByAuthor === 1 ? t('1_post_by_this_id') : t('x_posts_by_this_id', { number: numberOfPostsByAuthor })}`}
showTooltip={isInPostPageView || postReplyCount < 6}
/>
2024-09-18 17:08:42 +02:00
)}
){' '}
2024-09-18 17:08:42 +02:00
{pinned && (
<span className={styles.stickyIconWrapper}>
<img src='assets/icons/sticky.gif' alt='' className={styles.stickyIcon} title={t('sticky')} />
</span>
)}
{locked && (
<span className={`${styles.closedIconWrapper} ${pinned && styles.addPaddingInBetween}`}>
<img src='assets/icons/closed.gif' alt='' className={styles.closedIcon} title={t('closed')} />
</span>
)}
{title && (
<span className={styles.subjectWrapper}>
{title.length <= 30 ? (
<span className={styles.subject}>{title}</span>
) : (
<Tooltip
children={<span className={styles.subject}>{title.slice(0, 30) + '(...)'}</span>}
content={title.length < 1000 ? title : title.slice(0, 1000) + `... ${t('title_too_long')}`}
/>
)}
</span>
)}
</span>
<span className={styles.dateTimePostNum}>
{subplebbitAddress && (isInAllView || isInSubscriptionsView) && !isReply && boardPath && (
2024-09-18 17:08:42 +02:00
<div className={styles.postNumLink}>
{' '}
<Link to={`/${boardPath}`}>Board: {boardPath}</Link>
2024-09-18 17:08:42 +02:00
</div>
)}
{isInModQueueView && isOverThreshold ? (
<>
<Tooltip children={<span>{getFormattedDate(timestamp)}</span>} content={getFormattedTimeAgo(timestamp)} /> (
<span className={styles.alert}>{getFormattedTimeAgo(timestamp)}</span>)
</>
) : (
<Tooltip children={<span>{getFormattedDate(timestamp)}</span>} content={getFormattedTimeAgo(timestamp)} />
)}{' '}
{cid ? (
<span className={styles.postNumLink}>
<Link
to={boardPath ? `/${boardPath}/thread/${cid}` : `/thread/${cid}`}
className={styles.linkToPost}
title={t('link_to_post')}
onClick={(e) => !cid && e.preventDefault()}
>
2025-12-24 18:07:14 +01:00
No.
</Link>
<span className={styles.replyToPost} title={t('reply_to_post')} onMouseDown={onReplyModalClick}>
2025-12-24 18:07:14 +01:00
{post?.number || '?'}
2024-09-18 17:08:42 +02:00
</span>
</span>
) : (
<>
<span>No.</span>
<span className={styles.pendingCid}>
{state === 'failed' || stateString === 'Failed' ? _.capitalize(t('failed')) : state === 'pending' ? _.capitalize(t('pending')) : ''}
</span>
</>
)}
2024-09-18 17:08:42 +02:00
</span>
</span>
</div>
{(hasThumbnail || link) && !(deleted || removed) && <PostMediaContent key={cid} post={post} link={link} />}
</>
);
};
const PostMediaContent = ({ post, link }: { post: any; link: string }) => {
2024-11-21 17:23:14 +01:00
const [showThumbnail, setShowThumbnail] = useState(true);
const { thumbnailUrl, linkWidth, linkHeight, spoiler, deleted, removed, parentCid } = post || {};
const commentMediaInfo = useCommentMediaInfo(link, thumbnailUrl, linkWidth, linkHeight);
2024-11-21 17:23:14 +01:00
const hasThumbnail = getHasThumbnail(commentMediaInfo, link);
2024-11-29 21:53:53 +01:00
return (
hasThumbnail && (
<CommentMedia
commentMediaInfo={commentMediaInfo}
deleted={deleted}
removed={removed}
linkHeight={linkHeight}
linkWidth={linkWidth}
2024-11-29 21:53:53 +01:00
showThumbnail={showThumbnail}
setShowThumbnail={setShowThumbnail}
parentCid={parentCid}
spoiler={spoiler}
2024-11-29 21:53:53 +01:00
/>
)
);
2024-11-21 17:23:14 +01:00
};
const ReplyBacklinks = ({ post }: PostProps) => {
const { cid, parentCid } = post || {};
const { replies } = useReplies({ comment: post, flat: true });
2024-06-04 17:48:53 +02:00
return (
cid &&
2024-06-04 17:48:53 +02:00
parentCid &&
2024-09-03 10:23:50 +02:00
replies.length > 0 && (
2024-06-04 17:48:53 +02:00
<div className={styles.mobileReplyBacklinks}>
{replies.map(
(reply: Comment, index: number) =>
reply?.parentCid === cid &&
reply?.cid &&
!(reply?.deleted || reply?.removed) && <ReplyQuotePreview key={index} isBacklinkReply={true} backlinkReply={reply} />,
)}
2024-06-04 17:48:53 +02:00
</div>
)
);
};
2025-12-28 22:16:38 +01:00
const Reply = ({ postReplyCount, reply, roles, threadNumber }: 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 defaultSubplebbits = useDefaultSubplebbits();
const boardPath = subplebbitAddress ? getBoardPath(subplebbitAddress, defaultSubplebbits) : undefined;
const location = useLocation();
const route = boardPath ? `/${boardPath}/thread/${cid}` : `/thread/${cid}`;
const isRouteLinkToReply = cid ? location.pathname.startsWith(route) : false;
const { hidden } = useHide({ cid });
return (
<div className={styles.replyMobile}>
<div className={styles.reply}>
<div
2024-09-18 17:08:42 +02:00
className={`${styles.replyContainer} ${isRouteLinkToReply && styles.highlight}`}
data-cid={cid}
data-author-address={author?.shortAddress}
data-post-cid={postCid}
>
2025-12-28 22:16:38 +01:00
<PostInfoAndMedia post={post} postReplyCount={postReplyCount} roles={roles} threadNumber={threadNumber} />
{!hidden && (!(removed || deleted) || ((removed || deleted) && reason)) && <CommentContent comment={post} />}
<ReplyBacklinks post={reply} />
</div>
</div>
</div>
);
};
const PostMobile = ({
post,
roles,
showAllReplies,
showReplies = true,
targetReplyCid,
isModQueue,
modQueueStatus,
modQueueError,
isPublishing,
onApprove,
onReject,
}: PostProps) => {
const { t } = useTranslation();
const { author, cid, pinned, postCid, replyCount, state, subplebbitAddress } = post || {};
const params = useParams();
const location = useLocation();
2025-12-27 18:07:23 +01:00
const navigationType = useNavigationType();
const isInPendingPostView = isPendingPostView(location.pathname, params);
const isInPostView = isPostPageView(location.pathname, params);
const defaultSubplebbits = useDefaultSubplebbits();
const boardPath = subplebbitAddress ? getBoardPath(subplebbitAddress, defaultSubplebbits) : undefined;
const linksCount = useCountLinksInReplies(post);
2025-12-27 18:07:23 +01:00
const { replies, hasMore, loadMore } = useReplies({ comment: post });
const isInPostPageView = isPostPageView(location.pathname, params);
const { hidden, unhide } = useHide({ cid });
const stateString = useStateString(post) || t('loading_post');
2024-07-21 12:17:40 +02:00
2025-12-27 18:07:23 +01:00
// Filter out deleted replies with no children for both virtuoso and non-virtuoso rendering
const filteredReplies = useMemo(() => (replies || []).filter((reply) => !(reply.deleted && (reply.replyCount === 0 || !reply.replyCount))), [replies]);
// Virtuoso scroll position management for infinite replies
const virtuosoRef = useRef<VirtuosoHandle | null>(null);
const virtuosoStateKey = `replies-mobile-${cid}`;
useEffect(() => {
if (!showAllReplies || !isInPostPageView) return;
const currentKey = virtuosoStateKey;
const setLastVirtuosoState = () => {
virtuosoRef.current?.getState((snapshot: StateSnapshot) => {
if (snapshot?.ranges?.length) {
lastVirtuosoStates[currentKey] = snapshot;
}
});
};
window.addEventListener('scroll', setLastVirtuosoState);
return () => window.removeEventListener('scroll', setLastVirtuosoState);
}, [virtuosoStateKey, showAllReplies, isInPostPageView]);
const lastVirtuosoState = navigationType === 'POP' ? lastVirtuosoStates?.[virtuosoStateKey] : undefined;
const shouldScrollToReply = showAllReplies && showReplies && !isInPendingPostView && !!targetReplyCid;
useScrollToReply({
targetReplyCid,
replies: filteredReplies,
hasMore,
loadMore,
virtuosoRef,
enabled: shouldScrollToReply,
});
2025-12-27 18:07:23 +01:00
// Footer component for Virtuoso showing loading state
const RepliesFooter = () =>
hasMore ? (
<div className={styles.stateString}>
<LoadingEllipsis string={t('loading')} />
</div>
) : null;
return (
<>
{hidden && !isInPostPageView ? (
<>
<hr className={styles.unhideButtonHr} />
<span className={styles.mobileUnhideButton}>
<span className='button' onClick={unhide}>
Show Hidden Thread
</span>
</span>
</>
) : (
<div className={styles.postMobile}>
{(showReplies || isModQueue) && (
<div className={styles.hrWrapper}>
<hr />
</div>
)}
<div className={showReplies || isModQueue ? styles.thread : styles.quotePreview}>
<div className={styles.postContainer}>
2024-12-24 17:13:12 +01:00
<div
className={`${styles.postOp} ${shouldShowSnow() ? styles.xmasHatWrapper : ''}`}
data-cid={cid}
data-author-address={author?.shortAddress}
data-post-cid={postCid}
>
2025-05-22 13:15:40 +02:00
{shouldShowSnow() && <img src='assets/xmashat.gif' className={styles.xmasHat} alt='' />}
2025-12-28 22:16:38 +01:00
<PostInfoAndMedia post={post} postReplyCount={replyCount} roles={roles} threadNumber={post?.number} />
<CommentContent comment={post} />
</div>
{!isInPostView && !isInPendingPostView && (showReplies || isModQueue) && (
<div className={styles.postLink}>
<span className={styles.info}>
{replyCount > 0 && `${replyCount} Replies`}
{linksCount > 0 && ` / ${linksCount} Links`}
</span>
{isModQueue ? (
<div className={styles.modQueueActions}>
{modQueueStatus === 'approved' ? (
<span className={styles.modQueueStatusApproved}>{t('approved')}</span>
) : modQueueStatus === 'rejected' ? (
<span className={styles.modQueueStatusRejected}>{t('rejected')}</span>
) : modQueueStatus === 'failed' ? (
<span className={styles.modQueueStatusRejected}>
{t('failed')}
{modQueueError ? `: ${modQueueError}` : ''}
</span>
) : isPublishing ? (
<LoadingEllipsis string={t('publishing')} />
) : (
<>
<button className={`button ${styles.approveButton}`} onClick={onApprove} disabled={isPublishing}>
{t('approve')}
</button>
<button className={`button ${styles.rejectButton}`} onClick={onReject} disabled={isPublishing}>
{t('reject')}
</button>
</>
)}
</div>
) : (
<Link to={boardPath ? `/${boardPath}/thread/${cid}` : `/thread/${cid}`} className='button'>
{t('view_thread')}
</Link>
)}
</div>
)}
</div>
{/* Virtuoso infinite scroll for post page view when there's more content to paginate */}
{!(pinned && !isInPostView) && showAllReplies && !isInPendingPostView && showReplies && hasMore && (
2025-12-27 18:07:23 +01:00
<Virtuoso
increaseViewportBy={{ bottom: 1200, top: 1200 }}
totalCount={filteredReplies.length}
data={filteredReplies}
itemContent={(index, reply) => (
<div className={styles.replyContainer}>
2025-12-28 22:16:38 +01:00
<Reply postReplyCount={replyCount} reply={reply} roles={roles} threadNumber={post?.number} />
2025-12-27 18:07:23 +01:00
</div>
)}
useWindowScroll={true}
components={{ Footer: RepliesFooter }}
endReached={loadMore}
ref={virtuosoRef}
restoreStateFrom={lastVirtuosoState}
initialScrollTop={lastVirtuosoState?.scrollTop}
/>
)}
{/* Non-virtualized rendering for post page view when all replies fit on one page */}
{!(pinned && !isInPostView) &&
showAllReplies &&
!isInPendingPostView &&
showReplies &&
!hasMore &&
filteredReplies.map((reply, index) => (
<div key={index} className={styles.replyContainer}>
2025-12-28 22:16:38 +01:00
<Reply postReplyCount={replyCount} reply={reply} roles={roles} threadNumber={post?.number} />
</div>
))}
2025-12-27 18:07:23 +01:00
{/* Non-virtualized rendering for board view (last 5 replies) */}
{!(pinned && !isInPostView) &&
2025-12-27 18:07:23 +01:00
!showAllReplies &&
!isInPendingPostView &&
replies &&
showReplies &&
2025-12-27 18:07:23 +01:00
filteredReplies.slice(-5).map((reply, index) => (
<div key={index} className={styles.replyContainer}>
2025-12-28 22:16:38 +01:00
<Reply postReplyCount={replyCount} reply={reply} roles={roles} threadNumber={post?.number} />
2025-12-27 18:07:23 +01:00
</div>
))}
</div>
{!isInPendingPostView && stateString && stateString !== 'Failed' && state !== 'succeeded' && isInPostPageView && !(!showReplies && !showAllReplies) ? (
<div className={styles.stateString}>
<LoadingEllipsis string={stateString} />
</div>
) : (
state === 'failed' && <span className={styles.error}>{t('failed')}</span>
)}
</div>
)}
</>
);
};
export default PostMobile;