Skip to content

Commit

Permalink
Remove last usage of useFollowProfile
Browse files Browse the repository at this point in the history
  • Loading branch information
gaearon committed Nov 15, 2023
1 parent 2015155 commit 2963294
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 103 deletions.
55 changes: 0 additions & 55 deletions src/lib/hooks/useFollowProfile.ts

This file was deleted.

28 changes: 26 additions & 2 deletions src/state/queries/suggested-follows.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,24 @@
import {AppBskyActorGetSuggestions, moderateProfile} from '@atproto/api'
import {
AppBskyActorGetSuggestions,
AppBskyGraphGetSuggestedFollowsByActor,
moderateProfile,
} from '@atproto/api'
import {
useInfiniteQuery,
useMutation,
useQuery,
InfiniteData,
QueryKey,
} from '@tanstack/react-query'

import {useSession} from '#/state/session'
import {useModerationOpts} from '#/state/queries/preferences'

export const suggestedFollowsQueryKey = ['suggested-follows']
const suggestedFollowsQueryKey = ['suggested-follows']
const suggestedFollowsByActorQuery = (did: string) => [
'suggested-follows-by-actor',
did,
]

export function useSuggestedFollowsQuery() {
const {agent, currentAccount} = useSession()
Expand Down Expand Up @@ -60,6 +69,21 @@ export function useSuggestedFollowsQuery() {
})
}

export function useSuggestedFollowsByActorQuery({did}: {did: string}) {
const {agent} = useSession()

return useQuery<AppBskyGraphGetSuggestedFollowsByActor.OutputSchema, Error>({
queryKey: suggestedFollowsByActorQuery(did),
queryFn: async () => {
const res = await agent.app.bsky.graph.getSuggestedFollowsByActor({
actor: did,
})
return res.data
},
})
}

// TODO: Delete and replace usages with the one above.
export function useGetSuggestedFollowersByActor() {
const {agent} = useSession()

Expand Down
97 changes: 51 additions & 46 deletions src/view/com/profile/ProfileHeaderSuggestedFollows.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,27 +6,30 @@ 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'
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 {
useProfileFollowMutation,
useProfileUnfollowMutation,
} from '#/state/queries/profile'

const OUTER_PADDING = 10
const INNER_PADDING = 14
Expand All @@ -43,7 +46,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,
Expand All @@ -66,31 +68,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, dataUpdatedAt} = useSuggestedFollowsByActorQuery({
did: actorDid,
})

return (
Expand Down Expand Up @@ -149,9 +128,13 @@ export function ProfileHeaderSuggestedFollows({
<SuggestedFollowSkeleton />
<SuggestedFollowSkeleton />
</>
) : suggestedFollows ? (
suggestedFollows.map(profile => (
<SuggestedFollow key={profile.did} profile={profile} />
) : data ? (
data.suggestions.map(profile => (
<SuggestedFollow
key={profile.did}
profile={profile}
dataUpdatedAt={dataUpdatedAt}
/>
))
) : (
<View />
Expand Down Expand Up @@ -214,29 +197,51 @@ function SuggestedFollowSkeleton() {
)
}

const SuggestedFollow = observer(function SuggestedFollowImpl({
profile,
function SuggestedFollow({
profile: profileUnshadowed,
dataUpdatedAt,
}: {
profile: AppBskyActorDefs.ProfileView
dataUpdatedAt: number
}) {
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, dataUpdatedAt)
const followMutation = useProfileFollowMutation()
const unfollowMutation = useProfileUnfollowMutation()

const onPress = React.useCallback(async () => {
const onPressFollow = React.useCallback(async () => {
if (profile.viewer?.following) {
return
}
try {
const {following: isFollowing} = await toggle()
track('ProfileHeader:SuggestedFollowFollowed')
await followMutation.mutateAsync({did: profile.did})
} catch (e: any) {
Toast.show('An issue occurred, please try again.')
}
}, [followMutation, profile, track])

if (isFollowing) {
track('ProfileHeader:SuggestedFollowFollowed')
}
const onPressUnfollow = React.useCallback(async () => {
if (!profile.viewer?.following) {
return
}
try {
await unfollowMutation.mutateAsync({
did: profile.did,
followUri: profile.viewer?.following,
})
} catch (e: any) {
Toast.show('An issue occurred, please try again.')
}
}, [toggle, track])
}, [unfollowMutation, profile])

if (!moderationOpts) {
return null
}
const moderation = moderateProfile(profile, moderationOpts)
const following = profile.viewer?.following
return (
<Link
href={makeProfileLink(profile)}
Expand Down Expand Up @@ -278,13 +283,13 @@ const SuggestedFollow = observer(function SuggestedFollowImpl({
label={following ? 'Unfollow' : 'Follow'}
type="inverted"
labelStyle={{textAlign: 'center'}}
onPress={onPress}
onPress={following ? onPressUnfollow : onPressFollow}
withLoading
/>
</View>
</Link>
)
})
}

const styles = StyleSheet.create({
suggestedFollowCardOuter: {
Expand Down

0 comments on commit 2963294

Please sign in to comment.