Skip to content

The Postgres development platform. Supabase gives you a dedicated Postgres database to build your web, mobile, and AI applications.

Improve storage error message for storage size limit

Joshen Limd61daa9cd360
diff --git a/studio/components/to-be-cleaned/Storage/StorageSettings/StorageSettings.tsx b/studio/components/to-be-cleaned/Storage/StorageSettings/StorageSettings.tsx
index 8ebb388a2a8..745469b8b1c 100644
--- a/studio/components/to-be-cleaned/Storage/StorageSettings/StorageSettings.tsx
+++ b/studio/components/to-be-cleaned/Storage/StorageSettings/StorageSettings.tsx
@@ -43,12 +43,17 @@ const StorageConfig = ({ config, projectRef }: any) => {
STORAGE_FILE_SIZE_LIMIT_MAX_BYTES
)} bytes`
+ const { value: formattedMaxLimit } = convertFromBytes(
+ STORAGE_FILE_SIZE_LIMIT_MAX_BYTES,
+ selectedUnit
+ )
+
const onValidate = (values: any) => {
const errors = {} as any
if (values.fileSizeLimit > STORAGE_FILE_SIZE_LIMIT_MAX_BYTES) {
- errors['fileSizeLimit'] = `Must be a number up to ${new Intl.NumberFormat('en-US').format(
- STORAGE_FILE_SIZE_LIMIT_MAX_BYTES
- )} bytes.`
+ errors[
+ 'fileSizeLimit'
+ ] = `Maximum limit is up to ${formattedMaxLimit.toLocaleString()} ${selectedUnit}.`
}
return errors
}
diff --git a/studio/components/to-be-cleaned/Storage/StorageSettings/StorageSettings.utils.ts b/studio/components/to-be-cleaned/Storage/StorageSettings/StorageSettings.utils.ts
index 71d74047ffe..a3d9da5d43a 100644
--- a/studio/components/to-be-cleaned/Storage/StorageSettings/StorageSettings.utils.ts
+++ b/studio/components/to-be-cleaned/Storage/StorageSettings/StorageSettings.utils.ts
@@ -2,15 +2,20 @@ import { StorageSizeUnits } from './StorageSettings.constants'
const k = 1024
-export const convertFromBytes = (bytes: number) => {
+export const convertFromBytes = (bytes: number, unit?: StorageSizeUnits) => {
// Up to GB since that's our storage upload limit
if (bytes <= 0) return { value: 0, unit: StorageSizeUnits.BYTES }
- const i = Math.floor(Math.log(bytes) / Math.log(k))
- const formattedIdx = i > 3 ? 3 : i
- const unit = Object.values(StorageSizeUnits)[formattedIdx]
+ const i =
+ unit !== undefined
+ ? Object.values(StorageSizeUnits).indexOf(unit)
+ : Math.floor(Math.log(bytes) / Math.log(k))
+
+ const formattedIdx = unit !== undefined ? (i < 0 ? 0 : i) : i > 3 ? 3 : i
+
+ const formattedUnit = Object.values(StorageSizeUnits)[formattedIdx]
const value = bytes / Math.pow(k, formattedIdx)
- return { value, unit }
+ return { value, unit: formattedUnit }
}
export const convertToBytes = (size: number, unit: StorageSizeUnits = StorageSizeUnits.BYTES) => {
diff --git a/studio/tests/components/Storage/StorageSettings.utils.test.js b/studio/tests/components/Storage/StorageSettings.utils.test.js
index 6d5b960b04d..baec4373a38 100644
--- a/studio/tests/components/Storage/StorageSettings.utils.test.js
+++ b/studio/tests/components/Storage/StorageSettings.utils.test.js
@@ -40,6 +40,19 @@ describe('StorageSettings.utils: convertFromBytes', () => {
const output = convertFromBytes(mockInput)
expect(output).toStrictEqual({ value: 10000, unit: StorageSizeUnits.GB })
})
+ test('should be able to convert given input into specific output unit', () => {
+ const mockInput1 = 5368709120
+ const output1 = convertFromBytes(mockInput1, StorageSizeUnits.GB)
+ expect(output1).toStrictEqual({ value: 5, unit: StorageSizeUnits.GB })
+
+ const mockInput2 = 5368709120
+ const output2 = convertFromBytes(mockInput2, StorageSizeUnits.MB)
+ expect(output2).toStrictEqual({ value: 5120, unit: StorageSizeUnits.MB })
+
+ const mockInput3 = 5368709120
+ const output3 = convertFromBytes(mockInput3, StorageSizeUnits.KB)
+ expect(output3).toStrictEqual({ value: 5242880, unit: StorageSizeUnits.KB })
+ })
})
describe('StorageSettings.utils: convertToBytes', () => {