Something went wrong. Try again.
Mirror — see github.com/blacksky-algorithms/blacksky.community
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359import {memo, useCallback, useMemo, useState} from 'react'import {View} from 'react-native'import { type AppBskyActorDefs, type AppBskyLabelerDefs, moderateProfile, type ModerationOpts, type RichText as RichTextAPI,} from '@atproto/api'import {msg, plural} from '@lingui/core/macro'import {useLingui} from '@lingui/react'import {Plural, Trans} from '@lingui/react/macro'
// eslint-disable-next-line @typescript-eslint/no-unused-varsimport {MAX_LABELERS} from '#/lib/constants'import {useHaptics} from '#/lib/haptics'import {isAppLabeler} from '#/lib/moderation'import {useProfileShadow} from '#/state/cache/profile-shadow'import {type Shadow} from '#/state/cache/types'import {useLabelerSubscriptionMutation} from '#/state/queries/labeler'import {useLikeMutation, useUnlikeMutation} from '#/state/queries/like'import {usePreferencesQuery} from '#/state/queries/preferences'import {useRequireAuth, useSession} from '#/state/session'import {ProfileMenu} from '#/view/com/profile/ProfileMenu'import {atoms as a, tokens, useTheme} from '#/alf'import {Button, ButtonText} from '#/components/Button'import {type DialogOuterProps, useDialogControl} from '#/components/Dialog'import { Heart2_Filled_Stroke2_Corner0_Rounded as HeartFilled, Heart2_Stroke2_Corner0_Rounded as Heart,} from '#/components/icons/Heart2'import {Link} from '#/components/Link'import * as Prompt from '#/components/Prompt'import {RichText} from '#/components/RichText'import * as Toast from '#/components/Toast'import {Text} from '#/components/Typography'import {useAnalytics} from '#/analytics'import {IS_IOS} from '#/env'import {ProfileHeaderDisplayName} from './DisplayName'import {EditProfileDialog} from './EditProfileDialog'import {ProfileHeaderHandle} from './Handle'import {ProfileHeaderMetrics} from './Metrics'import {ProfileHeaderShell} from './Shell'
interface Props { profile: AppBskyActorDefs.ProfileViewDetailed labeler: AppBskyLabelerDefs.LabelerViewDetailed descriptionRT: RichTextAPI | null moderationOpts: ModerationOpts hideBackButton?: boolean isPlaceholderProfile?: boolean}
let ProfileHeaderLabeler = ({ profile: profileUnshadowed, labeler, descriptionRT, moderationOpts, hideBackButton = false, isPlaceholderProfile,}: Props): React.ReactNode => { const profile: Shadow<AppBskyActorDefs.ProfileViewDetailed> = useProfileShadow(profileUnshadowed) const t = useTheme() const ax = useAnalytics() const {_} = useLingui() const {currentAccount, hasSession} = useSession() const playHaptic = useHaptics() const isSelf = currentAccount?.did === profile.did
const moderation = useMemo( () => moderateProfile(profile, moderationOpts), [profile, moderationOpts], ) const {mutateAsync: likeMod, isPending: isLikePending} = useLikeMutation() const {mutateAsync: unlikeMod, isPending: isUnlikePending} = useUnlikeMutation() const [likeUri, setLikeUri] = useState(labeler.viewer?.like || '') const [likeCount, setLikeCount] = useState(labeler.likeCount || 0)
const onToggleLiked = useCallback(async () => { if (!labeler) { return } try { playHaptic()
if (likeUri) { await unlikeMod({uri: likeUri}) setLikeCount(c => c - 1) setLikeUri('') } else { const res = await likeMod({uri: labeler.uri, cid: labeler.cid}) setLikeCount(c => c + 1) setLikeUri(res.uri) } } catch (e: any) { Toast.show( _( msg`There was an issue contacting the server, please check your internet connection and try again.`, ), {type: 'error'}, ) ax.logger.error(`Failed to toggle labeler like`, {message: e.message}) } }, [ax, labeler, playHaptic, likeUri, unlikeMod, likeMod, _])
return ( <ProfileHeaderShell profile={profile} moderation={moderation} hideBackButton={hideBackButton} isPlaceholderProfile={isPlaceholderProfile}> <View style={[a.px_lg, a.pt_md, a.pb_sm]} pointerEvents={IS_IOS ? 'auto' : 'box-none'}> <View style={[a.flex_row, a.justify_end, a.align_center, a.gap_xs, a.pb_lg]} pointerEvents={IS_IOS ? 'auto' : 'box-none'}> <HeaderLabelerButtons profile={profile} /> </View> <View style={[a.flex_col, a.gap_2xs, a.pt_2xs, a.pb_md]}> <ProfileHeaderDisplayName profile={profile} moderation={moderation} /> <ProfileHeaderHandle profile={profile} /> </View> {!isPlaceholderProfile && ( <> {isSelf && <ProfileHeaderMetrics profile={profile} />} {descriptionRT && !moderation.ui('profileView').blur ? ( <View pointerEvents="auto"> <RichText testID="profileHeaderDescription" style={[a.text_md]} numberOfLines={15} value={descriptionRT} enableTags authorHandle={profile.handle} /> </View> ) : undefined} {!isAppLabeler(profile.did) && ( <View style={[a.flex_row, a.gap_xs, a.align_center, a.pt_lg]}> <Button testID="toggleLikeBtn" size="small" color="secondary" shape="round" label={_(msg`Like this labeler`)} disabled={!hasSession || isLikePending || isUnlikePending} onPress={onToggleLiked}> {likeUri ? ( <HeartFilled fill={t.palette.negative_400} /> ) : ( <Heart fill={t.atoms.text_contrast_medium.color} /> )} </Button>
{typeof likeCount === 'number' && ( <Link to={{ screen: 'ProfileLabelerLikedBy', params: { name: labeler.creator.handle || labeler.creator.did, }, }} size="tiny" label={_( msg`Liked by ${plural(likeCount, { one: '# user', other: '# users', })}`, )}> {({hovered, focused, pressed}) => ( <Text style={[ a.font_semi_bold, a.text_sm, t.atoms.text_contrast_medium, (hovered || focused || pressed) && t.atoms.text_contrast_high, ]}> <Trans> Liked by{' '} <Plural value={likeCount} one="# user" other="# users" /> </Trans> </Text> )} </Link> )} </View> )} </> )} </View> </ProfileHeaderShell> )}ProfileHeaderLabeler = memo(ProfileHeaderLabeler)export {ProfileHeaderLabeler}
/** * Keep this in sync with the value of {@link MAX_LABELERS} */function CantSubscribePrompt({ control,}: { control: DialogOuterProps['control']}) { const {_} = useLingui() return ( <Prompt.Outer control={control}> <Prompt.Content> <Prompt.TitleText>Unable to subscribe</Prompt.TitleText> <Prompt.DescriptionText> <Trans> We're sorry! You can only subscribe to twenty labelers, and you've reached your limit of twenty. </Trans> </Prompt.DescriptionText> </Prompt.Content> <Prompt.Actions> <Prompt.Action onPress={() => control.close()} cta={_(msg`OK`)} /> </Prompt.Actions> </Prompt.Outer> )}
export function HeaderLabelerButtons({ profile, minimal = false,}: { profile: Shadow<AppBskyActorDefs.ProfileViewDetailed> /** disable the subscribe button */ minimal?: boolean}) { const t = useTheme() const ax = useAnalytics() const {_} = useLingui() const {currentAccount} = useSession() const requireAuth = useRequireAuth() const playHaptic = useHaptics() const editProfileControl = useDialogControl() const {data: preferences} = usePreferencesQuery() const { mutateAsync: toggleSubscription, variables, reset, } = useLabelerSubscriptionMutation() const isSubscribed = variables?.subscribe ?? preferences?.moderationPrefs.labelers.find(l => l.did === profile.did)
const cantSubscribePrompt = Prompt.usePromptControl()
const isMe = currentAccount?.did === profile.did
const onPressSubscribe = () => requireAuth(async (): Promise<void> => { playHaptic() const subscribe = !isSubscribed
try { await toggleSubscription({ did: profile.did, subscribe, })
ax.metric( subscribe ? 'moderation:subscribedToLabeler' : 'moderation:unsubscribedFromLabeler', {}, ) } catch (e: any) { reset() if (e.message === 'MAX_LABELERS') { cantSubscribePrompt.open() return } ax.logger.error(`Failed to subscribe to labeler`, {message: e.message}) } }) return ( <> {isMe ? ( <> <Button testID="profileHeaderEditProfileButton" size="small" color="secondary" onPress={editProfileControl.open} label={_(msg`Edit profile`)} style={a.rounded_full}> <ButtonText> <Trans>Edit Profile</Trans> </ButtonText> </Button> <EditProfileDialog profile={profile} control={editProfileControl} /> </> ) : !isAppLabeler(profile.did) && !minimal ? ( // hidden in the minimal header, because it's not shadowed so the two buttons // can get out of sync. if you want to reenable, you'll need to add shadowing // to the subscribed state -sfn <Button testID="toggleSubscribeBtn" label={ isSubscribed ? _(msg`Unsubscribe from this labeler`) : _(msg`Subscribe to this labeler`) } onPress={onPressSubscribe}> {state => ( <View style={[ { paddingVertical: 9, paddingHorizontal: 12, borderRadius: 6, gap: 6, backgroundColor: isSubscribed ? state.hovered || state.pressed ? t.palette.contrast_50 : t.palette.contrast_25 : state.hovered || state.pressed ? tokens.color.temp_purple_dark : tokens.color.temp_purple, }, ]}> <Text style={[ { color: isSubscribed ? t.palette.contrast_700 : t.palette.white, }, a.font_semi_bold, a.text_center, a.leading_tight, ]}> {isSubscribed ? ( <Trans>Unsubscribe</Trans> ) : ( <Trans>Subscribe to Labeler</Trans> )} </Text> </View> )} </Button> ) : null} <ProfileMenu profile={profile} /> <CantSubscribePrompt control={cantSubscribePrompt} /> </> )}