about summary refs log tree commit diff
path: root/src/view/com/profile/FollowButton.tsx
blob: adb496f6d7b5017dda15404d4c827520a988ba7b (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
import React from 'react'
import {StyleProp, TextStyle, View} from 'react-native'
import {observer} from 'mobx-react-lite'
import {AppBskyActorDefs} from '@atproto/api'
import {Button, ButtonType} from '../util/forms/Button'
import * as Toast from '../util/Toast'
import {FollowState} from 'state/models/cache/my-follows'
import {useFollowProfile} from 'lib/hooks/useFollowProfile'

export const FollowButton = observer(function FollowButtonImpl({
  unfollowedType = 'inverted',
  followedType = 'default',
  profile,
  onToggleFollow,
  labelStyle,
}: {
  unfollowedType?: ButtonType
  followedType?: ButtonType
  profile: AppBskyActorDefs.ProfileViewBasic
  onToggleFollow?: (v: boolean) => void
  labelStyle?: StyleProp<TextStyle>
}) {
  const {state, following, toggle} = useFollowProfile(profile)

  const onPress = React.useCallback(async () => {
    try {
      const {following} = await toggle()
      onToggleFollow?.(following)
    } catch (e: any) {
      Toast.show('An issue occurred, please try again.')
    }
  }, [toggle, onToggleFollow])

  if (state === FollowState.Unknown) {
    return <View />
  }

  return (
    <Button
      type={following ? followedType : unfollowedType}
      labelStyle={labelStyle}
      onPress={onPress}
      label={following ? 'Unfollow' : 'Follow'}
      withLoading={true}
    />
  )
})