Add queries

This commit is contained in:
Eric Bailey
2024-08-08 15:22:09 -05:00
parent b2c9dc4e34
commit c4e5782e1e
2 changed files with 61 additions and 6 deletions
+53 -6
View File
@@ -4,7 +4,7 @@ import {
AtUri,
BskyAgent,
} from '@atproto/api'
import {useMutation, useQueryClient} from '@tanstack/react-query'
import {useMutation, useQuery, useQueryClient} from '@tanstack/react-query'
import {networkRetry, retry} from '#/lib/async/retry'
import {logger} from '#/logger'
@@ -85,17 +85,13 @@ export async function writePostgateRecord({
postgate: AppBskyFeedPostgate.Record
}) {
const postUrip = new AtUri(postUri)
const record = createPostgateRecord({
post: postUri,
detachedQuotes: postgate.detachedQuotes,
})
await networkRetry(2, () =>
agent.api.com.atproto.repo.putRecord({
repo: agent.session!.did,
collection: POSTGATE_COLLECTION,
rkey: postUrip.rkey,
record,
record: postgate,
}),
)
}
@@ -125,6 +121,20 @@ export async function upsertPostgate(
})
}
export const createPostgateQueryKey = (postUri: string) => [
'postgate-record',
postUri,
]
export function usePostgateQuery({postUri}: {postUri: string}) {
const agent = useAgent()
return useQuery({
queryKey: createPostgateQueryKey(postUri),
queryFn() {
return getPostgateRecord({agent, postUri})
},
})
}
export function useToggleQuoteDetachmentMutation() {
const agent = useAgent()
const queryClient = useQueryClient()
@@ -194,3 +204,40 @@ export function useToggleQuoteDetachmentMutation() {
},
})
}
export function useToggleQuotepostEnabledMutation() {
const agent = useAgent()
return useMutation({
mutationFn: async ({
postUri,
action,
}: {
postUri: string
action: 'enable' | 'disable'
}) => {
await upsertPostgate({agent, postUri: postUri}, async prev => {
console.log({action})
if (prev) {
if (action === 'disable') {
return mergePostgateRecords(prev, {
quotepostRules: [{$type: 'app.bsky.feed.postgate#disableRule'}],
})
} else if (action === 'enable') {
return {
...prev,
quotepostRules: [],
}
}
} else {
if (action === 'disable') {
return createPostgateRecord({
post: postUri,
quotepostRules: [{$type: 'app.bsky.feed.postgate#disableRule'}],
})
}
}
})
},
})
}
+8
View File
@@ -21,6 +21,7 @@ export function createPostgateRecord(
createdAt: new Date().toISOString(),
post: postgate.post,
detachedQuotes: postgate.detachedQuotes || [],
quotepostRules: postgate.quotepostRules || [],
}
}
@@ -31,9 +32,16 @@ export function mergePostgateRecords(
const detachedQuotes = Array.from(
new Set([...(prev.detachedQuotes || []), ...(next.detachedQuotes || [])]),
)
const quotepostRules = [
...(prev.quotepostRules || []),
...(next.quotepostRules || []),
].filter(
(rule, i, all) => all.findIndex(_rule => _rule.$type === rule.$type) === i,
)
return createPostgateRecord({
post: prev.post,
detachedQuotes,
quotepostRules,
})
}