2023-11-16 20:11:56 +01:00
|
|
|
import {ComAtprotoServerCreateAppPassword} from '@atproto/api'
|
|
|
|
import {useQuery, useQueryClient, useMutation} from '@tanstack/react-query'
|
2023-11-17 00:28:50 +01:00
|
|
|
|
|
|
|
import {useSession} from '#/state/session'
|
|
|
|
import {STALE} from '#/state/queries'
|
2023-11-16 20:11:56 +01:00
|
|
|
|
|
|
|
export const RQKEY = () => ['app-passwords']
|
|
|
|
|
|
|
|
export function useAppPasswordsQuery() {
|
|
|
|
const {agent} = useSession()
|
|
|
|
return useQuery({
|
2023-11-17 00:43:53 +01:00
|
|
|
staleTime: STALE.MINUTES.ONE,
|
2023-11-16 20:11:56 +01:00
|
|
|
queryKey: RQKEY(),
|
|
|
|
queryFn: async () => {
|
|
|
|
const res = await agent.com.atproto.server.listAppPasswords({})
|
|
|
|
return res.data.passwords
|
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
export function useAppPasswordCreateMutation() {
|
|
|
|
const {agent} = useSession()
|
|
|
|
const queryClient = useQueryClient()
|
|
|
|
return useMutation<
|
|
|
|
ComAtprotoServerCreateAppPassword.OutputSchema,
|
|
|
|
Error,
|
|
|
|
{name: string}
|
|
|
|
>({
|
|
|
|
mutationFn: async ({name}) => {
|
|
|
|
return (
|
|
|
|
await agent.com.atproto.server.createAppPassword({
|
|
|
|
name,
|
|
|
|
})
|
|
|
|
).data
|
|
|
|
},
|
|
|
|
onSuccess() {
|
|
|
|
queryClient.invalidateQueries({
|
|
|
|
queryKey: RQKEY(),
|
|
|
|
})
|
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
export function useAppPasswordDeleteMutation() {
|
|
|
|
const {agent} = useSession()
|
|
|
|
const queryClient = useQueryClient()
|
|
|
|
return useMutation<void, Error, {name: string}>({
|
|
|
|
mutationFn: async ({name}) => {
|
|
|
|
await agent.com.atproto.server.revokeAppPassword({
|
|
|
|
name,
|
|
|
|
})
|
|
|
|
},
|
|
|
|
onSuccess() {
|
|
|
|
queryClient.invalidateQueries({
|
|
|
|
queryKey: RQKEY(),
|
|
|
|
})
|
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|