import { UseFormReturn } from 'react-hook-form' import { InputVariants } from '@ui/components/shadcn/ui/input' import { useParams } from 'common' import { useDiskAutoscaleCustomConfigQuery } from 'data/config/disk-autoscale-config-query' import { cn, FormControl_Shadcn_, FormField_Shadcn_, Input_Shadcn_, Skeleton } from 'ui' import { Admonition } from 'ui-patterns' import { FormItemLayout } from 'ui-patterns/form/FormItemLayout/FormItemLayout' import { DiskStorageSchemaType } from '../DiskManagement.schema' import { DISK_AUTOSCALE_CONFIG_DEFAULTS } from '../ui/DiskManagement.constants' import FormMessage from '../ui/FormMessage' import { InputPostTab } from '../ui/InputPostTab' type AutoScaleFieldProps = { form: UseFormReturn } export const AutoScaleFields = ({ form }: AutoScaleFieldProps) => { const { ref: projectRef } = useParams() const { control, setValue, formState: { errors }, } = form const { totalSize, growthPercent, maxSizeGb, minIncrementGb } = form.watch() const { error, isPending: isLoading, isError } = useDiskAutoscaleCustomConfigQuery({ projectRef }) const _growthPercent = growthPercent ?? DISK_AUTOSCALE_CONFIG_DEFAULTS.growthPercent const _minIncrementGb = minIncrementGb ?? DISK_AUTOSCALE_CONFIG_DEFAULTS.minIncrementSize const _maxSizeGb = errors.maxSizeGb ? DISK_AUTOSCALE_CONFIG_DEFAULTS.maxSizeGb : (maxSizeGb ?? DISK_AUTOSCALE_CONFIG_DEFAULTS.maxSizeGb) const growthSize = Math.floor(totalSize * (_growthPercent / 100)) const autoscaleGrowValue = Math.min( Math.max(Math.floor((totalSize * _growthPercent) / 100), _minIncrementGb), 200 ) const totalSizeAfterGrowth = autoscaleGrowValue + totalSize const formattedTotalSizeAfterGrowth = totalSizeAfterGrowth < _maxSizeGb ? totalSizeAfterGrowth : _maxSizeGb const formattedGrowValue = formattedTotalSizeAfterGrowth - totalSize return ( <> { return ( {isLoading ? (
) : ( { setValue( 'growthPercent', e.target.value === '' ? null : e.target.valueAsNumber, { shouldDirty: true, shouldValidate: true, } ) }} placeholder={String(DISK_AUTOSCALE_CONFIG_DEFAULTS.growthPercent)} /> )}
{error && }
) }} /> { return ( growthSize && !errors.minIncrementGb ? `This value takes precedence as the minimum increment is larger than the growth percent` : undefined } className="[&>div>span]:text-foreground-lighter" > {isLoading ? (
) : ( { setValue( 'minIncrementGb', e.target.value === '' ? null : e.target.valueAsNumber, { shouldDirty: true, shouldValidate: true, } ) }} placeholder={String(DISK_AUTOSCALE_CONFIG_DEFAULTS.minIncrementSize)} /> )}
{error && }
) }} /> { return ( {isLoading ? (
) : ( { setValue( 'maxSizeGb', e.target.value === '' ? null : e.target.valueAsNumber, { shouldDirty: true, shouldValidate: true, } ) }} placeholder={String(DISK_AUTOSCALE_CONFIG_DEFAULTS.maxSizeGb)} /> )}
{error && }
) }} /> { Disk size will automatically be expanded by{' '} {String(formattedGrowValue).length > 4 ? formattedGrowValue.toFixed(2) : formattedGrowValue}{' '} GB {' '} to a total of {formattedTotalSizeAfterGrowth} GB{' '} when the database reaches 90% of the disk size } ) }