diff --git a/src/state/cache/profile-shadow.ts b/src/state/cache/profile-shadow.ts index 34fe5995d3..ca791bc9e8 100644 --- a/src/state/cache/profile-shadow.ts +++ b/src/state/cache/profile-shadow.ts @@ -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, ) { - 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( } function* findProfilesInCache( + queryClient: QueryClient, did: string, ): Generator { yield* findAllProfilesInListMembersQueryData(queryClient, did) diff --git a/src/state/queries/profile.ts b/src/state/queries/profile.ts index 0029008627..19492cf66f 100644 --- a/src/state/queries/profile.ts +++ b/src/state/queries/profile.ts @@ -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, ) { + 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, ) { + 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] }