2024-09-03 22:06:07 +02:00
|
|
|
import { useMemo } from 'react';
|
2025-03-05 17:10:26 +01:00
|
|
|
import { useAccount } from '@plebbit/plebbit-react-hooks';
|
|
|
|
|
import useSubplebbitsStore from '@plebbit/plebbit-react-hooks/dist/stores/subplebbits';
|
2024-09-03 22:06:07 +02:00
|
|
|
import useAnonModeStore from '../stores/use-anon-mode-store';
|
2024-09-04 16:15:47 +02:00
|
|
|
import useAnonMode from './use-anon-mode';
|
2024-06-04 16:06:28 +02:00
|
|
|
|
2024-06-08 11:27:29 +02:00
|
|
|
interface AuthorPrivilegesProps {
|
2024-06-04 16:06:28 +02:00
|
|
|
commentAuthorAddress: string;
|
|
|
|
|
subplebbitAddress: string;
|
2024-09-04 16:15:47 +02:00
|
|
|
postCid?: string;
|
2024-06-04 16:06:28 +02:00
|
|
|
}
|
|
|
|
|
|
2024-09-04 16:15:47 +02:00
|
|
|
const useAuthorPrivileges = ({ commentAuthorAddress, subplebbitAddress, postCid }: AuthorPrivilegesProps) => {
|
|
|
|
|
const account = useAccount();
|
|
|
|
|
const accountAuthorAddress = account?.author?.address;
|
2025-03-05 17:10:26 +01:00
|
|
|
const subplebbit = useSubplebbitsStore((state) => state.subplebbits[subplebbitAddress]);
|
|
|
|
|
const { roles } = subplebbit || {};
|
2024-09-04 16:15:47 +02:00
|
|
|
const { getAddressSigner, getThreadSigner } = useAnonModeStore();
|
|
|
|
|
const { anonMode } = useAnonMode(postCid);
|
2024-06-04 16:06:28 +02:00
|
|
|
|
2024-09-03 22:06:07 +02:00
|
|
|
const { isCommentAuthorMod, isAccountMod, isAccountCommentAuthor, commentAuthorRole, accountAuthorRole } = useMemo(() => {
|
|
|
|
|
const commentAuthorRole = roles?.[commentAuthorAddress]?.role;
|
|
|
|
|
const isCommentAuthorMod = commentAuthorRole === 'admin' || commentAuthorRole === 'owner' || commentAuthorRole === 'moderator';
|
|
|
|
|
const accountAuthorRole = roles?.[accountAuthorAddress]?.role;
|
|
|
|
|
const isAccountMod = accountAuthorRole === 'admin' || accountAuthorRole === 'owner' || accountAuthorRole === 'moderator';
|
|
|
|
|
|
2025-02-01 16:25:52 +01:00
|
|
|
let isAccountCommentAuthor = postCid && accountAuthorAddress === commentAuthorAddress;
|
2024-09-03 22:06:07 +02:00
|
|
|
|
2024-09-04 16:15:47 +02:00
|
|
|
if (!isAccountCommentAuthor && anonMode) {
|
|
|
|
|
const addressSigner = getAddressSigner(commentAuthorAddress);
|
|
|
|
|
const threadSigner = postCid ? getThreadSigner(postCid) : null;
|
|
|
|
|
|
|
|
|
|
isAccountCommentAuthor = (addressSigner && addressSigner.address === commentAuthorAddress) || (threadSigner && threadSigner.address === commentAuthorAddress);
|
2024-09-03 22:06:07 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return { isCommentAuthorMod, isAccountMod, isAccountCommentAuthor, commentAuthorRole, accountAuthorRole };
|
2024-09-04 16:15:47 +02:00
|
|
|
}, [roles, commentAuthorAddress, accountAuthorAddress, anonMode, getAddressSigner, getThreadSigner, postCid]);
|
2024-06-04 16:06:28 +02:00
|
|
|
|
2024-06-08 11:27:29 +02:00
|
|
|
return { isCommentAuthorMod, isAccountMod, isAccountCommentAuthor, commentAuthorRole, accountAuthorRole };
|
2024-06-04 16:06:28 +02:00
|
|
|
};
|
|
|
|
|
|
2024-06-08 11:27:29 +02:00
|
|
|
export default useAuthorPrivileges;
|