fix anon mode

This commit is contained in:
Tom (plebeius.eth)
2024-08-25 18:58:38 +02:00
parent a0f83304e5
commit 899239e10e
3 changed files with 40 additions and 48 deletions
-8
View File
@@ -266,14 +266,6 @@ const PostFormTable = ({ closeForm, postCid }: { closeForm: () => void; postCid:
publishReply(); publishReply();
}; };
const hasSetInitialDisplayName = useRef(false);
useEffect(() => {
if (!hasSetInitialDisplayName.current && displayName) {
setPublishReplyOptions({ displayName });
hasSetInitialDisplayName.current = true;
}
}, [displayName, setPublishReplyOptions]);
useEffect(() => { useEffect(() => {
if (typeof replyIndex === 'number') { if (typeof replyIndex === 'number') {
resetPublishReplyOptions(); resetPublishReplyOptions();
+9 -16
View File
@@ -39,7 +39,6 @@ const ReplyModal = ({ closeModal, showReplyModal, parentCid, postCid, scrollY, s
const address = comment?.author?.address; const address = comment?.author?.address;
const hasCalledAnonAddressRef = useRef(false); const hasCalledAnonAddressRef = useRef(false);
const getAnonAddressForReply = useCallback(async () => { const getAnonAddressForReply = useCallback(async () => {
if (anonMode && !hasCalledAnonAddressRef.current) { if (anonMode && !hasCalledAnonAddressRef.current) {
hasCalledAnonAddressRef.current = true; hasCalledAnonAddressRef.current = true;
@@ -49,6 +48,7 @@ const ReplyModal = ({ closeModal, showReplyModal, parentCid, postCid, scrollY, s
signer: existingSigner, signer: existingSigner,
author: { author: {
address: existingSigner.address, address: existingSigner.address,
displayName: displayName || undefined,
}, },
}); });
} else { } else {
@@ -57,17 +57,12 @@ const ReplyModal = ({ closeModal, showReplyModal, parentCid, postCid, scrollY, s
signer: newSigner, signer: newSigner,
author: { author: {
address: newSigner.address, address: newSigner.address,
displayName: displayName || undefined,
}, },
}); });
} }
} }
}, [anonMode, address, getExistingSigner, getNewSigner, setPublishReplyOptions]); }, [address, getExistingSigner, getNewSigner, setPublishReplyOptions, anonMode, displayName]);
useEffect(() => {
if (anonMode) {
getAnonAddressForReply();
}
}, [anonMode, getAnonAddressForReply]);
const onPublishReply = () => { const onPublishReply = () => {
const currentContent = textRef.current?.value || ''; const currentContent = textRef.current?.value || '';
@@ -86,6 +81,12 @@ const ReplyModal = ({ closeModal, showReplyModal, parentCid, postCid, scrollY, s
publishReply(); publishReply();
}; };
useEffect(() => {
if (anonMode) {
getAnonAddressForReply();
}
}, [anonMode, getAnonAddressForReply]);
useEffect(() => { useEffect(() => {
if (typeof replyIndex === 'number') { if (typeof replyIndex === 'number') {
resetPublishReplyOptions(); resetPublishReplyOptions();
@@ -93,14 +94,6 @@ const ReplyModal = ({ closeModal, showReplyModal, parentCid, postCid, scrollY, s
} }
}, [replyIndex, resetPublishReplyOptions, closeModal]); }, [replyIndex, resetPublishReplyOptions, closeModal]);
const hasSetInitialDisplayName = useRef(false);
useEffect(() => {
if (!hasSetInitialDisplayName.current && displayName) {
setPublishReplyOptions({ displayName });
hasSetInitialDisplayName.current = true;
}
}, [displayName, setPublishReplyOptions]);
const nodeRef = useRef<HTMLDivElement>(null); const nodeRef = useRef<HTMLDivElement>(null);
const isMobile = useIsMobile(); const isMobile = useIsMobile();
+31 -24
View File
@@ -8,16 +8,17 @@ import useAnonMode from './use-anon-mode';
type SetReplyStoreData = { type SetReplyStoreData = {
subplebbitAddress: string; subplebbitAddress: string;
parentCid: string; parentCid: string;
author?: any | undefined; author?: any;
displayName?: string | undefined; displayName?: string;
content: string | undefined; content: string;
link: string | undefined; link?: string;
signer?: any | undefined; signer?: any;
spoiler: boolean | undefined; spoiler: boolean;
}; };
type ReplyState = { type ReplyState = {
author: { [parentCid: string]: any | undefined }; author: { [parentCid: string]: any | undefined };
displayName: { [parentCid: string]: string | undefined };
content: { [parentCid: string]: string | undefined }; content: { [parentCid: string]: string | undefined };
link: { [parentCid: string]: string | undefined }; link: { [parentCid: string]: string | undefined };
signer: { [parentCid: string]: any | undefined }; signer: { [parentCid: string]: any | undefined };
@@ -31,6 +32,7 @@ const { addChallenge } = useChallengesStore.getState();
const useReplyStore = create<ReplyState>((set) => ({ const useReplyStore = create<ReplyState>((set) => ({
author: {}, author: {},
displayName: {},
content: {}, content: {},
link: {}, link: {},
signer: {}, signer: {},
@@ -40,20 +42,16 @@ const useReplyStore = create<ReplyState>((set) => ({
setReplyStore: (data: SetReplyStoreData) => setReplyStore: (data: SetReplyStoreData) =>
set((state) => { set((state) => {
const { subplebbitAddress, parentCid, author, displayName, content, link, signer, spoiler } = data; const { subplebbitAddress, parentCid, author, displayName, content, link, signer, spoiler } = data;
const updatedAuthor = displayName ? { ...author, displayName } : author;
const updatedAuthor = { const publishCommentOptions: PublishCommentOptions = {
...(state.author[parentCid] || author),
...(displayName ? { displayName } : {}),
};
const publishCommentOptions = {
subplebbitAddress, subplebbitAddress,
parentCid, parentCid,
...(updatedAuthor ? { author: updatedAuthor } : {}),
...(signer ? { signer } : {}),
content, content,
link, link,
spoiler, spoiler,
...(author && { author: updatedAuthor }),
...(signer && { signer }),
onChallenge: (...args: any) => addChallenge(args), onChallenge: (...args: any) => addChallenge(args),
onChallengeVerification: (challengeVerification: ChallengeVerification, comment: Comment) => { onChallengeVerification: (challengeVerification: ChallengeVerification, comment: Comment) => {
alertChallengeVerificationFailed(challengeVerification, comment); alertChallengeVerificationFailed(challengeVerification, comment);
@@ -64,8 +62,11 @@ const useReplyStore = create<ReplyState>((set) => ({
}, },
}; };
console.log('Final publishCommentOptions:', publishCommentOptions);
return { return {
author: { ...state.author, [parentCid]: updatedAuthor }, author: { ...state.author, [parentCid]: updatedAuthor },
displayName: { ...state.displayName, [parentCid]: displayName },
content: { ...state.content, [parentCid]: content }, content: { ...state.content, [parentCid]: content },
link: { ...state.link, [parentCid]: link }, link: { ...state.link, [parentCid]: link },
signer: { ...state.signer, [parentCid]: signer }, signer: { ...state.signer, [parentCid]: signer },
@@ -77,6 +78,7 @@ const useReplyStore = create<ReplyState>((set) => ({
resetReplyStore: (parentCid) => resetReplyStore: (parentCid) =>
set((state) => ({ set((state) => ({
author: { ...state.author, [parentCid]: undefined }, author: { ...state.author, [parentCid]: undefined },
displayName: { ...state.displayName, [parentCid]: undefined },
content: { ...state.content, [parentCid]: undefined }, content: { ...state.content, [parentCid]: undefined },
link: { ...state.link, [parentCid]: undefined }, link: { ...state.link, [parentCid]: undefined },
signer: { ...state.signer, [parentCid]: undefined }, signer: { ...state.signer, [parentCid]: undefined },
@@ -89,7 +91,7 @@ const useReply = ({ cid, subplebbitAddress }: { cid: string; subplebbitAddress:
const parentCid = cid; const parentCid = cid;
const { author, displayName, signer, content, link, spoiler, publishCommentOptions } = useReplyStore((state) => ({ const { author, displayName, signer, content, link, spoiler, publishCommentOptions } = useReplyStore((state) => ({
author: state.author[parentCid], author: state.author[parentCid],
displayName: state.author[parentCid]?.displayName, displayName: state.displayName[parentCid],
signer: state.signer[parentCid], signer: state.signer[parentCid],
content: state.content[parentCid], content: state.content[parentCid],
link: state.link[parentCid], link: state.link[parentCid],
@@ -104,23 +106,28 @@ const useReply = ({ cid, subplebbitAddress }: { cid: string; subplebbitAddress:
const setPublishReplyOptions = useCallback( const setPublishReplyOptions = useCallback(
(options: Partial<SetReplyStoreData>) => { (options: Partial<SetReplyStoreData>) => {
setReplyStore({ const newOptions: Partial<SetReplyStoreData> = {
subplebbitAddress, subplebbitAddress,
parentCid, parentCid,
...(anonMode ? { displayName } : {}), content: options.content || content,
...(anonMode ? { author } : {}), displayName: options.displayName || displayName,
content, link: options.link || link,
link, spoiler: options.spoiler ?? spoiler,
spoiler, author: anonMode ? signer?.author || options.author || author || { displayName } : undefined,
...(anonMode ? { signer } : {}), signer: anonMode ? signer || options.signer : undefined,
...options, };
});
console.log('Final options to set in state:', newOptions);
setReplyStore(newOptions as SetReplyStoreData);
}, },
[subplebbitAddress, parentCid, author, displayName, signer, content, link, spoiler, setReplyStore, anonMode], [subplebbitAddress, parentCid, author, displayName, signer, content, link, spoiler, setReplyStore, anonMode],
); );
const resetPublishReplyOptions = useCallback(() => resetReplyStore(parentCid), [parentCid, resetReplyStore]); const resetPublishReplyOptions = useCallback(() => resetReplyStore(parentCid), [parentCid, resetReplyStore]);
console.log('Final options before publishing', publishCommentOptions);
const { index, publishComment } = usePublishComment(publishCommentOptions); const { index, publishComment } = usePublishComment(publishCommentOptions);
return { setPublishReplyOptions, resetPublishReplyOptions, replyIndex: index, publishReply: publishComment, setReplyStore }; return { setPublishReplyOptions, resetPublishReplyOptions, replyIndex: index, publishReply: publishComment, setReplyStore };