diff --git a/dashboard/src/app/settings/aws/page.tsx b/dashboard/src/app/settings/aws/page.tsx index 608c3a76..58798cd0 100644 --- a/dashboard/src/app/settings/aws/page.tsx +++ b/dashboard/src/app/settings/aws/page.tsx @@ -5,6 +5,7 @@ import { useRouter } from "next/navigation"; import { api, type AwsConnection, type LogGroup, type Recommendation } from "@/lib/api"; import { useToast } from "@/components/toast"; import { RoleGuard } from "@/components/role-guard"; +import { formatBytes } from "@/lib/formatBytes"; const STATUS_STYLES: Record = { active: { bg: "bg-emerald-500/10", text: "text-emerald-400", dot: "bg-emerald-400" }, @@ -834,11 +835,3 @@ function AddConnectionModal({ ); } - -function formatBytes(bytes: number): string { - if (bytes === 0) return "0 B"; - const k = 1024; - const sizes = ["B", "KB", "MB", "GB", "TB"]; - const i = Math.floor(Math.log(bytes) / Math.log(k)); - return `${(bytes / Math.pow(k, i)).toFixed(1)} ${sizes[i]}`; -} diff --git a/dashboard/src/lib/formatBytes.ts b/dashboard/src/lib/formatBytes.ts new file mode 100644 index 00000000..c194e67e --- /dev/null +++ b/dashboard/src/lib/formatBytes.ts @@ -0,0 +1,11 @@ +export function formatBytes(bytes: number, decimals: number = 1): string { + if (!Number.isFinite(bytes) || bytes < 0) return "N/A"; + if (bytes === 0) return "0 B"; + + const sizes: string[] = ["B", "KB", "MB", "GB", "TB", "PB"]; + const k: number = 1024; + const i: number = Math.min(Math.floor(Math.log(bytes) / Math.log(k)), sizes.length - 1); + const d: number = Math.min(Math.max(Math.floor(decimals), 0), 20); + + return `${(bytes / Math.pow(k, i)).toFixed(d)} ${sizes[i]}`; +}