import { useEffect } from 'react'; import type { FC } from 'react'; import { FormattedMessage } from 'react-intl'; import { fetchRelationships } from '@/mastodon/actions/accounts'; import { useAccount } from '@/mastodon/hooks/useAccount'; import type { AccountRole } from '@/mastodon/models/account'; import { useAppDispatch, useAppSelector } from '@/mastodon/store'; import { AdminBadge, AutomatedBadge, Badge, BlockedBadge, GroupBadge, MutedBadge, } from '../badge'; import classes from './styles.module.scss'; export const AccountBadges: FC<{ accountId: string }> = ({ accountId }) => { const account = useAccount(accountId); const localDomain = useAppSelector( (state) => state.meta.get('domain') as string, ); const relationship = useAppSelector((state) => state.relationships.get(accountId), ); const dispatch = useAppDispatch(); useEffect(() => { if (!relationship) { dispatch(fetchRelationships([accountId])); } }, [accountId, dispatch, relationship]); const badges = []; if (!account) { return null; } const domain = account.acct.includes('@') ? account.acct.split('@')[1] : localDomain; account.roles.forEach((role) => { if (isAdminBadge(role)) { badges.push( , ); } else { badges.push( , ); } }); if (account.bot) { badges.push(); } if (account.group) { badges.push(); } if (relationship) { if (relationship.blocking) { badges.push(); } if (relationship.domain_blocking) { badges.push( } />, ); } if (relationship.muting) { badges.push( , ); } } if (!badges.length) { return null; } return
{badges}
; }; function isAdminBadge(role: AccountRole) { const name = role.name.toLowerCase(); return name === 'admin' || name === 'owner'; }