Pass queryClient explicitly to updateProfileShadow

This commit is contained in:
Dan Abramov
2024-03-21 22:53:00 +00:00
parent e56b7e00b5
commit f18b801979
2 changed files with 27 additions and 21 deletions
+9 -6
View File
@@ -1,7 +1,10 @@
import {useEffect, useState, useMemo} from 'react'
import EventEmitter from 'eventemitter3'
import {useEffect, useMemo, useState} from 'react'
import {AppBskyActorDefs} from '@atproto/api'
import {QueryClient} from '@tanstack/react-query'
import EventEmitter from 'eventemitter3'
import {batchedUpdates} from '#/lib/batchedUpdates'
import {findAllProfilesInQueryData as findAllProfilesInActorSearchQueryData} from '../queries/actor-search'
import {findAllProfilesInQueryData as findAllProfilesInListMembersQueryData} from '../queries/list-members'
import {findAllProfilesInQueryData as findAllProfilesInMyBlockedAccountsQueryData} from '../queries/my-blocked-accounts'
import {findAllProfilesInQueryData as findAllProfilesInMyMutedAccountsQueryData} from '../queries/my-muted-accounts'
@@ -11,9 +14,7 @@ import {findAllProfilesInQueryData as findAllProfilesInProfileQueryData} from '.
import {findAllProfilesInQueryData as findAllProfilesInProfileFollowersQueryData} from '../queries/profile-followers'
import {findAllProfilesInQueryData as findAllProfilesInProfileFollowsQueryData} from '../queries/profile-follows'
import {findAllProfilesInQueryData as findAllProfilesInSuggestedFollowsQueryData} from '../queries/suggested-follows'
import {findAllProfilesInQueryData as findAllProfilesInActorSearchQueryData} from '../queries/actor-search'
import {Shadow, castAsShadow} from './types'
import {queryClient} from 'lib/react-query'
import {castAsShadow, Shadow} from './types'
export type {Shadow} from './types'
export interface ProfileShadow {
@@ -58,10 +59,11 @@ export function useProfileShadow<
}
export function updateProfileShadow(
queryClient: QueryClient,
did: string,
value: Partial<ProfileShadow>,
) {
const cachedProfiles = findProfilesInCache(did)
const cachedProfiles = findProfilesInCache(queryClient, did)
for (let post of cachedProfiles) {
shadows.set(post, {...shadows.get(post), ...value})
}
@@ -90,6 +92,7 @@ function mergeShadow<TProfileView extends AppBskyActorDefs.ProfileView>(
}
function* findProfilesInCache(
queryClient: QueryClient,
did: string,
): Generator<AppBskyActorDefs.ProfileView, void> {
yield* findAllProfilesInListMembersQueryData(queryClient, did)
+18 -15
View File
@@ -191,6 +191,7 @@ export function useProfileFollowMutationQueue(
logContext: LogEvents['profile:follow']['logContext'] &
LogEvents['profile:unfollow']['logContext'],
) {
const queryClient = useQueryClient()
const did = profile.did
const initialFollowingUri = profile.viewer?.following
const followMutation = useProfileFollowMutation(logContext)
@@ -216,7 +217,7 @@ export function useProfileFollowMutationQueue(
},
onSuccess(finalFollowingUri) {
// finalize
updateProfileShadow(did, {
updateProfileShadow(queryClient, did, {
followingUri: finalFollowingUri,
})
},
@@ -224,19 +225,19 @@ 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]
}
@@ -270,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()
@@ -292,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]
}
@@ -342,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()
@@ -367,7 +370,7 @@ export function useProfileBlockMutationQueue(
},
onSuccess(finalBlockingUri) {
// finalize
updateProfileShadow(did, {
updateProfileShadow(queryClient, did, {
blockingUri: finalBlockingUri,
})
},
@@ -375,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]
}