Skip to content

Open source CapCut alternative.

TypeScript

feat(site): audio assets render as filled waveform cards in Library and Media

David Le2ce1464c472d
diff --git a/site/src/cut/components/AssetRefs.tsx b/site/src/cut/components/AssetRefs.tsx
index 8751c914..e2cce1c6 100644
--- a/site/src/cut/components/AssetRefs.tsx
+++ b/site/src/cut/components/AssetRefs.tsx
@@ -311,7 +311,16 @@ export function CopyRefButton({ name, className }: { name: string; className?: s
/** A card's name caption with its short handle; clicking copies the reference
* token, ready to paste into any prompt. */
-export function CopyNameLabel({ name, className }: { name: string; className?: string }) {
+export function CopyNameLabel({
+ name,
+ className,
+ dark,
+}: {
+ name: string;
+ className?: string;
+ /** On a dark/filled surface (audio cards): white badge and copied text. */
+ dark?: boolean;
+}) {
const [copied, copy] = useCopied();
const ref = useRefFor(name);
const token = ref ? refToken(ref) : mentionToken(name);
@@ -325,9 +334,16 @@ export function CopyNameLabel({ name, className }: { name: string; className?: s
copy(token);
}}
>
- {ref?.handle && <RefHandleBadge handle={ref.handle} className="shrink-0" />}
+ {ref?.handle && (
+ <RefHandleBadge
+ handle={ref.handle}
+ className={cn("shrink-0", dark && "bg-white/25 text-white")}
+ />
+ )}
{copied ? (
- <span className="truncate text-emerald-600">Copied {token}</span>
+ <span className={cn("truncate", dark ? "text-white" : "text-emerald-600")}>
+ Copied {token}
+ </span>
) : (
<span className="truncate">{name}</span>
)}
diff --git a/site/src/cut/components/AudioPanel.tsx b/site/src/cut/components/AudioPanel.tsx
index 3c80dff4..2717fe41 100644
--- a/site/src/cut/components/AudioPanel.tsx
+++ b/site/src/cut/components/AudioPanel.tsx
@@ -338,7 +338,7 @@ export function PeakStrip({ peaks, className }: { peaks: number[]; className?: s
/** Stand-in peaks while an asset's real ones haven't landed — the same gentle
* wave the timeline drag ghost draws, so the pill still reads as audio. */
-const STAND_IN_PEAKS = Array.from({ length: 48 }, (_, i) => 0.32 + 0.26 * Math.abs(Math.sin(i / 2)));
+export const STAND_IN_PEAKS = Array.from({ length: 48 }, (_, i) => 0.32 + 0.26 * Math.abs(Math.sin(i / 2)));
/** The emerald audio surface, mirroring a timeline audio clip: gradient pill
* with a white waveform. Panel rows, chat cards, and attachment tiles draw on
@@ -366,6 +366,81 @@ export function AudioPillSurface({
);
}
+/** Rounded voice-memo bars for the square audio cards — thicker and softer
+ * than PeakStrip so they read at card size. */
+function CardBars({ peaks, className }: { peaks: number[]; className?: string }) {
+ const BARS = 30;
+ const step = Math.max(1, Math.floor(peaks.length / BARS));
+ const bars: number[] = [];
+ for (let i = 0; i < BARS; i++) {
+ let m = 0;
+ for (let j = i * step; j < Math.min(peaks.length, (i + 1) * step); j++) {
+ if (peaks[j] > m) m = peaks[j];
+ }
+ bars.push(m);
+ }
+ return (
+ <div className={cn("flex items-center gap-[2px]", className)} aria-hidden>
+ {bars.map((p, i) => (
+ <div
+ key={i}
+ className="min-w-0 flex-1 rounded-full bg-[#8fcba9]"
+ style={{ height: `${Math.round(Math.max(0.15, p) * 100)}%` }}
+ />
+ ))}
+ </div>
+ );
+}
+
+/** The square audio card face: muted green fill, rounded mint bars, a play
+ * circle bottom-left, and a duration pill bottom-right. The Library and Media
+ * cards drop it in as their audio tile and overlay their own controls. Play
+ * goes through the shared preview player; unmounting silences its own preview. */
+export function AudioCardFace({
+ url,
+ duration,
+ peaks,
+ durationClassName,
+}: {
+ url: string;
+ duration: number;
+ peaks?: number[];
+ /** Extra classes for the duration pill (e.g. hide it while hover controls
+ * take its corner). */
+ durationClassName?: string | false;
+}) {
+ const playing = usePreviewAudio((s) => s.url) === url;
+ useEffect(() => () => usePreviewAudio.getState().stop(url), [url]);
+ return (
+ <div className="relative size-full bg-[#3e6f5f]">
+ <CardBars
+ peaks={peaks?.length ? peaks : STAND_IN_PEAKS}
+ className="absolute inset-x-[8%] top-1/2 h-[36%] -translate-y-1/2"
+ />
+ <button
+ type="button"
+ title={playing ? "Pause" : "Play"}
+ aria-label={playing ? "Pause" : "Play"}
+ className="absolute bottom-2 left-2 grid size-8 place-items-center rounded-full bg-[#e3f2e8] text-[#2f5d4c] shadow transition-transform hover:scale-105"
+ onClick={(e) => {
+ e.stopPropagation();
+ usePreviewAudio.getState().toggle(url);
+ }}
+ >
+ {playing ? <Pause className="size-3.5" /> : <Play className="size-3.5 translate-x-px" />}
+ </button>
+ <span
+ className={cn(
+ "absolute right-2 bottom-3 rounded-md bg-[#2b4e42] px-1.5 py-0.5 font-mono text-[10px] text-[#d6eddf] tabular-nums",
+ durationClassName
+ )}
+ >
+ {formatTime(duration)}
+ </span>
+ </div>
+ );
+}
+
/** Off-screen drag image that mirrors an audio clip on the timeline — an
* emerald pill with the name and a white waveform — so the drag reads as the
* thing that will land, not the panel row. Lives just long enough for the
diff --git a/site/src/cut/components/LibraryView.tsx b/site/src/cut/components/LibraryView.tsx
index e46f01d0..0c73a71e 100644
--- a/site/src/cut/components/LibraryView.tsx
+++ b/site/src/cut/components/LibraryView.tsx
@@ -9,7 +9,6 @@ import {
FolderPlus,
Link as LinkIcon,
Loader2,
- Music,
Plus,
Trash2,
Upload,
@@ -59,6 +58,7 @@ import { useRevealFlash } from "@/cut/lib/refReveal";
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";
// A dragged library selection travels as a JSON array of asset ids, so a whole
@@ -435,11 +435,14 @@ export function LibraryCard({
// 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" />
) : (
- <div className="grid size-full place-items-center bg-gradient-to-br from-emerald-100 to-emerald-50 text-emerald-600">
- <Music className="size-5" />
- </div>
+ <AudioCardFace
+ url={libraryMediaUrl(a.fileName)}
+ duration={a.duration}
+ // On hover the + button takes the pill's corner.
+ durationClassName={!!onUse && "transition-opacity group-hover:opacity-0"}
+ />
)}
- {a.type !== "image" && (
+ {a.type === "video" && (
<span className="absolute right-1.5 bottom-1.5 rounded-md bg-black/65 px-1.5 py-0.5 font-mono text-[10px] text-white tabular-nums">
{formatTime(a.duration)}
</span>
@@ -448,7 +451,11 @@ export function LibraryCard({
<button
aria-label="Add to timeline"
title="Add to timeline"
- className="absolute bottom-1.5 left-1.5 grid size-6 place-items-center rounded-full bg-primary text-primary-foreground opacity-0 shadow transition-all group-hover:opacity-100 hover:scale-110"
+ className={cn(
+ "absolute grid size-6 place-items-center rounded-full bg-primary text-primary-foreground opacity-0 shadow transition-all group-hover:opacity-100 hover:scale-110",
+ // Audio keeps play bottom-left; + swaps in where the badge hides.
+ a.type === "audio" ? "right-1.5 bottom-1.5" : "bottom-1.5 left-1.5"
+ )}
onClick={(e) => {
e.stopPropagation();
onUse();
@@ -503,7 +510,12 @@ export function LibraryCard({
</div>
<CopyNameLabel
name={a.name}
- className="absolute top-1.5 left-1.5 max-w-[70%] rounded-lg bg-black/55 px-2 py-1 text-[11px] font-medium text-white backdrop-blur-sm"
+ dark={a.type === "audio"}
+ className={cn(
+ "absolute top-1.5 left-1.5 max-w-[70%] px-2 py-1 text-[11px] font-medium text-white transition-[max-width] group-hover:max-w-[calc(100%-4.75rem)]",
+ // The emerald fill is its own backdrop; thumbnails need the scrim pill.
+ a.type !== "audio" && "rounded-lg bg-black/55 backdrop-blur-sm"
+ )}
/>
</div>
</div>
diff --git a/site/src/cut/components/SidePanel.tsx b/site/src/cut/components/SidePanel.tsx
index a2b92d64..226e7f9e 100644
--- a/site/src/cut/components/SidePanel.tsx
+++ b/site/src/cut/components/SidePanel.tsx
@@ -44,7 +44,7 @@ import { cn } from "@/lib/utils";
import { cardIconButton } from "@/cut/components/iconButton";
import { useRevealEffect, useRevealFlash } from "@/cut/lib/refReveal";
import { CopyNameLabel } from "./AssetRefs";
-import { AudioPanel } from "./AudioPanel";
+import { AudioCardFace, AudioPanel } from "./AudioPanel";
import { buildDragGhost, FolderCrumb, FolderShelf } from "./desktopFolders";
import { GenerateVideoPanel } from "./GeneratePanel";
import { ImageGenPanel } from "./ImageGenPanel";
@@ -469,16 +469,26 @@ function AssetCard({ asset, projectId }: { asset: MediaAsset; projectId: string
// 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" />
) : (
- <div className="grid size-full place-items-center bg-gradient-to-br from-emerald-100 to-emerald-50 text-emerald-600">
- <Music className="size-4.5" />
- </div>
+ <AudioCardFace
+ url={asset.url}
+ duration={asset.duration}
+ peaks={asset.peaks}
+ // On hover the + button takes the pill's corner, as on Library cards.
+ durationClassName="transition-opacity group-hover:opacity-0"
+ />
)}
- {asset.type !== "image" && (
+ {asset.type === "video" && (
<span className="absolute right-1 bottom-1 rounded-[5px] bg-black/65 px-1 py-px font-mono text-[9.5px] text-white tabular-nums">
{formatTime(asset.duration)}
</span>
)}
- <span className="absolute top-1 left-1 flex gap-1 opacity-0 transition-opacity group-hover:opacity-100">
+ <span
+ className={cn(
+ "absolute flex gap-1 opacity-0 transition-opacity group-hover:opacity-100",
+ // Audio keeps play bottom-left; + swaps in where the duration pill hides.
+ asset.type === "audio" ? "right-1.5 bottom-2.5" : "top-1 left-1"
+ )}
+ >
<span
role="button"
title="Add to timeline"
@@ -509,8 +519,17 @@ function AssetCard({ asset, projectId }: { asset: MediaAsset; projectId: string
<Trash2 className="size-3" />
</span>
</span>
+ {asset.type === "audio" && (
+ <CopyNameLabel
+ name={asset.name}
+ dark
+ className="absolute top-1.5 left-1.5 max-w-[70%] px-2 py-1 text-[11px] font-medium text-white transition-[max-width] group-hover:max-w-[calc(100%-4.75rem)]"
+ />
+ )}
</div>
- <CopyNameLabel name={asset.name} className="text-[11px] text-muted-foreground" />
+ {asset.type !== "audio" && (
+ <CopyNameLabel name={asset.name} className="text-[11px] text-muted-foreground" />
+ )}
</div>
<AlertDialog open={confirmUses !== null} onOpenChange={(o) => !o && setConfirmUses(null)}>
<AlertDialogContent aria-describedby={undefined}>