mirror of https://git.lenooby09.tech/LeNooby09/social-app.git
1import React from 'react'
2import {View} from 'react-native'
3import {AppBskyActorDefs, ModerationDecision} from '@atproto/api'
4import {msg} from '@lingui/macro'
5import {useLingui} from '@lingui/react'
6import {useNavigation} from '@react-navigation/native'
7
8import {createHitslop} from '#/lib/constants'
9import {NavigationProp} from '#/lib/routes/types'
10import {sanitizeDisplayName} from '#/lib/strings/display-names'
11import {useProfileShadow} from '#/state/cache/profile-shadow'
12import {useSession} from '#/state/session'
13import {
14 DropdownItem,
15 NativeDropdown,
16} from '#/view/com/util/forms/NativeDropdown'
17import * as Toast from '#/view/com/util/Toast'
18import {atoms as a, select, useTheme} from '#/alf'
19import {Button} from '#/components/Button'
20import {useFollowMethods} from '#/components/hooks/useFollowMethods'
21import {PlusSmall_Stroke2_Corner0_Rounded as Plus} from '#/components/icons/Plus'
22
23export function AviFollowButton({
24 author,
25 moderation,
26 children,
27}: {
28 author: AppBskyActorDefs.ProfileViewBasic
29 moderation: ModerationDecision
30 children: React.ReactNode
31}) {
32 const {_} = useLingui()
33 const t = useTheme()
34 const profile = useProfileShadow(author)
35 const {follow} = useFollowMethods({
36 profile: profile,
37 logContext: 'AvatarButton',
38 })
39 const {currentAccount, hasSession} = useSession()
40 const navigation = useNavigation<NavigationProp>()
41
42 const name = sanitizeDisplayName(
43 profile.displayName || profile.handle,
44 moderation.ui('displayName'),
45 )
46 const isFollowing =
47 profile.viewer?.following || profile.did === currentAccount?.did
48
49 function onPress() {
50 follow()
51 Toast.show(_(msg`Following ${name}`))
52 }
53
54 const items: DropdownItem[] = [
55 {
56 label: _(msg`View profile`),
57 onPress: () => {
58 navigation.navigate('Profile', {name: profile.did})
59 },
60 icon: {
61 ios: {
62 name: 'arrow.up.right.square',
63 },
64 android: '',
65 web: ['far', 'arrow-up-right-from-square'],
66 },
67 },
68 {
69 label: _(msg`Follow ${name}`),
70 onPress: onPress,
71 icon: {
72 ios: {
73 name: 'person.badge.plus',
74 },
75 android: '',
76 web: ['far', 'user-plus'],
77 },
78 },
79 ]
80
81 return hasSession ? (
82 <View style={a.relative}>
83 {children}
84
85 {!isFollowing && (
86 <Button
87 label={_(msg`Open ${name} profile shortcut menu`)}
88 hitSlop={createHitslop(3)}
89 style={[
90 a.rounded_full,
91 a.absolute,
92 {
93 height: 30,
94 width: 30,
95 bottom: -7,
96 right: -7,
97 },
98 ]}>
99 <NativeDropdown items={items}>
100 <View
101 style={[a.h_full, a.w_full, a.justify_center, a.align_center]}>
102 <View
103 style={[
104 a.rounded_full,
105 a.align_center,
106 select(t.name, {
107 light: t.atoms.bg_contrast_100,
108 dim: t.atoms.bg_contrast_100,
109 dark: t.atoms.bg_contrast_200,
110 }),
111 {
112 borderWidth: 1,
113 borderColor: t.atoms.bg.backgroundColor,
114 },
115 ]}>
116 <Plus
117 size="sm"
118 fill={
119 select(t.name, {
120 light: t.atoms.bg_contrast_600,
121 dim: t.atoms.bg_contrast_500,
122 dark: t.atoms.bg_contrast_600,
123 }).backgroundColor
124 }
125 />
126 </View>
127 </View>
128 </NativeDropdown>
129 </Button>
130 )}
131 </View>
132 ) : (
133 children
134 )
135}