Skip to content

Open source CapCut alternative.

TypeScript

feat(site): show file size on media and library cards on hover

David Le1db743516495
diff --git a/site/src/cut/components/LibraryView.tsx b/site/src/cut/components/LibraryView.tsx
index 2c4b9375..940fd865 100644
--- a/site/src/cut/components/LibraryView.tsx
+++ b/site/src/cut/components/LibraryView.tsx
@@ -66,7 +66,8 @@ import { formatTime } from "@/cut/lib/time";
import { cn } from "@/lib/utils";
import { CopyNameLabel } from "./AssetRefs";
import { AudioCardFace } from "./AudioPanel";
-import { buildDragGhost, FolderCrumb, FolderShelf, Marquee } from "./desktopFolders";
+import { buildDragGhost, FolderCrumb, FolderShelf, formatBytes, Marquee } from "./desktopFolders";
+import { useMediaFileSize } from "@/cut/hooks/useMediaFileSize";
// A dragged library selection travels as a JSON array of asset ids, so a whole
// marquee-selected collection can be dropped onto a folder at once.
@@ -590,6 +591,12 @@ export function LibraryCard({
// An ffmpeg still washes out iPhone HDR (HLG) footage — the browser tone-maps
// the video correctly, so we render the frame instead of a baked thumbnail.
const posterT = Math.min(1, Math.max(0.1, (a.duration || 2) / 10));
+ // The size pill only shows on hover, so the lookup waits for the first one.
+ const [hovered, setHovered] = useState(false);
+ const sizeBytes = useMediaFileSize(
+ offline ? "" : libraryMediaUrl(a.fileName, a.residency),
+ hovered
+ );
return (
<div
@@ -603,7 +610,10 @@ export function LibraryCard({
onDragStartExtra?.(e);
}}
onDragEnd={clearAssetDrag}
- onMouseEnter={() => void videoRef.current?.play().catch(() => {})}
+ onMouseEnter={() => {
+ setHovered(true);
+ void videoRef.current?.play().catch(() => {});
+ }}
onMouseLeave={() => {
const v = videoRef.current;
if (v) {
@@ -660,6 +670,23 @@ export function LibraryCard({
{formatTime(a.duration)}
</span>
)}
+ {sizeBytes != null && (
+ <span
+ className={cn(
+ "absolute font-mono tabular-nums opacity-0 transition-opacity group-hover:opacity-100",
+ a.type === "audio"
+ ? // Clear of the play circle, matching the face's duration pill.
+ "bottom-3 left-12 rounded-md bg-[#2b4e42] px-1.5 py-0.5 text-[10px] text-[#d6eddf]"
+ : cn(
+ "rounded-md bg-black/65 px-1.5 py-0.5 text-[10px] text-white",
+ // The + button takes the corner on hover; sit beside it.
+ onUse ? "bottom-2 left-9" : "bottom-1.5 left-1.5"
+ )
+ )}
+ >
+ {formatBytes(sizeBytes)}
+ </span>
+ )}
{onUse && (
<button
aria-label="Add to timeline"
diff --git a/site/src/cut/components/SidePanel.tsx b/site/src/cut/components/SidePanel.tsx
index 5218edce..6253ed90 100644
--- a/site/src/cut/components/SidePanel.tsx
+++ b/site/src/cut/components/SidePanel.tsx
@@ -41,6 +41,8 @@ import { deleteExport, downloadProjectExport, revealExport } from "@/cut/lib/exp
import { useExports, useWatchExportLands, type ExportJob } from "@/cut/lib/exportStore";
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 {
addAssetToLibraryTemplate,
addLibraryAssetToProject,
@@ -860,6 +862,9 @@ function AssetCard({ asset, projectId }: { asset: MediaAsset; projectId: string
// A media-heavy project would open every one of these connections at once;
// the source waits until the tile has been scrolled near.
const [tileRef, seen] = useInView<HTMLDivElement>();
+ // The size pill only shows on hover, so the lookup waits for the first one.
+ const [hovered, setHovered] = useState(false);
+ const sizeBytes = useMediaFileSize(asset.url, hovered && !asset.upload);
const add = () => {
const s = useEditor.getState();
@@ -903,6 +908,7 @@ function AssetCard({ asset, projectId }: { asset: MediaAsset; projectId: string
}}
onDragEnd={clearAssetDrag}
onMouseEnter={() => {
+ setHovered(true);
void videoRef.current?.play().catch(() => {});
}}
onMouseLeave={() => {
@@ -950,6 +956,19 @@ function AssetCard({ asset, projectId }: { asset: MediaAsset; projectId: string
{formatTime(asset.duration)}
</span>
)}
+ {sizeBytes != null && (
+ <span
+ className={cn(
+ "absolute font-mono tabular-nums opacity-0 transition-opacity group-hover:opacity-100",
+ asset.type === "audio"
+ ? // Clear of the play circle, matching the face's duration pill.
+ "bottom-3 left-12 rounded-md bg-[#2b4e42] px-1.5 py-0.5 text-[10px] text-[#d6eddf]"
+ : "bottom-1 left-1 rounded-[5px] bg-black/65 px-1 py-px text-[9.5px] text-white"
+ )}
+ >
+ {formatBytes(sizeBytes)}
+ </span>
+ )}
<span
className={cn(
"absolute flex gap-1 opacity-0 transition-opacity group-hover:opacity-100",
diff --git a/site/src/cut/components/desktopFolders.tsx b/site/src/cut/components/desktopFolders.tsx
index 877f1786..95210bce 100644
--- a/site/src/cut/components/desktopFolders.tsx
+++ b/site/src/cut/components/desktopFolders.tsx
@@ -28,6 +28,7 @@ export interface DeskFolder {
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`;
}
diff --git a/site/src/cut/hooks/useMediaFileSize.ts b/site/src/cut/hooks/useMediaFileSize.ts
new file mode 100644
index 00000000..a3e4e8c3
--- /dev/null
+++ b/site/src/cut/hooks/useMediaFileSize.ts
@@ -0,0 +1,42 @@
+"use client";
+
+import { useEffect, useState } from "react";
+
+/** One lookup per URL for the whole session; every card showing the same file
+ * shares the result. */
+const sizes = new Map<string, Promise<number | null>>();
+
+function headSize(url: string): Promise<number | null> {
+ let p = sizes.get(url);
+ if (!p) {
+ p = fetch(url, { method: "HEAD" })
+ .then((res) => {
+ const n = Number(res.headers.get("Content-Length"));
+ return res.ok && Number.isFinite(n) && n > 0 ? n : null;
+ })
+ .catch(() => null);
+ sizes.set(url, p);
+ }
+ return p;
+}
+
+/**
+ * A served media file's size in bytes, read from a HEAD request's
+ * Content-Length — the servers already know every file's size, so no size
+ * field has to ride the document. Fetches nothing until `active` turns true
+ * (the card's first hover); null until known, or when the server won't say.
+ */
+export function useMediaFileSize(url: string, active: boolean): number | null {
+ const [size, setSize] = useState<number | null>(null);
+ useEffect(() => {
+ if (!active || !url) return;
+ let live = true;
+ void headSize(url).then((n) => {
+ if (live) setSize(n);
+ });
+ return () => {
+ live = false;
+ };
+ }, [url, active]);
+ return size;
+}