2023-11-16 20:11:56 +01:00
|
|
|
import {ComAtprotoServerCreateAppPassword} from '@atproto/api'
|
2024-04-04 19:57:38 +02:00
|
|
|
import {useMutation, useQuery, useQueryClient} from '@tanstack/react-query'
|
2023-11-17 00:28:50 +01:00
|
|
|
|
|
|
|
import {STALE} from '#/state/queries'
|
2024-04-25 23:29:05 +02:00
|
|
|
import {useAgent} from '../session'
|
2023-11-16 20:11:56 +01:00
|
|
|
|
2024-04-04 19:57:38 +02:00
|
|
|
const RQKEY_ROOT = 'app-passwords'
|
|
|
|
export const RQKEY = () => [RQKEY_ROOT]
|
2023-11-16 20:11:56 +01:00
|
|
|
|
|
|
|
export function useAppPasswordsQuery() {
|
2024-04-25 23:29:05 +02:00
|
|
|
const {getAgent} = useAgent()
|
2023-11-16 20:11:56 +01:00
|
|
|
return useQuery({
|
2023-12-06 21:31:54 +01:00
|
|
|
staleTime: STALE.MINUTES.FIVE,
|
2023-11-16 20:11:56 +01:00
|
|
|
queryKey: RQKEY(),
|
|
|
|
queryFn: async () => {
|
2023-11-17 03:26:22 +01:00
|
|
|
const res = await getAgent().com.atproto.server.listAppPasswords({})
|
2023-11-16 20:11:56 +01:00
|
|
|
return res.data.passwords
|
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
export function useAppPasswordCreateMutation() {
|
|
|
|
const queryClient = useQueryClient()
|
2024-04-25 23:29:05 +02:00
|
|
|
const {getAgent} = useAgent()
|
2023-11-16 20:11:56 +01:00
|
|
|
return useMutation<
|
|
|
|
ComAtprotoServerCreateAppPassword.OutputSchema,
|
|
|
|
Error,
|
|
|
|
{name: string}
|
|
|
|
>({
|
|
|
|
mutationFn: async ({name}) => {
|
|
|
|
return (
|
2023-11-17 03:26:22 +01:00
|
|
|
await getAgent().com.atproto.server.createAppPassword({
|
2023-11-16 20:11:56 +01:00
|
|
|
name,
|
|
|
|
})
|
|
|
|
).data
|
|
|
|
},
|
|
|
|
onSuccess() {
|
|
|
|
queryClient.invalidateQueries({
|
|
|
|
queryKey: RQKEY(),
|
|
|
|
})
|
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
export function useAppPasswordDeleteMutation() {
|
|
|
|
const queryClient = useQueryClient()
|
2024-04-25 23:29:05 +02:00
|
|
|
const {getAgent} = useAgent()
|
2023-11-16 20:11:56 +01:00
|
|
|
return useMutation<void, Error, {name: string}>({
|
|
|
|
mutationFn: async ({name}) => {
|
2023-11-17 03:26:22 +01:00
|
|
|
await getAgent().com.atproto.server.revokeAppPassword({
|
2023-11-16 20:11:56 +01:00
|
|
|
name,
|
|
|
|
})
|
|
|
|
},
|
|
|
|
onSuccess() {
|
|
|
|
queryClient.invalidateQueries({
|
|
|
|
queryKey: RQKEY(),
|
|
|
|
})
|
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|