Files
supabase/apps/studio/data/jit-db-access/jit-db-access-revoke-mutation.ts
Danny White 05fc4aca13 chore(studio): clean up temporary access response typing (#45354)
## What kind of change does this PR introduce?

Cleanup.

## What is the current behavior?

Temporary access still has a couple of leftover JIT fallback messages
and an unnecessary local unavailable-reason type after the Platform
response types were split into `JitAccessResponse` and
`JitStateResponse`.

## What is the new behavior?

Studio relies on the generated `JitStateResponse` discriminated union
for the toggle warning and uses temporary access copy consistently in
the remaining fallback toasts.


<!-- This is an auto-generated comment: release notes by coderabbit.ai
-->

## Summary by CodeRabbit

* **Improvements**
* Refined error messaging for temporary database access grant and revoke
operations.
* Enhanced condition detection for toggle failure warnings in database
access configuration.

<!-- end of auto-generated comment: release notes by coderabbit.ai -->
2026-04-29 14:12:33 +08:00

55 lines
1.7 KiB
TypeScript

import { useMutation, useQueryClient } from '@tanstack/react-query'
import { toast } from 'sonner'
import { jitDbAccessKeys } from './keys'
import { del, handleError } from '@/data/fetchers'
import type { ResponseError, UseCustomMutationOptions } from '@/types'
type JitDbAccessRevokeVariables = {
projectRef: string
userId: string
}
async function revokeJitDbAccess({ projectRef, userId }: JitDbAccessRevokeVariables) {
if (!projectRef) throw new Error('projectRef is required')
if (!userId) throw new Error('userId is required')
const { data, error } = await del('/v1/projects/{ref}/database/jit/{user_id}', {
params: { path: { ref: projectRef, user_id: userId } },
})
if (error) handleError(error)
return data
}
type JitDbAccessRevokeData = Awaited<ReturnType<typeof revokeJitDbAccess>>
export const useJitDbAccessRevokeMutation = ({
onSuccess,
onError,
...options
}: Omit<
UseCustomMutationOptions<JitDbAccessRevokeData, ResponseError, JitDbAccessRevokeVariables>,
'mutationFn'
> = {}) => {
const queryClient = useQueryClient()
return useMutation<JitDbAccessRevokeData, ResponseError, JitDbAccessRevokeVariables>({
mutationFn: (vars) => revokeJitDbAccess(vars),
async onSuccess(data, variables, context) {
const { projectRef } = variables
await queryClient.invalidateQueries({ queryKey: jitDbAccessKeys.members(projectRef) })
await onSuccess?.(data, variables, context)
},
async onError(data, variables, context) {
if (onError === undefined) {
toast.error(`Failed to revoke temporary access: ${data.message}`)
} else {
onError(data, variables, context)
}
},
...options,
})
}