diff --git a/src/components/post-form/post-form.tsx b/src/components/post-form/post-form.tsx index fb749158..14713720 100644 --- a/src/components/post-form/post-form.tsx +++ b/src/components/post-form/post-form.tsx @@ -142,8 +142,11 @@ const PostFormTable = ({ closeForm, postCid }: { closeForm: () => void; postCid: } }; - const getAnonAddressForPost = async () => { - if (anonMode) { + const hasCalledAnonAddressRef = useRef(false); + + const getAnonAddressForPost = useCallback(async () => { + if (anonMode && !hasCalledAnonAddressRef.current) { + hasCalledAnonAddressRef.current = true; const newSigner = (await getNewSigner()) || {}; setSubmitStore({ signer: newSigner, @@ -153,7 +156,7 @@ const PostFormTable = ({ closeForm, postCid }: { closeForm: () => void; postCid: }, }); } - }; + }, [anonMode, getNewSigner, setSubmitStore, displayName]); const onPublishPost = async () => { if (!title && !content && !link) { @@ -165,9 +168,7 @@ const PostFormTable = ({ closeForm, postCid }: { closeForm: () => void; postCid: return; } - getAnonAddressForPost().then(() => { - publishComment(); - }); + publishComment(); }; const params = useParams(); @@ -194,8 +195,9 @@ const PostFormTable = ({ closeForm, postCid }: { closeForm: () => void; postCid: const cid = params?.commentCid as string; const { setPublishReplyOptions, resetPublishReplyOptions, replyIndex, publishReply } = useReply({ cid, subplebbitAddress }); - const getAnonAddressForReply = async () => { - if (anonMode) { + const getAnonAddressForReply = useCallback(async () => { + if (anonMode && !hasCalledAnonAddressRef.current) { + hasCalledAnonAddressRef.current = true; const existingSigner = getExistingSigner(address); if (existingSigner) { console.log('onPublishReply - existingSigner:', existingSigner); @@ -207,7 +209,7 @@ const PostFormTable = ({ closeForm, postCid }: { closeForm: () => void; postCid: }, }); } else { - const newSigner = (await getNewSigner()) || {}; + const newSigner = await getNewSigner(); setPublishReplyOptions({ signer: newSigner, author: { @@ -217,7 +219,7 @@ const PostFormTable = ({ closeForm, postCid }: { closeForm: () => void; postCid: }); } } - }; + }, [address, getExistingSigner, getNewSigner, setPublishReplyOptions, displayName, anonMode]); const onPublishReply = () => { const currentContent = textRef.current?.value || ''; @@ -233,9 +235,7 @@ const PostFormTable = ({ closeForm, postCid }: { closeForm: () => void; postCid: return; } - getAnonAddressForReply().then(() => { - publishReply(); - }); + publishReply(); }; useEffect(() => { @@ -246,6 +246,16 @@ const PostFormTable = ({ closeForm, postCid }: { closeForm: () => void; postCid: } }, [replyIndex, resetPublishReplyOptions, closeForm]); + useEffect(() => { + if (anonMode) { + if (isInPostView) { + getAnonAddressForReply(); + } else { + getAnonAddressForPost(); + } + } + }, [anonMode, getAnonAddressForPost, getAnonAddressForReply, isInPostView]); + return ( diff --git a/src/components/reply-modal/reply-modal.tsx b/src/components/reply-modal/reply-modal.tsx index 2020722d..55234ca7 100644 --- a/src/components/reply-modal/reply-modal.tsx +++ b/src/components/reply-modal/reply-modal.tsx @@ -1,4 +1,4 @@ -import { useEffect, useRef, useState } from 'react'; +import { useCallback, useEffect, useRef, useState } from 'react'; import { useLocation, useParams } from 'react-router-dom'; import { useTranslation } from 'react-i18next'; import Draggable from 'react-draggable'; @@ -37,29 +37,32 @@ const ReplyModal = ({ closeModal, parentCid, postCid, scrollY }: ReplyModalProps const comment = useComment({ commentCid: postCid }); const address = comment?.author?.address; - const getAnonAddressForReply = async () => { - if (anonMode) { - const existingSigner = getExistingSigner(address); - if (existingSigner) { - setPublishReplyOptions({ - signer: existingSigner, - author: { - displayName, - address: existingSigner.address, - }, - }); - } else { - const newSigner = (await getNewSigner()) || {}; - setPublishReplyOptions({ - signer: newSigner, - author: { - displayName, - address: newSigner.address, - }, - }); + const hasCalledAnonAddressRef = useRef(false); + + const getAnonAddressForReply = useCallback(async () => { + if (anonMode && !hasCalledAnonAddressRef.current) { + hasCalledAnonAddressRef.current = true; + let signer = getExistingSigner(address); + if (!signer) { + signer = await getNewSigner(); + if (signer) { + setPublishReplyOptions({ + signer, + author: { + displayName, + address: signer.address, + }, + }); + } } } - }; + }, [anonMode, address, getExistingSigner, getNewSigner, displayName, setPublishReplyOptions]); + + useEffect(() => { + if (anonMode) { + getAnonAddressForReply(); + } + }, [anonMode, getAnonAddressForReply]); const onPublishReply = async () => { const currentContent = textRef.current?.value || ''; @@ -75,10 +78,8 @@ const ReplyModal = ({ closeModal, parentCid, postCid, scrollY }: ReplyModalProps return; } - getAnonAddressForReply().then(() => { - publishReply(); - closeModal(); - }); + publishReply(); + closeModal(); }; const nodeRef = useRef(null);