Update post embed fetching to use new methods (#1916)

* Update post embed fetching to use new methods

* Use session agent
This commit is contained in:
Eric Bailey 2023-11-15 19:09:13 -06:00 committed by GitHub
parent 9bcd00b831
commit f23e9978d8
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 51 additions and 22 deletions

View file

@ -1,5 +1,6 @@
import {AppBskyFeedDefs} from '@atproto/api'
import {useQuery, useMutation} from '@tanstack/react-query'
import React from 'react'
import {AppBskyFeedDefs, AtUri} from '@atproto/api'
import {useQuery, useMutation, useQueryClient} from '@tanstack/react-query'
import {useSession} from '../session'
import {updatePostShadow} from '../cache/post-shadow'
@ -21,6 +22,39 @@ export function usePostQuery(uri: string | undefined) {
})
}
export function useGetPost() {
const queryClient = useQueryClient()
const {agent} = useSession()
return React.useCallback(
async ({uri}: {uri: string}) => {
return queryClient.fetchQuery({
queryKey: RQKEY(uri || ''),
async queryFn() {
const urip = new AtUri(uri)
if (!urip.host.startsWith('did:')) {
const res = await agent.resolveHandle({
handle: urip.host,
})
urip.host = res.data.did
}
const res = await agent.getPosts({
uris: [urip.toString()!],
})
if (res.success && res.data.posts[0]) {
return res.data.posts[0]
}
throw new Error('useGetPost: post not found')
},
})
},
[agent, queryClient],
)
}
export function usePostLikeMutation() {
const {agent} = useSession()
return useMutation<