Files
5chan/src/hooks/use-author-privileges.ts
T

31 lines
1.5 KiB
TypeScript
Raw Normal View History

import { useMemo } from 'react';
import { useAccount } from '@plebbit/plebbit-react-hooks';
import useSubplebbitsStore from '@plebbit/plebbit-react-hooks/dist/stores/subplebbits';
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;
const subplebbit = useSubplebbitsStore((state) => state.subplebbits[subplebbitAddress]);
const { roles } = subplebbit || {};
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 13:13:38 +01:00
const isAccountCommentAuthor = !!postCid && accountAuthorAddress === commentAuthorAddress;
return { isCommentAuthorMod, isAccountMod, isAccountCommentAuthor, commentAuthorRole, accountAuthorRole };
2025-11-25 13:13:38 +01:00
}, [roles, commentAuthorAddress, accountAuthorAddress, 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;