2024-09-03 22:06:07 +02:00
|
|
|
import { useMemo } from 'react';
|
2026-03-09 17:01:35 +08:00
|
|
|
import { useAccount } from '@bitsocialnet/bitsocial-react-hooks';
|
2026-03-13 13:25:10 +08:00
|
|
|
import { useCommunityField } from './use-stable-community';
|
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;
|
2026-03-13 13:25:10 +08:00
|
|
|
communityAddress?: string;
|
2024-09-04 16:15:47 +02:00
|
|
|
postCid?: string;
|
2024-06-04 16:06:28 +02:00
|
|
|
}
|
|
|
|
|
|
2026-04-17 10:48:27 +07:00
|
|
|
const useAuthorPrivileges = ({ commentAuthorAddress, communityAddress }: AuthorPrivilegesProps) => {
|
2024-09-04 16:15:47 +02:00
|
|
|
const account = useAccount();
|
|
|
|
|
const accountAuthorAddress = account?.author?.address;
|
2026-01-02 16:42:16 +01:00
|
|
|
// Only subscribe to roles field to avoid rerenders from updatingState changes
|
2026-04-17 10:48:27 +07:00
|
|
|
const roles = useCommunityField(communityAddress, (community) => community?.roles);
|
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-11-25 14:57:39 +01:00
|
|
|
const isAccountCommentAuthor = accountAuthorAddress === commentAuthorAddress;
|
2024-09-03 22:06:07 +02:00
|
|
|
|
|
|
|
|
return { isCommentAuthorMod, isAccountMod, isAccountCommentAuthor, commentAuthorRole, accountAuthorRole };
|
2025-11-25 14:57:39 +01:00
|
|
|
}, [roles, commentAuthorAddress, accountAuthorAddress]);
|
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;
|