Skip to content

Open source CapCut alternative.

TypeScript

refactor(site): one byte formatter for every surface

David Lee66d7a544197
diff --git a/site/src/cut/components/LibraryView.tsx b/site/src/cut/components/LibraryView.tsx
index ec146e85..8bf3db76 100644
--- a/site/src/cut/components/LibraryView.tsx
+++ b/site/src/cut/components/LibraryView.tsx
@@ -124,12 +124,8 @@ import {
SPECIMEN_META,
} from "@/cut/lib/fontSpecimen";
import { AudioCardFace } from "./AudioPanel";
-import {
- FolderCrumb,
- FolderShelf,
- formatBytes,
- Marquee,
-} from "./desktopFolders";
+import { FolderCrumb, FolderShelf, Marquee } from "./desktopFolders";
+import { formatBytes } from "@/lib/bytes";
import { useMediaFileSize } from "@/cut/hooks/useMediaFileSize";
// A dragged library selection travels as a JSON array of asset ids, so a whole
diff --git a/site/src/cut/components/NavStorage.tsx b/site/src/cut/components/NavStorage.tsx
index f9b811dc..da51d365 100644
--- a/site/src/cut/components/NavStorage.tsx
+++ b/site/src/cut/components/NavStorage.tsx
@@ -17,7 +17,7 @@ import { useCloudUsage } from "@/cut/lib/backend/hooks";
import { useUpgradeToPro } from "@/cut/lib/proUpgrade";
import { cn } from "@/lib/utils";
import { useProSubscription } from "@/queries/billing";
-import { formatBytes } from "./desktopFolders";
+import { formatBytes } from "@/lib/bytes";
import { usageLabel } from "./StoragePill";
export function NavStorage() {
diff --git a/site/src/cut/components/ProjectsHome.tsx b/site/src/cut/components/ProjectsHome.tsx
index 091ec326..e3ad9588 100644
--- a/site/src/cut/components/ProjectsHome.tsx
+++ b/site/src/cut/components/ProjectsHome.tsx
@@ -90,7 +90,8 @@ import {
} from "@/cut/lib/types";
import { cn } from "@/lib/utils";
import { setObjectDragImage } from "@/cut/lib/assetDrag";
-import { FolderCrumb, FolderShelf, formatBytes, Marquee } from "./desktopFolders";
+import { FolderCrumb, FolderShelf, Marquee } from "./desktopFolders";
+import { formatBytes } from "@/lib/bytes";
type View = "gallery" | "list";
diff --git a/site/src/cut/components/SidePanel.tsx b/site/src/cut/components/SidePanel.tsx
index 535d69e2..1c7481ed 100644
--- a/site/src/cut/components/SidePanel.tsx
+++ b/site/src/cut/components/SidePanel.tsx
@@ -56,7 +56,7 @@ import {
import { useElapsed } from "@/cut/hooks/useElapsed";
import { useInView } from "@/cut/hooks/useInView";
import { useMediaFileSize } from "@/cut/hooks/useMediaFileSize";
-import { formatBytes } from "@/cut/components/desktopFolders";
+import { formatBytes } from "@/lib/bytes";
import {
addAssetToLibraryTemplate,
addLibraryAssetToProject,
diff --git a/site/src/cut/components/StoragePill.tsx b/site/src/cut/components/StoragePill.tsx
index b682d8e1..628cbeb7 100644
--- a/site/src/cut/components/StoragePill.tsx
+++ b/site/src/cut/components/StoragePill.tsx
@@ -18,7 +18,7 @@ import { daysUntil } from "@/cut/lib/time";
import { track } from "@/lib/analytics";
import { cn } from "@/lib/utils";
import { useOpenBillingPortal, useProSubscription } from "@/queries/billing";
-import { formatBytes } from "./desktopFolders";
+import { formatBytes } from "@/lib/bytes";
const PILL =
"flex cursor-pointer items-center gap-1.5 whitespace-nowrap rounded-full border border-border bg-card px-3 py-1.5 text-xs font-medium shadow-xs transition-colors";
diff --git a/site/src/cut/components/StorageUpgradeDialog.tsx b/site/src/cut/components/StorageUpgradeDialog.tsx
index 273c7588..9cb0945b 100644
--- a/site/src/cut/components/StorageUpgradeDialog.tsx
+++ b/site/src/cut/components/StorageUpgradeDialog.tsx
@@ -22,7 +22,7 @@ import {
import { daysUntil } from "@/cut/lib/time";
import { track } from "@/lib/analytics";
import { useProSubscription } from "@/queries/billing";
-import { formatBytes } from "./desktopFolders";
+import { formatBytes } from "@/lib/bytes";
export function StorageUpgradeDialog() {
const [detail, setDetail] = useState<StorageQuotaDetail | null>(null);
diff --git a/site/src/cut/components/desktopFolders.tsx b/site/src/cut/components/desktopFolders.tsx
index 87df7a94..38dbd858 100644
--- a/site/src/cut/components/desktopFolders.tsx
+++ b/site/src/cut/components/desktopFolders.tsx
@@ -12,6 +12,7 @@ import {
} from "@/components/ui/dropdown-menu";
import { Input } from "@/components/ui/input";
import type { AssetRef } from "@/cut/lib/assetRef";
+import { formatBytes } from "@/lib/bytes";
import { RefDropZone } from "./RefDropZone";
import { cn } from "@/lib/utils";
@@ -29,13 +30,6 @@ export interface DeskFolder {
locked?: boolean;
}
-export function formatBytes(n: number): string {
- if (n <= 0) return "0 MB";
- const mb = n / (1024 * 1024);
- if (mb < 1) return `${Math.max(1, Math.round(n / 1024))} KB`;
- return mb >= 1024 ? `${(mb / 1024).toFixed(1)} GB` : `${mb < 10 ? mb.toFixed(1) : Math.round(mb)} MB`;
-}
-
export function readDragIds(e: React.DragEvent, mime: string): string[] {
const raw = e.dataTransfer.getData(mime);
if (!raw) return [];
diff --git a/site/src/lib/bytes.ts b/site/src/lib/bytes.ts
new file mode 100644
index 00000000..766d1649
--- /dev/null
+++ b/site/src/lib/bytes.ts
@@ -0,0 +1,9 @@
+/** A byte count in the units a person reads at a glance: KB under a megabyte,
+ * MB up to a gigabyte, GB above it. One implementation, so the cloud shelf, the
+ * Outreach list, and an outreach email all say the same number. */
+export function formatBytes(n: number): string {
+ if (n <= 0) return "0 MB";
+ const mb = n / (1024 * 1024);
+ if (mb < 1) return `${Math.max(1, Math.round(n / 1024))} KB`;
+ return mb >= 1024 ? `${(mb / 1024).toFixed(1)} GB` : `${mb < 10 ? mb.toFixed(1) : Math.round(mb)} MB`;
+}