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();
};
const hasSetInitialDisplayName = useRef(false);
useEffect(() => {
if (!hasSetInitialDisplayName.current && displayName) {
setPublishReplyOptions({ displayName });
hasSetInitialDisplayName.current = true;
}
}, [displayName, setPublishReplyOptions]);
useEffect(() => {
if (typeof replyIndex === 'number') {
resetPublishReplyOptions();
+9 -16
View File
@@ -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<HTMLDivElement>(null);
const isMobile = useIsMobile();
+31 -24
View File
@@ -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<ReplyState>((set) => ({
author: {},
displayName: {},
content: {},
link: {},
signer: {},
@@ -40,20 +42,16 @@ const useReplyStore = create<ReplyState>((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<ReplyState>((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<ReplyState>((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<SetReplyStoreData>) => {
setReplyStore({
const newOptions: Partial<SetReplyStoreData> = {
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 };