fix edit menu logic

This commit is contained in:
Tom (plebeius.eth)
2024-09-04 16:15:47 +02:00
parent 2cc64d26aa
commit fcd1ce65fb
4 changed files with 130 additions and 49 deletions
+13 -7
View File
@@ -1,16 +1,20 @@
import { useMemo } from 'react';
import { useAccount, useSubplebbit } from '@plebbit/plebbit-react-hooks';
import useAnonModeStore from '../stores/use-anon-mode-store';
import useAnonMode from './use-anon-mode';
interface AuthorPrivilegesProps {
commentAuthorAddress: string;
subplebbitAddress: string;
postCid?: string;
}
const useAuthorPrivileges = ({ commentAuthorAddress, subplebbitAddress }: AuthorPrivilegesProps) => {
const accountAuthorAddress = useAccount()?.author?.address;
const useAuthorPrivileges = ({ commentAuthorAddress, subplebbitAddress, postCid }: AuthorPrivilegesProps) => {
const account = useAccount();
const accountAuthorAddress = account?.author?.address;
const { roles } = useSubplebbit({ subplebbitAddress }) || {};
const { getAddressSigner } = useAnonModeStore();
const { getAddressSigner, getThreadSigner } = useAnonModeStore();
const { anonMode } = useAnonMode(postCid);
const { isCommentAuthorMod, isAccountMod, isAccountCommentAuthor, commentAuthorRole, accountAuthorRole } = useMemo(() => {
const commentAuthorRole = roles?.[commentAuthorAddress]?.role;
@@ -20,13 +24,15 @@ const useAuthorPrivileges = ({ commentAuthorAddress, subplebbitAddress }: Author
let isAccountCommentAuthor = accountAuthorAddress === commentAuthorAddress;
if (!isAccountCommentAuthor) {
const existingSigner = getAddressSigner(commentAuthorAddress);
isAccountCommentAuthor = !!existingSigner;
if (!isAccountCommentAuthor && anonMode) {
const addressSigner = getAddressSigner(commentAuthorAddress);
const threadSigner = postCid ? getThreadSigner(postCid) : null;
isAccountCommentAuthor = (addressSigner && addressSigner.address === commentAuthorAddress) || (threadSigner && threadSigner.address === commentAuthorAddress);
}
return { isCommentAuthorMod, isAccountMod, isAccountCommentAuthor, commentAuthorRole, accountAuthorRole };
}, [roles, commentAuthorAddress, accountAuthorAddress, getAddressSigner]);
}, [roles, commentAuthorAddress, accountAuthorAddress, anonMode, getAddressSigner, getThreadSigner, postCid]);
return { isCommentAuthorMod, isAccountMod, isAccountCommentAuthor, commentAuthorRole, accountAuthorRole };
};