mirror of
https://github.com/bitsocialnet/5chan.git
synced 2026-08-03 07:41:04 +02:00
31 lines
1.5 KiB
TypeScript
31 lines
1.5 KiB
TypeScript
import { useMemo } from 'react';
|
|
import { useAccount } from '@bitsocialnet/bitsocial-react-hooks';
|
|
import { useCommunityField } from './use-stable-community';
|
|
|
|
interface AuthorPrivilegesProps {
|
|
commentAuthorAddress: string;
|
|
communityAddress?: string;
|
|
postCid?: string;
|
|
}
|
|
|
|
const useAuthorPrivileges = ({ commentAuthorAddress, communityAddress }: AuthorPrivilegesProps) => {
|
|
const account = useAccount();
|
|
const accountAuthorAddress = account?.author?.address;
|
|
// Only subscribe to roles field to avoid rerenders from updatingState changes
|
|
const roles = useCommunityField(communityAddress, (community) => community?.roles);
|
|
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';
|
|
|
|
const isAccountCommentAuthor = accountAuthorAddress === commentAuthorAddress;
|
|
|
|
return { isCommentAuthorMod, isAccountMod, isAccountCommentAuthor, commentAuthorRole, accountAuthorRole };
|
|
}, [roles, commentAuthorAddress, accountAuthorAddress]);
|
|
|
|
return { isCommentAuthorMod, isAccountMod, isAccountCommentAuthor, commentAuthorRole, accountAuthorRole };
|
|
};
|
|
|
|
export default useAuthorPrivileges;
|