Files
5chan/src/hooks/use-author-privileges.ts
T
plebeius e556a9e662 chore(deps): replace plebbit/plebbit-react-hooks with bitsocialhq/pkc-react-hooks
bitsocialhq/pkc-react-hooks is a fork of plebbit-react-hooks that we're maintaining temporarily as an attempt to modernize the original repository using AI tools, possibly merging changes back into plebbit-react-hooks, which should also be rebranded to pkc-react-hooks eventually, under the org pkcprotocol.
2026-02-28 15:02:56 +08:00

31 lines
1.5 KiB
TypeScript

import { useMemo } from 'react';
import { useAccount } from '@bitsocialhq/pkc-react-hooks';
import { useSubplebbitField } from './use-stable-subplebbit';
interface AuthorPrivilegesProps {
commentAuthorAddress: string;
subplebbitAddress: string;
postCid?: string;
}
const useAuthorPrivileges = ({ commentAuthorAddress, subplebbitAddress }: AuthorPrivilegesProps) => {
const account = useAccount();
const accountAuthorAddress = account?.author?.address;
// Only subscribe to roles field to avoid rerenders from updatingState changes
const roles = useSubplebbitField(subplebbitAddress, (subplebbit) => subplebbit?.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;