fix(anon mode): user id could change for pending post, name field could bug out

This commit is contained in:
Tom (plebeius.eth)
2024-09-19 17:33:00 +02:00
parent b18c1bc491
commit bb976409ff
8 changed files with 40 additions and 110 deletions
+3 -9
View File
@@ -3,13 +3,12 @@ import useAnonModeStore from '../stores/use-anon-mode-store';
import { useCallback, useMemo } from 'react';
const useAnonMode = (postCid?: string) => {
const { anonMode, threadSigners, setThreadSigner, setAddressSigner, getAddressSigner, setCurrentAnonSignerAddress } = useAnonModeStore((state) => ({
const { anonMode, threadSigners, setThreadSigner, setAddressSigner, getAddressSigner } = useAnonModeStore((state) => ({
anonMode: state.anonMode,
threadSigners: state.threadSigners,
setThreadSigner: state.setThreadSigner,
setAddressSigner: state.setAddressSigner,
getAddressSigner: state.getAddressSigner,
setCurrentAnonSignerAddress: state.setCurrentAnonSignerAddress,
}));
const threadSigner = useMemo(() => (postCid ? threadSigners[postCid] : undefined), [postCid, threadSigners]);
@@ -27,7 +26,6 @@ const useAnonMode = (postCid?: string) => {
} else {
setAddressSigner(signer);
}
setCurrentAnonSignerAddress(signer.address);
}
return signer;
} catch (error) {
@@ -36,7 +34,6 @@ const useAnonMode = (postCid?: string) => {
} else {
try {
const signer = await account?.plebbit.createSigner({ type: 'ed25519', privateKey: threadSigner?.privateKey });
setCurrentAnonSignerAddress(signer.address);
return signer;
} catch (error) {
console.error('Failed to retrieve anonymous signer:', error);
@@ -44,17 +41,14 @@ const useAnonMode = (postCid?: string) => {
}
}
return null;
}, [anonMode, postCid, threadSigner, account, setThreadSigner, setAddressSigner, setCurrentAnonSignerAddress]);
}, [anonMode, postCid, threadSigner, account, setThreadSigner, setAddressSigner]);
const getExistingSigner = useCallback(
(address: string) => {
const signer = getAddressSigner(address);
if (signer) {
setCurrentAnonSignerAddress(signer.address);
}
return signer;
},
[getAddressSigner, setCurrentAnonSignerAddress],
[getAddressSigner],
);
return { anonMode, getNewSigner, getExistingSigner };
-43
View File
@@ -1,43 +0,0 @@
import { useEffect, useReducer } from 'react';
import { useComment } from '@plebbit/plebbit-react-hooks';
type State = {
resolvedPostCid: string | null;
currentParentCid: string | undefined;
};
type Action = { type: 'SET_POST_CID'; payload: string } | { type: 'SET_PARENT_CID'; payload: string };
const reducer = (state: State, action: Action): State => {
switch (action.type) {
case 'SET_POST_CID':
return { ...state, resolvedPostCid: action.payload };
case 'SET_PARENT_CID':
return { ...state, currentParentCid: action.payload };
default:
return state;
}
};
const usePostCidForPendingPost = (initialParentCid: string | undefined): string | null => {
const [state, dispatch] = useReducer(reducer, {
resolvedPostCid: null,
currentParentCid: initialParentCid,
});
const comment = useComment({ commentCid: state.currentParentCid });
useEffect(() => {
if (comment) {
if (comment.postCid) {
dispatch({ type: 'SET_POST_CID', payload: comment.postCid });
} else if (comment.parentCid) {
dispatch({ type: 'SET_PARENT_CID', payload: comment.parentCid });
}
}
}, [comment]);
return state.resolvedPostCid;
};
export default usePostCidForPendingPost;
+20 -8
View File
@@ -1,5 +1,5 @@
import { useCallback } from 'react';
import { ChallengeVerification, Comment, PublishCommentOptions, usePublishComment } from '@plebbit/plebbit-react-hooks';
import { ChallengeVerification, Comment, PublishCommentOptions, useAccount, usePublishComment } from '@plebbit/plebbit-react-hooks';
import { create } from 'zustand';
import { alertChallengeVerificationFailed } from '../lib/utils/challenge-utils';
import useChallengesStore from '../stores/use-challenges-store';
@@ -50,8 +50,6 @@ const useReplyStore = create<ReplyState>((set) => ({
content,
link,
spoiler,
...(author && { author: updatedAuthor }),
...(signer && { signer }),
onChallenge: (...args: any) => addChallenge(args),
onChallengeVerification: (challengeVerification: ChallengeVerification, comment: Comment) => {
alertChallengeVerificationFailed(challengeVerification, comment);
@@ -62,6 +60,14 @@ const useReplyStore = create<ReplyState>((set) => ({
},
};
if (updatedAuthor) {
publishCommentOptions.author = updatedAuthor;
}
if (signer) {
publishCommentOptions.signer = signer;
}
return {
author: { ...state.author, [parentCid]: updatedAuthor },
displayName: { ...state.displayName, [parentCid]: displayName },
@@ -102,6 +108,8 @@ const useReply = ({ cid, subplebbitAddress }: { cid: string; subplebbitAddress:
const setReplyStore = useReplyStore((state) => state.setReplyStore);
const resetReplyStore = useReplyStore((state) => state.resetReplyStore);
const account = useAccount();
const setPublishReplyOptions = useCallback(
(options: Partial<SetReplyStoreData>) => {
const newOptions: Partial<SetReplyStoreData> = {
@@ -110,7 +118,6 @@ const useReply = ({ cid, subplebbitAddress }: { cid: string; subplebbitAddress:
content: options.content ?? content,
link: options.link ?? link,
spoiler: options.spoiler ?? spoiler,
signer: anonMode ? signer || options.signer : undefined,
};
if ('displayName' in options) {
@@ -118,18 +125,23 @@ const useReply = ({ cid, subplebbitAddress }: { cid: string; subplebbitAddress:
}
if (anonMode) {
const currentAuthor = signer?.author || author || {};
newOptions.signer = signer || options.signer;
newOptions.author = {
...currentAuthor,
...(author || {}),
address: newOptions.signer?.address,
...('displayName' in options && { displayName: options.displayName }),
};
} else {
newOptions.author = undefined;
newOptions.signer = undefined;
newOptions.author = {
address: account?.author?.address,
...('displayName' in options && { displayName: options.displayName }),
};
}
setReplyStore(newOptions as SetReplyStoreData);
},
[subplebbitAddress, parentCid, author, signer, content, link, spoiler, setReplyStore, anonMode],
[subplebbitAddress, parentCid, author, signer, content, link, spoiler, setReplyStore, anonMode, account],
);
const resetPublishReplyOptions = useCallback(() => resetReplyStore(parentCid), [parentCid, resetReplyStore]);