2024-04-04 11:41:17 +02:00
|
|
|
import { useEffect } from 'react';
|
2024-04-29 12:35:37 +02:00
|
|
|
import { useTranslation } from 'react-i18next';
|
2024-07-02 15:39:20 +02:00
|
|
|
import { Comment, Role, useComment, useEditedComment, useSubplebbit } from '@plebbit/plebbit-react-hooks';
|
2025-03-05 17:10:26 +01:00
|
|
|
import useSubplebbitsStore from '@plebbit/plebbit-react-hooks/dist/stores/subplebbits';
|
2024-04-08 17:32:12 +02:00
|
|
|
import { useLocation, useParams } from 'react-router-dom';
|
2025-03-05 12:06:27 +01:00
|
|
|
import { isAllView, isDescriptionView, isRulesView } from '../../lib/utils/view-utils';
|
2024-06-11 15:49:26 +02:00
|
|
|
import useIsMobile from '../../hooks/use-is-mobile';
|
|
|
|
|
import PostDesktop from '../../components/post-desktop';
|
|
|
|
|
import PostMobile from '../../components/post-mobile';
|
2024-04-09 15:03:26 +02:00
|
|
|
import SubplebbitDescription from '../../components/subplebbit-description';
|
|
|
|
|
import SubplebbitRules from '../../components/subplebbit-rules';
|
2024-06-11 15:49:26 +02:00
|
|
|
import styles from './post.module.css';
|
|
|
|
|
export interface PostProps {
|
|
|
|
|
index?: number;
|
|
|
|
|
isHidden?: boolean;
|
2024-12-24 17:13:12 +01:00
|
|
|
hasThumbnail?: boolean;
|
2024-06-11 15:49:26 +02:00
|
|
|
post?: any;
|
2024-07-01 10:26:38 +02:00
|
|
|
postReplyCount?: number;
|
2024-06-11 15:49:26 +02:00
|
|
|
reply?: any;
|
|
|
|
|
roles?: Role[];
|
|
|
|
|
showAllReplies?: boolean;
|
|
|
|
|
showReplies?: boolean;
|
2025-03-05 17:10:26 +01:00
|
|
|
replies?: Comment[];
|
2024-06-11 15:49:26 +02:00
|
|
|
}
|
|
|
|
|
|
2025-03-05 17:10:26 +01:00
|
|
|
export const Post = ({ post, showAllReplies = false, showReplies = true, replies }: PostProps) => {
|
|
|
|
|
const subplebbit = useSubplebbitsStore((state) => state.subplebbits[post?.subplebbitAddress]);
|
2024-06-11 15:49:26 +02:00
|
|
|
const isMobile = useIsMobile();
|
|
|
|
|
|
2024-06-21 22:02:56 +02:00
|
|
|
let comment = post;
|
|
|
|
|
|
|
|
|
|
// handle pending mod or author edit
|
|
|
|
|
const { editedComment } = useEditedComment({ comment });
|
|
|
|
|
if (editedComment) {
|
|
|
|
|
comment = editedComment;
|
|
|
|
|
}
|
|
|
|
|
|
2024-06-11 15:49:26 +02:00
|
|
|
return (
|
|
|
|
|
<div className={styles.thread}>
|
|
|
|
|
<div className={styles.postContainer}>
|
|
|
|
|
{isMobile ? (
|
2025-03-05 17:10:26 +01:00
|
|
|
<PostMobile post={comment} roles={subplebbit?.roles} showAllReplies={showAllReplies} showReplies={showReplies} replies={replies} />
|
2024-06-11 15:49:26 +02:00
|
|
|
) : (
|
2025-03-05 17:10:26 +01:00
|
|
|
<PostDesktop post={comment} roles={subplebbit?.roles} showAllReplies={showAllReplies} showReplies={showReplies} replies={replies} />
|
2024-06-11 15:49:26 +02:00
|
|
|
)}
|
|
|
|
|
</div>
|
|
|
|
|
</div>
|
|
|
|
|
);
|
|
|
|
|
};
|
2024-04-02 17:00:36 +02:00
|
|
|
|
|
|
|
|
const PostPage = () => {
|
2024-04-29 12:35:37 +02:00
|
|
|
const { t } = useTranslation();
|
2024-04-02 17:00:36 +02:00
|
|
|
const params = useParams();
|
2024-05-09 16:04:24 +02:00
|
|
|
const location = useLocation();
|
2024-04-08 17:32:12 +02:00
|
|
|
const { commentCid, subplebbitAddress } = params;
|
2024-10-29 17:40:09 +01:00
|
|
|
const isInAllView = isAllView(location.pathname);
|
2024-05-09 16:04:24 +02:00
|
|
|
const isInDescriptionView = isDescriptionView(location.pathname, params);
|
|
|
|
|
const isInRulesView = isRulesView(location.pathname, params);
|
2024-04-08 17:32:12 +02:00
|
|
|
|
|
|
|
|
const subplebbit = useSubplebbit({ subplebbitAddress });
|
2024-04-29 14:53:46 +02:00
|
|
|
const { createdAt, description, rules, shortAddress, suggested, title } = subplebbit;
|
2024-04-08 17:32:12 +02:00
|
|
|
|
2024-06-15 12:37:45 +02:00
|
|
|
const comment = useComment({ commentCid });
|
|
|
|
|
|
|
|
|
|
// if the comment is a reply, return the post comment instead, then the reply will be highlighted in the thread
|
2024-06-04 21:35:22 +02:00
|
|
|
const postComment = useComment({ commentCid: comment?.postCid });
|
2024-07-02 15:39:20 +02:00
|
|
|
let post: Comment;
|
2024-06-04 21:35:22 +02:00
|
|
|
if (comment.parentCid) {
|
|
|
|
|
post = postComment;
|
|
|
|
|
} else {
|
|
|
|
|
post = comment;
|
|
|
|
|
}
|
2024-06-15 12:37:45 +02:00
|
|
|
|
2025-03-02 12:10:16 +01:00
|
|
|
const { error, replyCount } = post || {};
|
2024-04-02 17:00:36 +02:00
|
|
|
|
2024-04-04 11:41:17 +02:00
|
|
|
useEffect(() => {
|
|
|
|
|
window.scrollTo(0, 0);
|
|
|
|
|
}, []);
|
|
|
|
|
|
2024-07-02 15:39:20 +02:00
|
|
|
useEffect(() => {
|
|
|
|
|
const boardTitle = title ? title : shortAddress || subplebbitAddress;
|
|
|
|
|
const postTitle = post?.title?.slice(0, 30) || post?.content?.slice(0, 30);
|
2024-09-18 14:23:07 +02:00
|
|
|
const postDucumentTitle = (postTitle ? postTitle.trim() + '... - ' : '') + boardTitle + ' - plebchan';
|
|
|
|
|
document.title = isInAllView ? `${t('all')} - plebchan` : postDucumentTitle;
|
|
|
|
|
}, [title, shortAddress, subplebbitAddress, post?.title, post?.content, isInAllView, t]);
|
2024-07-02 15:39:20 +02:00
|
|
|
|
2024-04-02 17:00:36 +02:00
|
|
|
return (
|
|
|
|
|
<div className={styles.content}>
|
2024-07-07 19:12:12 +02:00
|
|
|
{/* TODO: remove this replyCount error once api supports scrolling replies pages */}
|
|
|
|
|
{replyCount > 60 && <span className={styles.error}>Error: this thread has too many replies, some of them cannot be displayed right now.</span>}
|
2024-11-21 17:23:14 +01:00
|
|
|
{error && <span className={styles.error}>Error: {error?.message || error?.toString?.()}</span>}
|
2024-04-08 17:32:12 +02:00
|
|
|
{isInDescriptionView ? (
|
2024-04-29 14:53:46 +02:00
|
|
|
<SubplebbitDescription
|
|
|
|
|
avatarUrl={suggested?.avatarUrl}
|
|
|
|
|
createdAt={createdAt}
|
|
|
|
|
description={description}
|
2024-09-18 12:37:39 +02:00
|
|
|
replyCount={location.pathname.startsWith('/p/all/') ? 0 : rules?.length > 0 ? 1 : 0}
|
2024-04-29 14:53:46 +02:00
|
|
|
subplebbitAddress={subplebbitAddress}
|
|
|
|
|
shortAddress={shortAddress}
|
|
|
|
|
title={title}
|
|
|
|
|
/>
|
2024-04-08 17:32:12 +02:00
|
|
|
) : isInRulesView ? (
|
2024-04-09 15:03:26 +02:00
|
|
|
<SubplebbitRules createdAt={createdAt} rules={rules} subplebbitAddress={subplebbitAddress} />
|
2024-04-08 17:32:12 +02:00
|
|
|
) : (
|
2025-03-05 12:01:48 +01:00
|
|
|
<Post post={post} showAllReplies={true} />
|
2024-04-08 17:32:12 +02:00
|
|
|
)}
|
2024-04-02 17:00:36 +02:00
|
|
|
</div>
|
|
|
|
|
);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
export default PostPage;
|