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-06-11 15:49:26 +02:00
|
|
|
import { Comment, Role, useComment, useEditedComment, useSubplebbit } from '@plebbit/plebbit-react-hooks';
|
2024-04-08 17:32:12 +02:00
|
|
|
import { useLocation, useParams } from 'react-router-dom';
|
2024-05-31 19:59:08 +02:00
|
|
|
import { isDescriptionView, isRulesView, isSettingsView } from '../../lib/utils/view-utils';
|
2024-06-11 15:49:26 +02:00
|
|
|
import useIsMobile from '../../hooks/use-is-mobile';
|
2024-04-29 12:35:37 +02:00
|
|
|
import useReplyModal from '../../hooks/use-reply-modal';
|
2024-06-11 15:49:26 +02:00
|
|
|
import PostDesktop from '../../components/post-desktop';
|
|
|
|
|
import PostMobile from '../../components/post-mobile';
|
2024-04-29 12:35:37 +02:00
|
|
|
import ReplyModal from '../../components/reply-modal';
|
2024-05-09 16:04:24 +02:00
|
|
|
import SettingsModal from '../../components/settings-modal';
|
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;
|
|
|
|
|
post?: any;
|
|
|
|
|
reply?: any;
|
|
|
|
|
roles?: Role[];
|
|
|
|
|
showAllReplies?: boolean;
|
|
|
|
|
showReplies?: boolean;
|
|
|
|
|
openReplyModal?: (cid: string) => void;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export const Post = ({ post, showAllReplies = false, showReplies = true, openReplyModal }: PostProps) => {
|
|
|
|
|
const subplebbit = useSubplebbit({ subplebbitAddress: post?.subplebbitAddress });
|
|
|
|
|
const isMobile = useIsMobile();
|
|
|
|
|
|
|
|
|
|
return (
|
|
|
|
|
<div className={styles.thread}>
|
|
|
|
|
<div className={styles.postContainer}>
|
|
|
|
|
{isMobile ? (
|
|
|
|
|
<PostMobile post={post} roles={subplebbit?.roles} showAllReplies={showAllReplies} showReplies={showReplies} openReplyModal={openReplyModal} />
|
|
|
|
|
) : (
|
|
|
|
|
<PostDesktop post={post} roles={subplebbit?.roles} showAllReplies={showAllReplies} showReplies={showReplies} openReplyModal={openReplyModal} />
|
|
|
|
|
)}
|
|
|
|
|
</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-05-31 19:59:08 +02:00
|
|
|
const isInSettigsView = isSettingsView(location.pathname, params);
|
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-04-30 11:08:01 +02:00
|
|
|
const { activeCid, closeModal, openReplyModal, showReplyModal, scrollY } = useReplyModal();
|
2024-04-29 12:35:37 +02:00
|
|
|
|
2024-06-04 21:35:22 +02:00
|
|
|
const comment: Comment = useComment({ commentCid });
|
|
|
|
|
// if the comment is a reply, get the parent comment
|
|
|
|
|
const postComment = useComment({ commentCid: comment?.postCid });
|
|
|
|
|
let post;
|
|
|
|
|
if (comment.parentCid) {
|
|
|
|
|
post = postComment;
|
|
|
|
|
} else {
|
|
|
|
|
post = comment;
|
|
|
|
|
}
|
|
|
|
|
// handle pending mod or author edit
|
|
|
|
|
const { editedComment } = useEditedComment({ comment: post });
|
|
|
|
|
if (editedComment) {
|
|
|
|
|
post = editedComment;
|
|
|
|
|
}
|
|
|
|
|
|
2024-04-29 12:35:37 +02:00
|
|
|
const { deleted, locked, removed } = post || {};
|
|
|
|
|
const isThreadClosed = deleted || locked || removed;
|
2024-04-02 17:00:36 +02:00
|
|
|
|
2024-04-04 11:41:17 +02:00
|
|
|
useEffect(() => {
|
|
|
|
|
window.scrollTo(0, 0);
|
|
|
|
|
}, []);
|
|
|
|
|
|
2024-04-02 17:00:36 +02:00
|
|
|
return (
|
|
|
|
|
<div className={styles.content}>
|
2024-05-31 19:59:08 +02:00
|
|
|
{isInSettigsView && <SettingsModal />}
|
2024-04-30 11:08:01 +02:00
|
|
|
{showReplyModal && activeCid && <ReplyModal closeModal={closeModal} parentCid={activeCid} scrollY={scrollY} />}
|
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}
|
|
|
|
|
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
|
|
|
) : (
|
2024-04-29 12:35:37 +02:00
|
|
|
<Post post={post} showAllReplies={true} openReplyModal={isThreadClosed ? () => alert(t('thread_closed_alert')) : openReplyModal} />
|
2024-04-08 17:32:12 +02:00
|
|
|
)}
|
2024-04-02 17:00:36 +02:00
|
|
|
</div>
|
|
|
|
|
);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
export default PostPage;
|