Files
bsky-social-app/src/view/com/profile/FollowButton.tsx
T
dan 7eaa573b57 [Statsig] Track likes, reposts, follows (#3195)
* [Statsig] Track likes

* Move tracking to intent

* Track repost/unrepost

* Track profile follows/unfollows

* Less copy paste

* Reorder
2024-03-13 22:31:51 +00:00

74 lines
1.8 KiB
TypeScript

import React from 'react'
import {StyleProp, TextStyle, View} from 'react-native'
import {AppBskyActorDefs} from '@atproto/api'
import {Button, ButtonType} from '../util/forms/Button'
import * as Toast from '../util/Toast'
import {useProfileFollowMutationQueue} from '#/state/queries/profile'
import {Shadow} from '#/state/cache/types'
import {useLingui} from '@lingui/react'
import {msg} from '@lingui/macro'
export function FollowButton({
unfollowedType = 'inverted',
followedType = 'default',
profile,
labelStyle,
logContext,
}: {
unfollowedType?: ButtonType
followedType?: ButtonType
profile: Shadow<AppBskyActorDefs.ProfileViewBasic>
labelStyle?: StyleProp<TextStyle>
logContext: 'ProfileCard'
}) {
const [queueFollow, queueUnfollow] = useProfileFollowMutationQueue(
profile,
logContext,
)
const {_} = useLingui()
const onPressFollow = async () => {
try {
await queueFollow()
} catch (e: any) {
if (e?.name !== 'AbortError') {
Toast.show(_(msg`An issue occurred, please try again.`))
}
}
}
const onPressUnfollow = async () => {
try {
await queueUnfollow()
} catch (e: any) {
if (e?.name !== 'AbortError') {
Toast.show(_(msg`An issue occurred, please try again.`))
}
}
}
if (!profile.viewer) {
return <View />
}
if (profile.viewer.following) {
return (
<Button
type={followedType}
labelStyle={labelStyle}
onPress={onPressUnfollow}
label={_(msg({message: 'Unfollow', context: 'action'}))}
/>
)
} else {
return (
<Button
type={unfollowedType}
labelStyle={labelStyle}
onPress={onPressFollow}
label={_(msg({message: 'Follow', context: 'action'}))}
/>
)
}
}