Open source CapCut alternative.
TypeScript
feat(site): home and library tiles band by shape [rebuild]
David Le61c22bfc3dd3
diff --git a/site/src/cut/components/LibraryView.tsx b/site/src/cut/components/LibraryView.tsxindex 121e7ff7..a3e46deb 100644--- a/site/src/cut/components/LibraryView.tsx+++ b/site/src/cut/components/LibraryView.tsx@@ -76,6 +76,7 @@ import {} from "@/cut/lib/residency";import { TemplateCard } from "./TemplateCard";import { homeHref, useCutBase } from "@/cut/lib/nav";+import { shapeBand } from "@/cut/lib/types";import { useRevealFlash } from "@/cut/lib/refReveal";import { formatTime } from "@/cut/lib/time";import { cn } from "@/lib/utils";@@ -295,6 +296,20 @@ export function LibraryView() {const bothShelves = listed.length > 1;const shown = all.filter((a) => (a.folderId ?? null) === openFolder);+ // Similar-shape tiles get their own band of wrapped rows, so a wide tile+ // never shares a row with a tall one; audio and unmeasured assets band as+ // squares. Order within and across bands follows the listing.+ const shapeOf = (a: LibraryAsset) =>+ a.type === "audio" || !a.width || !a.height ? 0 : shapeBand(a.width, a.height);+ const shownBands = new Map<number, LibraryAsset[]>();+ for (const a of shown) {+ const band = shownBands.get(shapeOf(a)) ?? [];+ if (band.length === 0) shownBands.set(shapeOf(a), band);+ band.push(a);+ }+ // Every tile takes the same area — a wide clip spreads, a tall one stands,+ // and each carries equal weight on the page.+ const TILE_AREA = 180 * 180;const shownTemplates = templates.filter((t) => (t.folderId ?? null) === openFolder);const openFolderName = folders.find((f) => f.id === openFolder)?.name;const hasContent =@@ -472,26 +487,31 @@ export function LibraryView() {</button>) : shown.length === 0 && uploading === 0 ? null : (<Marquee- className="grid min-h-[40vh] grid-cols-[repeat(auto-fill,minmax(160px,1fr))] content-start gap-4"+ className="flex min-h-[40vh] flex-col content-start gap-8"selected={selected}setSelected={setSelected}>- {shown.map((a) => (- <LibraryCard- key={a.id}- asset={a}- selected={selected.has(a.id)}- offline={!live(a.residency)}- // Clicking an item this browser can only remember is the moment- // something is actually blocked, so that is when the gate's- // banner — and the way out of it — comes up.- onClick={live(a.residency) ? undefined : () => setNeedsApp(true)}- onDelete={live(a.residency) ? () => setDeleting(a) : undefined}- onDragStartExtra={(e) => onCardDragExtra(e, a)}- />+ {[...shownBands.entries()].map(([shape, band]) => (+ <div key={shape} className="flex flex-wrap items-start gap-4">+ {band.map((a) => (+ <LibraryCard+ key={a.id}+ asset={a}+ area={TILE_AREA}+ selected={selected.has(a.id)}+ offline={!live(a.residency)}+ // Clicking an item this browser can only remember is the+ // moment something is actually blocked, so that is when the+ // gate's banner — and the way out of it — comes up.+ onClick={live(a.residency) ? undefined : () => setNeedsApp(true)}+ onDelete={live(a.residency) ? () => setDeleting(a) : undefined}+ onDragStartExtra={(e) => onCardDragExtra(e, a)}+ />+ ))}+ </div>))}{uploading > 0 && (- <div className="flex aspect-square flex-col items-center justify-center gap-2 rounded-xl border border-dashed border-input text-xs text-muted-foreground">+ <div className="flex size-[180px] flex-col items-center justify-center gap-2 rounded-xl border border-dashed border-input text-xs text-muted-foreground"><Loader2 className="size-4 animate-spin" /><span>Uploading… <LiveElapsed />@@ -579,6 +599,7 @@ export function LibraryCard({asset: a,selected,offline = false,+ area,onClick,onDelete,onUse,@@ -589,6 +610,11 @@ export function LibraryCard({/** The shelf this item is on isn't answering: it lists from memory, so the* card shows what it knows and reaches for no media it can't load. */offline?: boolean;+ /** Tile area in square pixels. When set, the tile takes the media's own+ * aspect at this shared area — a wide clip spreads, a tall one stands, and+ * every card carries the same weight. Unset, the tile fills its grid cell+ * as a square. Audio and unmeasured assets sit square either way. */+ area?: number;onClick?: () => void;onDelete?: () => void;onUse?: () => void;@@ -612,6 +638,16 @@ export function LibraryCard({offline ? "" : libraryMediaUrl(a.fileName, a.residency),hovered);+ const frame =+ area && a.type !== "audio" && a.width && a.height+ ? { w: a.width, h: a.height }+ : { w: 1, h: 1 };+ const tileStyle = area+ ? {+ width: Math.round(Math.sqrt((area * frame.w) / frame.h)),+ aspectRatio: `${frame.w} / ${frame.h}`,+ }+ : undefined;return (<div@@ -641,9 +677,11 @@ export function LibraryCard({ref={tileRef}data-drag-objectclassName={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",+ "relative max-w-full 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",+ !area && "aspect-square",selected || flash ? "border-[#0a84ff] ring-2 ring-[#0a84ff]" : "border-border")}+ style={tileStyle}>{offline ? (// Nothing to load from a shelf that isn't answering, so the carddiff --git a/site/src/cut/components/ProjectsHome.tsx b/site/src/cut/components/ProjectsHome.tsxindex c7b2420f..2351ed1a 100644--- a/site/src/cut/components/ProjectsHome.tsx+++ b/site/src/cut/components/ProjectsHome.tsx@@ -82,9 +82,8 @@ import { copyProjectAcross } from "@/cut/lib/projectCopy";import { homeHref, projectHref, useCutBase } from "@/cut/lib/nav";import { daysUntil, formatTime } from "@/cut/lib/time";import {- aspectOrientation,- normalizeAspect,parseRatio,+ shapeBand,type ProjectFolder,type ProjectSummary,} from "@/cut/lib/types";@@ -636,35 +635,30 @@ export function ProjectsHome() {/>);- // Same-shape tiles sit together: the list clusters by orientation, groups- // ordered by their most recently edited project, recency kept within each- // group. A run of uniform tiles reads as a clean row.- const clusterByShape = (shown: { p: ProjectSummary; r: Residency }[]) => {- const groups = new Map<string, typeof shown>();+ // Similar-shape tiles get their own band of wrapped rows, so a wide tile+ // never shares a row with a tall one. Bands are ordered by their most+ // recently edited project, recency kept within each band.+ const bandsByShape = (shown: { p: ProjectSummary; r: Residency }[]) => {+ const bands = new Map<number, typeof shown>();for (const it of shown) {- const key = aspectOrientation(normalizeAspect(it.p.aspect) ?? "9:16");- const group = groups.get(key) ?? [];- if (group.length === 0) groups.set(key, group);- group.push(it);+ const frame = parseRatio(it.p.aspect) ?? { w: 9, h: 16 };+ const key = shapeBand(frame.w, frame.h);+ const band = bands.get(key) ?? [];+ if (band.length === 0) bands.set(key, band);+ band.push(it);}- return [...groups.values()].flat();+ return [...bands.entries()];};// Every tile takes the same area — a wide project spreads, a tall one// stands, and each carries equal weight on the page.const TILE_AREA = 180 * 320;- const renderGallery = (shown: { p: ProjectSummary; r: Residency }[]) => (- <Marquee- className="flex min-h-[42vh] flex-wrap content-start items-start gap-5"- selected={selected}- setSelected={setSelected}- >- {clusterByShape(shown).map(({ p, r }) => {- // Docs without an aspect are 9:16.- const frame = parseRatio(p.aspect) ?? { w: 9, h: 16 };- const tileW = Math.round(Math.sqrt((TILE_AREA * frame.w) / frame.h));- return (+ const renderTile = ({ p, r }: { p: ProjectSummary; r: Residency }) => {+ // Docs without an aspect are 9:16.+ const frame = parseRatio(p.aspect) ?? { w: 9, h: 16 };+ const tileW = Math.round(Math.sqrt((TILE_AREA * frame.w) / frame.h));+ return (<divkey={p.id}data-sel-id={p.id}@@ -719,8 +713,20 @@ export function ProjectsHome() {{formatBytes(p.sizeBytes ?? 0)} · edited {formatDate(p.updatedAt)}</div></div>- );- })}+ );+ };++ const renderGallery = (shown: { p: ProjectSummary; r: Residency }[]) => (+ <Marquee+ className="flex min-h-[42vh] flex-col content-start gap-8"+ selected={selected}+ setSelected={setSelected}+ >+ {bandsByShape(shown).map(([shape, band]) => (+ <div key={shape} className="flex flex-wrap items-start gap-5">+ {band.map(renderTile)}+ </div>+ ))}</Marquee>);diff --git a/site/src/cut/lib/types.ts b/site/src/cut/lib/types.tsindex e2d72b7e..3852f35c 100644--- a/site/src/cut/lib/types.ts+++ b/site/src/cut/lib/types.ts@@ -98,6 +98,13 @@ export function aspectOrientation(aspect: Aspect): "landscape" | "portrait" | "sreturn r.w === r.h ? "square" : r.w > r.h ? "landscape" : "portrait";}+/** Which shape band a w×h frame tiles with. The log of the ratio, in coarse+ * steps: 16:9 bands with 3:2, while 4:3, 1:1, 3:4, and 9:16 each band apart —+ * any ratio lands somewhere, with no landscape/portrait dichotomy. */+export function shapeBand(w: number, h: number): number {+ return Math.round(Math.log2(w / h) * 3);+}+/** Project the project aspect onto a model/provider's supported ratio list:* the entry whose shape is closest (log-ratio distance), first entry on ties. */export function nearestAspect<T extends string>(aspect: Aspect, supported: readonly T[]): T {