import MotionNumber from '@number-flow/react' import { useParams } from 'common' import { InfoIcon } from 'lucide-react' import { AnimatePresence, motion } from 'motion/react' import { useTheme } from 'next-themes' import { useMemo } from 'react' import { UseFormReturn, useWatch } from 'react-hook-form' import { Badge, cn, Tooltip, TooltipContent, TooltipTrigger } from 'ui' import { DiskStorageSchemaType } from '../DiskManagement.schema' import { AUTOSCALING_THRESHOLD } from './DiskManagement.constants' import { useDiskBreakdownQuery } from '@/data/config/disk-breakdown-query' import { useDiskUtilizationQuery } from '@/data/config/disk-utilization-query' import { useSelectedProjectQuery } from '@/hooks/misc/useSelectedProject' import { GB } from '@/lib/constants' import { formatBytes } from '@/lib/helpers' interface DiskSpaceBarProps { form: UseFormReturn } export const DiskSpaceBar = ({ form }: DiskSpaceBarProps) => { const { ref } = useParams() const { resolvedTheme } = useTheme() const { formState } = form const isDarkMode = resolvedTheme?.includes('dark') const { data: project } = useSelectedProjectQuery() const { data: diskUtil, // to do, error handling } = useDiskUtilizationQuery({ projectRef: ref, }) const { data: diskBreakdown } = useDiskBreakdownQuery({ projectRef: ref, connectionString: project?.connectionString, }) const diskBreakdownBytes = useMemo(() => { return { availableBytes: diskUtil?.metrics.fs_avail_bytes ?? 0, totalUsedBytes: diskUtil?.metrics.fs_used_bytes ?? 0, totalDiskSizeBytes: diskUtil?.metrics.fs_size_bytes, dbSizeBytes: Math.max(0, diskBreakdown?.db_size_bytes ?? 0), walSizeBytes: Math.max(0, diskBreakdown?.wal_size_bytes ?? 0), systemBytes: Math.max( 0, (diskUtil?.metrics.fs_used_bytes ?? 0) - (diskBreakdown?.db_size_bytes ?? 0) - (diskBreakdown?.wal_size_bytes ?? 0) ), } }, [diskUtil, diskBreakdown]) const showNewSize = formState.dirtyFields.totalSize !== undefined && diskBreakdown const newTotalSize = useWatch({ control: form.control, name: 'totalSize' }) const totalSize = formState.defaultValues?.totalSize || 0 const usedSizeTotal = Math.round(((diskBreakdownBytes?.totalUsedBytes ?? 0) / GB) * 100) / 100 const usedTotalPercentage = Math.min((usedSizeTotal / totalSize) * 100, 100) const usedSizeDatabase = Math.round(((diskBreakdownBytes?.dbSizeBytes ?? 0) / GB) * 100) / 100 const usedPercentageDatabase = totalSize === 0 ? 0 : Math.min((usedSizeDatabase / totalSize) * 100, 100) const newUsedPercentageDatabase = Math.min((usedSizeDatabase / newTotalSize) * 100, 100) const usedSizeWAL = Math.round(((diskBreakdownBytes?.walSizeBytes ?? 0) / GB) * 100) / 100 const usedPercentageWAL = totalSize === 0 ? 0 : Math.min((usedSizeWAL / totalSize) * 100, 100) const newUsedPercentageWAL = Math.min((usedSizeWAL / newTotalSize) * 100, 100) const usedSizeSystem = Math.round(((diskBreakdownBytes?.systemBytes ?? 0) / GB) * 100) / 100 const usedPercentageSystem = totalSize === 0 ? 0 : Math.min((usedSizeSystem / totalSize) * 100, 100) const newUsedPercentageSystem = Math.min((usedSizeSystem / newTotalSize) * 100, 100) const resizePercentage = AUTOSCALING_THRESHOLD * 100 // Anchored to the actual right edge of the highlighted "border-r" zone below, rather than // trusting resizePercentage alone, since usedPercentage* are each rounded independently and // can drift a fraction of a percent from usedTotalPercentage. const resizeMarkerLeftPercentage = usedPercentageDatabase + usedPercentageWAL + usedPercentageSystem + Math.max(0, resizePercentage - usedTotalPercentage) return (
{usedSizeTotal.toFixed(2)} GB used of {' '} GB
{!showNewSize && (
)}
{showNewSize && ( New disk size )} {!showNewSize && (

Autoscaling

Supabase expands your disk storage automatically when the database reaches 90% of the disk size. However, disk modifications, including auto-scaling, are limited to 4 within a rolling 24-hour window.

If you exhaust these modifications and reach 95% of the disk space, your project{' '} will enter read-only mode.

)}
{!showNewSize && (
)}
) } const LegendItem = ({ name, description, color, size, }: { name: string description: string color: string size: number }) => (
{name}
{name} - {formatBytes(size, 2, 'GB')}

{description}

)