fix signer asynchronicity for anon mode

This commit is contained in:
Tom (plebeius.eth)
2024-08-06 21:51:45 +02:00
parent db67a9452c
commit 19d7475470
2 changed files with 50 additions and 39 deletions
+23 -13
View File
@@ -142,8 +142,11 @@ const PostFormTable = ({ closeForm, postCid }: { closeForm: () => void; postCid:
} }
}; };
const getAnonAddressForPost = async () => { const hasCalledAnonAddressRef = useRef(false);
if (anonMode) {
const getAnonAddressForPost = useCallback(async () => {
if (anonMode && !hasCalledAnonAddressRef.current) {
hasCalledAnonAddressRef.current = true;
const newSigner = (await getNewSigner()) || {}; const newSigner = (await getNewSigner()) || {};
setSubmitStore({ setSubmitStore({
signer: newSigner, signer: newSigner,
@@ -153,7 +156,7 @@ const PostFormTable = ({ closeForm, postCid }: { closeForm: () => void; postCid:
}, },
}); });
} }
}; }, [anonMode, getNewSigner, setSubmitStore, displayName]);
const onPublishPost = async () => { const onPublishPost = async () => {
if (!title && !content && !link) { if (!title && !content && !link) {
@@ -165,9 +168,7 @@ const PostFormTable = ({ closeForm, postCid }: { closeForm: () => void; postCid:
return; return;
} }
getAnonAddressForPost().then(() => { publishComment();
publishComment();
});
}; };
const params = useParams(); const params = useParams();
@@ -194,8 +195,9 @@ const PostFormTable = ({ closeForm, postCid }: { closeForm: () => void; postCid:
const cid = params?.commentCid as string; const cid = params?.commentCid as string;
const { setPublishReplyOptions, resetPublishReplyOptions, replyIndex, publishReply } = useReply({ cid, subplebbitAddress }); const { setPublishReplyOptions, resetPublishReplyOptions, replyIndex, publishReply } = useReply({ cid, subplebbitAddress });
const getAnonAddressForReply = async () => { const getAnonAddressForReply = useCallback(async () => {
if (anonMode) { if (anonMode && !hasCalledAnonAddressRef.current) {
hasCalledAnonAddressRef.current = true;
const existingSigner = getExistingSigner(address); const existingSigner = getExistingSigner(address);
if (existingSigner) { if (existingSigner) {
console.log('onPublishReply - existingSigner:', existingSigner); console.log('onPublishReply - existingSigner:', existingSigner);
@@ -207,7 +209,7 @@ const PostFormTable = ({ closeForm, postCid }: { closeForm: () => void; postCid:
}, },
}); });
} else { } else {
const newSigner = (await getNewSigner()) || {}; const newSigner = await getNewSigner();
setPublishReplyOptions({ setPublishReplyOptions({
signer: newSigner, signer: newSigner,
author: { author: {
@@ -217,7 +219,7 @@ const PostFormTable = ({ closeForm, postCid }: { closeForm: () => void; postCid:
}); });
} }
} }
}; }, [address, getExistingSigner, getNewSigner, setPublishReplyOptions, displayName, anonMode]);
const onPublishReply = () => { const onPublishReply = () => {
const currentContent = textRef.current?.value || ''; const currentContent = textRef.current?.value || '';
@@ -233,9 +235,7 @@ const PostFormTable = ({ closeForm, postCid }: { closeForm: () => void; postCid:
return; return;
} }
getAnonAddressForReply().then(() => { publishReply();
publishReply();
});
}; };
useEffect(() => { useEffect(() => {
@@ -246,6 +246,16 @@ const PostFormTable = ({ closeForm, postCid }: { closeForm: () => void; postCid:
} }
}, [replyIndex, resetPublishReplyOptions, closeForm]); }, [replyIndex, resetPublishReplyOptions, closeForm]);
useEffect(() => {
if (anonMode) {
if (isInPostView) {
getAnonAddressForReply();
} else {
getAnonAddressForPost();
}
}
}, [anonMode, getAnonAddressForPost, getAnonAddressForReply, isInPostView]);
return ( return (
<table className={styles.postFormTable}> <table className={styles.postFormTable}>
<tbody> <tbody>
+27 -26
View File
@@ -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 { useLocation, useParams } from 'react-router-dom';
import { useTranslation } from 'react-i18next'; import { useTranslation } from 'react-i18next';
import Draggable from 'react-draggable'; import Draggable from 'react-draggable';
@@ -37,29 +37,32 @@ const ReplyModal = ({ closeModal, parentCid, postCid, scrollY }: ReplyModalProps
const comment = useComment({ commentCid: postCid }); const comment = useComment({ commentCid: postCid });
const address = comment?.author?.address; const address = comment?.author?.address;
const getAnonAddressForReply = async () => { const hasCalledAnonAddressRef = useRef(false);
if (anonMode) {
const existingSigner = getExistingSigner(address); const getAnonAddressForReply = useCallback(async () => {
if (existingSigner) { if (anonMode && !hasCalledAnonAddressRef.current) {
setPublishReplyOptions({ hasCalledAnonAddressRef.current = true;
signer: existingSigner, let signer = getExistingSigner(address);
author: { if (!signer) {
displayName, signer = await getNewSigner();
address: existingSigner.address, if (signer) {
}, setPublishReplyOptions({
}); signer,
} else { author: {
const newSigner = (await getNewSigner()) || {}; displayName,
setPublishReplyOptions({ address: signer.address,
signer: newSigner, },
author: { });
displayName, }
address: newSigner.address,
},
});
} }
} }
}; }, [anonMode, address, getExistingSigner, getNewSigner, displayName, setPublishReplyOptions]);
useEffect(() => {
if (anonMode) {
getAnonAddressForReply();
}
}, [anonMode, getAnonAddressForReply]);
const onPublishReply = async () => { const onPublishReply = async () => {
const currentContent = textRef.current?.value || ''; const currentContent = textRef.current?.value || '';
@@ -75,10 +78,8 @@ const ReplyModal = ({ closeModal, parentCid, postCid, scrollY }: ReplyModalProps
return; return;
} }
getAnonAddressForReply().then(() => { publishReply();
publishReply(); closeModal();
closeModal();
});
}; };
const nodeRef = useRef<HTMLDivElement>(null); const nodeRef = useRef<HTMLDivElement>(null);