Something went wrong. Try again.
Mirror — see github.com/blacksky-algorithms/blacksky.community
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183import {View} from 'react-native'
import {hasKnownBadge} from '#/lib/badges'import {HITSLOP_20} from '#/lib/constants'import {useProfileShadow} from '#/state/cache/profile-shadow'import {atoms as a, useAlf, type ViewStyleProp} from '#/alf'import {useNativeFontScale} from '#/alf/util/dimensions'import {ActorBadgeButtons, ActorBadges} from '#/components/badges'import {BotBadge, BotBadgeButton, isBotAccount} from '#/components/BotBadge'import {useSimpleVerificationState} from '#/components/verification'import {VerificationCheck} from '#/components/verification/VerificationCheck'import {VerificationCheckButton} from '#/components/verification/VerificationCheckButton'import type * as bsky from '#/types/bsky'import {BetaBadge, BetaBadgeButton, useIsBetaBadgeVisible} from './BetaBadge'
export type Size = 'xs' | 'sm' | 'md' | 'lg' | 'xl'
const verificationIconSizes: Record<Size, number> = { xs: 10, sm: 12, md: 14, lg: 18, xl: 22,} as const
const botIconSizes: Record<Size, number> = { xs: 11, sm: 13, md: 15, lg: 19, xl: 23,} as const
const badgeIconSizes: Record<Size, number> = { xs: 12, sm: 14, md: 16, lg: 20, xl: 24,} as const
const betaIconSizes: Record<Size, number> = { xs: 8, sm: 8, md: 8, lg: 10, xl: 12,} as const
/* * Inside a Text, the badges sit with their bottom on the baseline on every * platform (iOS needs patches/react-native/inline-view-baseline.patch). They * are a little taller than the cap height of the names they follow, which * leaves them about 0.75pt above its centre on iOS, Android and web (measured * in the Storybook "Profile badges" section), so nudge them down by that. */const INLINE_OFFSET = 0.75
const betaBadgePadding: Record<Size, number> = { xs: 1, sm: 2, md: 3, lg: 4, xl: 5,} as const
export function ProfileBadges({ profile, interactive = false, inline = false, size, style, allowFontScaling = true,}: ViewStyleProp & { profile: bsky.profile.AnyProfileView interactive?: boolean /** * Set when the badges are rendered inside a `Text`, after a name, so that * they're centred on the name's cap height. */ inline?: boolean size: Size allowFontScaling?: boolean}) { const shadowed = useProfileShadow(profile) const verification = useSimpleVerificationState({profile}) const badgeVisibility = [ verification.showBadge, useIsBetaBadgeVisible(profile), isBotAccount(shadowed), ] const badgeCount = badgeVisibility.filter(Boolean).length const nativeScaleMultiplier = useNativeFontScale() const { fonts: {scaleMultiplier: alfScaleMultiplier}, } = useAlf()
const hasBadges = hasKnownBadge(shadowed)
if (badgeCount < 1 && !hasBadges) return null
const isOnTheSmallSide = size === 'xs' || size === 'sm'
const scaleMultiplier = allowFontScaling ? nativeScaleMultiplier * alfScaleMultiplier : 1
const verificationIconWidth = verificationIconSizes[size] * scaleMultiplier const botIconWidth = botIconSizes[size] * scaleMultiplier const badgeIconWidth = badgeIconSizes[size] * scaleMultiplier const betaIconWidth = betaIconSizes[size] * scaleMultiplier const betaBadgeScaledPadding = betaBadgePadding[size] * scaleMultiplier
const gap = isOnTheSmallSide ? a.gap_2xs : a.gap_xs const padding = gap.gap / 2 const hitSlops = [] let visibleBadgeIndex = 0 for (const isVisible of badgeVisibility) { if (!isVisible) { hitSlops.push(HITSLOP_20) continue } const index = visibleBadgeIndex++ hitSlops.push({ ...HITSLOP_20, left: index === 0 ? HITSLOP_20.left : padding, right: index === badgeCount - 1 ? HITSLOP_20.right : padding, }) }
return ( <View style={[ a.flex_row, a.align_center, gap, inline && [ a.px_2xs, {transform: [{translateY: INLINE_OFFSET * scaleMultiplier}]}, ], style, ]}> {interactive ? ( <> <VerificationCheckButton profile={shadowed} width={verificationIconWidth} hitSlop={hitSlops[0]} /> <BetaBadgeButton profile={shadowed} width={betaIconWidth} padding={betaBadgeScaledPadding} hitSlop={hitSlops[1]} /> <BotBadgeButton profile={shadowed} width={botIconWidth} hitSlop={hitSlops[2]} /> <ActorBadgeButtons profile={shadowed} width={badgeIconWidth} /> </> ) : ( <> {verification.showBadge ? ( <VerificationCheck verifier={verification.role === 'verifier'} width={verificationIconWidth} /> ) : null} <BetaBadge profile={shadowed} width={betaIconWidth} padding={betaBadgeScaledPadding} /> <ActorBadges profile={shadowed} width={badgeIconWidth} /> <BotBadge profile={shadowed} width={botIconWidth} /> </> )} </View> )}