[Session] Add useAgent hook and replace (#3706)

* Hook it up

* Memoize getAgent method

* Use one shared reference

---------

Co-authored-by: Dan Abramov <dan.abramov@gmail.com>
This commit is contained in:
Eric Bailey 2024-04-25 16:29:05 -05:00 committed by GitHub
parent d8c8e1e854
commit 45d354cd0c
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
53 changed files with 231 additions and 114 deletions

View file

@ -5,7 +5,7 @@ import {useQuery, useQueryClient} from '@tanstack/react-query'
import {isJustAMute} from '#/lib/moderation'
import {logger} from '#/logger'
import {STALE} from '#/state/queries'
import {getAgent} from '#/state/session'
import {useAgent} from '#/state/session'
import {DEFAULT_LOGGED_OUT_PREFERENCES, useModerationOpts} from './preferences'
const DEFAULT_MOD_OPTS = {
@ -18,6 +18,7 @@ export const RQKEY = (prefix: string) => [RQKEY_ROOT, prefix]
export function useActorAutocompleteQuery(prefix: string) {
const moderationOpts = useModerationOpts()
const {getAgent} = useAgent()
prefix = prefix.toLowerCase()
@ -46,6 +47,7 @@ export type ActorAutocompleteFn = ReturnType<typeof useActorAutocompleteFn>
export function useActorAutocompleteFn() {
const queryClient = useQueryClient()
const moderationOpts = useModerationOpts()
const {getAgent} = useAgent()
return React.useCallback(
async ({query, limit = 8}: {query: string; limit?: number}) => {
@ -74,7 +76,7 @@ export function useActorAutocompleteFn() {
moderationOpts || DEFAULT_MOD_OPTS,
)
},
[queryClient, moderationOpts],
[queryClient, moderationOpts, getAgent],
)
}

View file

@ -2,7 +2,7 @@ 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 = (query: string) => [RQKEY_ROOT, query]
@ -14,6 +14,7 @@ export function useActorSearch({
query: string
enabled?: boolean
}) {
const {getAgent} = useAgent()
return useQuery<AppBskyActorDefs.ProfileView[]>({
staleTime: STALE.MINUTES.ONE,
queryKey: RQKEY(query || ''),

View file

@ -2,12 +2,13 @@ import {ComAtprotoServerCreateAppPassword} from '@atproto/api'
import {useMutation, useQuery, useQueryClient} from '@tanstack/react-query'
import {STALE} from '#/state/queries'
import {getAgent} from '../session'
import {useAgent} from '../session'
const RQKEY_ROOT = 'app-passwords'
export const RQKEY = () => [RQKEY_ROOT]
export function useAppPasswordsQuery() {
const {getAgent} = useAgent()
return useQuery({
staleTime: STALE.MINUTES.FIVE,
queryKey: RQKEY(),
@ -20,6 +21,7 @@ export function useAppPasswordsQuery() {
export function useAppPasswordCreateMutation() {
const queryClient = useQueryClient()
const {getAgent} = useAgent()
return useMutation<
ComAtprotoServerCreateAppPassword.OutputSchema,
Error,
@ -42,6 +44,7 @@ export function useAppPasswordCreateMutation() {
export function useAppPasswordDeleteMutation() {
const queryClient = useQueryClient()
const {getAgent} = useAgent()
return useMutation<void, Error, {name: string}>({
mutationFn: async ({name}) => {
await getAgent().com.atproto.server.revokeAppPassword({

View file

@ -17,7 +17,7 @@ import {sanitizeDisplayName} from '#/lib/strings/display-names'
import {sanitizeHandle} from '#/lib/strings/handles'
import {STALE} from '#/state/queries'
import {usePreferencesQuery} from '#/state/queries/preferences'
import {getAgent, useSession} from '#/state/session'
import {useAgent, useSession} from '#/state/session'
import {router} from '#/routes'
export type FeedSourceFeedInfo = {
@ -140,6 +140,7 @@ export function getAvatarTypeFromUri(uri: string) {
export function useFeedSourceInfoQuery({uri}: {uri: string}) {
const type = getFeedTypeFromUri(uri)
const {getAgent} = useAgent()
return useQuery({
staleTime: STALE.INFINITY,
@ -166,6 +167,7 @@ export function useFeedSourceInfoQuery({uri}: {uri: string}) {
export const useGetPopularFeedsQueryKey = ['getPopularFeeds']
export function useGetPopularFeedsQuery() {
const {getAgent} = useAgent()
return useInfiniteQuery<
AppBskyUnspeccedGetPopularFeedGenerators.OutputSchema,
Error,
@ -187,6 +189,7 @@ export function useGetPopularFeedsQuery() {
}
export function useSearchPopularFeedsMutation() {
const {getAgent} = useAgent()
return useMutation({
mutationFn: async (query: string) => {
const res = await getAgent().app.bsky.unspecced.getPopularFeedGenerators({
@ -238,6 +241,7 @@ const pinnedFeedInfosQueryKeyRoot = 'pinnedFeedsInfos'
export function usePinnedFeedsInfos() {
const {hasSession} = useSession()
const {getAgent} = useAgent()
const {data: preferences, isLoading: isLoadingPrefs} = usePreferencesQuery()
const pinnedUris = preferences?.feeds?.pinned ?? []

View file

@ -2,7 +2,7 @@ import React from 'react'
import {useMutation, useQueryClient} from '@tanstack/react-query'
import {STALE} from '#/state/queries'
import {getAgent} from '#/state/session'
import {useAgent} from '#/state/session'
const handleQueryKeyRoot = 'handle'
const fetchHandleQueryKey = (handleOrDid: string) => [
@ -14,6 +14,7 @@ const fetchDidQueryKey = (handleOrDid: string) => [didQueryKeyRoot, handleOrDid]
export function useFetchHandle() {
const queryClient = useQueryClient()
const {getAgent} = useAgent()
return React.useCallback(
async (handleOrDid: string) => {
@ -27,12 +28,13 @@ export function useFetchHandle() {
}
return handleOrDid
},
[queryClient],
[queryClient, getAgent],
)
}
export function useUpdateHandleMutation() {
const queryClient = useQueryClient()
const {getAgent} = useAgent()
return useMutation({
mutationFn: async ({handle}: {handle: string}) => {
@ -48,6 +50,7 @@ export function useUpdateHandleMutation() {
export function useFetchDid() {
const queryClient = useQueryClient()
const {getAgent} = useAgent()
return React.useCallback(
async (handleOrDid: string) => {
@ -64,6 +67,6 @@ export function useFetchDid() {
},
})
},
[queryClient],
[queryClient, getAgent],
)
}

View file

@ -3,7 +3,7 @@ import {useQuery} from '@tanstack/react-query'
import {cleanError} from '#/lib/strings/errors'
import {STALE} from '#/state/queries'
import {getAgent} from '#/state/session'
import {useAgent} from '#/state/session'
function isInviteAvailable(invite: ComAtprotoServerDefs.InviteCode): boolean {
return invite.available - invite.uses.length > 0 && !invite.disabled
@ -16,6 +16,7 @@ export type InviteCodesQueryResponse = Exclude<
undefined
>
export function useInviteCodesQuery() {
const {getAgent} = useAgent()
return useQuery({
staleTime: STALE.MINUTES.FIVE,
queryKey: [inviteCodesQueryKeyRoot],

View file

@ -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,6 +31,7 @@ export function useLabelerInfoQuery({
did?: string
enabled?: boolean
}) {
const {getAgent} = useAgent()
return useQuery({
enabled: !!did && enabled !== false,
queryKey: labelerInfoQueryKey(did as string),
@ -45,6 +46,7 @@ export function useLabelerInfoQuery({
}
export function useLabelersInfoQuery({dids}: {dids: string[]}) {
const {getAgent} = useAgent()
return useQuery({
enabled: !!dids.length,
queryKey: labelersInfoQueryKey(dids),
@ -56,6 +58,7 @@ export function useLabelersInfoQuery({dids}: {dids: string[]}) {
}
export function useLabelersDetailedInfoQuery({dids}: {dids: string[]}) {
const {getAgent} = useAgent()
return useQuery({
enabled: !!dids.length,
queryKey: labelersDetailedInfoQueryKey(dids),
@ -73,6 +76,7 @@ export function useLabelersDetailedInfoQuery({dids}: {dids: string[]}) {
export function useLabelerSubscriptionMutation() {
const queryClient = useQueryClient()
const {getAgent} = useAgent()
return useMutation({
async mutationFn({did, subscribe}: {did: string; subscribe: boolean}) {

View file

@ -1,8 +1,9 @@
import {useMutation} from '@tanstack/react-query'
import {getAgent} from '#/state/session'
import {useAgent} from '#/state/session'
export function useLikeMutation() {
const {getAgent} = useAgent()
return useMutation({
mutationFn: async ({uri, cid}: {uri: string; cid: string}) => {
const res = await getAgent().like(uri, cid)
@ -12,6 +13,7 @@ export function useLikeMutation() {
}
export function useUnlikeMutation() {
const {getAgent} = useAgent()
return useMutation({
mutationFn: async ({uri}: {uri: string}) => {
await getAgent().deleteLike(uri)

View file

@ -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 {getAgent} = useAgent()
return useInfiniteQuery<
AppBskyGraphGetList.OutputSchema,
Error,

View file

@ -19,7 +19,7 @@ import {useMutation, useQuery, useQueryClient} from '@tanstack/react-query'
import {STALE} from '#/state/queries'
import {RQKEY as LIST_MEMBERS_RQKEY} from '#/state/queries/list-members'
import {getAgent, useSession} from '#/state/session'
import {useAgent, useSession} from '#/state/session'
// sanity limit is SANITY_PAGE_LIMIT*PAGE_SIZE total records
const SANITY_PAGE_LIMIT = 1000
@ -40,6 +40,7 @@ export interface ListMembersip {
*/
export function useDangerousListMembershipsQuery() {
const {currentAccount} = useSession()
const {getAgent} = useAgent()
return useQuery<ListMembersip[]>({
staleTime: STALE.MINUTES.FIVE,
queryKey: RQKEY(),
@ -91,6 +92,7 @@ export function getMembership(
export function useListMembershipAddMutation() {
const {currentAccount} = useSession()
const {getAgent} = useAgent()
const queryClient = useQueryClient()
return useMutation<
{uri: string; cid: string},
@ -149,6 +151,7 @@ export function useListMembershipAddMutation() {
export function useListMembershipRemoveMutation() {
const {currentAccount} = useSession()
const {getAgent} = useAgent()
const queryClient = useQueryClient()
return useMutation<
void,

View file

@ -13,7 +13,7 @@ import chunk from 'lodash.chunk'
import {uploadBlob} from '#/lib/api'
import {until} from '#/lib/async/until'
import {STALE} from '#/state/queries'
import {getAgent, useSession} from '../session'
import {useAgent, useSession} from '../session'
import {invalidate as invalidateMyLists} from './my-lists'
import {RQKEY as PROFILE_LISTS_RQKEY} from './profile-lists'
@ -21,6 +21,7 @@ const RQKEY_ROOT = 'list'
export const RQKEY = (uri: string) => [RQKEY_ROOT, uri]
export function useListQuery(uri?: string) {
const {getAgent} = useAgent()
return useQuery<AppBskyGraphDefs.ListView, Error>({
staleTime: STALE.MINUTES.ONE,
queryKey: RQKEY(uri || ''),
@ -48,6 +49,7 @@ export interface ListCreateMutateParams {
export function useListCreateMutation() {
const {currentAccount} = useSession()
const queryClient = useQueryClient()
const {getAgent} = useAgent()
return useMutation<{uri: string; cid: string}, Error, ListCreateMutateParams>(
{
async mutationFn({
@ -114,6 +116,7 @@ export interface ListMetadataMutateParams {
}
export function useListMetadataMutation() {
const {currentAccount} = useSession()
const {getAgent} = useAgent()
const queryClient = useQueryClient()
return useMutation<
{uri: string; cid: string},
@ -181,6 +184,7 @@ export function useListMetadataMutation() {
export function useListDeleteMutation() {
const {currentAccount} = useSession()
const {getAgent} = useAgent()
const queryClient = useQueryClient()
return useMutation<void, Error, {uri: string}>({
mutationFn: async ({uri}) => {
@ -249,6 +253,7 @@ export function useListDeleteMutation() {
export function useListMuteMutation() {
const queryClient = useQueryClient()
const {getAgent} = useAgent()
return useMutation<void, Error, {uri: string; mute: boolean}>({
mutationFn: async ({uri, mute}) => {
if (mute) {
@ -275,6 +280,7 @@ export function useListMuteMutation() {
export function useListBlockMutation() {
const queryClient = useQueryClient()
const {getAgent} = useAgent()
return useMutation<void, Error, {uri: string; block: boolean}>({
mutationFn: async ({uri, block}) => {
if (block) {

View file

@ -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 {getAgent} = useAgent()
return useInfiniteQuery<
AppBskyGraphGetBlocks.OutputSchema,
Error,

View file

@ -3,7 +3,7 @@ import {QueryClient, useQuery} from '@tanstack/react-query'
import {accumulate} from '#/lib/async/accumulate'
import {STALE} from '#/state/queries'
import {getAgent, useSession} from '#/state/session'
import {useAgent, useSession} from '#/state/session'
export type MyListsFilter =
| 'all'
@ -16,6 +16,7 @@ export const RQKEY = (filter: MyListsFilter) => [RQKEY_ROOT, filter]
export function useMyListsQuery(filter: MyListsFilter) {
const {currentAccount} = useSession()
const {getAgent} = useAgent()
return useQuery<AppBskyGraphDefs.ListView[]>({
staleTime: STALE.MINUTES.ONE,
queryKey: RQKEY(filter),

View file

@ -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 {getAgent} = useAgent()
return useInfiniteQuery<
AppBskyGraphGetMutes.OutputSchema,
Error,

View file

@ -27,7 +27,7 @@ import {
} from '@tanstack/react-query'
import {useMutedThreads} from '#/state/muted-threads'
import {getAgent} from '#/state/session'
import {useAgent} from '#/state/session'
import {STALE} from '..'
import {useModerationOpts} from '../preferences'
import {embedViewRecordToPostView, getEmbeddedPost} from '../util'
@ -47,6 +47,7 @@ export function RQKEY() {
}
export function useNotificationFeedQuery(opts?: {enabled?: boolean}) {
const {getAgent} = useAgent()
const queryClient = useQueryClient()
const moderationOpts = useModerationOpts()
const threadMutes = useMutedThreads()

View file

@ -12,7 +12,7 @@ import BroadcastChannel from '#/lib/broadcast'
import {logger} from '#/logger'
import {isNative} from '#/platform/detection'
import {useMutedThreads} from '#/state/muted-threads'
import {getAgent, useSession} from '#/state/session'
import {useAgent, useSession} from '#/state/session'
import {useModerationOpts} from '../preferences'
import {truncateAndInvalidate} from '../util'
import {RQKEY as RQKEY_NOTIFS} from './feed'
@ -46,6 +46,7 @@ const apiContext = React.createContext<ApiContext>({
export function Provider({children}: React.PropsWithChildren<{}>) {
const {hasSession} = useSession()
const {getAgent} = useAgent()
const queryClient = useQueryClient()
const moderationOpts = useModerationOpts()
const threadMutes = useMutedThreads()
@ -197,7 +198,7 @@ export function Provider({children}: React.PropsWithChildren<{}>) {
}
},
}
}, [setNumUnread, queryClient, moderationOpts, threadMutes])
}, [setNumUnread, queryClient, moderationOpts, threadMutes, getAgent])
checkUnreadRef.current = api.checkUnread
return (

View file

@ -4,6 +4,7 @@ import {
AppBskyFeedDefs,
AppBskyFeedPost,
AtUri,
BskyAgent,
ModerationDecision,
} from '@atproto/api'
import {
@ -19,7 +20,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'
@ -104,6 +105,7 @@ export function usePostFeedQuery(
const queryClient = useQueryClient()
const feedTuners = useFeedTuners(feedDesc)
const moderationOpts = useModerationOpts()
const {getAgent} = useAgent()
const enabled = opts?.enabled !== false && Boolean(moderationOpts)
const lastRun = useRef<{
data: InfiniteData<FeedPageUnselected>
@ -142,6 +144,7 @@ export function usePostFeedQuery(
feedDesc,
feedParams: params || {},
feedTuners,
getAgent,
}),
cursor: undefined,
}
@ -372,10 +375,12 @@ function createApi({
feedDesc,
feedParams,
feedTuners,
getAgent,
}: {
feedDesc: FeedDescriptor
feedParams: FeedParams
feedTuners: FeedTunerFn[]
getAgent: () => BskyAgent
}) {
if (feedDesc === 'home') {
if (feedParams.mergeFeedEnabled) {

View file

@ -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 {getAgent} = useAgent()
return useInfiniteQuery<
AppBskyFeedGetLikes.OutputSchema,
Error,

View file

@ -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 {getAgent} = useAgent()
return useInfiniteQuery<
AppBskyFeedGetRepostedBy.OutputSchema,
Error,

View file

@ -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 findAllPostsInSearchQueryData} from 'state/queries/search-posts'
import {findAllPostsInQueryData as findAllPostsInNotifsQueryData} from './notifications/feed'
import {findAllPostsInQueryData as findAllPostsInFeedQueryData} from './post-feed'
@ -66,6 +66,7 @@ export type ThreadNode =
export function usePostThreadQuery(uri: string | undefined) {
const queryClient = useQueryClient()
const {getAgent} = useAgent()
return useQuery<ThreadNode, Error>({
gcTime: 0,
queryKey: RQKEY(uri || ''),

View file

@ -7,13 +7,14 @@ import {useToggleMutationQueue} from '#/lib/hooks/useToggleMutationQueue'
import {logEvent, LogEvents, toClout} from '#/lib/statsig/statsig'
import {updatePostShadow} from '#/state/cache/post-shadow'
import {Shadow} from '#/state/cache/types'
import {getAgent, useSession} from '#/state/session'
import {useAgent, useSession} from '#/state/session'
import {findProfileQueryData} from './profile'
const RQKEY_ROOT = 'post'
export const RQKEY = (postUri: string) => [RQKEY_ROOT, postUri]
export function usePostQuery(uri: string | undefined) {
const {getAgent} = useAgent()
return useQuery<AppBskyFeedDefs.PostView>({
queryKey: RQKEY(uri || ''),
async queryFn() {
@ -30,6 +31,7 @@ export function usePostQuery(uri: string | undefined) {
export function useGetPost() {
const queryClient = useQueryClient()
const {getAgent} = useAgent()
return useCallback(
async ({uri}: {uri: string}) => {
return queryClient.fetchQuery({
@ -56,7 +58,7 @@ export function useGetPost() {
},
})
},
[queryClient],
[queryClient, getAgent],
)
}
@ -125,6 +127,7 @@ function usePostLikeMutation(
const {currentAccount} = useSession()
const queryClient = useQueryClient()
const postAuthor = post.author
const {getAgent} = useAgent()
return useMutation<
{uri: string}, // responds with the uri of the like
Error,
@ -162,6 +165,7 @@ function usePostLikeMutation(
function usePostUnlikeMutation(
logContext: LogEvents['post:unlike']['logContext'],
) {
const {getAgent} = useAgent()
return useMutation<void, Error, {postUri: string; likeUri: string}>({
mutationFn: ({likeUri}) => {
logEvent('post:unlike', {logContext})
@ -234,6 +238,7 @@ export function usePostRepostMutationQueue(
function usePostRepostMutation(
logContext: LogEvents['post:repost']['logContext'],
) {
const {getAgent} = useAgent()
return useMutation<
{uri: string}, // responds with the uri of the repost
Error,
@ -252,6 +257,7 @@ function usePostRepostMutation(
function usePostUnrepostMutation(
logContext: LogEvents['post:unrepost']['logContext'],
) {
const {getAgent} = useAgent()
return useMutation<void, Error, {postUri: string; repostUri: string}>({
mutationFn: ({repostUri}) => {
logEvent('post:unrepost', {logContext})
@ -265,6 +271,7 @@ function usePostUnrepostMutation(
export function usePostDeleteMutation() {
const queryClient = useQueryClient()
const {getAgent} = useAgent()
return useMutation<void, Error, {uri: string}>({
mutationFn: async ({uri}) => {
await getAgent().deletePost(uri)

View file

@ -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,6 +33,7 @@ const preferencesQueryKeyRoot = 'getPreferences'
export const preferencesQueryKey = [preferencesQueryKeyRoot]
export function usePreferencesQuery() {
const {getAgent} = useAgent()
return useQuery({
staleTime: STALE.SECONDS.FIFTEEN,
structuralSharing: true,
@ -118,6 +119,7 @@ export function useModerationOpts() {
export function useClearPreferencesMutation() {
const queryClient = useQueryClient()
const {getAgent} = useAgent()
return useMutation({
mutationFn: async () => {
@ -131,6 +133,7 @@ export function useClearPreferencesMutation() {
}
export function usePreferencesSetContentLabelMutation() {
const {getAgent} = useAgent()
const queryClient = useQueryClient()
return useMutation<
@ -150,6 +153,7 @@ export function usePreferencesSetContentLabelMutation() {
export function useSetContentLabelMutation() {
const queryClient = useQueryClient()
const {getAgent} = useAgent()
return useMutation({
mutationFn: async ({
@ -172,6 +176,7 @@ export function useSetContentLabelMutation() {
export function usePreferencesSetAdultContentMutation() {
const queryClient = useQueryClient()
const {getAgent} = useAgent()
return useMutation<void, unknown, {enabled: boolean}>({
mutationFn: async ({enabled}) => {
@ -186,6 +191,7 @@ export function usePreferencesSetAdultContentMutation() {
export function usePreferencesSetBirthDateMutation() {
const queryClient = useQueryClient()
const {getAgent} = useAgent()
return useMutation<void, unknown, {birthDate: Date}>({
mutationFn: async ({birthDate}: {birthDate: Date}) => {
@ -200,6 +206,7 @@ export function usePreferencesSetBirthDateMutation() {
export function useSetFeedViewPreferencesMutation() {
const queryClient = useQueryClient()
const {getAgent} = useAgent()
return useMutation<void, unknown, Partial<BskyFeedViewPreference>>({
mutationFn: async prefs => {
@ -214,6 +221,7 @@ export function useSetFeedViewPreferencesMutation() {
export function useSetThreadViewPreferencesMutation() {
const queryClient = useQueryClient()
const {getAgent} = useAgent()
return useMutation<void, unknown, Partial<ThreadViewPreferences>>({
mutationFn: async prefs => {
@ -228,6 +236,7 @@ export function useSetThreadViewPreferencesMutation() {
export function useSetSaveFeedsMutation() {
const queryClient = useQueryClient()
const {getAgent} = useAgent()
return useMutation<
void,
@ -246,6 +255,7 @@ export function useSetSaveFeedsMutation() {
export function useSaveFeedMutation() {
const queryClient = useQueryClient()
const {getAgent} = useAgent()
return useMutation<void, unknown, {uri: string}>({
mutationFn: async ({uri}) => {
@ -261,6 +271,7 @@ export function useSaveFeedMutation() {
export function useRemoveFeedMutation() {
const queryClient = useQueryClient()
const {getAgent} = useAgent()
return useMutation<void, unknown, {uri: string}>({
mutationFn: async ({uri}) => {
@ -276,6 +287,7 @@ export function useRemoveFeedMutation() {
export function usePinFeedMutation() {
const queryClient = useQueryClient()
const {getAgent} = useAgent()
return useMutation<void, unknown, {uri: string}>({
mutationFn: async ({uri}) => {
@ -291,6 +303,7 @@ export function usePinFeedMutation() {
export function useUnpinFeedMutation() {
const queryClient = useQueryClient()
const {getAgent} = useAgent()
return useMutation<void, unknown, {uri: string}>({
mutationFn: async ({uri}) => {
@ -306,6 +319,7 @@ export function useUnpinFeedMutation() {
export function useUpsertMutedWordsMutation() {
const queryClient = useQueryClient()
const {getAgent} = useAgent()
return useMutation({
mutationFn: async (mutedWords: AppBskyActorDefs.MutedWord[]) => {
@ -320,6 +334,7 @@ export function useUpsertMutedWordsMutation() {
export function useUpdateMutedWordMutation() {
const queryClient = useQueryClient()
const {getAgent} = useAgent()
return useMutation({
mutationFn: async (mutedWord: AppBskyActorDefs.MutedWord) => {
@ -334,6 +349,7 @@ export function useUpdateMutedWordMutation() {
export function useRemoveMutedWordMutation() {
const queryClient = useQueryClient()
const {getAgent} = useAgent()
return useMutation({
mutationFn: async (mutedWord: AppBskyActorDefs.MutedWord) => {

View file

@ -1,7 +1,7 @@
import {AppBskyFeedGetActorFeeds} from '@atproto/api'
import {InfiniteData, QueryKey, 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 @@ export function useProfileFeedgensQuery(
opts?: {enabled?: boolean},
) {
const enabled = opts?.enabled !== false
const {getAgent} = useAgent()
return useInfiniteQuery<
AppBskyFeedGetActorFeeds.OutputSchema,
Error,

View file

@ -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 {getAgent} = useAgent()
return useInfiniteQuery<
AppBskyGraphGetFollowers.OutputSchema,
Error,

View file

@ -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 {getAgent} = useAgent()
return useInfiniteQuery<
AppBskyGraphGetFollows.OutputSchema,
Error,

View file

@ -1,7 +1,7 @@
import {AppBskyGraphGetLists} from '@atproto/api'
import {InfiniteData, QueryKey, useInfiniteQuery} from '@tanstack/react-query'
import {getAgent} from '#/state/session'
import {useAgent} from '#/state/session'
const PAGE_SIZE = 30
type RQPageParam = string | undefined
@ -11,6 +11,7 @@ export const RQKEY = (did: string) => [RQKEY_ROOT, did]
export function useProfileListsQuery(did: string, opts?: {enabled?: boolean}) {
const enabled = opts?.enabled !== false
const {getAgent} = useAgent()
return useInfiniteQuery<
AppBskyGraphGetLists.OutputSchema,
Error,

View file

@ -26,7 +26,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 {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'
@ -54,6 +54,7 @@ export function useProfileQuery({
staleTime?: number
}) {
const queryClient = useQueryClient()
const {getAgent} = useAgent()
return useQuery<AppBskyActorDefs.ProfileViewDetailed>({
// WARNING
// this staleTime is load-bearing
@ -78,6 +79,7 @@ export function useProfileQuery({
}
export function useProfilesQuery({handles}: {handles: string[]}) {
const {getAgent} = useAgent()
return useQuery({
staleTime: STALE.MINUTES.FIVE,
queryKey: profilesQueryKey(handles),
@ -89,6 +91,7 @@ export function useProfilesQuery({handles}: {handles: string[]}) {
}
export function usePrefetchProfileQuery() {
const {getAgent} = useAgent()
const queryClient = useQueryClient()
const prefetchProfileQuery = useCallback(
async (did: string) => {
@ -100,7 +103,7 @@ export function usePrefetchProfileQuery() {
},
})
},
[queryClient],
[queryClient, getAgent],
)
return prefetchProfileQuery
}
@ -116,6 +119,7 @@ interface ProfileUpdateParams {
}
export function useProfileUpdateMutation() {
const queryClient = useQueryClient()
const {getAgent} = useAgent()
return useMutation<void, Error, ProfileUpdateParams>({
mutationFn: async ({
profile,
@ -257,6 +261,7 @@ function useProfileFollowMutation(
profile: Shadow<AppBskyActorDefs.ProfileViewDetailed>,
) {
const {currentAccount} = useSession()
const {getAgent} = useAgent()
const queryClient = useQueryClient()
return useMutation<{uri: string; cid: string}, Error, {did: string}>({
mutationFn: async ({did}) => {
@ -283,6 +288,7 @@ function useProfileFollowMutation(
function useProfileUnfollowMutation(
logContext: LogEvents['profile:unfollow']['logContext'],
) {
const {getAgent} = useAgent()
return useMutation<void, Error, {did: string; followUri: string}>({
mutationFn: async ({followUri}) => {
logEvent('profile:unfollow', {logContext})
@ -343,6 +349,7 @@ export function useProfileMuteMutationQueue(
function useProfileMuteMutation() {
const queryClient = useQueryClient()
const {getAgent} = useAgent()
return useMutation<void, Error, {did: string}>({
mutationFn: async ({did}) => {
await getAgent().mute(did)
@ -355,6 +362,7 @@ function useProfileMuteMutation() {
function useProfileUnmuteMutation() {
const queryClient = useQueryClient()
const {getAgent} = useAgent()
return useMutation<void, Error, {did: string}>({
mutationFn: async ({did}) => {
await getAgent().unmute(did)
@ -421,6 +429,7 @@ export function useProfileBlockMutationQueue(
function useProfileBlockMutation() {
const {currentAccount} = useSession()
const {getAgent} = useAgent()
const queryClient = useQueryClient()
return useMutation<{uri: string; cid: string}, Error, {did: string}>({
mutationFn: async ({did}) => {
@ -441,6 +450,7 @@ function useProfileBlockMutation() {
function useProfileUnblockMutation() {
const {currentAccount} = useSession()
const {getAgent} = useAgent()
const queryClient = useQueryClient()
return useMutation<void, Error, {did: string; blockUri: string}>({
mutationFn: async ({blockUri}) => {

View file

@ -2,7 +2,7 @@ import {AppBskyActorDefs, AtUri} from '@atproto/api'
import {useQuery, useQueryClient, UseQueryResult} from '@tanstack/react-query'
import {STALE} from '#/state/queries'
import {getAgent} from '#/state/session'
import {useAgent} from '#/state/session'
import {profileBasicQueryKey as RQKEY_PROFILE_BASIC} from './profile'
const RQKEY_ROOT = 'resolved-did'
@ -24,6 +24,7 @@ export function useResolveUriQuery(uri: string | undefined): UriUseQueryResult {
export function useResolveDidQuery(didOrHandle: string | undefined) {
const queryClient = useQueryClient()
const {getAgent} = useAgent()
return useQuery<string, Error>({
staleTime: STALE.HOURS.ONE,

View file

@ -6,7 +6,7 @@ import {
useInfiniteQuery,
} from '@tanstack/react-query'
import {getAgent} from '#/state/session'
import {useAgent} from '#/state/session'
import {embedViewRecordToPostView, getEmbeddedPost} from './util'
const searchPostsQueryKeyRoot = 'search-posts'
@ -25,6 +25,7 @@ export function useSearchPostsQuery({
sort?: 'top' | 'latest'
enabled?: boolean
}) {
const {getAgent} = useAgent()
return useInfiniteQuery<
AppBskyFeedSearchPosts.OutputSchema,
Error,

View file

@ -2,12 +2,13 @@ import {AppBskyFeedGetSuggestedFeeds} from '@atproto/api'
import {InfiniteData, QueryKey, useInfiniteQuery} from '@tanstack/react-query'
import {STALE} from '#/state/queries'
import {getAgent} from '#/state/session'
import {useAgent} from '#/state/session'
const suggestedFeedsQueryKeyRoot = 'suggestedFeeds'
export const suggestedFeedsQueryKey = [suggestedFeedsQueryKeyRoot]
export function useSuggestedFeedsQuery() {
const {getAgent} = useAgent()
return useInfiniteQuery<
AppBskyFeedGetSuggestedFeeds.OutputSchema,
Error,

View file

@ -14,7 +14,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]
@ -27,6 +27,7 @@ const suggestedFollowsByActorQueryKey = (did: string) => [
export function useSuggestedFollowsQuery() {
const {currentAccount} = useSession()
const {getAgent} = useAgent()
const moderationOpts = useModerationOpts()
return useInfiniteQuery<
@ -77,6 +78,7 @@ export function useSuggestedFollowsQuery() {
}
export function useSuggestedFollowsByActorQuery({did}: {did: string}) {
const {getAgent} = useAgent()
return useQuery<AppBskyGraphGetSuggestedFollowsByActor.OutputSchema, Error>({
queryKey: suggestedFollowsByActorQueryKey(did),
queryFn: async () => {

View file

@ -23,16 +23,14 @@ import {readLabelers} from './agent-config'
let __globalAgent: BskyAgent = PUBLIC_BSKY_AGENT
/**
* NOTE
* Never hold on to the object returned by this function.
* Call `getAgent()` at the time of invocation to ensure
* that you never have a stale agent.
*/
export function getAgent() {
function __getAgent() {
return __globalAgent
}
export function useAgent() {
return React.useMemo(() => ({getAgent: __getAgent}), [])
}
export type SessionAccount = persisted.PersistedAccount
export type SessionState = {