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 {useEffect, useMemo, useState} from 'react'
import EventEmitter from 'eventemitter3'
import {AppBskyActorDefs} from '@atproto/api' import {AppBskyActorDefs} from '@atproto/api'
import {QueryClient} from '@tanstack/react-query'
import EventEmitter from 'eventemitter3'
import {batchedUpdates} from '#/lib/batchedUpdates' import {batchedUpdates} from '#/lib/batchedUpdates'
import {findAllProfilesInQueryData as findAllProfilesInActorSearchQueryData} from '../queries/actor-search'
import {findAllProfilesInQueryData as findAllProfilesInListMembersQueryData} from '../queries/list-members' import {findAllProfilesInQueryData as findAllProfilesInListMembersQueryData} from '../queries/list-members'
import {findAllProfilesInQueryData as findAllProfilesInMyBlockedAccountsQueryData} from '../queries/my-blocked-accounts' import {findAllProfilesInQueryData as findAllProfilesInMyBlockedAccountsQueryData} from '../queries/my-blocked-accounts'
import {findAllProfilesInQueryData as findAllProfilesInMyMutedAccountsQueryData} from '../queries/my-muted-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 findAllProfilesInProfileFollowersQueryData} from '../queries/profile-followers'
import {findAllProfilesInQueryData as findAllProfilesInProfileFollowsQueryData} from '../queries/profile-follows' import {findAllProfilesInQueryData as findAllProfilesInProfileFollowsQueryData} from '../queries/profile-follows'
import {findAllProfilesInQueryData as findAllProfilesInSuggestedFollowsQueryData} from '../queries/suggested-follows' import {findAllProfilesInQueryData as findAllProfilesInSuggestedFollowsQueryData} from '../queries/suggested-follows'
import {findAllProfilesInQueryData as findAllProfilesInActorSearchQueryData} from '../queries/actor-search' import {castAsShadow, Shadow} from './types'
import {Shadow, castAsShadow} from './types'
import {queryClient} from 'lib/react-query'
export type {Shadow} from './types' export type {Shadow} from './types'
export interface ProfileShadow { export interface ProfileShadow {
@@ -58,10 +59,11 @@ export function useProfileShadow<
} }
export function updateProfileShadow( export function updateProfileShadow(
queryClient: QueryClient,
did: string, did: string,
value: Partial<ProfileShadow>, value: Partial<ProfileShadow>,
) { ) {
const cachedProfiles = findProfilesInCache(did) const cachedProfiles = findProfilesInCache(queryClient, did)
for (let post of cachedProfiles) { for (let post of cachedProfiles) {
shadows.set(post, {...shadows.get(post), ...value}) shadows.set(post, {...shadows.get(post), ...value})
} }
@@ -90,6 +92,7 @@ function mergeShadow<TProfileView extends AppBskyActorDefs.ProfileView>(
} }
function* findProfilesInCache( function* findProfilesInCache(
queryClient: QueryClient,
did: string, did: string,
): Generator<AppBskyActorDefs.ProfileView, void> { ): Generator<AppBskyActorDefs.ProfileView, void> {
yield* findAllProfilesInListMembersQueryData(queryClient, did) yield* findAllProfilesInListMembersQueryData(queryClient, did)
+18 -15
View File
@@ -191,6 +191,7 @@ export function useProfileFollowMutationQueue(
logContext: LogEvents['profile:follow']['logContext'] & logContext: LogEvents['profile:follow']['logContext'] &
LogEvents['profile:unfollow']['logContext'], LogEvents['profile:unfollow']['logContext'],
) { ) {
const queryClient = useQueryClient()
const did = profile.did const did = profile.did
const initialFollowingUri = profile.viewer?.following const initialFollowingUri = profile.viewer?.following
const followMutation = useProfileFollowMutation(logContext) const followMutation = useProfileFollowMutation(logContext)
@@ -216,7 +217,7 @@ export function useProfileFollowMutationQueue(
}, },
onSuccess(finalFollowingUri) { onSuccess(finalFollowingUri) {
// finalize // finalize
updateProfileShadow(did, { updateProfileShadow(queryClient, did, {
followingUri: finalFollowingUri, followingUri: finalFollowingUri,
}) })
}, },
@@ -224,19 +225,19 @@ export function useProfileFollowMutationQueue(
const queueFollow = useCallback(() => { const queueFollow = useCallback(() => {
// optimistically update // optimistically update
updateProfileShadow(did, { updateProfileShadow(queryClient, did, {
followingUri: 'pending', followingUri: 'pending',
}) })
return queueToggle(true) return queueToggle(true)
}, [did, queueToggle]) }, [queryClient, did, queueToggle])
const queueUnfollow = useCallback(() => { const queueUnfollow = useCallback(() => {
// optimistically update // optimistically update
updateProfileShadow(did, { updateProfileShadow(queryClient, did, {
followingUri: undefined, followingUri: undefined,
}) })
return queueToggle(false) return queueToggle(false)
}, [did, queueToggle]) }, [queryClient, did, queueToggle])
return [queueFollow, queueUnfollow] return [queueFollow, queueUnfollow]
} }
@@ -270,6 +271,7 @@ function useProfileUnfollowMutation(
export function useProfileMuteMutationQueue( export function useProfileMuteMutationQueue(
profile: Shadow<AppBskyActorDefs.ProfileViewDetailed>, profile: Shadow<AppBskyActorDefs.ProfileViewDetailed>,
) { ) {
const queryClient = useQueryClient()
const did = profile.did const did = profile.did
const initialMuted = profile.viewer?.muted const initialMuted = profile.viewer?.muted
const muteMutation = useProfileMuteMutation() const muteMutation = useProfileMuteMutation()
@@ -292,25 +294,25 @@ export function useProfileMuteMutationQueue(
}, },
onSuccess(finalMuted) { onSuccess(finalMuted) {
// finalize // finalize
updateProfileShadow(did, {muted: finalMuted}) updateProfileShadow(queryClient, did, {muted: finalMuted})
}, },
}) })
const queueMute = useCallback(() => { const queueMute = useCallback(() => {
// optimistically update // optimistically update
updateProfileShadow(did, { updateProfileShadow(queryClient, did, {
muted: true, muted: true,
}) })
return queueToggle(true) return queueToggle(true)
}, [did, queueToggle]) }, [queryClient, did, queueToggle])
const queueUnmute = useCallback(() => { const queueUnmute = useCallback(() => {
// optimistically update // optimistically update
updateProfileShadow(did, { updateProfileShadow(queryClient, did, {
muted: false, muted: false,
}) })
return queueToggle(false) return queueToggle(false)
}, [did, queueToggle]) }, [queryClient, did, queueToggle])
return [queueMute, queueUnmute] return [queueMute, queueUnmute]
} }
@@ -342,6 +344,7 @@ function useProfileUnmuteMutation() {
export function useProfileBlockMutationQueue( export function useProfileBlockMutationQueue(
profile: Shadow<AppBskyActorDefs.ProfileViewDetailed>, profile: Shadow<AppBskyActorDefs.ProfileViewDetailed>,
) { ) {
const queryClient = useQueryClient()
const did = profile.did const did = profile.did
const initialBlockingUri = profile.viewer?.blocking const initialBlockingUri = profile.viewer?.blocking
const blockMutation = useProfileBlockMutation() const blockMutation = useProfileBlockMutation()
@@ -367,7 +370,7 @@ export function useProfileBlockMutationQueue(
}, },
onSuccess(finalBlockingUri) { onSuccess(finalBlockingUri) {
// finalize // finalize
updateProfileShadow(did, { updateProfileShadow(queryClient, did, {
blockingUri: finalBlockingUri, blockingUri: finalBlockingUri,
}) })
}, },
@@ -375,19 +378,19 @@ export function useProfileBlockMutationQueue(
const queueBlock = useCallback(() => { const queueBlock = useCallback(() => {
// optimistically update // optimistically update
updateProfileShadow(did, { updateProfileShadow(queryClient, did, {
blockingUri: 'pending', blockingUri: 'pending',
}) })
return queueToggle(true) return queueToggle(true)
}, [did, queueToggle]) }, [queryClient, did, queueToggle])
const queueUnblock = useCallback(() => { const queueUnblock = useCallback(() => {
// optimistically update // optimistically update
updateProfileShadow(did, { updateProfileShadow(queryClient, did, {
blockingUri: undefined, blockingUri: undefined,
}) })
return queueToggle(false) return queueToggle(false)
}, [did, queueToggle]) }, [queryClient, did, queueToggle])
return [queueBlock, queueUnblock] return [queueBlock, queueUnblock]
} }