import * as Tooltip from '@radix-ui/react-tooltip' import { PermissionAction } from '@supabase/shared-types/out/constants' import dayjs from 'dayjs' import Link from 'next/link' import { useState } from 'react' import { Alert, Button, Form, InputNumber, Modal } from 'ui' import { number, object } from 'yup' import { useParams } from 'common/hooks' import { useProjectContext } from 'components/layouts/ProjectLayout/ProjectContext' import { FormHeader } from 'components/ui/Forms' import Panel from 'components/ui/Panel' import { useProjectDiskResizeMutation } from 'data/config/project-disk-resize-mutation' import { useProjectUsageQuery } from 'data/usage/project-usage-query' import { useCheckPermissions, useSelectedOrganization, useStore } from 'hooks' import { useOrgSubscriptionQuery } from 'data/subscriptions/org-subscription-query' export interface DiskSizeConfigurationProps { disabled?: boolean } const DiskSizeConfiguration = ({ disabled = false }: DiskSizeConfigurationProps) => { const { ui } = useStore() const { project } = useProjectContext() const organization = useSelectedOrganization() const { ref: projectRef } = useParams() const { lastDatabaseResizeAt } = project ?? {} const timeTillNextAvailableDatabaseResize = lastDatabaseResizeAt === null ? 0 : 6 * 60 - dayjs().diff(lastDatabaseResizeAt, 'minutes') const isAbleToResizeDatabase = timeTillNextAvailableDatabaseResize <= 0 const formattedTimeTillNextAvailableResize = timeTillNextAvailableDatabaseResize < 60 ? `${timeTillNextAvailableDatabaseResize} minute(s)` : `${Math.floor(timeTillNextAvailableDatabaseResize / 60)} hours and ${ timeTillNextAvailableDatabaseResize % 60 } minute(s)` const [showResetDbPass, setShowResetDbPass] = useState(false) const canUpdateDiskSizeConfig = useCheckPermissions(PermissionAction.UPDATE, 'projects', { resource: { project_id: project?.id, }, }) const { data: projectUsage } = useProjectUsageQuery({ projectRef }) const { data: projectSubscriptionData } = useOrgSubscriptionQuery({ orgSlug: organization?.slug }) const { mutate: updateProjectUsage, isLoading: isUpdatingDiskSize } = useProjectDiskResizeMutation({ onSuccess: (res, variables) => { ui.setNotification({ category: 'success', message: `Successfully updated disk size to ${variables.volumeSize} GB`, }) setShowResetDbPass(false) }, }) const confirmResetDbPass = async (values: { [prop: string]: any }) => { if (!projectRef) return console.error('Project ref is required') const volumeSize = values['new-disk-size'] updateProjectUsage({ projectRef, volumeSize }) } const currentDiskSize = projectUsage?.disk_volume_size_gb ?? 0 // to do, update with max_disk_volume_size_gb const maxDiskSize = 200 const INITIAL_VALUES = { 'new-disk-size': currentDiskSize, } const diskSizeValidationSchema = object({ 'new-disk-size': number() .required('Please enter a GB amount you want to resize the disk up to.') .moreThan(Number(currentDiskSize ?? 0), `Must be more than ${currentDiskSize} GB`) // to do, update with max_disk_volume_size_gb .lessThan(Number(maxDiskSize), 'Must be no more than 200 GB'), }) return (
{projectSubscriptionData?.plan.id !== 'free' ? (
{projectUsage?.disk_volume_size_gb && ( Current Disk Storage Size: {currentDiskSize} GB )}

Supabase employs auto-scaling storage and allows for manual disk size
{' '} adjustments when necessary

{!canUpdateDiskSizeConfig && (
You need additional permissions to increase the disk size
)}

We auto-scale your disk as you need more storage, but can only do this every 6 hours. If you upload more than 1.5x the current size of your storage, your database will go into read-only mode. If you know how big your database is going to be, you can manually increase the size here.

Read more about{' '} disk management .

) : ( Upgrade subscription } >
If you are intending to use more than 500MB of disk space, then you will need to upgrade to at least the Pro plan.
)} Increase Disk Storage Size} size="medium" visible={showResetDbPass} loading={isUpdatingDiskSize} onCancel={() => setShowResetDbPass(false)} hideFooter >
{() => currentDiskSize >= maxDiskSize ? ( <> You cannot manually expand the disk size any more than {maxDiskSize} GB. If you need more than this, contact us to learn more about the Enterprise plan. ) : ( <>
{isAbleToResizeDatabase ? `Upon updating your disk size, the next disk size update will only be available from ${dayjs().format( 'DD MMM YYYY, HH:mm (ZZ)' )}` : `Your database was last resized at ${dayjs(lastDatabaseResizeAt).format( 'DD MMM YYYY, HH:mm (ZZ)' )}. You can resize your database again in approximately ${formattedTimeTillNextAvailableResize}`}
) }
) } export default DiskSizeConfiguration