Files
supabase/apps/studio/data/network-restrictions/network-restrictions-review-mutation.ts
Connor Davis c920e07d9c feat(studio): review a pending network restrictions change (#52984)
## Problem

A network restrictions change that was never applied can now wait for
the customer's review ([API
Docs](https://api.supabase.com/api/v1#tag/projects/POST/v1/projects/{ref}/network-restrictions/review),
[CTRL-1934](https://linear.app/supabase/issue/CTRL-1934)). Studio
doesn't show it. Lists with an entry that isn't a valid IP range also
fail to save without saying which entry is wrong.

## Solution

- A change in review shows one callout with **Apply change** and
**Discard change**, and both lists together: added entries in green,
removed ones in red. Editing is disabled until it's resolved, and
discarding asks for confirmation. Only the change on screen is resolved
(`pending_requested_at`).
- A list the worker is still applying shows as applying and is polled,
instead of showing the "not applied correctly" error.
- Entries that aren't valid ranges are marked Invalid with a suggested
correction, and can be saved corrected. The add dialog rejects
IPv4-mapped IPv6 addresses and finds duplicates spelled differently.

There's no way to create a review yet: a change only ends up in review
when the backend fails to apply a list. I tested the review state
locally by editing the database directly.

Stacked on #53046, which syncs the API types with production, including
the review endpoint. This PR will be retargeted to `master` once that
merges.

## Screenshots

<img width="712" height="473" alt="Screenshot 2026-11-04 at 5 10 19 PM"
src="https://github.com/user-attachments/assets/c72cc38d-f7b4-4269-a787-f27dfe382086"
/>

### If pressing `apply`

<img width="704" height="150" alt="Screenshot 2026-11-04 at 5 19 27 PM"
src="https://github.com/user-attachments/assets/92dce8e5-8d58-48b8-8899-ed01c9f242c2"
/>

### If pressing `discard`

<img width="712" height="473" alt="Screenshot 2026-11-04 at 5 10 38 PM"
src="https://github.com/user-attachments/assets/85656004-d510-4cef-89c1-80d9f95ebb83"
/>

### Invalid

<img width="754" height="596" alt="Image"
src="https://github.com/user-attachments/assets/4ba480f6-af4c-4253-9693-2c545fb77ab1"
/>

### Invalid repair

<img width="704" height="680" alt="Screenshot 2026-11-04 at 6 03 20 PM"
src="https://github.com/user-attachments/assets/ed4e3dcf-8d9b-4588-afc0-938248c78712"
/>

### Invalid and Pending

<img width="704" height="593" alt="Image"
src="https://github.com/user-attachments/assets/7ad76b50-0d08-4505-89ad-b6afff6865af"
/>

### Invalid and Pending repair

<img width="704" height="585" alt="Screenshot 2026-11-04 at 6 05 04 PM"
src="https://github.com/user-attachments/assets/b123453f-0f30-452d-b84c-a556c52458a0"
/>

## Review instructions

1. Locally, put a project's network restrictions into review by editing
the database directly. There's no API that creates one.
2. Open Database Settings > Network restrictions and check the callout
and the added and removed entries.
3. Apply or discard the change.

## Checklist

Check all before review:

- [x] I have read
[CONTRIBUTING.md](https://github.com/supabase/supabase/blob/master/CONTRIBUTING.md)
- [x] If I wrote a new docs topic or edited an existing topic, I used
the `/write-the-docs` or `/edit-the-docs` skill, which applies the docs
[style
guide](https://github.com/supabase/supabase/tree/master/apps/docs/style-guide)
2026-11-06 13:15:46 -05:00

84 lines
2.6 KiB
TypeScript

import { useMutation, useQueryClient } from '@tanstack/react-query'
import { toast } from 'sonner'
import { networkRestrictionKeys } from './keys'
import { configKeys } from '@/data/config/keys'
import { handleError, post } from '@/data/fetchers'
import type { StudioError } from '@/lib/errors/studio-errors'
import type { GrantFor } from '@/lib/grants'
import type { UseCustomMutationOptions } from '@/types'
export type NetworkRestrictionsReviewVariables = {
projectRef: string
grant: GrantFor<'post', '/v1/projects/{ref}/network-restrictions/review'>
action: 'apply' | 'reject'
// The pending_requested_at of the change on screen: the API only resolves that change
requestedAt: string
}
async function reviewNetworkRestrictions({
projectRef,
grant,
action,
requestedAt,
}: NetworkRestrictionsReviewVariables) {
if (!projectRef) throw new Error('projectRef is required')
const { data, error } = await post('/v1/projects/{ref}/network-restrictions/review', {
params: { path: { ref: projectRef } },
body: { action, requested_at: requestedAt },
grant,
})
if (error) handleError(error)
return data
}
type NetworkRestrictionsReviewData = Awaited<ReturnType<typeof reviewNetworkRestrictions>>
export const useNetworkRestrictionsReviewMutation = ({
onSuccess,
onError,
...options
}: Omit<
UseCustomMutationOptions<
NetworkRestrictionsReviewData,
StudioError,
NetworkRestrictionsReviewVariables
>,
'mutationFn'
> = {}) => {
const queryClient = useQueryClient()
return useMutation<
NetworkRestrictionsReviewData,
StudioError,
NetworkRestrictionsReviewVariables
>({
mutationFn: (vars) => reviewNetworkRestrictions(vars),
async onSuccess(data, variables, context) {
const { projectRef } = variables
await Promise.all([
queryClient.invalidateQueries({ queryKey: networkRestrictionKeys.list(projectRef) }),
queryClient.invalidateQueries({ queryKey: configKeys.projectConfig(projectRef) }),
])
await onSuccess?.(data, variables, context)
},
async onError(error, variables, context) {
// A 409 means the change on screen was already resolved or replaced, so show the current state
if (error.code === 409) {
await queryClient.invalidateQueries({
queryKey: networkRestrictionKeys.list(variables.projectRef),
})
}
if (onError === undefined) {
const verb = variables.action === 'apply' ? 'apply' : 'discard'
toast.error(`Failed to ${verb} network restrictions change: ${error.message}`)
} else {
onError(error, variables, context)
}
},
...options,
})
}