diff --git a/src/state/queries/actor-search.ts b/src/state/queries/actor-search.ts index f19916103c..1e301a1bac 100644 --- a/src/state/queries/actor-search.ts +++ b/src/state/queries/actor-search.ts @@ -2,22 +2,29 @@ import {AppBskyActorDefs} from '@atproto/api' import {QueryClient, useQuery} from '@tanstack/react-query' import {STALE} from '#/state/queries' -import {getAgent} from '#/state/session' +import {useAgent} from '#/state/session' const RQKEY_ROOT = 'actor-search' -export const RQKEY = (prefix: string) => [RQKEY_ROOT, prefix] +export const RQKEY = (query: string) => [RQKEY_ROOT, query] -export function useActorSearch(prefix: string) { +export function useActorSearch({ + query, + enabled, +}: { + query: string + enabled?: boolean +}) { + const agent = useAgent() return useQuery({ staleTime: STALE.MINUTES.ONE, - queryKey: RQKEY(prefix || ''), + queryKey: RQKEY(query || ''), async queryFn() { - const res = await getAgent().searchActors({ - q: prefix, + const res = await agent.searchActors({ + q: query, }) return res.data.actors }, - enabled: !!prefix, + enabled: enabled && !!query, }) } diff --git a/src/state/queries/labeler.ts b/src/state/queries/labeler.ts index 78301eb0df..058e8fcdee 100644 --- a/src/state/queries/labeler.ts +++ b/src/state/queries/labeler.ts @@ -5,7 +5,7 @@ import {z} from 'zod' import {labelersDetailedInfoQueryKeyRoot} from '#/lib/react-query' import {STALE} from '#/state/queries' import {preferencesQueryKey} from '#/state/queries/preferences' -import {getAgent} from '#/state/session' +import {useAgent} from '#/state/session' const labelerInfoQueryKeyRoot = 'labeler-info' export const labelerInfoQueryKey = (did: string) => [ @@ -31,11 +31,12 @@ export function useLabelerInfoQuery({ did?: string enabled?: boolean }) { + const agent = useAgent() return useQuery({ enabled: !!did && enabled !== false, queryKey: labelerInfoQueryKey(did as string), queryFn: async () => { - const res = await getAgent().app.bsky.labeler.getServices({ + const res = await agent.app.bsky.labeler.getServices({ dids: [did as string], detailed: true, }) @@ -45,24 +46,26 @@ export function useLabelerInfoQuery({ } export function useLabelersInfoQuery({dids}: {dids: string[]}) { + const agent = useAgent() return useQuery({ enabled: !!dids.length, queryKey: labelersInfoQueryKey(dids), queryFn: async () => { - const res = await getAgent().app.bsky.labeler.getServices({dids}) + const res = await agent.app.bsky.labeler.getServices({dids}) return res.data.views as AppBskyLabelerDefs.LabelerView[] }, }) } export function useLabelersDetailedInfoQuery({dids}: {dids: string[]}) { + const agent = useAgent() return useQuery({ enabled: !!dids.length, queryKey: labelersDetailedInfoQueryKey(dids), gcTime: 1000 * 60 * 60 * 6, // 6 hours staleTime: STALE.MINUTES.ONE, queryFn: async () => { - const res = await getAgent().app.bsky.labeler.getServices({ + const res = await agent.app.bsky.labeler.getServices({ dids, detailed: true, }) @@ -73,6 +76,7 @@ export function useLabelersDetailedInfoQuery({dids}: {dids: string[]}) { export function useLabelerSubscriptionMutation() { const queryClient = useQueryClient() + const agent = useAgent() return useMutation({ async mutationFn({did, subscribe}: {did: string; subscribe: boolean}) { @@ -83,9 +87,9 @@ export function useLabelerSubscriptionMutation() { }).parse({did, subscribe}) if (subscribe) { - await getAgent().addLabeler(did) + await agent.addLabeler(did) } else { - await getAgent().removeLabeler(did) + await agent.removeLabeler(did) } }, onSuccess() { diff --git a/src/state/queries/list-members.ts b/src/state/queries/list-members.ts index 87a409b88c..de9a36ab7f 100644 --- a/src/state/queries/list-members.ts +++ b/src/state/queries/list-members.ts @@ -7,7 +7,7 @@ import { } from '@tanstack/react-query' import {STALE} from '#/state/queries' -import {getAgent} from '#/state/session' +import {useAgent} from '#/state/session' const PAGE_SIZE = 30 type RQPageParam = string | undefined @@ -16,6 +16,7 @@ const RQKEY_ROOT = 'list-members' export const RQKEY = (uri: string) => [RQKEY_ROOT, uri] export function useListMembersQuery(uri: string) { + const agent = useAgent() return useInfiniteQuery< AppBskyGraphGetList.OutputSchema, Error, @@ -26,7 +27,7 @@ export function useListMembersQuery(uri: string) { staleTime: STALE.MINUTES.ONE, queryKey: RQKEY(uri), async queryFn({pageParam}: {pageParam: RQPageParam}) { - const res = await getAgent().app.bsky.graph.getList({ + const res = await agent.app.bsky.graph.getList({ list: uri, limit: PAGE_SIZE, cursor: pageParam, diff --git a/src/state/queries/my-blocked-accounts.ts b/src/state/queries/my-blocked-accounts.ts index 36b9ac5804..05a78825fa 100644 --- a/src/state/queries/my-blocked-accounts.ts +++ b/src/state/queries/my-blocked-accounts.ts @@ -6,13 +6,14 @@ import { useInfiniteQuery, } from '@tanstack/react-query' -import {getAgent} from '#/state/session' +import {useAgent} from '#/state/session' const RQKEY_ROOT = 'my-blocked-accounts' export const RQKEY = () => [RQKEY_ROOT] type RQPageParam = string | undefined export function useMyBlockedAccountsQuery() { + const agent = useAgent() return useInfiniteQuery< AppBskyGraphGetBlocks.OutputSchema, Error, @@ -22,7 +23,7 @@ export function useMyBlockedAccountsQuery() { >({ queryKey: RQKEY(), async queryFn({pageParam}: {pageParam: RQPageParam}) { - const res = await getAgent().app.bsky.graph.getBlocks({ + const res = await agent.app.bsky.graph.getBlocks({ limit: 30, cursor: pageParam, }) diff --git a/src/state/queries/my-muted-accounts.ts b/src/state/queries/my-muted-accounts.ts index 9e90044bf4..5fb0fa79b9 100644 --- a/src/state/queries/my-muted-accounts.ts +++ b/src/state/queries/my-muted-accounts.ts @@ -6,13 +6,14 @@ import { useInfiniteQuery, } from '@tanstack/react-query' -import {getAgent} from '#/state/session' +import {useAgent} from '#/state/session' const RQKEY_ROOT = 'my-muted-accounts' export const RQKEY = () => [RQKEY_ROOT] type RQPageParam = string | undefined export function useMyMutedAccountsQuery() { + const agent = useAgent() return useInfiniteQuery< AppBskyGraphGetMutes.OutputSchema, Error, @@ -22,7 +23,7 @@ export function useMyMutedAccountsQuery() { >({ queryKey: RQKEY(), async queryFn({pageParam}: {pageParam: RQPageParam}) { - const res = await getAgent().app.bsky.graph.getMutes({ + const res = await agent.app.bsky.graph.getMutes({ limit: 30, cursor: pageParam, }) diff --git a/src/state/queries/post-feed.ts b/src/state/queries/post-feed.ts index ee22bac691..cf4aec3d95 100644 --- a/src/state/queries/post-feed.ts +++ b/src/state/queries/post-feed.ts @@ -19,7 +19,7 @@ import {moderatePost_wrapped as moderatePost} from '#/lib/moderatePost_wrapped' import {logger} from '#/logger' import {STALE} from '#/state/queries' import {DEFAULT_LOGGED_OUT_PREFERENCES} from '#/state/queries/preferences/const' -import {getAgent} from '#/state/session' +import {useAgent} from '#/state/session' import {AuthorFeedAPI} from 'lib/api/feed/author' import {CustomFeedAPI} from 'lib/api/feed/custom' import {FollowingFeedAPI} from 'lib/api/feed/following' @@ -111,6 +111,7 @@ export function usePostFeedQuery( result: InfiniteData } | null>(null) const lastPageCountRef = useRef(0) + const agent = useAgent() // Make sure this doesn't invalidate unless really needed. const selectArgs = React.useMemo( @@ -153,7 +154,7 @@ export function usePostFeedQuery( * moderations happen later, which results in some posts being shown and * some not. */ - if (!getAgent().session) { + if (!agent.session) { assertSomePostsPassModeration(res.feed) } diff --git a/src/state/queries/post-liked-by.ts b/src/state/queries/post-liked-by.ts index 6fa341b773..ab9f5c7bba 100644 --- a/src/state/queries/post-liked-by.ts +++ b/src/state/queries/post-liked-by.ts @@ -6,7 +6,7 @@ import { useInfiniteQuery, } from '@tanstack/react-query' -import {getAgent} from '#/state/session' +import {useAgent} from '#/state/session' const PAGE_SIZE = 30 type RQPageParam = string | undefined @@ -16,6 +16,7 @@ const RQKEY_ROOT = 'liked-by' export const RQKEY = (resolvedUri: string) => [RQKEY_ROOT, resolvedUri] export function useLikedByQuery(resolvedUri: string | undefined) { + const agent = useAgent() return useInfiniteQuery< AppBskyFeedGetLikes.OutputSchema, Error, @@ -25,7 +26,7 @@ export function useLikedByQuery(resolvedUri: string | undefined) { >({ queryKey: RQKEY(resolvedUri || ''), async queryFn({pageParam}: {pageParam: RQPageParam}) { - const res = await getAgent().getLikes({ + const res = await agent.getLikes({ uri: resolvedUri || '', limit: PAGE_SIZE, cursor: pageParam, diff --git a/src/state/queries/post-reposted-by.ts b/src/state/queries/post-reposted-by.ts index f8cfff0d28..a27f203dd9 100644 --- a/src/state/queries/post-reposted-by.ts +++ b/src/state/queries/post-reposted-by.ts @@ -6,7 +6,7 @@ import { useInfiniteQuery, } from '@tanstack/react-query' -import {getAgent} from '#/state/session' +import {useAgent} from '#/state/session' const PAGE_SIZE = 30 type RQPageParam = string | undefined @@ -16,6 +16,7 @@ const RQKEY_ROOT = 'post-reposted-by' export const RQKEY = (resolvedUri: string) => [RQKEY_ROOT, resolvedUri] export function usePostRepostedByQuery(resolvedUri: string | undefined) { + const agent = useAgent() return useInfiniteQuery< AppBskyFeedGetRepostedBy.OutputSchema, Error, @@ -25,7 +26,7 @@ export function usePostRepostedByQuery(resolvedUri: string | undefined) { >({ queryKey: RQKEY(resolvedUri || ''), async queryFn({pageParam}: {pageParam: RQPageParam}) { - const res = await getAgent().getRepostedBy({ + const res = await agent.getRepostedBy({ uri: resolvedUri || '', limit: PAGE_SIZE, cursor: pageParam, diff --git a/src/state/queries/post-thread.ts b/src/state/queries/post-thread.ts index 832794bf54..a346f8a6dc 100644 --- a/src/state/queries/post-thread.ts +++ b/src/state/queries/post-thread.ts @@ -7,7 +7,7 @@ import { import {QueryClient, useQuery, useQueryClient} from '@tanstack/react-query' import {UsePreferencesQueryResponse} from '#/state/queries/preferences/types' -import {getAgent} from '#/state/session' +import {useAgent} from '#/state/session' import {findAllPostsInQueryData as findAllPostsInNotifsQueryData} from './notifications/feed' import {findAllPostsInQueryData as findAllPostsInFeedQueryData} from './post-feed' import {precacheThreadPostProfiles} from './profile' @@ -65,11 +65,12 @@ export type ThreadNode = export function usePostThreadQuery(uri: string | undefined) { const queryClient = useQueryClient() + const agent = useAgent() return useQuery({ gcTime: 0, queryKey: RQKEY(uri || ''), async queryFn() { - const res = await getAgent().getPostThread({uri: uri!}) + const res = await agent.getPostThread({uri: uri!}) if (res.success) { const nodes = responseToThreadNodes(res.data.thread) precacheThreadPostProfiles(queryClient, nodes) diff --git a/src/state/queries/preferences/index.ts b/src/state/queries/preferences/index.ts index 85e3f9a25d..d2db02a422 100644 --- a/src/state/queries/preferences/index.ts +++ b/src/state/queries/preferences/index.ts @@ -22,7 +22,7 @@ import { ThreadViewPreferences, UsePreferencesQueryResponse, } from '#/state/queries/preferences/types' -import {getAgent, useSession} from '#/state/session' +import {useAgent, useSession} from '#/state/session' import {saveLabelers} from '#/state/session/agent-config' export * from '#/state/queries/preferences/const' @@ -33,14 +33,13 @@ const preferencesQueryKeyRoot = 'getPreferences' export const preferencesQueryKey = [preferencesQueryKeyRoot] export function usePreferencesQuery() { + const agent = useAgent() return useQuery({ staleTime: STALE.SECONDS.FIFTEEN, structuralSharing: true, refetchOnWindowFocus: true, queryKey: preferencesQueryKey, queryFn: async () => { - const agent = getAgent() - if (agent.session?.did === undefined) { return DEFAULT_LOGGED_OUT_PREFERENCES } else { @@ -118,10 +117,11 @@ export function useModerationOpts() { export function useClearPreferencesMutation() { const queryClient = useQueryClient() + const agent = useAgent() return useMutation({ mutationFn: async () => { - await getAgent().app.bsky.actor.putPreferences({preferences: []}) + await agent.app.bsky.actor.putPreferences({preferences: []}) // triggers a refetch await queryClient.invalidateQueries({ queryKey: preferencesQueryKey, @@ -132,6 +132,7 @@ export function useClearPreferencesMutation() { export function usePreferencesSetContentLabelMutation() { const queryClient = useQueryClient() + const agent = useAgent() return useMutation< void, @@ -139,7 +140,7 @@ export function usePreferencesSetContentLabelMutation() { {label: string; visibility: LabelPreference; labelerDid: string | undefined} >({ mutationFn: async ({label, visibility, labelerDid}) => { - await getAgent().setContentLabelPref(label, visibility, labelerDid) + await agent.setContentLabelPref(label, visibility, labelerDid) // triggers a refetch await queryClient.invalidateQueries({ queryKey: preferencesQueryKey, @@ -150,6 +151,7 @@ export function usePreferencesSetContentLabelMutation() { export function useSetContentLabelMutation() { const queryClient = useQueryClient() + const agent = useAgent() return useMutation({ mutationFn: async ({ @@ -161,7 +163,7 @@ export function useSetContentLabelMutation() { visibility: LabelPreference labelerDid?: string }) => { - await getAgent().setContentLabelPref(label, visibility, labelerDid) + await agent.setContentLabelPref(label, visibility, labelerDid) // triggers a refetch await queryClient.invalidateQueries({ queryKey: preferencesQueryKey, @@ -172,10 +174,11 @@ export function useSetContentLabelMutation() { export function usePreferencesSetAdultContentMutation() { const queryClient = useQueryClient() + const agent = useAgent() return useMutation({ mutationFn: async ({enabled}) => { - await getAgent().setAdultContentEnabled(enabled) + await agent.setAdultContentEnabled(enabled) // triggers a refetch await queryClient.invalidateQueries({ queryKey: preferencesQueryKey, @@ -186,10 +189,11 @@ export function usePreferencesSetAdultContentMutation() { export function usePreferencesSetBirthDateMutation() { const queryClient = useQueryClient() + const agent = useAgent() return useMutation({ mutationFn: async ({birthDate}: {birthDate: Date}) => { - await getAgent().setPersonalDetails({birthDate: birthDate.toISOString()}) + await agent.setPersonalDetails({birthDate: birthDate.toISOString()}) // triggers a refetch await queryClient.invalidateQueries({ queryKey: preferencesQueryKey, @@ -200,10 +204,11 @@ export function usePreferencesSetBirthDateMutation() { export function useSetFeedViewPreferencesMutation() { const queryClient = useQueryClient() + const agent = useAgent() return useMutation>({ mutationFn: async prefs => { - await getAgent().setFeedViewPrefs('home', prefs) + await agent.setFeedViewPrefs('home', prefs) // triggers a refetch await queryClient.invalidateQueries({ queryKey: preferencesQueryKey, @@ -214,10 +219,11 @@ export function useSetFeedViewPreferencesMutation() { export function useSetThreadViewPreferencesMutation() { const queryClient = useQueryClient() + const agent = useAgent() return useMutation>({ mutationFn: async prefs => { - await getAgent().setThreadViewPrefs(prefs) + await agent.setThreadViewPrefs(prefs) // triggers a refetch await queryClient.invalidateQueries({ queryKey: preferencesQueryKey, @@ -228,6 +234,7 @@ export function useSetThreadViewPreferencesMutation() { export function useSetSaveFeedsMutation() { const queryClient = useQueryClient() + const agent = useAgent() return useMutation< void, @@ -235,7 +242,7 @@ export function useSetSaveFeedsMutation() { Pick >({ mutationFn: async ({saved, pinned}) => { - await getAgent().setSavedFeeds(saved, pinned) + await agent.setSavedFeeds(saved, pinned) // triggers a refetch await queryClient.invalidateQueries({ queryKey: preferencesQueryKey, @@ -246,10 +253,11 @@ export function useSetSaveFeedsMutation() { export function useSaveFeedMutation() { const queryClient = useQueryClient() + const agent = useAgent() return useMutation({ mutationFn: async ({uri}) => { - await getAgent().addSavedFeed(uri) + await agent.addSavedFeed(uri) track('CustomFeed:Save') // triggers a refetch await queryClient.invalidateQueries({ @@ -261,10 +269,11 @@ export function useSaveFeedMutation() { export function useRemoveFeedMutation() { const queryClient = useQueryClient() + const agent = useAgent() return useMutation({ mutationFn: async ({uri}) => { - await getAgent().removeSavedFeed(uri) + await agent.removeSavedFeed(uri) track('CustomFeed:Unsave') // triggers a refetch await queryClient.invalidateQueries({ @@ -276,10 +285,11 @@ export function useRemoveFeedMutation() { export function usePinFeedMutation() { const queryClient = useQueryClient() + const agent = useAgent() return useMutation({ mutationFn: async ({uri}) => { - await getAgent().addPinnedFeed(uri) + await agent.addPinnedFeed(uri) track('CustomFeed:Pin', {uri}) // triggers a refetch await queryClient.invalidateQueries({ @@ -291,10 +301,11 @@ export function usePinFeedMutation() { export function useUnpinFeedMutation() { const queryClient = useQueryClient() + const agent = useAgent() return useMutation({ mutationFn: async ({uri}) => { - await getAgent().removePinnedFeed(uri) + await agent.removePinnedFeed(uri) track('CustomFeed:Unpin', {uri}) // triggers a refetch await queryClient.invalidateQueries({ @@ -306,10 +317,11 @@ export function useUnpinFeedMutation() { export function useUpsertMutedWordsMutation() { const queryClient = useQueryClient() + const agent = useAgent() return useMutation({ mutationFn: async (mutedWords: AppBskyActorDefs.MutedWord[]) => { - await getAgent().upsertMutedWords(mutedWords) + await agent.upsertMutedWords(mutedWords) // triggers a refetch await queryClient.invalidateQueries({ queryKey: preferencesQueryKey, @@ -320,10 +332,11 @@ export function useUpsertMutedWordsMutation() { export function useUpdateMutedWordMutation() { const queryClient = useQueryClient() + const agent = useAgent() return useMutation({ mutationFn: async (mutedWord: AppBskyActorDefs.MutedWord) => { - await getAgent().updateMutedWord(mutedWord) + await agent.updateMutedWord(mutedWord) // triggers a refetch await queryClient.invalidateQueries({ queryKey: preferencesQueryKey, @@ -334,10 +347,11 @@ export function useUpdateMutedWordMutation() { export function useRemoveMutedWordMutation() { const queryClient = useQueryClient() + const agent = useAgent() return useMutation({ mutationFn: async (mutedWord: AppBskyActorDefs.MutedWord) => { - await getAgent().removeMutedWord(mutedWord) + await agent.removeMutedWord(mutedWord) // triggers a refetch await queryClient.invalidateQueries({ queryKey: preferencesQueryKey, diff --git a/src/state/queries/profile-followers.ts b/src/state/queries/profile-followers.ts index d7dfe25c64..131343cd10 100644 --- a/src/state/queries/profile-followers.ts +++ b/src/state/queries/profile-followers.ts @@ -6,7 +6,7 @@ import { useInfiniteQuery, } from '@tanstack/react-query' -import {getAgent} from '#/state/session' +import {useAgent} from '#/state/session' const PAGE_SIZE = 30 type RQPageParam = string | undefined @@ -15,6 +15,7 @@ const RQKEY_ROOT = 'profile-followers' export const RQKEY = (did: string) => [RQKEY_ROOT, did] export function useProfileFollowersQuery(did: string | undefined) { + const agent = useAgent() return useInfiniteQuery< AppBskyGraphGetFollowers.OutputSchema, Error, @@ -24,7 +25,7 @@ export function useProfileFollowersQuery(did: string | undefined) { >({ queryKey: RQKEY(did || ''), async queryFn({pageParam}: {pageParam: RQPageParam}) { - const res = await getAgent().app.bsky.graph.getFollowers({ + const res = await agent.app.bsky.graph.getFollowers({ actor: did || '', limit: PAGE_SIZE, cursor: pageParam, diff --git a/src/state/queries/profile-follows.ts b/src/state/queries/profile-follows.ts index 3abac2f108..eb177a8cd2 100644 --- a/src/state/queries/profile-follows.ts +++ b/src/state/queries/profile-follows.ts @@ -7,7 +7,7 @@ import { } from '@tanstack/react-query' import {STALE} from '#/state/queries' -import {getAgent} from '#/state/session' +import {useAgent} from '#/state/session' const PAGE_SIZE = 30 type RQPageParam = string | undefined @@ -17,6 +17,7 @@ const RQKEY_ROOT = 'profile-follows' export const RQKEY = (did: string) => [RQKEY_ROOT, did] export function useProfileFollowsQuery(did: string | undefined) { + const agent = useAgent() return useInfiniteQuery< AppBskyGraphGetFollows.OutputSchema, Error, @@ -27,7 +28,7 @@ export function useProfileFollowsQuery(did: string | undefined) { staleTime: STALE.MINUTES.ONE, queryKey: RQKEY(did || ''), async queryFn({pageParam}: {pageParam: RQPageParam}) { - const res = await getAgent().app.bsky.graph.getFollows({ + const res = await agent.app.bsky.graph.getFollows({ actor: did || '', limit: PAGE_SIZE, cursor: pageParam, diff --git a/src/state/queries/profile.ts b/src/state/queries/profile.ts index a962fecff7..4f09da20fa 100644 --- a/src/state/queries/profile.ts +++ b/src/state/queries/profile.ts @@ -25,7 +25,7 @@ import {Shadow} from '#/state/cache/types' import {STALE} from '#/state/queries' import {resetProfilePostsQueries} from '#/state/queries/post-feed' import {updateProfileShadow} from '../cache/profile-shadow' -import {getAgent, useSession} from '../session' +import {getAgent, useAgent, useSession} from '../session' import {RQKEY as RQKEY_MY_BLOCKED} from './my-blocked-accounts' import {RQKEY as RQKEY_MY_MUTED} from './my-muted-accounts' import {ThreadNode} from './post-thread' @@ -53,6 +53,7 @@ export function useProfileQuery({ staleTime?: number }) { const queryClient = useQueryClient() + const agent = useAgent() return useQuery({ // WARNING // this staleTime is load-bearing @@ -62,7 +63,7 @@ export function useProfileQuery({ refetchOnWindowFocus: true, queryKey: RQKEY(did ?? ''), queryFn: async () => { - const res = await getAgent().getProfile({actor: did ?? ''}) + const res = await agent.getProfile({actor: did ?? ''}) return res.data }, placeholderData: () => { @@ -77,11 +78,12 @@ export function useProfileQuery({ } export function useProfilesQuery({handles}: {handles: string[]}) { + const agent = useAgent() return useQuery({ staleTime: STALE.MINUTES.FIVE, queryKey: profilesQueryKey(handles), queryFn: async () => { - const res = await getAgent().getProfiles({actors: handles}) + const res = await agent.getProfiles({actors: handles}) return res.data }, }) @@ -89,17 +91,18 @@ export function useProfilesQuery({handles}: {handles: string[]}) { export function usePrefetchProfileQuery() { const queryClient = useQueryClient() + const agent = useAgent() const prefetchProfileQuery = useCallback( (did: string) => { queryClient.prefetchQuery({ queryKey: RQKEY(did), queryFn: async () => { - const res = await getAgent().getProfile({actor: did || ''}) + const res = await agent.getProfile({actor: did || ''}) return res.data }, }) }, - [queryClient], + [queryClient, agent], ) return prefetchProfileQuery } @@ -115,6 +118,7 @@ interface ProfileUpdateParams { } export function useProfileUpdateMutation() { const queryClient = useQueryClient() + const agent = useAgent() return useMutation({ mutationFn: async ({ profile, @@ -123,7 +127,7 @@ export function useProfileUpdateMutation() { newUserBanner, checkCommitted, }) => { - await getAgent().upsertProfile(async existing => { + await agent.upsertProfile(async existing => { existing = existing || {} if (typeof updates === 'function') { existing = updates(existing) @@ -254,7 +258,7 @@ function useProfileFollowMutation( logContext: LogEvents['profile:follow']['logContext'], profile: Shadow, ) { - const {currentAccount} = useSession() + const {currentAccount, agent} = useSession() const queryClient = useQueryClient() return useMutation<{uri: string; cid: string}, Error, {did: string}>({ mutationFn: async ({did}) => { @@ -270,7 +274,7 @@ function useProfileFollowMutation( followeeClout: toClout(profile.followersCount), followerClout: toClout(ownProfile?.followersCount), }) - return await getAgent().follow(did) + return await agent.follow(did) }, onSuccess(data, variables) { track('Profile:Follow', {username: variables.did}) @@ -281,11 +285,12 @@ function useProfileFollowMutation( function useProfileUnfollowMutation( logContext: LogEvents['profile:unfollow']['logContext'], ) { + const agent = useAgent() return useMutation({ mutationFn: async ({followUri}) => { logEvent('profile:unfollow', {logContext}) track('Profile:Unfollow', {username: followUri}) - return await getAgent().deleteFollow(followUri) + return await agent.deleteFollow(followUri) }, }) } @@ -341,9 +346,10 @@ export function useProfileMuteMutationQueue( function useProfileMuteMutation() { const queryClient = useQueryClient() + const agent = useAgent() return useMutation({ mutationFn: async ({did}) => { - await getAgent().mute(did) + await agent.mute(did) }, onSuccess() { queryClient.invalidateQueries({queryKey: RQKEY_MY_MUTED()}) @@ -353,9 +359,10 @@ function useProfileMuteMutation() { function useProfileUnmuteMutation() { const queryClient = useQueryClient() + const agent = useAgent() return useMutation({ mutationFn: async ({did}) => { - await getAgent().unmute(did) + await agent.unmute(did) }, onSuccess() { queryClient.invalidateQueries({queryKey: RQKEY_MY_MUTED()}) @@ -418,14 +425,14 @@ export function useProfileBlockMutationQueue( } function useProfileBlockMutation() { - const {currentAccount} = useSession() + const {currentAccount, agent} = useSession() const queryClient = useQueryClient() return useMutation<{uri: string; cid: string}, Error, {did: string}>({ mutationFn: async ({did}) => { if (!currentAccount) { throw new Error('Not signed in') } - return await getAgent().app.bsky.graph.block.create( + return await agent.app.bsky.graph.block.create( {repo: currentAccount.did}, {subject: did, createdAt: new Date().toISOString()}, ) @@ -438,7 +445,7 @@ function useProfileBlockMutation() { } function useProfileUnblockMutation() { - const {currentAccount} = useSession() + const {currentAccount, agent} = useSession() const queryClient = useQueryClient() return useMutation({ mutationFn: async ({blockUri}) => { @@ -446,7 +453,7 @@ function useProfileUnblockMutation() { throw new Error('Not signed in') } const {rkey} = new AtUri(blockUri) - await getAgent().app.bsky.graph.block.delete({ + await agent.app.bsky.graph.block.delete({ repo: currentAccount.did, rkey, }) diff --git a/src/state/queries/suggested-follows.ts b/src/state/queries/suggested-follows.ts index a93f935f25..11596a23f8 100644 --- a/src/state/queries/suggested-follows.ts +++ b/src/state/queries/suggested-follows.ts @@ -16,7 +16,7 @@ import { import {STALE} from '#/state/queries' import {useModerationOpts} from '#/state/queries/preferences' -import {getAgent, useSession} from '#/state/session' +import {useAgent, useSession} from '#/state/session' const suggestedFollowsQueryKeyRoot = 'suggested-follows' const suggestedFollowsQueryKey = [suggestedFollowsQueryKeyRoot] @@ -28,7 +28,7 @@ const suggestedFollowsByActorQueryKey = (did: string) => [ ] export function useSuggestedFollowsQuery() { - const {currentAccount} = useSession() + const {agent, currentAccount} = useSession() const moderationOpts = useModerationOpts() return useInfiniteQuery< @@ -42,7 +42,7 @@ export function useSuggestedFollowsQuery() { staleTime: STALE.HOURS.ONE, queryKey: suggestedFollowsQueryKey, queryFn: async ({pageParam}) => { - const res = await getAgent().app.bsky.actor.getSuggestions({ + const res = await agent.app.bsky.actor.getSuggestions({ limit: 25, cursor: pageParam, }) @@ -79,10 +79,11 @@ export function useSuggestedFollowsQuery() { } export function useSuggestedFollowsByActorQuery({did}: {did: string}) { + const agent = useAgent() return useQuery({ queryKey: suggestedFollowsByActorQueryKey(did), queryFn: async () => { - const res = await getAgent().app.bsky.graph.getSuggestedFollowsByActor({ + const res = await agent.app.bsky.graph.getSuggestedFollowsByActor({ actor: did, }) return res.data @@ -92,6 +93,7 @@ export function useSuggestedFollowsByActorQuery({did}: {did: string}) { export function useGetSuggestedFollowersByActor() { const queryClient = useQueryClient() + const agent = useAgent() return React.useCallback( async (actor: string) => { @@ -99,17 +101,16 @@ export function useGetSuggestedFollowersByActor() { staleTime: STALE.MINUTES.ONE, queryKey: suggestedFollowsByActorQueryKey(actor), queryFn: async () => { - const res = - await getAgent().app.bsky.graph.getSuggestedFollowsByActor({ - actor: actor, - }) + const res = await agent.app.bsky.graph.getSuggestedFollowsByActor({ + actor: actor, + }) return res.data }, }) return res }, - [queryClient], + [queryClient, agent], ) } diff --git a/src/state/session/index.tsx b/src/state/session/index.tsx index c1890afe3b..f44e4cb9fe 100644 --- a/src/state/session/index.tsx +++ b/src/state/session/index.tsx @@ -53,7 +53,7 @@ export function getAgent() { } const StateContext = React.createContext({ - currentAgent: INITIAL_AGENT, + agent: INITIAL_AGENT, isInitialLoad: true, isSwitchingAccounts: false, accounts: [], @@ -477,7 +477,7 @@ export function Provider({children}: React.PropsWithChildren<{}>) { const stateContext = React.useMemo( () => ({ - currentAgent, + agent: currentAgent, isInitialLoad, isSwitchingAccounts, currentAccount, @@ -537,6 +537,10 @@ export function useSession() { return React.useContext(StateContext) } +export function useAgent() { + return useSession().agent +} + export function useSessionApi() { return React.useContext(ApiContext) } diff --git a/src/state/session/types.ts b/src/state/session/types.ts index 926a5e267e..327124b5e0 100644 --- a/src/state/session/types.ts +++ b/src/state/session/types.ts @@ -17,7 +17,7 @@ export type CurrentAccount = Omit * Context shape returned from `useSession()` */ export type SessionStateContext = { - currentAgent: BskyAgent + agent: BskyAgent isInitialLoad: boolean isSwitchingAccounts: boolean hasSession: boolean