diff --git a/src/components/post-desktop/post-desktop.tsx b/src/components/post-desktop/post-desktop.tsx index a1b536c5..11e521fe 100644 --- a/src/components/post-desktop/post-desktop.tsx +++ b/src/components/post-desktop/post-desktop.tsx @@ -16,6 +16,7 @@ import useAuthorAddressClick from '../../hooks/use-author-address-click'; import useCountLinksInReplies from '../../hooks/use-count-links-in-replies'; import useFetchGifFirstFrame from '../../hooks/use-fetch-gif-first-frame'; import useHide from '../../hooks/use-hide'; +import usePostCidForPendingPost from '../../hooks/use-post-cid'; import useReplies from '../../hooks/use-replies'; import useStateString from '../../hooks/use-state-string'; import CommentMedia from '../comment-media'; @@ -70,9 +71,11 @@ const PostInfo = ({ openReplyModal, post, postReplyCount = 0, roles, isHidden }: // comment.author.shortAddress is undefined while the comment publishing state is pending, use account instead // in anon mode, use the newly generated signer.address instead, which will be comment.author.address const { anonMode } = useAnonMode(); - const { currentAnonSignerAddress } = useAnonModeStore(); + const { getThreadSigner, currentAnonSignerAddress } = useAnonModeStore(); + const postCidForSigner = usePostCidForPendingPost(parentCid); + const anonSignerAddress = postCidForSigner ? getThreadSigner(postCidForSigner)?.address || currentAnonSignerAddress : null; const account = useAccount(); - const pendingShortAddress = anonMode ? currentAnonSignerAddress && Plebbit.getShortAddress(currentAnonSignerAddress) : account?.author?.shortAddress; + const pendingShortAddress = anonMode ? anonSignerAddress && Plebbit.getShortAddress(anonSignerAddress) : account?.author?.shortAddress; const handleUserAddressClick = useAuthorAddressClick(); const numberOfPostsByAuthor = document.querySelectorAll(`[data-author-address="${shortAddress}"][data-post-cid="${postCid}"]`).length; diff --git a/src/components/post-mobile/post-mobile.tsx b/src/components/post-mobile/post-mobile.tsx index fbe1b93d..660084b7 100644 --- a/src/components/post-mobile/post-mobile.tsx +++ b/src/components/post-mobile/post-mobile.tsx @@ -14,6 +14,7 @@ import useAnonMode from '../../hooks/use-anon-mode'; import useAuthorAddressClick from '../../hooks/use-author-address-click'; import useCountLinksInReplies from '../../hooks/use-count-links-in-replies'; import useHide from '../../hooks/use-hide'; +import usePostCidForPendingPost from '../../hooks/use-post-cid'; import useReplies from '../../hooks/use-replies'; import useStateString from '../../hooks/use-state-string'; import CommentMedia from '../comment-media'; @@ -51,9 +52,11 @@ const PostInfoAndMedia = ({ openReplyModal, post, postReplyCount = 0, roles }: P // comment.author.shortAddress is undefined while the comment publishing state is pending, use account instead // in anon mode, use the newly generated signer.address instead, which will be comment.author.address const { anonMode } = useAnonMode(); - const { currentAnonSignerAddress } = useAnonModeStore(); + const { getThreadSigner, currentAnonSignerAddress } = useAnonModeStore(); + const postCidForSigner = usePostCidForPendingPost(parentCid); + const anonSignerAddress = postCidForSigner ? getThreadSigner(postCidForSigner)?.address || currentAnonSignerAddress : null; const account = useAccount(); - const pendingShortAddress = anonMode ? currentAnonSignerAddress && Plebbit.getShortAddress(currentAnonSignerAddress) : account?.author?.shortAddress; + const pendingShortAddress = anonMode ? anonSignerAddress && Plebbit.getShortAddress(anonSignerAddress) : account?.author?.shortAddress; const stateString = useStateString(post); diff --git a/src/hooks/use-post-cid.ts b/src/hooks/use-post-cid.ts new file mode 100644 index 00000000..075d1808 --- /dev/null +++ b/src/hooks/use-post-cid.ts @@ -0,0 +1,43 @@ +import { useEffect, useReducer } from 'react'; +import { useComment } from '@plebbit/plebbit-react-hooks'; + +type State = { + resolvedPostCid: string | null; + currentParentCid: string | undefined; +}; + +type Action = { type: 'SET_POST_CID'; payload: string } | { type: 'SET_PARENT_CID'; payload: string }; + +const reducer = (state: State, action: Action): State => { + switch (action.type) { + case 'SET_POST_CID': + return { ...state, resolvedPostCid: action.payload }; + case 'SET_PARENT_CID': + return { ...state, currentParentCid: action.payload }; + default: + return state; + } +}; + +const usePostCidForPendingPost = (initialParentCid: string | undefined): string | null => { + const [state, dispatch] = useReducer(reducer, { + resolvedPostCid: null, + currentParentCid: initialParentCid, + }); + + const comment = useComment({ commentCid: state.currentParentCid }); + + useEffect(() => { + if (comment) { + if (comment.postCid) { + dispatch({ type: 'SET_POST_CID', payload: comment.postCid }); + } else if (comment.parentCid) { + dispatch({ type: 'SET_PARENT_CID', payload: comment.parentCid }); + } + } + }, [comment]); + + return state.resolvedPostCid; +}; + +export default usePostCidForPendingPost;