diff options
Diffstat (limited to 'src/state/queries/profile.ts')
-rw-r--r-- | src/state/queries/profile.ts | 80 |
1 files changed, 47 insertions, 33 deletions
diff --git a/src/state/queries/profile.ts b/src/state/queries/profile.ts index e81ea0f3f..19492cf66 100644 --- a/src/state/queries/profile.ts +++ b/src/state/queries/profile.ts @@ -1,31 +1,33 @@ import {useCallback} from 'react' +import {Image as RNImage} from 'react-native-image-crop-picker' import { - AtUri, AppBskyActorDefs, - AppBskyActorProfile, AppBskyActorGetProfile, - AppBskyFeedDefs, + AppBskyActorProfile, AppBskyEmbedRecord, AppBskyEmbedRecordWithMedia, + AppBskyFeedDefs, + AtUri, } from '@atproto/api' import { + QueryClient, + useMutation, useQuery, useQueryClient, - useMutation, - QueryClient, } from '@tanstack/react-query' -import {Image as RNImage} from 'react-native-image-crop-picker' -import {useSession, getAgent} from '../session' -import {updateProfileShadow} from '../cache/profile-shadow' + +import {track} from '#/lib/analytics/analytics' import {uploadBlob} from '#/lib/api' import {until} from '#/lib/async/until' +import {useToggleMutationQueue} from '#/lib/hooks/useToggleMutationQueue' +import {logEvent, LogEvents} from '#/lib/statsig/statsig' import {Shadow} from '#/state/cache/types' +import {STALE} from '#/state/queries' import {resetProfilePostsQueries} from '#/state/queries/post-feed' -import {useToggleMutationQueue} from '#/lib/hooks/useToggleMutationQueue' -import {RQKEY as RQKEY_MY_MUTED} from './my-muted-accounts' +import {updateProfileShadow} from '../cache/profile-shadow' +import {getAgent, useSession} from '../session' import {RQKEY as RQKEY_MY_BLOCKED} from './my-blocked-accounts' -import {STALE} from '#/state/queries' -import {track} from '#/lib/analytics/analytics' +import {RQKEY as RQKEY_MY_MUTED} from './my-muted-accounts' import {ThreadNode} from './post-thread' export const RQKEY = (did: string) => ['profile', did] @@ -186,11 +188,14 @@ export function useProfileUpdateMutation() { export function useProfileFollowMutationQueue( profile: Shadow<AppBskyActorDefs.ProfileViewDetailed>, + logContext: LogEvents['profile:follow']['logContext'] & + LogEvents['profile:unfollow']['logContext'], ) { + const queryClient = useQueryClient() const did = profile.did const initialFollowingUri = profile.viewer?.following - const followMutation = useProfileFollowMutation() - const unfollowMutation = useProfileUnfollowMutation() + const followMutation = useProfileFollowMutation(logContext) + const unfollowMutation = useProfileUnfollowMutation(logContext) const queueToggle = useToggleMutationQueue({ initialState: initialFollowingUri, @@ -212,7 +217,7 @@ export function useProfileFollowMutationQueue( }, onSuccess(finalFollowingUri) { // finalize - updateProfileShadow(did, { + updateProfileShadow(queryClient, did, { followingUri: finalFollowingUri, }) }, @@ -220,26 +225,29 @@ export function useProfileFollowMutationQueue( const queueFollow = useCallback(() => { // optimistically update - updateProfileShadow(did, { + updateProfileShadow(queryClient, did, { followingUri: 'pending', }) return queueToggle(true) - }, [did, queueToggle]) + }, [queryClient, did, queueToggle]) const queueUnfollow = useCallback(() => { // optimistically update - updateProfileShadow(did, { + updateProfileShadow(queryClient, did, { followingUri: undefined, }) return queueToggle(false) - }, [did, queueToggle]) + }, [queryClient, did, queueToggle]) return [queueFollow, queueUnfollow] } -function useProfileFollowMutation() { +function useProfileFollowMutation( + logContext: LogEvents['profile:follow']['logContext'], +) { return useMutation<{uri: string; cid: string}, Error, {did: string}>({ mutationFn: async ({did}) => { + logEvent('profile:follow', {logContext}) return await getAgent().follow(did) }, onSuccess(data, variables) { @@ -248,9 +256,12 @@ function useProfileFollowMutation() { }) } -function useProfileUnfollowMutation() { +function useProfileUnfollowMutation( + logContext: LogEvents['profile:unfollow']['logContext'], +) { return useMutation<void, Error, {did: string; followUri: string}>({ mutationFn: async ({followUri}) => { + logEvent('profile:unfollow', {logContext}) track('Profile:Unfollow', {username: followUri}) return await getAgent().deleteFollow(followUri) }, @@ -260,6 +271,7 @@ function useProfileUnfollowMutation() { export function useProfileMuteMutationQueue( profile: Shadow<AppBskyActorDefs.ProfileViewDetailed>, ) { + const queryClient = useQueryClient() const did = profile.did const initialMuted = profile.viewer?.muted const muteMutation = useProfileMuteMutation() @@ -282,25 +294,25 @@ export function useProfileMuteMutationQueue( }, onSuccess(finalMuted) { // finalize - updateProfileShadow(did, {muted: finalMuted}) + updateProfileShadow(queryClient, did, {muted: finalMuted}) }, }) const queueMute = useCallback(() => { // optimistically update - updateProfileShadow(did, { + updateProfileShadow(queryClient, did, { muted: true, }) return queueToggle(true) - }, [did, queueToggle]) + }, [queryClient, did, queueToggle]) const queueUnmute = useCallback(() => { // optimistically update - updateProfileShadow(did, { + updateProfileShadow(queryClient, did, { muted: false, }) return queueToggle(false) - }, [did, queueToggle]) + }, [queryClient, did, queueToggle]) return [queueMute, queueUnmute] } @@ -332,6 +344,7 @@ function useProfileUnmuteMutation() { export function useProfileBlockMutationQueue( profile: Shadow<AppBskyActorDefs.ProfileViewDetailed>, ) { + const queryClient = useQueryClient() const did = profile.did const initialBlockingUri = profile.viewer?.blocking const blockMutation = useProfileBlockMutation() @@ -357,7 +370,7 @@ export function useProfileBlockMutationQueue( }, onSuccess(finalBlockingUri) { // finalize - updateProfileShadow(did, { + updateProfileShadow(queryClient, did, { blockingUri: finalBlockingUri, }) }, @@ -365,19 +378,19 @@ export function useProfileBlockMutationQueue( const queueBlock = useCallback(() => { // optimistically update - updateProfileShadow(did, { + updateProfileShadow(queryClient, did, { blockingUri: 'pending', }) return queueToggle(true) - }, [did, queueToggle]) + }, [queryClient, did, queueToggle]) const queueUnblock = useCallback(() => { // optimistically update - updateProfileShadow(did, { + updateProfileShadow(queryClient, did, { blockingUri: undefined, }) return queueToggle(false) - }, [did, queueToggle]) + }, [queryClient, did, queueToggle]) return [queueBlock, queueUnblock] } @@ -397,13 +410,14 @@ function useProfileBlockMutation() { }, onSuccess(_, {did}) { queryClient.invalidateQueries({queryKey: RQKEY_MY_BLOCKED()}) - resetProfilePostsQueries(did, 1000) + resetProfilePostsQueries(queryClient, did, 1000) }, }) } function useProfileUnblockMutation() { const {currentAccount} = useSession() + const queryClient = useQueryClient() return useMutation<void, Error, {did: string; blockUri: string}>({ mutationFn: async ({blockUri}) => { if (!currentAccount) { @@ -416,7 +430,7 @@ function useProfileUnblockMutation() { }) }, onSuccess(_, {did}) { - resetProfilePostsQueries(did, 1000) + resetProfilePostsQueries(queryClient, did, 1000) }, }) } |