Integrate live threadgate query

This commit is contained in:
Eric Bailey
2024-08-02 16:20:31 -05:00
parent c3d8beee6d
commit 380c4dbf24
3 changed files with 60 additions and 2 deletions
+43 -1
View File
@@ -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
},
})
}