Files
supabase/apps/studio/components/interfaces/DiskManagement/fields/ThroughputField.tsx
Joshen Lim fcfb0f0222 Refactor all usage of form.watch to either useWatch or subscribe (#48436)
## Context

Replaces all usage of `form.watch()` to use `useWatch` instead + follows
the "name what you watch" convention as specified in the react-hook-form
skills.

There's also a small refactor in `SmtpForm.tsx` which removes the
unnecessary use of a `useState` to track if SMTP is enabled or not

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

* **Improvements**
* Updated many Studio forms to watch specific fields more precisely,
improving live UI updates for previews, warnings, conditional sections,
and validation messages.
* Enhanced responsiveness across settings, authentication, billing,
storage, integrations, and support flows while keeping save/update
behavior the same.
* **Refined Experiences**
* Improved the analytics table creation flow with tighter, enum-based
column type validation and structured, type-specific column options.
* **Preserved Behavior**
* Maintained existing permission checks, submission flows, and
account-management workflows.
<!-- end of auto-generated comment: release notes by coderabbit.ai -->
2026-07-30 11:45:40 +08:00

127 lines
4.7 KiB
TypeScript

import { useParams } from 'common'
import { AnimatePresence, motion } from 'framer-motion'
import { useEffect } from 'react'
import { UseFormReturn, useWatch } from 'react-hook-form'
import {
FormControl,
FormField,
FormInputGroupInput,
InputGroup,
InputGroupAddon,
InputGroupText,
} from 'ui'
import { FormItemLayout } from 'ui-patterns/form/FormItemLayout/FormItemLayout'
import { DiskStorageSchemaType } from '../DiskManagement.schema'
import {
DISK_LIMITS,
DiskType,
RESTRICTED_COMPUTE_FOR_IOPS_ON_GP3,
} from '../ui/DiskManagement.constants'
import { DiskManagementThroughputReadReplicas } from '../ui/DiskManagementReadReplicas'
import { useDiskAttributesQuery } from '@/data/config/disk-attributes-query'
type ThroughputFieldProps = {
form: UseFormReturn<DiskStorageSchemaType>
disableInput: boolean
}
export function ThroughputField({ form, disableInput }: ThroughputFieldProps) {
const { ref: projectRef } = useParams()
const { control, formState, setValue, getValues } = form
const watchedStorageType = useWatch({ control, name: 'storageType' })
const watchedTotalSize = useWatch({ control, name: 'totalSize' })
const watchedComputeSize = useWatch({ control, name: 'computeSize' })
const throughput_mbps = formState.defaultValues?.throughput
useDiskAttributesQuery({ projectRef })
const disableIopsInput =
RESTRICTED_COMPUTE_FOR_IOPS_ON_GP3.includes(watchedComputeSize) && watchedStorageType === 'gp3'
// Watch storageType and allocatedStorage to adjust constraints dynamically
useEffect(() => {
if (watchedStorageType === 'io2') {
setValue('throughput', undefined) // Throughput is not configurable for 'io2'
} else if (watchedStorageType === 'gp3') {
// Ensure throughput is within the allowed range if it's greater than or equal to 400 GB
const currentThroughput = form.getValues('throughput')
const { minThroughput, maxThroughput } = DISK_LIMITS[DiskType.GP3]
if (
!currentThroughput ||
currentThroughput < minThroughput ||
currentThroughput > maxThroughput
) {
setValue('throughput', minThroughput) // Reset to default if undefined or out of bounds
}
}
}, [watchedStorageType, watchedTotalSize, setValue, form])
return (
<AnimatePresence initial={false}>
{getValues('storageType') === 'gp3' && (
<motion.div
key="throughPutContainer"
initial={{ opacity: 0, x: -4, height: 0 }}
animate={{ opacity: 1, x: 0, height: 'auto' }}
exit={{ opacity: 0, x: -4, height: 0 }}
transition={{ duration: 0.1 }}
style={{ overflow: 'hidden' }}
>
<FormField
name="throughput"
control={control}
render={({ field }) => (
<FormItemLayout
label="Throughput"
layout="flex-row-reverse"
id={field.name}
description={
<span className="flex flex-col gap-y-2">
<p>Higher throughput suits applications with high data transfer needs.</p>
{!formState.errors.throughput && (
<DiskManagementThroughputReadReplicas
isDirty={formState.dirtyFields.throughput !== undefined}
oldThroughput={throughput_mbps ?? 0}
newThroughput={field.value ?? 0}
oldStorageType={formState.defaultValues?.storageType as DiskType}
newStorageType={getValues('storageType') as DiskType}
/>
)}
</span>
}
labelOptional={
<p className="text-foreground-lighter">Amount of data read/written per second.</p>
}
>
<FormControl className="max-w-32">
<InputGroup>
<FormInputGroupInput
type="number"
{...field}
id={field.name}
value={field.value}
onChange={(e) => {
setValue('throughput', e.target.valueAsNumber, {
shouldDirty: true,
shouldValidate: true,
})
}}
disabled={disableInput || disableIopsInput || watchedStorageType === 'io2'}
/>
<InputGroupAddon align="inline-end">
<InputGroupText>MB/s</InputGroupText>
</InputGroupAddon>
</InputGroup>
</FormControl>
</FormItemLayout>
)}
/>
</motion.div>
)}
</AnimatePresence>
)
}