Files
supabase/apps/studio/components/interfaces/ProjectCreation/HighAvailabilityInput.tsx
Alaister Young de4bec77d6 [MUL-1338] fix(studio): lock compute size to large for HA projects (#49249)
When the High availability (Multigres) toggle is enabled in the New
Project form, the Compute size dropdown now offers only **Large** and
the form value is forced to `large`. Previously HA projects showed the
same micro/small/medium options as regular projects.

**Added:**
- `HIGH_AVAILABILITY_INSTANCE_SIZE` constant (`'large'`) alongside the
other `HIGH_AVAILABILITY_*` constants

**Changed:**
- `ComputeSizeSelector` watches `highAvailability` and renders only
Large when it's on (hiding the "Larger instance sizes available after
creation" row); the `cloudProvider` read is now a reactive `useWatch`
instead of a render-time `getValues()`, so the list re-filters when HA
forces the provider to `AWS_K8S`
- `HighAvailabilityInput` forces `instanceSize` to `large` when HA
toggles on and restores the previously selected size when it toggles
off, alongside the existing `dbRegion`/`cloudProvider` handling
- The compute size and region selects ignore Radix's spurious
`onValueChange('')` — Radix emits it when a select's value and option
list change in the same tick, which wiped the forced value (details in
the inline comments)
- HA projects skip the "Confirm compute costs" modal on submit — HA is
free during Alpha, so the forced large size shouldn't trigger the
$110/mo confirmation

## To test

- On a paid org, open the New Project form: with HA off, the Compute
size dropdown shows micro/small/medium plus the disabled "Larger
instance sizes available after creation" row
- Toggle High availability on: the dropdown shows only Large, the
trigger reads "large / 8 GB RAM / 2-core CPU" (not the placeholder), the
region locks as before, and the footer shows $110/m
- Check the network tab: the `available-regions` request goes out with
`desired_instance_size=large` and returns 200 (no request with an empty
`desired_instance_size`)
- Select medium first, toggle HA on then off: medium is restored (same
for other sizes); rapid toggling shouldn't leave the field blank
- With HA on, submitting goes straight through without the "Confirm
compute costs" modal; a non-HA medium project still shows it

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

## Summary by CodeRabbit

* **New Features**
* High-availability projects now automatically use the required
dedicated instance size.
* Disabling high availability restores the previously selected instance
size.
* Compute size options are filtered based on cloud provider and
high-availability settings.

* **Bug Fixes**
* Prevented accidental clearing of compute size or region selections
during option updates.
* Compute-cost confirmation is no longer required for high-availability
projects.

<!-- end of auto-generated comment: release notes by coderabbit.ai -->

---------

Co-authored-by: Alaister Young <10985857+alaister@users.noreply.github.com>
2026-08-19 13:03:39 +00:00

142 lines
5.4 KiB
TypeScript

import { useEffect, useRef } from 'react'
import { UseFormReturn } from 'react-hook-form'
import { type CloudProvider } from 'shared-data'
import { Badge, FormControl, FormField, Switch, useWatch } from 'ui'
import { FormItemLayout } from 'ui-patterns/form/FormItemLayout/FormItemLayout'
import { HIGH_AVAILABILITY_INSTANCE_SIZE } from './ProjectCreation.constants'
import { CreateProjectForm } from './ProjectCreation.schema'
import Panel from '@/components/ui/Panel'
import { useCheckEntitlements } from '@/hooks/misc/useCheckEntitlements'
interface HighAvailabilityInputProps {
form: UseFormReturn<CreateProjectForm>
// Derived from an org-wide regions query owned by the parent form, since fetching it
// here would mean duplicating that query's slug/cloud-provider/instance-size context.
highAvailabilityRegionName: string | undefined
}
export const HighAvailabilityInput = ({
form,
highAvailabilityRegionName,
}: HighAvailabilityInputProps) => {
const { getValues, setValue } = form
const { hasAccess } = useCheckEntitlements('instances.high_availability')
const highAvailability = useWatch({ control: form.control, name: 'highAvailability' })
// Fields to revert to when toggling off HA, so previously selected values aren't lost.
const beforeHighAvailability = useRef<{
cloudProvider: CloudProvider | undefined
postgresVersionSelection: string | undefined
dbRegion: string | null
instanceSize: string | null
}>({
cloudProvider: undefined,
postgresVersionSelection: undefined,
dbRegion: null,
instanceSize: null,
})
const handleHighAvailabilityChange = (checked: boolean) => {
if (checked) {
const currentCloudProvider = getValues('cloudProvider') as CloudProvider
if (currentCloudProvider !== 'AWS_K8S') {
beforeHighAvailability.current.cloudProvider = currentCloudProvider
setValue('cloudProvider', 'AWS_K8S')
}
beforeHighAvailability.current.postgresVersionSelection = getValues(
'postgresVersionSelection'
)
setValue('useOrioleDb', false)
const currentRegion = getValues('dbRegion')
if (
highAvailabilityRegionName !== undefined &&
currentRegion !== highAvailabilityRegionName
) {
beforeHighAvailability.current.dbRegion = currentRegion ?? null
setValue('dbRegion', highAvailabilityRegionName)
}
const currentInstanceSize = getValues('instanceSize')
if (currentInstanceSize !== HIGH_AVAILABILITY_INSTANCE_SIZE) {
beforeHighAvailability.current.instanceSize = currentInstanceSize ?? null
setValue('instanceSize', HIGH_AVAILABILITY_INSTANCE_SIZE)
}
} else {
if (beforeHighAvailability.current.cloudProvider !== undefined) {
setValue('cloudProvider', beforeHighAvailability.current.cloudProvider)
beforeHighAvailability.current.cloudProvider = undefined
}
if (beforeHighAvailability.current.postgresVersionSelection !== undefined) {
setValue(
'postgresVersionSelection',
beforeHighAvailability.current.postgresVersionSelection
)
beforeHighAvailability.current.postgresVersionSelection = undefined
}
if (beforeHighAvailability.current.dbRegion !== null) {
setValue('dbRegion', beforeHighAvailability.current.dbRegion)
beforeHighAvailability.current.dbRegion = null
}
if (beforeHighAvailability.current.instanceSize !== null) {
setValue('instanceSize', beforeHighAvailability.current.instanceSize)
beforeHighAvailability.current.instanceSize = null
}
}
}
// Catches the case where highAvailabilityRegionName wasn't loaded yet at the moment the
// toggle fired above (the org's available-regions query for AWS_K8S may still be in
// flight). The region auto-fill effect in the parent form skips dirty fields, so a
// manually chosen region would otherwise keep showing in the trigger — force it over
// explicitly once the HA region becomes known.
useEffect(() => {
if (!highAvailability || highAvailabilityRegionName === undefined) return
const currentRegion = getValues('dbRegion')
if (currentRegion === highAvailabilityRegionName) return
if (beforeHighAvailability.current.dbRegion === null) {
beforeHighAvailability.current.dbRegion = currentRegion ?? null
}
setValue('dbRegion', highAvailabilityRegionName)
}, [highAvailability, highAvailabilityRegionName, getValues, setValue])
if (!hasAccess) return null
return (
<Panel.Content>
<FormField
control={form.control}
name="highAvailability"
render={({ field }) => (
<FormItemLayout
label={
<div className="flex items-center gap-x-2">
<span>High availability</span>
<Badge variant="warning">Alpha</Badge>
</div>
}
description="Horizontally scalable Postgres for highly available deployments. Free during Alpha for up to 2 projects."
layout="horizontal"
>
<FormControl>
<Switch
aria-label="Enable high availability"
checked={field.value}
onCheckedChange={(checked) => {
handleHighAvailabilityChange(checked)
field.onChange(checked)
}}
/>
</FormControl>
</FormItemLayout>
)}
/>
</Panel.Content>
)
}