diff --git a/src/components/post-form/post-form.tsx b/src/components/post-form/post-form.tsx index 8172181c..bf29a4f7 100644 --- a/src/components/post-form/post-form.tsx +++ b/src/components/post-form/post-form.tsx @@ -266,14 +266,6 @@ const PostFormTable = ({ closeForm, postCid }: { closeForm: () => void; postCid: publishReply(); }; - const hasSetInitialDisplayName = useRef(false); - useEffect(() => { - if (!hasSetInitialDisplayName.current && displayName) { - setPublishReplyOptions({ displayName }); - hasSetInitialDisplayName.current = true; - } - }, [displayName, setPublishReplyOptions]); - useEffect(() => { if (typeof replyIndex === 'number') { resetPublishReplyOptions(); diff --git a/src/components/reply-modal/reply-modal.tsx b/src/components/reply-modal/reply-modal.tsx index 01eb6ab7..57cf1e6f 100644 --- a/src/components/reply-modal/reply-modal.tsx +++ b/src/components/reply-modal/reply-modal.tsx @@ -39,7 +39,6 @@ const ReplyModal = ({ closeModal, showReplyModal, parentCid, postCid, scrollY, s const address = comment?.author?.address; const hasCalledAnonAddressRef = useRef(false); - const getAnonAddressForReply = useCallback(async () => { if (anonMode && !hasCalledAnonAddressRef.current) { hasCalledAnonAddressRef.current = true; @@ -49,6 +48,7 @@ const ReplyModal = ({ closeModal, showReplyModal, parentCid, postCid, scrollY, s signer: existingSigner, author: { address: existingSigner.address, + displayName: displayName || undefined, }, }); } else { @@ -57,17 +57,12 @@ const ReplyModal = ({ closeModal, showReplyModal, parentCid, postCid, scrollY, s signer: newSigner, author: { address: newSigner.address, + displayName: displayName || undefined, }, }); } } - }, [anonMode, address, getExistingSigner, getNewSigner, setPublishReplyOptions]); - - useEffect(() => { - if (anonMode) { - getAnonAddressForReply(); - } - }, [anonMode, getAnonAddressForReply]); + }, [address, getExistingSigner, getNewSigner, setPublishReplyOptions, anonMode, displayName]); const onPublishReply = () => { const currentContent = textRef.current?.value || ''; @@ -86,6 +81,12 @@ const ReplyModal = ({ closeModal, showReplyModal, parentCid, postCid, scrollY, s publishReply(); }; + useEffect(() => { + if (anonMode) { + getAnonAddressForReply(); + } + }, [anonMode, getAnonAddressForReply]); + useEffect(() => { if (typeof replyIndex === 'number') { resetPublishReplyOptions(); @@ -93,14 +94,6 @@ const ReplyModal = ({ closeModal, showReplyModal, parentCid, postCid, scrollY, s } }, [replyIndex, resetPublishReplyOptions, closeModal]); - const hasSetInitialDisplayName = useRef(false); - useEffect(() => { - if (!hasSetInitialDisplayName.current && displayName) { - setPublishReplyOptions({ displayName }); - hasSetInitialDisplayName.current = true; - } - }, [displayName, setPublishReplyOptions]); - const nodeRef = useRef(null); const isMobile = useIsMobile(); diff --git a/src/hooks/use-publish-reply.ts b/src/hooks/use-publish-reply.ts index 783469d5..81399ef9 100644 --- a/src/hooks/use-publish-reply.ts +++ b/src/hooks/use-publish-reply.ts @@ -8,16 +8,17 @@ import useAnonMode from './use-anon-mode'; type SetReplyStoreData = { subplebbitAddress: string; parentCid: string; - author?: any | undefined; - displayName?: string | undefined; - content: string | undefined; - link: string | undefined; - signer?: any | undefined; - spoiler: boolean | undefined; + author?: any; + displayName?: string; + content: string; + link?: string; + signer?: any; + spoiler: boolean; }; type ReplyState = { author: { [parentCid: string]: any | undefined }; + displayName: { [parentCid: string]: string | undefined }; content: { [parentCid: string]: string | undefined }; link: { [parentCid: string]: string | undefined }; signer: { [parentCid: string]: any | undefined }; @@ -31,6 +32,7 @@ const { addChallenge } = useChallengesStore.getState(); const useReplyStore = create((set) => ({ author: {}, + displayName: {}, content: {}, link: {}, signer: {}, @@ -40,20 +42,16 @@ const useReplyStore = create((set) => ({ setReplyStore: (data: SetReplyStoreData) => set((state) => { const { subplebbitAddress, parentCid, author, displayName, content, link, signer, spoiler } = data; + const updatedAuthor = displayName ? { ...author, displayName } : author; - const updatedAuthor = { - ...(state.author[parentCid] || author), - ...(displayName ? { displayName } : {}), - }; - - const publishCommentOptions = { + const publishCommentOptions: PublishCommentOptions = { subplebbitAddress, parentCid, - ...(updatedAuthor ? { author: updatedAuthor } : {}), - ...(signer ? { signer } : {}), content, link, spoiler, + ...(author && { author: updatedAuthor }), + ...(signer && { signer }), onChallenge: (...args: any) => addChallenge(args), onChallengeVerification: (challengeVerification: ChallengeVerification, comment: Comment) => { alertChallengeVerificationFailed(challengeVerification, comment); @@ -64,8 +62,11 @@ const useReplyStore = create((set) => ({ }, }; + console.log('Final publishCommentOptions:', publishCommentOptions); + return { author: { ...state.author, [parentCid]: updatedAuthor }, + displayName: { ...state.displayName, [parentCid]: displayName }, content: { ...state.content, [parentCid]: content }, link: { ...state.link, [parentCid]: link }, signer: { ...state.signer, [parentCid]: signer }, @@ -77,6 +78,7 @@ const useReplyStore = create((set) => ({ resetReplyStore: (parentCid) => set((state) => ({ author: { ...state.author, [parentCid]: undefined }, + displayName: { ...state.displayName, [parentCid]: undefined }, content: { ...state.content, [parentCid]: undefined }, link: { ...state.link, [parentCid]: undefined }, signer: { ...state.signer, [parentCid]: undefined }, @@ -89,7 +91,7 @@ const useReply = ({ cid, subplebbitAddress }: { cid: string; subplebbitAddress: const parentCid = cid; const { author, displayName, signer, content, link, spoiler, publishCommentOptions } = useReplyStore((state) => ({ author: state.author[parentCid], - displayName: state.author[parentCid]?.displayName, + displayName: state.displayName[parentCid], signer: state.signer[parentCid], content: state.content[parentCid], link: state.link[parentCid], @@ -104,23 +106,28 @@ const useReply = ({ cid, subplebbitAddress }: { cid: string; subplebbitAddress: const setPublishReplyOptions = useCallback( (options: Partial) => { - setReplyStore({ + const newOptions: Partial = { subplebbitAddress, parentCid, - ...(anonMode ? { displayName } : {}), - ...(anonMode ? { author } : {}), - content, - link, - spoiler, - ...(anonMode ? { signer } : {}), - ...options, - }); + content: options.content || content, + displayName: options.displayName || displayName, + link: options.link || link, + spoiler: options.spoiler ?? spoiler, + author: anonMode ? signer?.author || options.author || author || { displayName } : undefined, + signer: anonMode ? signer || options.signer : undefined, + }; + + console.log('Final options to set in state:', newOptions); + + setReplyStore(newOptions as SetReplyStoreData); }, [subplebbitAddress, parentCid, author, displayName, signer, content, link, spoiler, setReplyStore, anonMode], ); const resetPublishReplyOptions = useCallback(() => resetReplyStore(parentCid), [parentCid, resetReplyStore]); + console.log('Final options before publishing', publishCommentOptions); + const { index, publishComment } = usePublishComment(publishCommentOptions); return { setPublishReplyOptions, resetPublishReplyOptions, replyIndex: index, publishReply: publishComment, setReplyStore };