import { AnimatePresence, motion } from 'framer-motion' import { Info } from 'lucide-react' import MotionNumber from 'motion-number' import { UseFormReturn } from 'react-hook-form' import { useParams } from 'common' import { useProjectContext } from 'components/layouts/ProjectLayout/ProjectContext' import { useDiskUtilizationQuery } from 'data/config/disk-utilization-query' import { useDatabaseSizeQuery } from 'data/database/database-size-query' import { GB } from 'lib/constants' import { formatBytes } from 'lib/helpers' import { badgeVariants, cn, Tooltip_Shadcn_, TooltipContent_Shadcn_, TooltipTrigger_Shadcn_, } from 'ui' import { DiskStorageSchemaType } from '../DiskManagement.schema' import { AUTOSCALING_THRESHOLD } from './DiskManagement.constants' interface DiskSpaceBarProps { form: UseFormReturn } export default function DiskSpaceBar({ form }: DiskSpaceBarProps) { const { ref } = useParams() const { formState, watch } = form const { data: diskUtil, // to do, error handling } = useDiskUtilizationQuery({ projectRef: ref, }) const usedSize = Math.round(((diskUtil?.metrics.fs_used_bytes ?? 0) / GB) * 100) / 100 const totalSize = formState.defaultValues?.totalSize || 0 const show = formState.dirtyFields.totalSize !== undefined && usedSize const newTotalSize = watch('totalSize') const usedPercentage = (usedSize / totalSize) * 100 const resizePercentage = AUTOSCALING_THRESHOLD * 100 const newUsedPercentage = (usedSize / newTotalSize) * 100 const newResizePercentage = AUTOSCALING_THRESHOLD * 100 const { project } = useProjectContext() const { data } = useDatabaseSizeQuery({ projectRef: project?.ref, connectionString: project?.connectionString, }) const databaseSizeBytes = data?.result[0].db_size ?? 0 return (
{usedSize.toFixed(2)} GB used of {' '} GB
{!show ? (
) : (
)} {show && ( New disk size )}
{!show && (
Autoscaling

Supabase expands your disk storage automatically when the database reached 90% of the disk size. However, any disk modifications, including auto-scaling, can only take place once every 6 hours.

If within those 6 hours you reach 95% of the disk space, your project{' '} will enter read-only mode.

)}
{!show && (
Used Space
Available space
)}

Note: Disk Size refers to the total space your project occupies on disk, including the database itself (currently{' '} {formatBytes(databaseSizeBytes, 2, 'GB')}), additional files like the write-ahead log (WAL), and other internal resources.

) }