Files
supabase/apps/studio/data/ssl-enforcement/ssl-enforcement-update-mutation.ts
Etienne Stalmans 51c5b9f013 chore: sync ssl enforcement and temporary access (#48743)
## I have read the
[CONTRIBUTING.md](https://github.com/supabase/supabase/blob/master/CONTRIBUTING.md)
file.

YES

## What kind of change does this PR introduce?

Chore - fix-up

## What is the current behavior?

Temporary access depends on ssl enforcement. The frontend doesn't
enforce this very well or keep state between the two configs.

## What is the new behavior?

This updates the two configs to be interdependent and updates to each
one triggers a frontend state change on the other.

## Additional context

Before:



https://github.com/user-attachments/assets/8f040b62-587c-4268-9e27-27dd09b052a3



After:



https://github.com/user-attachments/assets/c62e006e-6147-4c94-b6cf-375ca300b890



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

- **New Features**
- Added confirmation dialogs and downtime warnings before changing
database SSL enforcement.
- Added loading states and success or failure notifications for SSL
updates.
- Enabled SSL enforcement directly from temporary database access
settings.

- **Bug Fixes**
- Prevented SSL enforcement from being disabled while temporary database
access is enabled.
  - Improved settings refresh after SSL enforcement changes.
<!-- end of auto-generated comment: release notes by coderabbit.ai -->
2026-08-06 10:52:37 +02:00

67 lines
2.1 KiB
TypeScript

import { useMutation, useQueryClient } from '@tanstack/react-query'
import { toast } from 'sonner'
import { sslEnforcementKeys } from './keys'
import { handleError, put } from '@/data/fetchers'
import { jitDbAccessKeys } from '@/data/jit-db-access/keys'
import type { ResponseError, UseCustomMutationOptions } from '@/types'
export type SSLEnforcementUpdateVariables = {
projectRef: string
requestedConfig: { database: boolean }
}
export type SSLEnforcementUpdateResponse = {
appliedSuccessfully: boolean
currentConfig: { database: boolean }
error?: any
}
export async function updateSSLEnforcement({
projectRef,
requestedConfig,
}: SSLEnforcementUpdateVariables) {
if (!projectRef) throw new Error('projectRef is required')
const { data, error } = await put(`/v1/projects/{ref}/ssl-enforcement`, {
params: { path: { ref: projectRef } },
body: { requestedConfig },
})
if (error) handleError(error)
return data
}
type SSLEnforcementUpdateData = Awaited<ReturnType<typeof updateSSLEnforcement>>
export const useSSLEnforcementUpdateMutation = ({
onSuccess,
onError,
...options
}: Omit<
UseCustomMutationOptions<SSLEnforcementUpdateData, ResponseError, SSLEnforcementUpdateVariables>,
'mutationFn'
> = {}) => {
const queryClient = useQueryClient()
return useMutation<SSLEnforcementUpdateData, ResponseError, SSLEnforcementUpdateVariables>({
mutationFn: (vars) => updateSSLEnforcement(vars),
async onSuccess(data, variables, context) {
const { projectRef } = variables
await queryClient.invalidateQueries({ queryKey: sslEnforcementKeys.list(projectRef) })
// JIT DB access can report `unavailableReason: 'ssl_enforcement_required'`,
// so its status needs to be refetched whenever SSL enforcement changes.
await queryClient.invalidateQueries({ queryKey: jitDbAccessKeys.list(projectRef) })
await onSuccess?.(data, variables, context)
},
async onError(data, variables, context) {
if (onError === undefined) {
toast.error(`Failed to update SSL enforcement: ${data.message}`)
} else {
onError(data, variables, context)
}
},
...options,
})
}