diff --git a/src/components/comment-content/comment-content.tsx b/src/components/comment-content/comment-content.tsx new file mode 100644 index 00000000..776524ef --- /dev/null +++ b/src/components/comment-content/comment-content.tsx @@ -0,0 +1,133 @@ +import { useState } from 'react'; +import { useLocation, useParams } from 'react-router-dom'; +import { Trans, useTranslation } from 'react-i18next'; +import { getFormattedDate, getFormattedTimeAgo } from '../../lib/utils/time-utils'; +import { isPostPageView } from '../../lib/utils/view-utils'; +import useIsMobile from '../../hooks/use-is-mobile'; +import useStateString from '../../hooks/use-state-string'; +import LoadingEllipsis from '../../components/loading-ellipsis'; +import ReplyQuotePreview from '../../components/reply-quote-preview'; +import Markdown from '../../components/markdown'; +import Tooltip from '../../components/tooltip'; +import { Comment, useComment } from '@plebbit/plebbit-react-hooks'; +import styles from '../../views/post/post.module.css'; +import _ from 'lodash'; + +const CommentContent = ({ comment: post }: { comment: Comment }) => { + const { t } = useTranslation(); + const params = useParams(); + const location = useLocation(); + const isInPostView = isPostPageView(location.pathname, params); + const [showOriginal, setShowOriginal] = useState(false); + const isMobile = useIsMobile(); + + // TODO: commentAuthor is not yet available outside of editedComment, wait for API to be updated + const { cid, content, deleted, edit, isRules, original, parentCid, postCid, reason, removed, state } = post || {}; + // const banned = !!post?.commentAuthor?.banExpiresAt; + + const [showFullComment, setShowFullComment] = useState(false); + const displayContent = + content && + (!isInPostView && content.length > 1000 && !showFullComment + ? content.slice(0, 1000) + : isInPostView && content.length > 2000 && !showFullComment + ? content.slice(0, 2000) + : content); + + const quotelinkReply = useComment({ commentCid: parentCid }); + const isReply = parentCid; + const isReplyingToReply = (postCid && postCid !== parentCid) || quotelinkReply?.postCid !== parentCid; + + const stateString = useStateString(post); + + const loadingString =
+ {isReply && state !== 'failed' && isReplyingToReply && !(deleted || removed) &&+ ); +}; + +export default CommentContent; diff --git a/src/components/comment-content/index.ts b/src/components/comment-content/index.ts new file mode 100644 index 00000000..a6db6043 --- /dev/null +++ b/src/components/comment-content/index.ts @@ -0,0 +1 @@ +export { default } from './comment-content'; diff --git a/src/components/post-desktop/post-desktop.tsx b/src/components/post-desktop/post-desktop.tsx index 4f136294..da4f2e32 100644 --- a/src/components/post-desktop/post-desktop.tsx +++ b/src/components/post-desktop/post-desktop.tsx @@ -1,7 +1,7 @@ import { useState } from 'react'; import { Trans, useTranslation } from 'react-i18next'; import { Link, useLocation, useParams } from 'react-router-dom'; -import { Comment, useAuthorAvatar, useComment, useEditedComment, useSubplebbit } from '@plebbit/plebbit-react-hooks'; +import { Comment, useAuthorAvatar, useEditedComment, useSubplebbit } from '@plebbit/plebbit-react-hooks'; import Plebbit from '@plebbit/plebbit-js/dist/browser/index.js'; import styles from '../../views/post/post.module.css'; import { getDisplayMediaInfoType, getHasThumbnail, getMediaDimensions } from '../../lib/utils/media-utils'; @@ -17,11 +17,11 @@ import useFetchGifFirstFrame from '../../hooks/use-fetch-gif-first-frame'; import useHide from '../../hooks/use-hide'; import useReplies from '../../hooks/use-replies'; import useStateString from '../../hooks/use-state-string'; +import CommentContent from '../comment-content'; import CommentMedia from '../comment-media'; import EditMenu from '../edit-menu/edit-menu'; import { canEmbed } from '../embed'; import LoadingEllipsis from '../loading-ellipsis'; -import Markdown from '../markdown'; import PostMenuDesktop from './post-menu-desktop'; import ReplyQuotePreview from '../reply-quote-preview'; import Tooltip from '../tooltip'; @@ -285,120 +285,6 @@ const PostMediaContent = ({ post, hasThumbnail, spoiler, t }: { post: any; hasTh ); }; -const PostMessage = ({ post }: PostProps) => { - const { cid, content, deleted, edit, isRules, original, parentCid, postCid, reason, removed, state } = post || {}; - // TODO: commentAuthor is not yet available outside of editedComment, wait for API to be updated - // const banned = !!post?.commentAuthor?.banExpiresAt; - const { t } = useTranslation(); - const params = useParams(); - const location = useLocation(); - const isInPostView = isPostPageView(location.pathname, params); - const [showOriginal, setShowOriginal] = useState(false); - - const [showFullComment, setShowFullComment] = useState(false); - const displayContent = - content && - (!isInPostView && content.length > 1000 && !showFullComment - ? content.slice(0, 1000) - : isInPostView && content.length > 2000 && !showFullComment - ? content.slice(0, 2000) - : content); - - const quotelinkReply = useComment({ commentCid: parentCid }); - const isReply = parentCid; - const isReplyingToReply = (postCid && postCid !== parentCid) || quotelinkReply?.postCid !== parentCid; - - const stateString = useStateString(post); - - const loadingString = ( -} + {removed ? ( + reason ? ( + <> + ({t('this_post_was_removed')}) +
+
+ {`${_.capitalize(t('reason'))}: "${reason}"`} + > + ) : ( + {_.capitalize(t('this_post_was_removed'))}. + ) + ) : deleted ? ( + reason ? ( + <> + {t('user_deleted_this_post')}{' '} + {`${_.capitalize(t('reason'))}: "${reason}"`} + > + ) : ( + {t('user_deleted_this_post')} + ) + ) : ( + <> + {!showOriginal &&} + {((!isInPostView && content?.length > 1000 && !showFullComment) || (isInPostView && content?.length > 2000 && !showFullComment)) && ( + +
+
+setShowFullComment(true)} /> }} /> + + )} + {edit && original?.content !== content && ( + + {showOriginal &&} +
+
+>} /> }} + />{' '} + {reason && <>{t('reason_reason', { reason: reason, interpolation: { escapeValue: false } })} >} + {showOriginal ? ( + + )} + > + )} + {/* TODO: commentAuthor is not yet available outside of editedComment, wait for API to be updated */} + {/* {banned && ( + +setShowOriginal(!showOriginal)} /> }} + /> + ) : ( + setShowOriginal(!showOriginal)} /> }} + /> + )} +
+
++ + )} */} + {!cid && state === 'pending' && stateString !== 'Failed' && ( + <> +
+
+ {loadingString} + > + )} +
- {isReply && state !== 'failed' && isReplyingToReply && !(deleted || removed) &&- ); -}; - const Reply = ({ openReplyModal, postReplyCount, reply, roles }: PostProps) => { let post = reply; // handle pending mod or author edit @@ -420,7 +306,7 @@ const Reply = ({ openReplyModal, postReplyCount, reply, roles }: PostProps) => {} - {removed ? ( - reason ? ( - <> - ({t('this_post_was_removed')}) -
-
- {`${_.capitalize(t('reason'))}: "${reason}"`} - > - ) : ( - {_.capitalize(t('this_post_was_removed'))}. - ) - ) : deleted ? ( - reason ? ( - <> - {t('user_deleted_this_post')}{' '} - {`${_.capitalize(t('reason'))}: "${reason}"`} - > - ) : ( - {t('user_deleted_this_post')} - ) - ) : ( - <> - {!showOriginal &&} - {edit && original?.content !== post?.content && ( - - {showOriginal && } -
->} /> }} - />{' '} - {reason && <>{t('reason_reason', { reason: reason, interpolation: { escapeValue: false } })} >} - {showOriginal ? ( - - )} - > - )} - {/* TODO: commentAuthor is not yet available outside of editedComment, wait for API to be updated */} - {/* {banned && ( - -setShowOriginal(!showOriginal)} /> }} - /> - ) : ( - setShowOriginal(!showOriginal)} /> }} - /> - )} -
-
-- - )} */} - {((!isInPostView && content?.length > 1000 && !showFullComment) || (isInPostView && content?.length > 2000 && !showFullComment)) && ( - -
-setShowFullComment(true)} /> }} /> - - )} - {!cid && state === 'pending' && stateString !== 'Failed' && ( - <> -
- {loadingString} - > - )} -
- {isReply && !(removed || deleted) && state !== 'failed' && isReplyingToReply &&- ); -}; - const Reply = ({ openReplyModal, postReplyCount, reply, roles }: PostProps) => { let post = reply; // handle pending mod or author edit @@ -351,7 +236,7 @@ const Reply = ({ openReplyModal, postReplyCount, reply, roles }: PostProps) => { data-post-cid={postCid} >} - {removed ? ( - reason ? ( - <> - ({t('this_post_was_removed')}) -
-
- {`${_.capitalize(t('reason'))}: "${reason}"`} - > - ) : ( - {_.capitalize(t('this_post_was_removed'))}. - ) - ) : deleted ? ( - reason ? ( - <> - {t('user_deleted_this_post')}{' '} - {`${_.capitalize(t('reason'))}: "${reason}"`} - > - ) : ( - {t('user_deleted_this_post')} - ) - ) : ( - <> - {!showOriginal &&} - {edit && original?.content !== post?.content && ( - - {showOriginal && } -
->} /> }} - />{' '} - {reason && <>{t('reason_reason', { reason: reason, interpolation: { escapeValue: false } })} >} - {showOriginal ? ( - - )} - > - )} - {/* TODO: commentAuthor is not available outside of editedComment, update when available */} - {/* {banned && ( - -setShowOriginal(!showOriginal)} /> }} - /> - ) : ( - setShowOriginal(!showOriginal)} /> }} - /> - )} -
-
-- - )} */} - {((!isInPostView && content?.length > 1000 && !showFullComment) || (isInPostView && content?.length > 2000 && !showFullComment)) && ( - -
-setShowFullComment(true)} /> }} /> - - )} - {!cid && state === 'pending' && stateString !== 'Failed' && ( - <> -
- {loadingString} - > - )} -
}