From 82fe23897992f31eb28848bb2831c01d02a33912 Mon Sep 17 00:00:00 2001 From: "plebeius.eth" Date: Fri, 17 May 2024 15:29:03 +0200 Subject: [PATCH] feat(multisubs): add subplebbit address in post info --- .../board-buttons/board-buttons.tsx | 8 +-- src/components/post-form/post-form.tsx | 16 +++--- .../post/post-desktop/post-desktop.tsx | 54 +++++++++++++------ .../post/post-mobile/post-mobile.tsx | 41 ++++++++++---- src/components/post/post.tsx | 27 +--------- 5 files changed, 83 insertions(+), 63 deletions(-) diff --git a/src/components/board-buttons/board-buttons.tsx b/src/components/board-buttons/board-buttons.tsx index b9e8ee84..3e1078e8 100644 --- a/src/components/board-buttons/board-buttons.tsx +++ b/src/components/board-buttons/board-buttons.tsx @@ -57,13 +57,13 @@ export const MobileBoardButtons = () => { const location = useLocation(); const accountComment = useAccountComment({ commentIndex: params?.accountCommentIndex as any }); const subplebbitAddress = params?.subplebbitAddress || accountComment?.subplebbitAddress; - const isInPostPage = isPostPageView(location.pathname, params); + const isInPostView = isPostPageView(location.pathname, params); const isInCatalogView = isCatalogView(location.pathname, params); const isInPendingPostPage = isPendingPostView(location.pathname, params); return (
- {isInPostPage || isInPendingPostPage ? ( + {isInPostView || isInPendingPostPage ? (
@@ -92,13 +92,13 @@ export const DesktopBoardButtons = () => { const isInCatalogView = isCatalogView(location.pathname, params); const isInAllView = isAllView(location.pathname); const isInPendingPostPage = isPendingPostView(location.pathname, params); - const isInPostPage = isPostPageView(location.pathname, params); + const isInPostView = isPostPageView(location.pathname, params); const isInSubscriptionsView = isSubscriptionsView(location.pathname); return (

- {isInPostPage || isInPendingPostPage ? ( + {isInPostView || isInPendingPostPage ? ( <> [] [] [] [ ] diff --git a/src/components/post-form/post-form.tsx b/src/components/post-form/post-form.tsx index 18ba1ad5..85197f08 100644 --- a/src/components/post-form/post-form.tsx +++ b/src/components/post-form/post-form.tsx @@ -122,7 +122,7 @@ const PostFormTable = ({ closeForm }: { closeForm: () => void }) => { }, [index, resetSubmitStore, navigate]); // in post page, publish a reply to the post - const isInPostPage = isPostPageView(location.pathname, params); + const isInPostView = isPostPageView(location.pathname, params); const cid = params?.commentCid as string; const { setContent, resetContent, replyIndex, publishReply } = useReply({ cid, subplebbitAddress }); @@ -164,10 +164,10 @@ const PostFormTable = ({ closeForm }: { closeForm: () => void }) => { defaultValue={displayName || undefined} onChange={(e) => setAccount({ ...account, author: { ...account?.author, displayName: e.target.value } })} /> - {isInPostPage && } + {isInPostView && } - {!isInPostPage && ( + {!isInPostView && ( {t('subject')} @@ -192,7 +192,7 @@ const PostFormTable = ({ closeForm }: { closeForm: () => void }) => { ref={textRef} onChange={(e) => { const content = e.target.value.replace(/\n/g, '\n\n'); - isInPostPage ? setContent.content(content) : setSubmitStore({ content }); + isInPostView ? setContent.content(content) : setSubmitStore({ content }); }} /> @@ -208,7 +208,7 @@ const PostFormTable = ({ closeForm }: { closeForm: () => void }) => { ref={urlRef} onChange={(e) => { setUrl(e.target.value); - isInPostPage ? setContent.link(e.target.value) : setSubmitStore({ link: e.target.value }); + isInPostView ? setContent.link(e.target.value) : setSubmitStore({ link: e.target.value }); }} /> @@ -252,7 +252,7 @@ const PostForm = () => { const location = useLocation(); const params = useParams(); const isInDescriptionView = isDescriptionView(location.pathname, params); - const isInPostPage = isPostPageView(location.pathname, params); + const isInPostView = isPostPageView(location.pathname, params); const isInRulesView = isRulesView(location.pathname, params); const comment = useComment({ commentCid: useParams().commentCid }); @@ -274,7 +274,7 @@ const PostForm = () => {
[ ]
@@ -292,7 +292,7 @@ const PostForm = () => { ) : ( <> {showForm && setShowForm(false)} />} diff --git a/src/components/post/post-desktop/post-desktop.tsx b/src/components/post/post-desktop/post-desktop.tsx index f2c8eb5c..66364455 100644 --- a/src/components/post/post-desktop/post-desktop.tsx +++ b/src/components/post/post-desktop/post-desktop.tsx @@ -1,10 +1,12 @@ import { useEffect, useRef, useState } from 'react'; import { Trans, useTranslation } from 'react-i18next'; +import { useLocation, useParams } from 'react-router-dom'; import { Link } from 'react-router-dom'; import { useAccount } from '@plebbit/plebbit-react-hooks'; import Plebbit from '@plebbit/plebbit-js/dist/browser/index.js'; import { getCommentMediaInfo, getDisplayMediaInfoType, getHasThumbnail } from '../../../lib/utils/media-utils'; import { getFormattedDate } from '../../../lib/utils/time-utils'; +import { isAllView, isPendingPostView, isPostPageView, isSubscriptionsView } from '../../../lib/utils/view-utils'; import useCountLinksInReplies from '../../../hooks/use-count-links-in-replies'; import useReplies from '../../../hooks/use-replies'; import useStateString from '../../../hooks/use-state-string'; @@ -16,16 +18,20 @@ import { PostProps } from '../post'; import styles from '../post.module.css'; import _ from 'lodash'; -const PostInfo = ({ isInPostPage, openReplyModal, post, roles }: PostProps) => { +const PostInfo = ({ openReplyModal, post, roles }: PostProps) => { const { t } = useTranslation(); const { author, cid, locked, pinned, parentCid, postCid, shortCid, state, subplebbitAddress, timestamp, title } = post || {}; const { address, displayName, shortAddress } = author || {}; const { isDescription, isRules } = post || {}; // custom properties, not from api - const stateString = useStateString(post); - const isReply = parentCid; + const params = useParams(); + const location = useLocation(); + const isInAllView = isAllView(location.pathname); + const isInPostView = isPostPageView(location.pathname, params); + const isInSubscriptionsView = isSubscriptionsView(location.pathname); + const shortDisplayName = displayName?.trim().length > 20 ? displayName?.trim().slice(0, 20).trim() + '...' : displayName?.trim(); const authorRole = roles?.[address]?.role; const displayTitle = title && title.length > 75 ? title?.slice(0, 75) + '...' : title; @@ -90,6 +96,12 @@ const PostInfo = ({ isInPostPage, openReplyModal, post, roles }: PostProps) => { )}
)} + {subplebbitAddress && (isInAllView || isInSubscriptionsView) && ( + + {' '} + p/{subplebbitAddress && Plebbit.getShortAddress(subplebbitAddress)} + + )} {pinned && ( @@ -100,7 +112,7 @@ const PostInfo = ({ isInPostPage, openReplyModal, post, roles }: PostProps) => { )} - {!isInPostPage && !isReply && ( + {!isInPostView && !isReply && ( [{_.capitalize(t('reply'))}] @@ -171,9 +183,14 @@ const PostMedia = ({ post }: PostProps) => { ); }; -const PostMessage = ({ isInPostPage, post }: PostProps) => { +const PostMessage = ({ post }: PostProps) => { const { cid, content, parentCid, postCid, state, subplebbitAddress } = post || {}; - const displayContent = content && !isInPostPage && content.length > 1000 ? content?.slice(0, 1000) + '(...)' : content; + + const params = useParams(); + const location = useLocation(); + const isInPostView = isPostPageView(location.pathname, params); + + const displayContent = content && !isInPostView && content.length > 1000 ? content?.slice(0, 1000) + '(...)' : content; const isReply = parentCid; const isReplyingToReply = postCid !== parentCid; @@ -195,7 +212,7 @@ const PostMessage = ({ isInPostPage, post }: PostProps) => { )} - {!isReply && content.length > 1000 && !isInPostPage && ( + {!isReply && content.length > 1000 && !isInPostView && (
}} /> @@ -211,10 +228,15 @@ const PostMessage = ({ isInPostPage, post }: PostProps) => { ); }; -const PostDesktop = ({ isInPostPage, isPendingPostPage, openReplyModal, post, roles, showAllReplies }: PostProps) => { +const PostDesktop = ({ openReplyModal, post, roles, showAllReplies }: PostProps) => { const { cid, content, link, pinned, replyCount, subplebbitAddress } = post || {}; const { isDescription, isRules } = post || {}; // custom properties, not from api + const params = useParams(); + const location = useLocation(); + const isInPendingPostView = isPendingPostView(location.pathname, params); + const isInPostView = isPostPageView(location.pathname, params); + const replies = useReplies(post); const visiblelinksCount = useCountLinksInReplies(post, 5); const totallinksCount = useCountLinksInReplies(post); @@ -226,16 +248,16 @@ const PostDesktop = ({ isInPostPage, isPendingPostPage, openReplyModal, post, ro

- {!isInPostPage && ( + {!isInPostView && ( )} {link && } - + {!content &&
} - {content && } - {!isDescription && !isRules && !isPendingPostPage && (replies.length > 5 || (pinned && replies.length > 0)) && !isInPostPage && ( + {content && } + {!isDescription && !isRules && !isInPendingPostView && (replies.length > 5 || (pinned && replies.length > 0)) && !isInPostView && ( @@ -252,8 +274,8 @@ const PostDesktop = ({ isInPostPage, isPendingPostPage, openReplyModal, post, ro )} )} - {!(pinned && !isInPostPage) && - !isPendingPostPage && + {!(pinned && !isInPostView) && + !isInPendingPostView && !isDescription && !isRules && replies && @@ -262,9 +284,9 @@ const PostDesktop = ({ isInPostPage, isPendingPostPage, openReplyModal, post, ro
{'>>'}
- + {reply.link && } - {reply.content && } + {reply.content && }
diff --git a/src/components/post/post-mobile/post-mobile.tsx b/src/components/post/post-mobile/post-mobile.tsx index 8a738e1f..891abfff 100644 --- a/src/components/post/post-mobile/post-mobile.tsx +++ b/src/components/post/post-mobile/post-mobile.tsx @@ -1,10 +1,11 @@ import { useState } from 'react'; import { Trans, useTranslation } from 'react-i18next'; -import { Link } from 'react-router-dom'; +import { Link, useLocation, useParams } from 'react-router-dom'; import { useAccount } from '@plebbit/plebbit-react-hooks'; import Plebbit from '@plebbit/plebbit-js/dist/browser/index.js'; import { getCommentMediaInfo, getHasThumbnail } from '../../../lib/utils/media-utils'; import { getFormattedDate } from '../../../lib/utils/time-utils'; +import { isAllView, isPendingPostView, isPostPageView, isSubscriptionsView } from '../../../lib/utils/view-utils'; import useCountLinksInReplies from '../../../hooks/use-count-links-in-replies'; import useReplies from '../../../hooks/use-replies'; import useStateString from '../../../hooks/use-state-string'; @@ -21,6 +22,10 @@ const PostInfoAndMedia = ({ openReplyModal, post, roles }: PostProps) => { const { isDescription, isRules } = post || {}; // custom properties, not from api const { address, displayName, shortAddress } = author || {}; + const location = useLocation(); + const isInAllView = isAllView(location.pathname); + const isInSubscriptionsView = isSubscriptionsView(location.pathname); + const authorRole = roles?.[address]?.role; const shortDisplayName = displayName?.trim().length > 20 ? displayName?.trim().slice(0, 20).trim() + '...' : displayName?.trim(); const displayTitle = title && title.length > 30 ? title?.slice(0, 30) + '(...)' : title; @@ -67,6 +72,12 @@ const PostInfoAndMedia = ({ openReplyModal, post, roles }: PostProps) => { )}
+ {subplebbitAddress && (isInAllView || isInSubscriptionsView) && ( +
+ {' '} + p/{subplebbitAddress && Plebbit.getShortAddress(subplebbitAddress)} +
+ )} {getFormattedDate(timestamp)}{' '} {!(isDescription || isRules) && ( @@ -99,9 +110,14 @@ const PostInfoAndMedia = ({ openReplyModal, post, roles }: PostProps) => { ); }; -const PostMessageMobile = ({ isInPostPage, post }: PostProps) => { +const PostMessageMobile = ({ post }: PostProps) => { const { cid, content, parentCid, postCid, state, subplebbitAddress } = post || {}; - const displayContent = content && !isInPostPage && content.length > 1000 ? content?.slice(0, 1000) : content; + + const params = useParams(); + const location = useLocation(); + const isInPostView = isPostPageView(location.pathname, params); + + const displayContent = content && !isInPostView && content.length > 1000 ? content?.slice(0, 1000) : content; const isReply = parentCid; const isReplyingToReply = postCid !== parentCid; @@ -124,7 +140,7 @@ const PostMessageMobile = ({ isInPostPage, post }: PostProps) => { )} - {!isReply && content.length > 1000 && !isInPostPage && ( + {!isReply && content.length > 1000 && !isInPostView && (
}} /> @@ -141,11 +157,16 @@ const PostMessageMobile = ({ isInPostPage, post }: PostProps) => { ); }; -const PostMobile = ({ isInPostPage, isPendingPostPage, openReplyModal, post, roles, showAllReplies }: PostProps) => { +const PostMobile = ({ openReplyModal, post, roles, showAllReplies }: PostProps) => { const { t } = useTranslation(); const { cid, content, pinned, replyCount, subplebbitAddress } = post || {}; const { isDescription, isRules } = post || {}; // custom properties, not from api + const params = useParams(); + const location = useLocation(); + const isInPendingPostView = isPendingPostView(location.pathname, params); + const isInPostView = isPostPageView(location.pathname, params); + const linksCount = useCountLinksInReplies(post); const replies = useReplies(post); @@ -159,9 +180,9 @@ const PostMobile = ({ isInPostPage, isPendingPostPage, openReplyModal, post, rol
- {content && } + {content && }
- {!isInPostPage && !isPendingPostPage && ( + {!isInPostView && !isInPendingPostView && (
{replyCount > 0 && `${replyCount} Replies`} @@ -173,8 +194,8 @@ const PostMobile = ({ isInPostPage, isPendingPostPage, openReplyModal, post, rol
)}
- {!(pinned && !isInPostPage) && - !isPendingPostPage && + {!(pinned && !isInPostView) && + !isInPendingPostView && !isDescription && !isRules && replies && @@ -184,7 +205,7 @@ const PostMobile = ({ isInPostPage, isPendingPostPage, openReplyModal, post, rol
- {content && } + {content && }
diff --git a/src/components/post/post.tsx b/src/components/post/post.tsx index 7dc022ce..ec65fdaa 100644 --- a/src/components/post/post.tsx +++ b/src/components/post/post.tsx @@ -1,6 +1,4 @@ -import { useLocation, useParams } from 'react-router-dom'; import { Role, useSubplebbit } from '@plebbit/plebbit-react-hooks'; -import { isPendingPostView, isPostPageView } from '../../lib/utils/view-utils'; import useWindowWidth from '../../hooks/use-window-width'; import styles from './post.module.css'; import PostDesktop from './post-desktop'; @@ -8,8 +6,6 @@ import PostMobile from './post-mobile'; export interface PostProps { index?: number; - isInPostPage?: boolean; - isPendingPostPage?: boolean; post?: any; reply?: any; roles?: Role[]; @@ -21,32 +17,13 @@ const Post = ({ post, showAllReplies = false, openReplyModal }: PostProps) => { const subplebbit = useSubplebbit({ subplebbitAddress: post?.subplebbitAddress }); const isMobile = useWindowWidth() < 640; - const params = useParams(); - const location = useLocation(); - const isInPostPage = isPostPageView(location.pathname, params); - const isPendingPostPage = isPendingPostView(location.pathname, params); - return (
{isMobile ? ( - + ) : ( - + )}