import { PermissionAction } from '@supabase/shared-types/out/constants' import { ArrowRight } from 'lucide-react' import { UseFormReturn } from 'react-hook-form' import { Alert, AlertTitle, Button, ButtonProps, Dialog, DialogContent, DialogDescription, DialogFooter, DialogHeader, DialogSection, DialogSectionSeparator, DialogTitle, DialogTrigger, WarningIcon, } from 'ui' import { DiskStorageSchemaType } from '../DiskManagement.schema' import { DiskManagementMessage } from '../DiskManagement.types' import { DISK_AUTOSCALE_CONFIG_DEFAULTS } from '../ui/DiskManagement.constants' import { BreakdownRow, PriceDelta, ValueChange, } from './DiskManagementReviewAndSubmitDialog.components' import { useDiskManagementReviewChanges } from './DiskManagementReviewAndSubmitDialog.hooks' import { TaxDisclaimer } from '@/components/interfaces/Billing/TaxDisclaimer' import { ButtonTooltip } from '@/components/ui/ButtonTooltip' import { useAsyncCheckPermissions } from '@/hooks/misc/useCheckPermissions' import { useSelectedProjectQuery } from '@/hooks/misc/useSelectedProject' import { formatCurrency } from '@/lib/helpers' interface DiskManagementReviewAndSubmitDialogProps { loading: boolean form: UseFormReturn numReplicas: number isDialogOpen: boolean disabled?: boolean setIsDialogOpen: (isOpen: boolean) => void onSubmit: (values: DiskStorageSchemaType) => Promise buttonSize?: ButtonProps['size'] message?: DiskManagementMessage | null } export const DiskManagementReviewAndSubmitDialog = ({ isDialogOpen, setIsDialogOpen, disabled, form, numReplicas, loading, onSubmit, message, buttonSize = 'medium', }: DiskManagementReviewAndSubmitDialogProps) => { const { data: project } = useSelectedProjectQuery() const { can: canUpdateDiskConfiguration } = useAsyncCheckPermissions( PermissionAction.UPDATE, 'projects', { resource: { project_id: project?.id } } ) const isDirty = !!Object.keys(form.formState.dirtyFields).length const { computeSizePrice, diskSizePrice, iopsPrice, throughputPrice, totalBeforePrice, totalAfterPrice, hasComputeChanges, hasTotalSizeChanges, hasStorageTypeChanges, hasIOPSChanges, hasGrowthPercentChanges, hasMinIncrementChanges, hasMaxSizeChanges, anyBillableDiskChange, anyDiskAttributeChange, showThroughputRow, hasAnyBreakdownRows, oldComputeLabel, newComputeLabel, } = useDiskManagementReviewChanges(form, numReplicas) return ( { e.preventDefault() const isValid = await form.trigger() if (isValid) setIsDialogOpen(true) }} disabled={disabled || !isDirty} tooltip={{ content: { side: 'bottom', text: !canUpdateDiskConfiguration ? 'You need additional permissions to update disk configuration' : disabled ? 'Current form values are invalid' : undefined, }, }} > Review changes Review changes Changes will be applied shortly after confirmation. {(hasComputeChanges || anyBillableDiskChange) && ( <>
Before {formatCurrency(totalBeforePrice)} per month
After {formatCurrency(totalAfterPrice)} per month
)} {hasAnyBreakdownRows && (
{hasComputeChanges && (
)} {hasStorageTypeChanges && ( )} {(hasIOPSChanges || hasStorageTypeChanges) && (
)} {showThroughputRow && (
)} {(hasTotalSizeChanges || hasStorageTypeChanges) && (
)} {hasGrowthPercentChanges && ( )} {hasMinIncrementChanges && ( )} {hasMaxSizeChanges && ( )} {numReplicas > 0 && (
Price change includes primary database and {numReplicas} replica {numReplicas > 1 ? 's' : ''}
)}
)} {message && ( <> {message.message} )}
) }