about summary refs log tree commit diff
path: root/src/view/com/profile/FollowButton.tsx
blob: 217d326e8f62588f8f3c84f95d7522d6637e1ca9 (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
import React from 'react'
import {StyleProp, TextStyle, View} from 'react-native'
import {observer} from 'mobx-react-lite'
import {Button, ButtonType} from '../util/forms/Button'
import * as Toast from '../util/Toast'
import {FollowState} from 'state/models/cache/my-follows'
import {useFollowDid} from 'lib/hooks/useFollowDid'

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

  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}
    />
  )
})