mirror of https://git.lenooby09.tech/LeNooby09/social-app.git
1import React from 'react'
2import {type AppBskyActorDefs} from '@atproto/api'
3import {msg, Trans} from '@lingui/macro'
4import {useLingui} from '@lingui/react'
5import {useNavigation} from '@react-navigation/native'
6
7import {logger} from '#/logger'
8import {useProfileShadow} from '#/state/cache/profile-shadow'
9import {
10 useProfileFollowMutationQueue,
11 useProfileQuery,
12} from '#/state/queries/profile'
13import {useRequireAuth} from '#/state/session'
14import * as Toast from '#/view/com/util/Toast'
15import {atoms as a, useBreakpoints} from '#/alf'
16import {Button, ButtonIcon, ButtonText} from '#/components/Button'
17import {Check_Stroke2_Corner0_Rounded as Check} from '#/components/icons/Check'
18import {PlusLarge_Stroke2_Corner0_Rounded as Plus} from '#/components/icons/Plus'
19
20export function ThreadItemAnchorFollowButton({did}: {did: string}) {
21 const {data: profile, isLoading} = useProfileQuery({did})
22
23 // We will never hit this - the profile will always be cached or loaded above
24 // but it keeps the typechecker happy
25 if (isLoading || !profile) return null
26
27 return <PostThreadFollowBtnLoaded profile={profile} />
28}
29
30function PostThreadFollowBtnLoaded({
31 profile: profileUnshadowed,
32}: {
33 profile: AppBskyActorDefs.ProfileViewDetailed
34}) {
35 const navigation = useNavigation()
36 const {_} = useLingui()
37 const {gtMobile} = useBreakpoints()
38 const profile = useProfileShadow(profileUnshadowed)
39 const [queueFollow, queueUnfollow] = useProfileFollowMutationQueue(
40 profile,
41 'PostThreadItem',
42 )
43 const requireAuth = useRequireAuth()
44
45 const isFollowing = !!profile.viewer?.following
46 const isFollowedBy = !!profile.viewer?.followedBy
47 const [wasFollowing, setWasFollowing] = React.useState<boolean>(isFollowing)
48
49 // This prevents the button from disappearing as soon as we follow.
50 const showFollowBtn = React.useMemo(
51 () => !isFollowing || !wasFollowing,
52 [isFollowing, wasFollowing],
53 )
54
55 /**
56 * We want this button to stay visible even after following, so that the user can unfollow if they want.
57 * However, we need it to disappear after we push to a screen and then come back. We also need it to
58 * show up if we view the post while following, go to the profile and unfollow, then come back to the
59 * post.
60 *
61 * We want to update wasFollowing both on blur and on focus so that we hit all these cases. On native,
62 * we could do this only on focus because the transition animation gives us time to not notice the
63 * sudden rendering of the button. However, on web if we do this, there's an obvious flicker once the
64 * button renders. So, we update the state in both cases.
65 */
66 React.useEffect(() => {
67 const updateWasFollowing = () => {
68 if (wasFollowing !== isFollowing) {
69 setWasFollowing(isFollowing)
70 }
71 }
72
73 const unsubscribeFocus = navigation.addListener('focus', updateWasFollowing)
74 const unsubscribeBlur = navigation.addListener('blur', updateWasFollowing)
75
76 return () => {
77 unsubscribeFocus()
78 unsubscribeBlur()
79 }
80 }, [isFollowing, wasFollowing, navigation])
81
82 const onPress = React.useCallback(() => {
83 if (!isFollowing) {
84 requireAuth(async () => {
85 try {
86 await queueFollow()
87 } catch (e: any) {
88 if (e?.name !== 'AbortError') {
89 logger.error('Failed to follow', {message: String(e)})
90 Toast.show(_(msg`There was an issue! ${e.toString()}`), 'xmark')
91 }
92 }
93 })
94 } else {
95 requireAuth(async () => {
96 try {
97 await queueUnfollow()
98 } catch (e: any) {
99 if (e?.name !== 'AbortError') {
100 logger.error('Failed to unfollow', {message: String(e)})
101 Toast.show(_(msg`There was an issue! ${e.toString()}`), 'xmark')
102 }
103 }
104 })
105 }
106 }, [isFollowing, requireAuth, queueFollow, _, queueUnfollow])
107
108 if (!showFollowBtn) return null
109
110 return (
111 <Button
112 testID="followBtn"
113 label={_(msg`Follow ${profile.handle}`)}
114 onPress={onPress}
115 size="small"
116 variant="solid"
117 color={isFollowing ? 'secondary' : 'secondary_inverted'}
118 style={[a.rounded_full]}>
119 {gtMobile && (
120 <ButtonIcon
121 icon={isFollowing ? Check : Plus}
122 position="left"
123 size="sm"
124 />
125 )}
126 <ButtonText>
127 {!isFollowing ? (
128 isFollowedBy ? (
129 <Trans>Follow back</Trans>
130 ) : (
131 <Trans>Follow</Trans>
132 )
133 ) : (
134 <Trans>Following</Trans>
135 )}
136 </ButtonText>
137 </Button>
138 )
139}