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

392 lines
15 KiB
TypeScript
Raw Normal View History

2024-04-01 14:24:10 +02:00
import { useState } from 'react';
2024-04-03 22:52:27 +02:00
import { Link, useLocation, useParams } from 'react-router-dom';
import { useTranslation } from 'react-i18next';
2024-03-22 15:31:02 +01:00
import { Comment } from '@plebbit/plebbit-react-hooks';
2024-04-03 16:24:57 +02:00
import Plebbit from '@plebbit/plebbit-js/dist/browser/index.js';
2024-03-29 13:54:11 +01:00
import { getCommentMediaInfoMemoized, getHasThumbnail } from '../../lib/utils/media-utils';
import { getFormattedDate } from '../../lib/utils/time-utils';
2024-04-03 22:52:27 +02:00
import { isPostPageView } from '../../lib/utils/view-utils';
import useCountLinksInReplies from '../../hooks/use-count-links-in-replies';
2024-03-29 13:54:11 +01:00
import useReplies from '../../hooks/use-replies';
import styles from './post.module.css';
import Markdown from '../markdown';
2024-04-01 14:24:10 +02:00
import Media from '../media';
2024-03-29 13:54:11 +01:00
2024-04-02 17:00:36 +02:00
const ReplyDesktop = ({ reply }: Comment) => {
2024-03-30 18:10:19 +01:00
const { t } = useTranslation();
2024-04-03 16:24:57 +02:00
const { author, content, link, linkHeight, linkWidth, parentCid, pinned, postCid, shortCid, subplebbitAddress, timestamp } = reply || {};
2024-03-30 18:10:19 +01:00
const { displayName, shortAddress } = author || {};
const commentMediaInfo = getCommentMediaInfoMemoized(reply);
const hasThumbnail = getHasThumbnail(commentMediaInfo, link);
const [showThumbnail, setShowThumbnail] = useState(true);
2024-04-03 16:24:57 +02:00
const isReplyingToReply = postCid !== parentCid;
2024-03-30 16:18:16 +01:00
return (
2024-04-02 17:00:36 +02:00
<div className={styles.replyDesktop}>
<div className={styles.sideArrows}>{'>>'}</div>
<div className={styles.reply}>
<div className={styles.postInfo}>
<span className={styles.checkbox}>
<input type='checkbox' />
</span>
<span className={styles.nameBlock}>
<span className={styles.name}>{displayName || 'Anonymous'} </span>
<span className={styles.userAddress}>(u/{shortAddress}) </span>
</span>
<span className={styles.dateTime}>{getFormattedDate(timestamp)} </span>
<span className={styles.postNum}>
<span className={styles.postNumLink}>
<Link to={`/p/${subplebbitAddress}/c/${reply.cid}`} className={styles.linkToPost} title='Link to post'>
c/
</Link>
<span className={styles.replyToPost} title='Reply to post'>
{shortCid}
2024-03-30 18:10:19 +01:00
</span>
</span>
2024-04-02 17:00:36 +02:00
{pinned && (
<span className={styles.stickyIconWrapper}>
<img src='assets/icons/sticky.gif' alt='' className={styles.stickyIcon} title={t('sticky')} />
</span>
)}
</span>
<span className={styles.postMenuBtn}></span>
</div>
{link && (
<div className={styles.file}>
<div className={styles.fileText}>
{t('link')}:{' '}
<a href={link} target='_blank' rel='noopener noreferrer'>
2024-04-03 16:24:57 +02:00
{link.length > 30 ? link?.slice(0, 30) + '...' : link}
2024-04-02 17:00:36 +02:00
</a>
{!showThumbnail && (commentMediaInfo?.type === 'iframe' || commentMediaInfo?.type === 'video') && (
<span>
{' '}
[
<span className={styles.closeMedia} onClick={() => setShowThumbnail(true)}>
{t('close')}
2024-04-01 21:07:36 +02:00
</span>
2024-04-02 17:00:36 +02:00
]
</span>
)}
</div>
2024-04-02 17:00:36 +02:00
{hasThumbnail && (
<Media
commentMediaInfo={commentMediaInfo}
isMobile={false}
isReply={true}
linkHeight={linkHeight}
linkWidth={linkWidth}
showThumbnail={showThumbnail}
setShowThumbnail={setShowThumbnail}
/>
)}
</div>
)}
{content && (
<blockquote className={styles.postMessage}>
2024-04-03 16:24:57 +02:00
{isReplyingToReply && (
<>
<Link to={`/p/${subplebbitAddress}/c/${parentCid}`} className={styles.quoteLink}>
{`c/${Plebbit.getShortCid(parentCid)}`}
</Link>
<br />
</>
)}
2024-04-02 17:00:36 +02:00
<Markdown content={content} />
</blockquote>
)}
2024-03-30 18:10:19 +01:00
</div>
2024-04-02 17:00:36 +02:00
</div>
2024-03-30 16:18:16 +01:00
);
};
2024-04-02 17:00:36 +02:00
const PostDesktop = ({ post, showAllReplies }: { post: Comment; showAllReplies: boolean }) => {
const { t } = useTranslation();
const { author, cid, content, link, linkHeight, linkWidth, locked, pinned, postCid, replyCount, shortCid, subplebbitAddress, timestamp, title } = post || {};
const { displayName, shortAddress } = author || {};
2024-04-03 16:24:57 +02:00
const displayTitle = title && title.length > 75 ? title?.slice(0, 75) + '...' : title;
const displayContent = content && content.length > 1000 ? content?.slice(0, 1000) + '(...)' : content;
2024-03-29 13:54:11 +01:00
const commentMediaInfo = getCommentMediaInfoMemoized(post);
const hasThumbnail = getHasThumbnail(commentMediaInfo, link);
const [showThumbnail, setShowThumbnail] = useState(true);
2024-03-29 13:54:11 +01:00
2024-03-30 18:10:19 +01:00
const replies = useReplies(post);
const visibleLinkCount = useCountLinksInReplies(post, 5);
const totalLinkCount = useCountLinksInReplies(post);
const repliesCount = pinned ? replyCount : replyCount - 5;
const linkCount = pinned ? totalLinkCount : totalLinkCount - visibleLinkCount;
2024-03-30 18:10:19 +01:00
2024-03-22 15:31:02 +01:00
return (
<div className={styles.postDesktop}>
<div className={styles.hrWrapper}>
<hr />
</div>
2024-03-29 13:54:11 +01:00
<span className={styles.threadHideButtonWrapper}>
<span className={`${styles.threadHideButton} ${styles.hideThread}`} />
</span>
{commentMediaInfo?.url && (
<div className={styles.file}>
<div className={styles.fileText}>
{t('link')}:{' '}
<a href={commentMediaInfo?.url} target='_blank' rel='noopener noreferrer'>
2024-04-03 16:24:57 +02:00
{commentMediaInfo.url.length > 30 ? commentMediaInfo?.url?.slice(0, 30) + '...' : commentMediaInfo?.url}
2024-03-29 13:54:11 +01:00
</a>{' '}
({commentMediaInfo?.type})
2024-04-01 14:24:10 +02:00
{!showThumbnail && (commentMediaInfo?.type === 'iframe' || commentMediaInfo?.type === 'video') && (
<span>
{' '}
[
<span className={styles.closeMedia} onClick={() => setShowThumbnail(true)}>
{t('close')}
</span>
]
</span>
)}
2024-03-29 13:54:11 +01:00
</div>
{hasThumbnail && (
2024-04-01 14:24:10 +02:00
<Media
commentMediaInfo={commentMediaInfo}
isMobile={false}
isReply={false}
linkHeight={linkHeight}
linkWidth={linkWidth}
showThumbnail={showThumbnail}
setShowThumbnail={setShowThumbnail}
/>
2024-03-29 13:54:11 +01:00
)}
</div>
)}
<div className={styles.postInfo}>
2024-03-30 18:10:19 +01:00
<span className={styles.checkbox}>
<input type='checkbox' />
</span>
{title && <span className={styles.subject}>{displayTitle} </span>}
<span className={styles.nameBlock}>
<span className={styles.name}>{displayName || 'Anonymous'} </span>
<span className={styles.userAddress}>(u/{shortAddress}) </span>
</span>
<span className={styles.dateTime}>{getFormattedDate(timestamp)} </span>
<span className={styles.postNum}>
<span className={styles.postNumLink}>
<Link to={`/p/${subplebbitAddress}/c/${cid}`} className={styles.linkToPost} title={t('link_to_post')}>
c/
</Link>
<span className={styles.replyToPost} title={t('reply_to_post')}>
{shortCid}
</span>
</span>
{pinned && (
<span className={styles.stickyIconWrapper}>
<img src='assets/icons/sticky.gif' alt='' className={styles.stickyIcon} title={t('sticky')} />
</span>
2024-03-26 16:58:39 +01:00
)}
{locked && (
<span className={styles.closedIconWrapper}>
<img src='assets/icons/closed.gif' alt='' className={styles.closedIcon} title={t('closed')} />
</span>
)}
<span className={styles.replyButton}>
[<Link to={`/p/${subplebbitAddress}/c/${postCid}`}>{t('reply')}</Link>]
</span>
</span>
<span className={styles.postMenuBtn}></span>
</div>
2024-04-01 14:24:10 +02:00
{!content && <div className={styles.spacer} />}
{content && (
<blockquote className={styles.postMessage}>
<Markdown content={displayContent} />
{content.length > 1000 && (
<span className={styles.abbr}>
<br />
Comment too long. <Link to={`/p/${subplebbitAddress}/c/${cid}`}>Click here</Link> to view the full text.
</span>
)}
</blockquote>
)}
{(replies.length > 5 || pinned) && (
<span className={styles.summary}>
<span className={styles.expandButton} />
<span>
{repliesCount} replies {linkCount > 0 && `and ${linkCount} links`} omitted.{' '}
</span>
<Link to={`/p/${subplebbitAddress}/c/${cid}`}>Click here</Link> to view.
</span>
)}
2024-03-30 18:10:19 +01:00
{!pinned &&
2024-04-03 16:24:57 +02:00
replies?.slice(0, showAllReplies ? replies.length : 5).map((reply, index) => (
2024-03-30 18:10:19 +01:00
<div key={reply.cid} className={styles.replyContainer}>
<ReplyDesktop index={index} reply={reply} />
</div>
))}
</div>
);
};
2024-04-02 17:00:36 +02:00
const ReplyMobile = ({ reply }: Comment) => {
2024-04-03 16:24:57 +02:00
const { author, content, link, linkHeight, linkWidth, parentCid, pinned, postCid, shortCid, subplebbitAddress, timestamp } = reply || {};
2024-03-30 22:19:49 +01:00
const { displayName, shortAddress } = author || {};
const commentMediaInfo = getCommentMediaInfoMemoized(reply);
const hasThumbnail = getHasThumbnail(commentMediaInfo, link);
const [showThumbnail, setShowThumbnail] = useState(true);
2024-04-03 16:24:57 +02:00
const isReplyingToReply = postCid !== parentCid;
2024-03-30 22:19:49 +01:00
return (
2024-04-02 17:00:36 +02:00
<div className={styles.replyMobile}>
<div className={styles.reply}>
<div className={styles.replyContainer}>
<div className={styles.postInfo}>
<span className={styles.postMenuBtn}>...</span>
<span className={styles.nameBlock}>
<span className={styles.name}>{displayName || 'Anonymous'} </span>
<span className={styles.address}>(u/{shortAddress})</span>
</span>
<span className={styles.dateTimePostNum}>
{getFormattedDate(timestamp)} <span className={styles.linkToPost}>c/</span>
<span className={styles.replyToPost}>{shortCid}</span>
</span>
2024-03-30 22:19:49 +01:00
</div>
2024-04-02 17:00:36 +02:00
{hasThumbnail && (
<Media
commentMediaInfo={commentMediaInfo}
isMobile={true}
isReply={false}
linkHeight={linkHeight}
linkWidth={linkWidth}
showThumbnail={showThumbnail}
setShowThumbnail={setShowThumbnail}
/>
)}
2024-04-03 16:24:57 +02:00
{content && (
<blockquote className={styles.postMessage}>
{isReplyingToReply && (
<>
<Link to={`/p/${subplebbitAddress}/c/${parentCid}`} className={styles.quoteLink}>
{`c/${Plebbit.getShortCid(parentCid)}`}
</Link>
<br />
</>
)}
<Markdown content={content} />
</blockquote>
)}
2024-03-30 22:19:49 +01:00
</div>
</div>
2024-04-02 17:00:36 +02:00
</div>
2024-03-30 22:19:49 +01:00
);
};
2024-04-02 17:00:36 +02:00
const PostMobile = ({ post, showAllReplies }: { post: Comment; showAllReplies: boolean }) => {
2024-04-03 22:52:27 +02:00
const { t } = useTranslation();
2024-03-30 22:19:49 +01:00
const { author, cid, content, link, linkHeight, linkWidth, pinned, replyCount, shortCid, subplebbitAddress, timestamp, title } = post || {};
2024-03-28 12:41:40 +01:00
const { address, displayName, shortAddress } = author || {};
const linkCount = useCountLinksInReplies(post);
2024-04-03 16:24:57 +02:00
const displayTitle = title && title.length > 30 ? title?.slice(0, 30) + '(...)' : title;
const displayContent = content && content.length > 1000 ? content?.slice(0, 1000) : content;
2024-03-28 12:41:40 +01:00
2024-03-29 20:43:58 +01:00
const commentMediaInfo = getCommentMediaInfoMemoized(post);
const hasThumbnail = getHasThumbnail(commentMediaInfo, link);
const [showThumbnail, setShowThumbnail] = useState(true);
2024-03-29 20:43:58 +01:00
2024-03-30 22:19:49 +01:00
const replies = useReplies(post);
2024-04-03 22:52:27 +02:00
const location = useLocation();
const params = useParams();
const isInPostPage = isPostPageView(location.pathname, params);
return (
<div className={styles.postMobile}>
2024-03-28 12:41:40 +01:00
<div className={styles.hrWrapper}>
<hr />
</div>
<div className={styles.thread}>
<div className={styles.postContainer}>
2024-03-28 12:41:40 +01:00
<div className={styles.postOp}>
<div className={styles.postInfo}>
<span className={styles.postMenuBtn} title='Post menu'>
...
</span>
<span className={styles.nameBlock}>
<span className={styles.name}>{displayName || 'Anonymous'} </span>
2024-04-03 16:24:57 +02:00
<span className={styles.address}>(u/{shortAddress || address?.slice(0, 12) + '...'})</span>
2024-03-28 12:41:40 +01:00
{title && (
<>
<br />
<span className={styles.subject}>{displayTitle}</span>
</>
)}
</span>
<span className={styles.dateTimePostNum}>
{getFormattedDate(timestamp)} <span className={styles.linkToPost}>c/</span>
<span className={styles.replyToPost}>{shortCid}</span>
</span>
</div>
2024-03-29 20:43:58 +01:00
{hasThumbnail && (
<Media
commentMediaInfo={commentMediaInfo}
isMobile={true}
isReply={false}
linkHeight={linkHeight}
linkWidth={linkWidth}
showThumbnail={showThumbnail}
setShowThumbnail={setShowThumbnail}
/>
2024-03-29 20:43:58 +01:00
)}
{content && (
<blockquote className={`${styles.postMessage} ${styles.clampLines}`}>
<Markdown content={displayContent} />
{content.length > 1000 && (
<span className={styles.abbr}>
<br />
Comment too long. <Link to={`/p/${subplebbitAddress}/c/${cid}`}>Click here</Link> to view the full text.
</span>
)}
</blockquote>
)}
2024-03-28 12:41:40 +01:00
</div>
2024-04-03 22:52:27 +02:00
{!isInPostPage && (
<div className={styles.postLink}>
<span className={styles.info}>
{replyCount} Replies
{linkCount > 0 && ` / ${linkCount} Links`}
</span>
<Link to={`/p/${subplebbitAddress}/c/${cid}`} className='button'>
{t('view_thread')}
</Link>
</div>
)}
</div>
2024-03-30 22:19:49 +01:00
{!pinned &&
2024-04-03 16:24:57 +02:00
replies?.slice(0, showAllReplies ? replies.length : 5).map((reply, index) => (
2024-03-30 22:19:49 +01:00
<div key={reply.cid} className={styles.replyContainer}>
<ReplyMobile index={index} reply={reply} />
</div>
))}
</div>
2024-03-22 15:31:02 +01:00
</div>
);
};
2024-04-02 17:00:36 +02:00
interface PostProps {
index?: number;
post: Comment;
showAllReplies?: boolean;
}
const Post = ({ post, showAllReplies = false }: PostProps) => {
return (
<div className={styles.thread}>
<div className={styles.postContainer}>
2024-04-02 17:00:36 +02:00
<PostDesktop post={post} showAllReplies={showAllReplies} />
<PostMobile post={post} showAllReplies={showAllReplies} />
</div>
</div>
);
};
2024-03-22 15:31:02 +01:00
export default Post;