Files
supabase/apps/studio/data/network-restrictions/network-restrictions-query.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

71 lines
2.2 KiB
TypeScript

import { useQuery } from '@tanstack/react-query'
import { networkRestrictionKeys } from './keys'
import { get, handleError, unguarded } from '@/data/fetchers'
import { UseCustomQueryOptions } from '@/types'
export type NetworkRestrictionsVariables = { projectRef?: string }
type NetworkRestrictionsConfig = { dbAllowedCidrs: string[]; dbAllowedCidrsV6?: string[] }
export type NetworkRestrictionsResponse = {
entitlement: 'disallowed' | 'allowed'
status: '' | 'stored' | 'applied'
config: NetworkRestrictionsConfig
old_config?: NetworkRestrictionsConfig
pending_config?: Partial<NetworkRestrictionsConfig>
pending_requested_at?: string
updated_at?: string
error?: any
}
export async function getNetworkRestrictions(
{ projectRef }: NetworkRestrictionsVariables,
signal?: AbortSignal
) {
if (!projectRef) throw new Error('projectRef is required')
const { data, error } = await get('/v1/projects/{ref}/network-restrictions', {
params: { path: { ref: projectRef } },
signal,
grant: unguarded.migrating,
})
// Not allowed error is a valid response to denote if a project
// has access to the network restrictions UI, so we'll handle it here
if (error) {
const isNotAllowedError =
(error as any)?.code === 400 &&
(error as any)?.message?.includes('not allowed to set up network restrictions')
if (isNotAllowedError) {
return {
entitlement: 'disallowed',
config: { dbAllowedCidrs: [] },
status: '',
} as NetworkRestrictionsResponse
} else {
handleError(error)
}
}
return data
}
export type NetworkRestrictionsData = Awaited<ReturnType<typeof getNetworkRestrictions>>
export type NetworkRestrictionsError = unknown
export const useNetworkRestrictionsQuery = <TData = NetworkRestrictionsData>(
{ projectRef }: NetworkRestrictionsVariables,
{
enabled = true,
...options
}: UseCustomQueryOptions<NetworkRestrictionsData, NetworkRestrictionsError, TData> = {}
) =>
useQuery<NetworkRestrictionsData, NetworkRestrictionsError, TData>({
queryKey: networkRestrictionKeys.list(projectRef),
queryFn: ({ signal }) => getNetworkRestrictions({ projectRef }, signal),
enabled: enabled && typeof projectRef !== 'undefined',
...options,
})