Replace getAgent() with reading agent (#4243)

* Replace getAgent() with agent

* Replace {agent} with agent
This commit is contained in:
dan 2024-05-28 16:37:51 +01:00 committed by GitHub
parent 8a2f43c218
commit 9bd411c151
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
74 changed files with 400 additions and 438 deletions

View file

@ -52,7 +52,7 @@ export function useProfileQuery({
staleTime?: number
}) {
const queryClient = useQueryClient()
const {getAgent} = useAgent()
const agent = useAgent()
return useQuery<AppBskyActorDefs.ProfileViewDetailed>({
// WARNING
// this staleTime is load-bearing
@ -62,7 +62,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,31 +77,31 @@ export function useProfileQuery({
}
export function useProfilesQuery({handles}: {handles: string[]}) {
const {getAgent} = useAgent()
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
},
})
}
export function usePrefetchProfileQuery() {
const {getAgent} = useAgent()
const agent = useAgent()
const queryClient = useQueryClient()
const prefetchProfileQuery = useCallback(
async (did: string) => {
await 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, getAgent],
[queryClient, agent],
)
return prefetchProfileQuery
}
@ -117,7 +117,7 @@ interface ProfileUpdateParams {
}
export function useProfileUpdateMutation() {
const queryClient = useQueryClient()
const {getAgent} = useAgent()
const agent = useAgent()
return useMutation<void, Error, ProfileUpdateParams>({
mutationFn: async ({
profile,
@ -131,7 +131,7 @@ export function useProfileUpdateMutation() {
| undefined
if (newUserAvatar) {
newUserAvatarPromise = uploadBlob(
getAgent(),
agent,
newUserAvatar.path,
newUserAvatar.mime,
)
@ -141,12 +141,12 @@ export function useProfileUpdateMutation() {
| undefined
if (newUserBanner) {
newUserBannerPromise = uploadBlob(
getAgent(),
agent,
newUserBanner.path,
newUserBanner.mime,
)
}
await getAgent().upsertProfile(async existing => {
await agent.upsertProfile(async existing => {
existing = existing || {}
if (typeof updates === 'function') {
existing = updates(existing)
@ -169,7 +169,7 @@ export function useProfileUpdateMutation() {
return existing
})
await whenAppViewReady(
getAgent,
agent,
profile.did,
checkCommitted ||
(res => {
@ -271,7 +271,7 @@ function useProfileFollowMutation(
profile: Shadow<AppBskyActorDefs.ProfileViewDetailed>,
) {
const {currentAccount} = useSession()
const {getAgent} = useAgent()
const agent = useAgent()
const queryClient = useQueryClient()
return useMutation<{uri: string; cid: string}, Error, {did: string}>({
mutationFn: async ({did}) => {
@ -287,7 +287,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})
@ -298,12 +298,12 @@ function useProfileFollowMutation(
function useProfileUnfollowMutation(
logContext: LogEvents['profile:unfollow']['logContext'],
) {
const {getAgent} = useAgent()
const agent = useAgent()
return useMutation<void, Error, {did: string; followUri: string}>({
mutationFn: async ({followUri}) => {
logEvent('profile:unfollow', {logContext})
track('Profile:Unfollow', {username: followUri})
return await getAgent().deleteFollow(followUri)
return await agent.deleteFollow(followUri)
},
})
}
@ -359,10 +359,10 @@ export function useProfileMuteMutationQueue(
function useProfileMuteMutation() {
const queryClient = useQueryClient()
const {getAgent} = useAgent()
const agent = useAgent()
return useMutation<void, Error, {did: string}>({
mutationFn: async ({did}) => {
await getAgent().mute(did)
await agent.mute(did)
},
onSuccess() {
queryClient.invalidateQueries({queryKey: RQKEY_MY_MUTED()})
@ -372,10 +372,10 @@ function useProfileMuteMutation() {
function useProfileUnmuteMutation() {
const queryClient = useQueryClient()
const {getAgent} = useAgent()
const agent = useAgent()
return useMutation<void, Error, {did: string}>({
mutationFn: async ({did}) => {
await getAgent().unmute(did)
await agent.unmute(did)
},
onSuccess() {
queryClient.invalidateQueries({queryKey: RQKEY_MY_MUTED()})
@ -440,14 +440,14 @@ export function useProfileBlockMutationQueue(
function useProfileBlockMutation() {
const {currentAccount} = useSession()
const {getAgent} = useAgent()
const agent = useAgent()
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()},
)
@ -461,7 +461,7 @@ function useProfileBlockMutation() {
function useProfileUnblockMutation() {
const {currentAccount} = useSession()
const {getAgent} = useAgent()
const agent = useAgent()
const queryClient = useQueryClient()
return useMutation<void, Error, {did: string; blockUri: string}>({
mutationFn: async ({blockUri}) => {
@ -469,7 +469,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,
})
@ -489,7 +489,7 @@ export function precacheProfile(
}
async function whenAppViewReady(
getAgent: () => BskyAgent,
agent: BskyAgent,
actor: string,
fn: (res: AppBskyActorGetProfile.Response) => boolean,
) {
@ -497,7 +497,7 @@ async function whenAppViewReady(
5, // 5 tries
1e3, // 1s delay between tries
fn,
() => getAgent().app.bsky.actor.getProfile({actor}),
() => agent.app.bsky.actor.getProfile({actor}),
)
}