diff options
Diffstat (limited to 'src/view/com/profile/ProfileHeaderSuggestedFollows.tsx')
-rw-r--r-- | src/view/com/profile/ProfileHeaderSuggestedFollows.tsx | 83 |
1 files changed, 36 insertions, 47 deletions
diff --git a/src/view/com/profile/ProfileHeaderSuggestedFollows.tsx b/src/view/com/profile/ProfileHeaderSuggestedFollows.tsx index cf759ddd1..f648c9801 100644 --- a/src/view/com/profile/ProfileHeaderSuggestedFollows.tsx +++ b/src/view/com/profile/ProfileHeaderSuggestedFollows.tsx @@ -6,20 +6,16 @@ import Animated, { useAnimatedStyle, Easing, } from 'react-native-reanimated' -import {useQuery} from '@tanstack/react-query' import {AppBskyActorDefs, moderateProfile} from '@atproto/api' -import {observer} from 'mobx-react-lite' import { FontAwesomeIcon, FontAwesomeIconStyle, } from '@fortawesome/react-native-fontawesome' import * as Toast from '../util/Toast' -import {useStores} from 'state/index' import {usePalette} from 'lib/hooks/usePalette' import {Text} from 'view/com/util/text/Text' import {UserAvatar} from 'view/com/util/UserAvatar' -import {useFollowProfile} from 'lib/hooks/useFollowProfile' import {Button} from 'view/com/util/forms/Button' import {sanitizeDisplayName} from 'lib/strings/display-names' import {sanitizeHandle} from 'lib/strings/handles' @@ -27,6 +23,10 @@ import {makeProfileLink} from 'lib/routes/links' import {Link} from 'view/com/util/Link' import {useAnalytics} from 'lib/analytics/analytics' import {isWeb} from 'platform/detection' +import {useModerationOpts} from '#/state/queries/preferences' +import {useSuggestedFollowsByActorQuery} from '#/state/queries/suggested-follows' +import {useProfileShadow} from '#/state/cache/profile-shadow' +import {useProfileFollowMutationQueue} from '#/state/queries/profile' const OUTER_PADDING = 10 const INNER_PADDING = 14 @@ -43,7 +43,6 @@ export function ProfileHeaderSuggestedFollows({ }) { const {track} = useAnalytics() const pal = usePalette('default') - const store = useStores() const animatedHeight = useSharedValue(0) const animatedStyles = useAnimatedStyle(() => ({ opacity: animatedHeight.value / TOTAL_HEIGHT, @@ -66,31 +65,8 @@ export function ProfileHeaderSuggestedFollows({ } }, [active, animatedHeight, track]) - const {isLoading, data: suggestedFollows} = useQuery({ - enabled: active, - cacheTime: 0, - staleTime: 0, - queryKey: ['suggested_follows_by_actor', actorDid], - async queryFn() { - try { - const { - data: {suggestions}, - success, - } = await store.agent.app.bsky.graph.getSuggestedFollowsByActor({ - actor: actorDid, - }) - - if (!success) { - return [] - } - - store.me.follows.hydrateMany(suggestions) - - return suggestions - } catch (e) { - return [] - } - }, + const {isLoading, data} = useSuggestedFollowsByActorQuery({ + did: actorDid, }) return ( @@ -149,8 +125,8 @@ export function ProfileHeaderSuggestedFollows({ <SuggestedFollowSkeleton /> <SuggestedFollowSkeleton /> </> - ) : suggestedFollows ? ( - suggestedFollows.map(profile => ( + ) : data ? ( + data.suggestions.map(profile => ( <SuggestedFollow key={profile.did} profile={profile} /> )) ) : ( @@ -214,29 +190,43 @@ function SuggestedFollowSkeleton() { ) } -const SuggestedFollow = observer(function SuggestedFollowImpl({ - profile, +function SuggestedFollow({ + profile: profileUnshadowed, }: { profile: AppBskyActorDefs.ProfileView }) { const {track} = useAnalytics() const pal = usePalette('default') - const store = useStores() - const {following, toggle} = useFollowProfile(profile) - const moderation = moderateProfile(profile, store.preferences.moderationOpts) + const moderationOpts = useModerationOpts() + const profile = useProfileShadow(profileUnshadowed) + const [queueFollow, queueUnfollow] = useProfileFollowMutationQueue(profile) - const onPress = React.useCallback(async () => { + const onPressFollow = React.useCallback(async () => { try { - const {following: isFollowing} = await toggle() - - if (isFollowing) { - track('ProfileHeader:SuggestedFollowFollowed') + track('ProfileHeader:SuggestedFollowFollowed') + await queueFollow() + } catch (e: any) { + if (e?.name !== 'AbortError') { + Toast.show('An issue occurred, please try again.') } + } + }, [queueFollow, track]) + + const onPressUnfollow = React.useCallback(async () => { + try { + await queueUnfollow() } catch (e: any) { - Toast.show('An issue occurred, please try again.') + if (e?.name !== 'AbortError') { + Toast.show('An issue occurred, please try again.') + } } - }, [toggle, track]) + }, [queueUnfollow]) + if (!moderationOpts) { + return null + } + const moderation = moderateProfile(profile, moderationOpts) + const following = profile.viewer?.following return ( <Link href={makeProfileLink(profile)} @@ -278,13 +268,12 @@ const SuggestedFollow = observer(function SuggestedFollowImpl({ label={following ? 'Unfollow' : 'Follow'} type="inverted" labelStyle={{textAlign: 'center'}} - onPress={onPress} - withLoading + onPress={following ? onPressUnfollow : onPressFollow} /> </View> </Link> ) -}) +} const styles = StyleSheet.create({ suggestedFollowCardOuter: { |