display anon mode address immediately upon publishing

This commit is contained in:
Tom (plebeius.eth)
2024-08-30 21:18:39 +02:00
parent 3bc672cbaa
commit 51e3ef8506
5 changed files with 48 additions and 13 deletions
+7 -1
View File
@@ -9,6 +9,8 @@ import { hashStringToColor, getTextColorForBackground } from '../../lib/utils/po
import { getFormattedDate, getFormattedTimeAgo } from '../../lib/utils/time-utils'; import { getFormattedDate, getFormattedTimeAgo } from '../../lib/utils/time-utils';
import { isValidURL } from '../../lib/utils/url-utils'; import { isValidURL } from '../../lib/utils/url-utils';
import { isAllView, isPendingPostView, isPostPageView, isSubscriptionsView } from '../../lib/utils/view-utils'; import { isAllView, isPendingPostView, isPostPageView, isSubscriptionsView } from '../../lib/utils/view-utils';
import useAnonModeStore from '../../stores/use-anon-mode-store';
import useAnonMode from '../../hooks/use-anon-mode';
import useAuthorAddressClick from '../../hooks/use-author-address-click'; import useAuthorAddressClick from '../../hooks/use-author-address-click';
import useEditCommentPrivileges from '../../hooks/use-author-privileges'; import useEditCommentPrivileges from '../../hooks/use-author-privileges';
import useCountLinksInReplies from '../../hooks/use-count-links-in-replies'; import useCountLinksInReplies from '../../hooks/use-count-links-in-replies';
@@ -64,8 +66,12 @@ const PostInfo = ({ openReplyModal, post, postReplyCount = 0, roles, isHidden }:
const isInPostPageView = isPostPageView(location.pathname, params); const isInPostPageView = isPostPageView(location.pathname, params);
const isInSubscriptionsView = isSubscriptionsView(location.pathname, params); const isInSubscriptionsView = isSubscriptionsView(location.pathname, params);
// comment.author.shortAddress is undefined while the comment publishing state is pending, use account instead
// in anon mode, use the newly generated signer.address instead, which will be comment.author.address
const { anonMode } = useAnonMode();
const { currentAnonSignerAddress } = useAnonModeStore();
const account = useAccount(); const account = useAccount();
const accountShortAddress = account?.author?.shortAddress; // if reply by account is pending, it doesn't have an author yet const accountShortAddress = anonMode ? currentAnonSignerAddress && Plebbit.getShortAddress(currentAnonSignerAddress) : account?.author?.shortAddress;
const { isCommentAuthorMod, isAccountMod, isAccountCommentAuthor } = useEditCommentPrivileges({ commentAuthorAddress: address, subplebbitAddress }); const { isCommentAuthorMod, isAccountMod, isAccountCommentAuthor } = useEditCommentPrivileges({ commentAuthorAddress: address, subplebbitAddress });
+7 -2
View File
@@ -8,6 +8,8 @@ import { getCommentMediaInfo, getHasThumbnail } from '../../lib/utils/media-util
import { getTextColorForBackground, hashStringToColor } from '../../lib/utils/post-utils'; import { getTextColorForBackground, hashStringToColor } from '../../lib/utils/post-utils';
import { getFormattedDate, getFormattedTimeAgo } from '../../lib/utils/time-utils'; import { getFormattedDate, getFormattedTimeAgo } from '../../lib/utils/time-utils';
import { isAllView, isPendingPostView, isPostPageView, isSubscriptionsView } from '../../lib/utils/view-utils'; import { isAllView, isPendingPostView, isPostPageView, isSubscriptionsView } from '../../lib/utils/view-utils';
import useAnonModeStore from '../../stores/use-anon-mode-store';
import useAnonMode from '../../hooks/use-anon-mode';
import useAuthorAddressClick from '../../hooks/use-author-address-click'; import useAuthorAddressClick from '../../hooks/use-author-address-click';
import useCountLinksInReplies from '../../hooks/use-count-links-in-replies'; import useCountLinksInReplies from '../../hooks/use-count-links-in-replies';
import useHide from '../../hooks/use-hide'; import useHide from '../../hooks/use-hide';
@@ -44,9 +46,12 @@ const PostInfoAndMedia = ({ openReplyModal, post, postReplyCount = 0, roles }: P
const isReply = parentCid; const isReply = parentCid;
// pending reply by account is not yet published // comment.author.shortAddress is undefined while the comment publishing state is pending, use account instead
// in anon mode, use the newly generated signer.address instead, which will be comment.author.address
const { anonMode } = useAnonMode();
const { currentAnonSignerAddress } = useAnonModeStore();
const account = useAccount(); const account = useAccount();
const accountShortAddress = account?.author?.shortAddress; const accountShortAddress = anonMode ? currentAnonSignerAddress && Plebbit.getShortAddress(currentAnonSignerAddress) : account?.author?.shortAddress;
const stateString = useStateString(post); const stateString = useStateString(post);
+15 -8
View File
@@ -15,6 +15,7 @@ import styles from './reply-modal.module.css';
import { LinkTypePreviewer } from '../post-form'; import { LinkTypePreviewer } from '../post-form';
import _ from 'lodash'; import _ from 'lodash';
import useAnonMode from '../../hooks/use-anon-mode'; import useAnonMode from '../../hooks/use-anon-mode';
import useAnonModeStore from '../../stores/use-anon-mode-store';
interface ReplyModalProps { interface ReplyModalProps {
closeModal: () => void; closeModal: () => void;
@@ -40,6 +41,8 @@ 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 { setCurrentAnonSignerAddress } = useAnonModeStore();
const getAnonAddressForReply = useCallback(async () => { const getAnonAddressForReply = useCallback(async () => {
if (anonMode && !hasCalledAnonAddressRef.current) { if (anonMode && !hasCalledAnonAddressRef.current) {
hasCalledAnonAddressRef.current = true; hasCalledAnonAddressRef.current = true;
@@ -52,18 +55,22 @@ const ReplyModal = ({ closeModal, showReplyModal, parentCid, postCid, scrollY, s
displayName: displayName || undefined, displayName: displayName || undefined,
}, },
}); });
setCurrentAnonSignerAddress(existingSigner.address);
} else { } else {
const newSigner = await getNewSigner(); const newSigner = await getNewSigner();
setPublishReplyOptions({ if (newSigner) {
signer: newSigner, setPublishReplyOptions({
author: { signer: newSigner,
address: newSigner.address, author: {
displayName: displayName || undefined, address: newSigner.address,
}, displayName: displayName || undefined,
}); },
});
setCurrentAnonSignerAddress(newSigner.address);
}
} }
} }
}, [address, getExistingSigner, getNewSigner, setPublishReplyOptions, anonMode, displayName]); }, [address, getExistingSigner, getNewSigner, setPublishReplyOptions, anonMode, displayName, setCurrentAnonSignerAddress]);
const onPublishReply = () => { const onPublishReply = () => {
const currentContent = textRef.current?.value.slice(contentPrefix.length).trim() || ''; const currentContent = textRef.current?.value.slice(contentPrefix.length).trim() || '';
+9 -2
View File
@@ -2,12 +2,13 @@ import { useAccount } from '@plebbit/plebbit-react-hooks';
import useAnonModeStore from '../stores/use-anon-mode-store'; import useAnonModeStore from '../stores/use-anon-mode-store';
const useAnonMode = (postCid?: string) => { const useAnonMode = (postCid?: string) => {
const { anonMode, threadSigners, setThreadSigner, setAddressSigner, getAddressSigner } = useAnonModeStore((state) => ({ const { anonMode, threadSigners, setThreadSigner, setAddressSigner, getAddressSigner, setCurrentAnonSignerAddress } = useAnonModeStore((state) => ({
anonMode: state.anonMode, anonMode: state.anonMode,
threadSigners: state.threadSigners, threadSigners: state.threadSigners,
setThreadSigner: state.setThreadSigner, setThreadSigner: state.setThreadSigner,
setAddressSigner: state.setAddressSigner, setAddressSigner: state.setAddressSigner,
getAddressSigner: state.getAddressSigner, getAddressSigner: state.getAddressSigner,
setCurrentAnonSignerAddress: state.setCurrentAnonSignerAddress,
})); }));
const threadSigner = postCid ? threadSigners[postCid] : undefined; const threadSigner = postCid ? threadSigners[postCid] : undefined;
@@ -25,6 +26,7 @@ const useAnonMode = (postCid?: string) => {
} else { } else {
setAddressSigner(signer); setAddressSigner(signer);
} }
setCurrentAnonSignerAddress(signer.address);
} }
return signer; return signer;
} catch (error) { } catch (error) {
@@ -33,6 +35,7 @@ const useAnonMode = (postCid?: string) => {
} else { } else {
try { try {
const signer = await account?.plebbit.createSigner({ type: 'ed25519', privateKey: threadSigner?.privateKey }); const signer = await account?.plebbit.createSigner({ type: 'ed25519', privateKey: threadSigner?.privateKey });
setCurrentAnonSignerAddress(signer.address);
return signer; return signer;
} catch (error) { } catch (error) {
console.error('Failed to retrieve anonymous signer:', error); console.error('Failed to retrieve anonymous signer:', error);
@@ -43,7 +46,11 @@ const useAnonMode = (postCid?: string) => {
}; };
const getExistingSigner = (address: string) => { const getExistingSigner = (address: string) => {
return getAddressSigner(address); const signer = getAddressSigner(address);
if (signer) {
setCurrentAnonSignerAddress(signer.address);
}
return signer;
}; };
return { anonMode, getNewSigner, getExistingSigner }; return { anonMode, getNewSigner, getExistingSigner };
+10
View File
@@ -10,6 +10,8 @@ interface AnonModeState {
getThreadSigner: (postCid: string) => any | undefined; getThreadSigner: (postCid: string) => any | undefined;
setAddressSigner: (signer: any) => void; setAddressSigner: (signer: any) => void;
getAddressSigner: (address: string) => any | undefined; getAddressSigner: (address: string) => any | undefined;
currentAnonSignerAddress: string | null;
setCurrentAnonSignerAddress: (address: string | null) => void;
} }
const anonModeStore = localForageLru.createInstance({ const anonModeStore = localForageLru.createInstance({
@@ -39,6 +41,11 @@ const useAnonModeStore = create<AnonModeState>((set, get) => ({
anonModeStore.setItem(signer.address, signer); anonModeStore.setItem(signer.address, signer);
}, },
getAddressSigner: (address: string) => get().addressSigners[address], getAddressSigner: (address: string) => get().addressSigners[address],
currentAnonSignerAddress: null,
setCurrentAnonSignerAddress: (address: string | null) => {
set({ currentAnonSignerAddress: address });
anonModeStore.setItem('currentAnonSignerAddress', address);
},
})); }));
const initializeAnonModeStore = async () => { const initializeAnonModeStore = async () => {
@@ -57,10 +64,13 @@ const initializeAnonModeStore = async () => {
} }
}); });
const currentAnonSignerAddress = await anonModeStore.getItem('currentAnonSignerAddress');
useAnonModeStore.setState((state) => ({ useAnonModeStore.setState((state) => ({
anonMode, // Set the retrieved anonMode state anonMode, // Set the retrieved anonMode state
threadSigners: { ...threadSigners, ...state.threadSigners }, threadSigners: { ...threadSigners, ...state.threadSigners },
addressSigners: { ...addressSigners, ...state.addressSigners }, addressSigners: { ...addressSigners, ...state.addressSigners },
currentAnonSignerAddress: currentAnonSignerAddress || null,
})); }));
}; };