bsky-app/src/state/queries/app-passwords.ts
dan 9bd411c151
Replace getAgent() with reading agent (#4243)
* Replace getAgent() with agent

* Replace {agent} with agent
2024-05-28 16:37:51 +01:00

61 lines
1.5 KiB
TypeScript

import {ComAtprotoServerCreateAppPassword} from '@atproto/api'
import {useMutation, useQuery, useQueryClient} from '@tanstack/react-query'
import {STALE} from '#/state/queries'
import {useAgent} from '../session'
const RQKEY_ROOT = 'app-passwords'
export const RQKEY = () => [RQKEY_ROOT]
export function useAppPasswordsQuery() {
const agent = useAgent()
return useQuery({
staleTime: STALE.MINUTES.FIVE,
queryKey: RQKEY(),
queryFn: async () => {
const res = await agent.com.atproto.server.listAppPasswords({})
return res.data.passwords
},
})
}
export function useAppPasswordCreateMutation() {
const queryClient = useQueryClient()
const agent = useAgent()
return useMutation<
ComAtprotoServerCreateAppPassword.OutputSchema,
Error,
{name: string; privileged: boolean}
>({
mutationFn: async ({name, privileged}) => {
return (
await agent.com.atproto.server.createAppPassword({
name,
privileged,
})
).data
},
onSuccess() {
queryClient.invalidateQueries({
queryKey: RQKEY(),
})
},
})
}
export function useAppPasswordDeleteMutation() {
const queryClient = useQueryClient()
const agent = useAgent()
return useMutation<void, Error, {name: string}>({
mutationFn: async ({name}) => {
await agent.com.atproto.server.revokeAppPassword({
name,
})
},
onSuccess() {
queryClient.invalidateQueries({
queryKey: RQKEY(),
})
},
})
}