Skip to content

Open source CapCut alternative.

TypeScript

perf(site): load card media only when it scrolls into view

David Lea410ed4094c5
diff --git a/site/src/cut/components/LibraryView.tsx b/site/src/cut/components/LibraryView.tsx
index 65eccb84..3f875885 100644
--- a/site/src/cut/components/LibraryView.tsx
+++ b/site/src/cut/components/LibraryView.tsx
@@ -32,6 +32,7 @@ import {
} from "@/components/ui/dialog";
import { Input } from "@/components/ui/input";
import { clearAssetDrag, setCardDragImage, setLibraryDragData } from "@/cut/lib/assetDrag";
+import { useInView } from "@/cut/hooks/useInView";
import { isMediaFile } from "@/cut/lib/media";
import { patchLibrary, refetchLibrary, useLibrary } from "@/cut/lib/queries";
import {
@@ -474,6 +475,10 @@ export function LibraryCard({
}) {
const videoRef = useRef<HTMLVideoElement>(null);
const { flash, attachReveal } = useRevealFlash("library", a.id);
+ // Each card is a real media element, so the source waits until the tile has
+ // been scrolled near: a large library would otherwise pull every file's
+ // metadata across the network the moment the page opened.
+ const [tileRef, seen] = useInView<HTMLDivElement>();
// Poster from the video itself so the still matches what plays on hover.
// 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.
@@ -500,21 +505,24 @@ export function LibraryCard({
}}
>
<div
+ ref={tileRef}
className={cn(
"relative aspect-square cursor-grab overflow-hidden rounded-xl border bg-muted transition-shadow group-hover:shadow-[0_4px_20px_rgba(0,0,0,0.1)] active:cursor-grabbing",
selected || flash ? "border-[#0a84ff] ring-2 ring-[#0a84ff]" : "border-border"
)}
>
{a.type === "video" ? (
- <video
- ref={videoRef}
- src={`${libraryMediaUrl(a.fileName)}#t=${posterT}`}
- muted
- loop
- playsInline
- preload="metadata"
- className="size-full object-cover"
- />
+ seen && (
+ <video
+ ref={videoRef}
+ src={`${libraryMediaUrl(a.fileName)}#t=${posterT}`}
+ muted
+ loop
+ playsInline
+ preload="metadata"
+ className="size-full object-cover"
+ />
+ )
) : a.type === "image" ? (
// eslint-disable-next-line @next/next/no-img-element -- library media file, not Next-optimizable
<img src={libraryMediaUrl(a.fileName)} alt={a.name} loading="lazy" className="size-full object-cover" />
diff --git a/site/src/cut/components/ProjectsHome.tsx b/site/src/cut/components/ProjectsHome.tsx
index ea81d63f..939b87d0 100644
--- a/site/src/cut/components/ProjectsHome.tsx
+++ b/site/src/cut/components/ProjectsHome.tsx
@@ -58,6 +58,7 @@ import {
type ProjectsSection,
type Residency,
} from "@/cut/lib/queries";
+import { useInView } from "@/cut/hooks/useInView";
import { useWebMode } from "@/cut/lib/flags";
import { track } from "@/lib/analytics";
import { authClient } from "@/lib/auth-client";
@@ -933,9 +934,15 @@ export function ProjectsHome() {
/** Card art: the actual edit (a rendered proxy) plays on hover; the poster is
* the first clip's real first frame. Falls back to the source when no proxy
- * has been rendered yet. */
+ * has been rendered yet.
+ *
+ * The source is withheld until the tile has been scrolled near, because each
+ * card is a real media element: a grid of cloud projects would otherwise open
+ * a connection per card on arrival and pull every file's metadata across the
+ * network before the first row settled. */
function CardPreview({ project: p, residency }: { project: ProjectSummary; residency: Residency }) {
const videoRef = useRef<HTMLVideoElement>(null);
+ const [tileRef, seen] = useInView<HTMLDivElement>();
const backend = backendFor(residency);
const fileUrl = (file: string) =>
backend.url(`/api/cut/projects/${p.id}/media/${encodeURIComponent(file)}`);
@@ -957,6 +964,7 @@ function CardPreview({ project: p, residency }: { project: ProjectSummary; resid
<img
src={fileUrl(p.previewFile!)}
alt=""
+ loading="lazy"
className="absolute inset-0 size-full object-cover"
/>
);
@@ -968,6 +976,7 @@ function CardPreview({ project: p, residency }: { project: ProjectSummary; resid
return (
<div
+ ref={tileRef}
className="absolute inset-0"
onMouseEnter={() => void videoRef.current?.play().catch(() => {})}
onMouseLeave={() => {
@@ -978,15 +987,17 @@ function CardPreview({ project: p, residency }: { project: ProjectSummary; resid
}
}}
>
- <video
- ref={videoRef}
- src={`${src}#t=${posterT}`}
- muted
- loop
- playsInline
- preload="metadata"
- className="size-full object-cover"
- />
+ {seen && (
+ <video
+ ref={videoRef}
+ src={`${src}#t=${posterT}`}
+ muted
+ loop
+ playsInline
+ preload="metadata"
+ className="size-full object-cover"
+ />
+ )}
</div>
);
}
diff --git a/site/src/cut/components/SidePanel.tsx b/site/src/cut/components/SidePanel.tsx
index 4567fa9b..14abc06d 100644
--- a/site/src/cut/components/SidePanel.tsx
+++ b/site/src/cut/components/SidePanel.tsx
@@ -39,6 +39,7 @@ import { RefDropZone } from "./RefDropZone";
import { deleteExport, downloadProjectExport, revealExport } from "@/cut/lib/exportClient";
import { useExports, useWatchExportLands, type ExportJob } from "@/cut/lib/exportStore";
import { useElapsed } from "@/cut/hooks/useElapsed";
+import { useInView } from "@/cut/hooks/useInView";
import {
addAssetToLibraryTemplate,
addLibraryAssetToProject,
@@ -691,6 +692,9 @@ function AssetCard({ asset, projectId }: { asset: MediaAsset; projectId: string
const [confirmUses, setConfirmUses] = useState<number | null>(null);
const videoRef = useRef<HTMLVideoElement>(null);
const { flash, attachReveal } = useRevealFlash("project", asset.id);
+ // 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>();
const add = () => {
const s = useEditor.getState();
@@ -745,6 +749,7 @@ function AssetCard({ asset, projectId }: { asset: MediaAsset; projectId: string
}}
>
<div
+ ref={tileRef}
className={cn(
"relative aspect-square overflow-hidden rounded-lg border border-border bg-muted transition-colors group-hover:border-input",
flash && "ring-2 ring-[#0a84ff] ring-offset-1"
@@ -752,15 +757,17 @@ function AssetCard({ asset, projectId }: { asset: MediaAsset; projectId: string
>
{asset.type === "video" ? (
// Native first frame as the poster — full-resolution, no blurry thumb.
- <video
- ref={videoRef}
- src={`${asset.url}#t=0.1`}
- preload="metadata"
- muted
- loop
- playsInline
- className="size-full object-cover"
- />
+ seen && (
+ <video
+ ref={videoRef}
+ src={`${asset.url}#t=0.1`}
+ preload="metadata"
+ muted
+ loop
+ playsInline
+ className="size-full object-cover"
+ />
+ )
) : asset.type === "image" ? (
// eslint-disable-next-line @next/next/no-img-element -- engine/static file, not Next-optimizable
<img src={asset.url} alt={asset.name} loading="lazy" className="size-full object-cover" />