Integrate live threadgate query
This commit is contained in:
@@ -1,4 +1,7 @@
|
||||
import {AppBskyFeedDefs, AppBskyFeedThreadgate} from '@atproto/api'
|
||||
import {AppBskyFeedDefs, AppBskyFeedThreadgate, AtUri} from '@atproto/api'
|
||||
import {useQuery} from '@tanstack/react-query'
|
||||
|
||||
import {useAgent} from '#/state/session'
|
||||
|
||||
export type ThreadgateSetting =
|
||||
| {type: 'nobody'}
|
||||
@@ -36,3 +39,42 @@ export function threadgateViewToSettings(
|
||||
.filter(n => !!n)
|
||||
return settings
|
||||
}
|
||||
|
||||
export const threadgateRecordQueryKeyRoot = 'threadgate-record'
|
||||
export const createThreadgateRecordQueryKey = (uri: string) => [
|
||||
threadgateRecordQueryKeyRoot,
|
||||
uri,
|
||||
]
|
||||
|
||||
export function useThreadgateRecordQuery({
|
||||
postUri,
|
||||
initialData,
|
||||
}: {
|
||||
postUri?: string
|
||||
initialData?: AppBskyFeedThreadgate.Record
|
||||
} = {}) {
|
||||
const agent = useAgent()
|
||||
|
||||
return useQuery({
|
||||
enabled: !!postUri,
|
||||
queryKey: createThreadgateRecordQueryKey(postUri || ''),
|
||||
placeholderData: initialData,
|
||||
async queryFn() {
|
||||
const urip = new AtUri(postUri!)
|
||||
|
||||
if (!urip.host.startsWith('did:')) {
|
||||
const res = await agent.resolveHandle({
|
||||
handle: urip.host,
|
||||
})
|
||||
urip.host = res.data.did
|
||||
}
|
||||
|
||||
const {value} = await agent.api.app.bsky.feed.threadgate.get({
|
||||
repo: urip.host,
|
||||
rkey: urip.rkey,
|
||||
})
|
||||
|
||||
return value
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user