Something went wrong. Try again.
Mirror — see github.com/blacksky-algorithms/blacksky.community
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464import {memo, useMemo, useState} from 'react'import {View} from 'react-native'import { moderateProfile, type ModerationDecision, type ModerationOpts,} from '@bsky/sdk/moderation'import {type RichText as RichTextAPI} from '@bsky/sdk/richtext'import {msg} from '@lingui/core/macro'import {useLingui} from '@lingui/react'import {Trans} from '@lingui/react/macro'
import {useHaptics} from '#/lib/haptics'import {sanitizeDisplayName} from '#/lib/strings/display-names'import {logger} from '#/logger'import {type Shadow, useProfileShadow} from '#/state/cache/profile-shadow'import { useProfileBlockMutationQueue, useProfileFollowMutationQueue,} from '#/state/queries/profile'import {useRequireAuth, useSession} from '#/state/session'import {ProfileMenu} from '#/view/com/profile/ProfileMenu'import {atoms as a, platform} from '#/alf'import {SubscribeProfileButton} from '#/components/activity-notifications/SubscribeProfileButton'import {Button, ButtonIcon, ButtonText} from '#/components/Button'import {DebugFieldDisplay} from '#/components/DebugFieldDisplay'import {useDialogControl} from '#/components/Dialog'import {MessageProfileButton} from '#/components/dms/MessageProfileButton'import {ArrowShareRight_Stroke2_Corner2_Rounded as ArrowShareRight} from '#/components/icons/ArrowShareRight'import {PlusLarge_Stroke2_Corner0_Rounded as Plus} from '#/components/icons/Plus'import { KnownFollowers, shouldShowKnownFollowers,} from '#/components/KnownFollowers'import * as Prompt from '#/components/Prompt'import {RichText} from '#/components/RichText'import * as Toast from '#/components/Toast'import {useAnalytics} from '#/analytics'import {IS_IOS, IS_NATIVE} from '#/env'import {InviteFriendsDialog} from '#/features/inviteFriends'import {useActorStatus} from '#/features/liveNow'import {ProfileHeaderLinks} from '#/features/profileLinks'import {type app} from '#/lexicons'import {GermButton, isGermButtonShown} from '../components/GermButton'import {ProfileHeaderDisplayName} from './DisplayName'import {EditProfileDialog} from './EditProfileDialog'import {ProfileHeaderHandle} from './Handle'import {ProfileHeaderMetrics} from './Metrics'import {ProfileHeaderShell} from './Shell'import {ProfileHeaderSuggestedFollows} from './SuggestedFollows'
interface Props { profile: app.bsky.actor.defs.ProfileViewDetailed descriptionRT: RichTextAPI | null moderationOpts: ModerationOpts hideBackButton?: boolean isPlaceholderProfile?: boolean}
let ProfileHeaderStandard = ({ profile: profileUnshadowed, descriptionRT, moderationOpts, hideBackButton = false, isPlaceholderProfile,}: Props): React.ReactNode => { const profile = useProfileShadow<app.bsky.actor.defs.ProfileViewDetailed>(profileUnshadowed) const {currentAccount} = useSession() const {_} = useLingui() const moderation = useMemo( () => moderateProfile(profile, moderationOpts), [profile, moderationOpts], ) const [, queueUnblock] = useProfileBlockMutationQueue(profile) const unblockPromptControl = Prompt.usePromptControl() const [showSuggestedFollows, setShowSuggestedFollows] = useState(false) const [hasSeenAllSuggestedFollows, setHasSeenAllSuggestedFollows] = useState(false) const isBlockedUser = profile.viewer?.blocking || profile.viewer?.blockedBy || profile.viewer?.blockingByList
const unblockAccount = async () => { try { await queueUnblock() Toast.show(_(msg({message: 'Account unblocked', context: 'toast'}))) } catch (err) { const e = err as Error if (e?.name !== 'AbortError') { logger.error('Failed to unblock account', {message: e}) Toast.show(_(msg`There was an issue! ${e.toString()}`), {type: 'error'}) } } }
const onRequestHide = () => { setHasSeenAllSuggestedFollows(true) setShowSuggestedFollows(false) }
const isMe = currentAccount?.did === profile.did
// Known followers are merged from Bluesky's API in useProfileQuery const knownFollowers = profile.viewer?.knownFollowers
const {isActive: live} = useActorStatus(profile)
return ( <> <ProfileHeaderShell profile={profile} moderation={moderation} hideBackButton={hideBackButton} isPlaceholderProfile={isPlaceholderProfile}> <View style={[a.px_lg, a.pt_md, a.pb_sm, a.overflow_hidden]} pointerEvents={IS_IOS ? 'auto' : 'box-none'}> <View style={[ {paddingLeft: 90}, a.flex_row, a.align_center, a.justify_end, a.gap_xs, a.pb_sm, a.flex_wrap, ]} pointerEvents={IS_IOS ? 'auto' : 'box-none'}> <HeaderStandardButtons profile={profile} moderation={moderation} moderationOpts={moderationOpts} onFollow={() => setShowSuggestedFollows(true)} onUnfollow={() => setShowSuggestedFollows(false)} /> </View> <View style={[a.flex_col, a.gap_xs, a.pb_sm, live ? a.pt_sm : a.pt_2xs]}> <ProfileHeaderDisplayName profile={profile} moderation={moderation} /> <ProfileHeaderHandle profile={profile} moderation={moderation} /> </View> {!isPlaceholderProfile && !isBlockedUser && ( <View style={a.gap_md}> <ProfileHeaderMetrics profile={profile} /> {descriptionRT && !moderation.ui('profileView').blur ? ( <View pointerEvents="auto"> <RichText testID="profileHeaderDescription" style={[a.text_md]} numberOfLines={15} selectable={platform({android: false, default: true})} value={descriptionRT} enableTags authorHandle={profile.handle} shouldProxyLinks={true} /> </View> ) : undefined}
<ProfileHeaderLinks profile={profile} hidden={!!moderation.ui('profileView').blur} germButton={ isGermButtonShown( profile.associated?.germ, profile, currentAccount?.did, ) ? ( <GermButton germ={profile.associated.germ} profile={profile} /> ) : undefined } />
{!isMe && !isBlockedUser && shouldShowKnownFollowers(knownFollowers) && ( <View style={[a.flex_row, a.align_center, a.gap_sm]}> <KnownFollowers profile={ knownFollowers !== profile.viewer?.knownFollowers ? { ...profile, viewer: {...profile.viewer, knownFollowers}, } : profile } moderationOpts={moderationOpts} /> </View> )} </View> )}
<DebugFieldDisplay subject={profile} /> </View>
<Prompt.Basic control={unblockPromptControl} title={_(msg`Unblock Account?`)} description={_( msg`The account will be able to interact with you after unblocking.`, )} onConfirm={() => { void unblockAccount() }} confirmButtonCta={ profile.viewer?.blocking ? _(msg`Unblock`) : _(msg`Block`) } confirmButtonColor="negative" /> </ProfileHeaderShell>
<ProfileHeaderSuggestedFollows isExpanded={!hasSeenAllSuggestedFollows && showSuggestedFollows} actorDid={profile.did} onRequestHide={onRequestHide} /> </> )}
ProfileHeaderStandard = memo(ProfileHeaderStandard)export {ProfileHeaderStandard}
export function HeaderStandardButtons({ profile, moderation, moderationOpts, onFollow, onUnfollow, minimal,}: { profile: Shadow<app.bsky.actor.defs.ProfileViewDetailed> moderation: ModerationDecision moderationOpts: ModerationOpts onFollow?: () => void onUnfollow?: () => void minimal?: boolean}) { const {_} = useLingui() const ax = useAnalytics() const {hasSession, currentAccount} = useSession() const haptics = useHaptics() const requireAuth = useRequireAuth() const [queueFollow, queueUnfollow] = useProfileFollowMutationQueue( profile, 'ProfileHeader', ) const [, queueUnblock] = useProfileBlockMutationQueue(profile) const editProfileControl = useDialogControl() const inviteFriendsControl = useDialogControl() const unblockPromptControl = Prompt.usePromptControl()
const isMe = currentAccount?.did === profile.did
const onPressFollow = () => { haptics.confirm() const displayNameOrHandle = profile.displayName || profile.handle requireAuth(async () => { try { await queueFollow() if (onFollow) { onFollow() } Toast.show( _( msg`Following ${sanitizeDisplayName( displayNameOrHandle, moderation.ui('displayName'), )}`, ), ) } catch (err) { const e = err as Error if (e?.name !== 'AbortError') { logger.error('Failed to follow', {message: String(e)}) Toast.show(_(msg`There was an issue! ${e.toString()}`), { type: 'error', }) } } }) }
const onPressUnfollow = () => { haptics.confirm() const displayNameOrHandle = profile.displayName || profile.handle requireAuth(async () => { try { await queueUnfollow() if (onUnfollow) { onUnfollow() } Toast.show( _( msg`No longer following ${sanitizeDisplayName( displayNameOrHandle, moderation.ui('displayName'), )}`, ), {type: 'default'}, ) } catch (err) { const e = err as Error if (e?.name !== 'AbortError') { logger.error('Failed to unfollow', {message: String(e)}) Toast.show(_(msg`There was an issue! ${e.toString()}`), { type: 'error', }) } } }) }
const unblockAccount = async () => { try { await queueUnblock() Toast.show(_(msg({message: 'Account unblocked', context: 'toast'}))) } catch (err) { const e = err as Error if (e?.name !== 'AbortError') { logger.error('Failed to unblock account', {message: e}) Toast.show(_(msg`There was an issue! ${e.toString()}`), {type: 'error'}) } } }
const subscriptionsAllowed = useMemo(() => { switch (profile.associated?.activitySubscription?.allowSubscriptions) { case 'followers': case undefined: return !!profile.viewer?.following case 'mutuals': return !!profile.viewer?.following && !!profile.viewer.followedBy case 'none': default: return false } }, [profile])
return ( <> {isMe ? ( <> <Button testID="profileHeaderEditProfileButton" size="small" color="secondary" onPress={() => { haptics.tap() editProfileControl.open() }} label={_(msg`Edit profile`)}> <ButtonText> <Trans>Edit Profile</Trans> </ButtonText> </Button> {/* Invite friends is a native-only share sheet (the dialog is a no-op on web), so gate the entry point to avoid a dead button. */} {IS_NATIVE && ( <Button testID="profileHeaderShareButton" size="small" color="secondary" shape="round" // expand the 33pt button toward a 44pt touch target, capped // horizontally at half the 4pt row gap so the target cannot // overlap the neighboring buttons' own targets hitSlop={{top: 6, bottom: 6, left: 2, right: 2}} onPress={() => { haptics.tap() ax.metric('invite:dialog:open', {logContext: 'ProfileHeader'}) inviteFriendsControl.open() }} label={_(msg`Invite friends`)}> <ButtonIcon icon={ArrowShareRight} /> </Button> )} <EditProfileDialog profile={profile} control={editProfileControl} /> {IS_NATIVE && <InviteFriendsDialog control={inviteFriendsControl} />} </> ) : profile.viewer?.blocking ? ( profile.viewer?.blockingByList ? null : ( <Button testID="unblockBtn" size="small" color="secondary" label={_(msg`Unblock`)} disabled={!hasSession} onPress={() => unblockPromptControl.open()}> <ButtonText> <Trans context="action">Unblock</Trans> </ButtonText> </Button> ) ) : !profile.viewer?.blockedBy ? ( <> {hasSession && (!minimal || profile.viewer?.following) && ( <> {subscriptionsAllowed && ( <SubscribeProfileButton profile={profile} moderationOpts={moderationOpts} disableHint={minimal} /> )}
<MessageProfileButton profile={profile} /> </> )}
{(!minimal || !profile.viewer?.following) && ( <Button testID={profile.viewer?.following ? 'unfollowBtn' : 'followBtn'} size="small" color={profile.viewer?.following ? 'secondary' : 'primary'} label={ profile.viewer?.following ? _(msg`Unfollow ${profile.handle}`) : _(msg`Follow ${profile.handle}`) } onPress={ profile.viewer?.following ? onPressUnfollow : onPressFollow }> {!profile.viewer?.following && <ButtonIcon icon={Plus} />} <ButtonText> {profile.viewer?.following ? ( <Trans>Following</Trans> ) : profile.viewer?.followedBy ? ( <Trans>Follow back</Trans> ) : ( <Trans>Follow</Trans> )} </ButtonText> </Button> )} </> ) : null} <ProfileMenu profile={profile} />
<Prompt.Basic control={unblockPromptControl} title={_(msg`Unblock Account?`)} description={_( msg`The account will be able to interact with you after unblocking.`, )} onConfirm={() => { void unblockAccount() }} confirmButtonCta={_(msg`Unblock`)} confirmButtonColor="negative" /> </> )}