Skip to content

Open source CapCut alternative.

TypeScript

feat(site): one media card shell behind Project Files and Library tiles

David Le1eb81cedd18d
diff --git a/site/src/cut/components/LibraryView.tsx b/site/src/cut/components/LibraryView.tsx
index aace3571..a3296dc8 100644
--- a/site/src/cut/components/LibraryView.tsx
+++ b/site/src/cut/components/LibraryView.tsx
@@ -58,11 +58,11 @@ import {
import { Skeleton } from "@/components/ui/skeleton";
import { MEDIA_CORS } from "@/cut/lib/mediaCors";
import {
- clearAssetDrag,
setLibraryDragData,
setObjectDragImage,
} from "@/cut/lib/assetDrag";
import { useInView } from "@/cut/hooks/useInView";
+import { MediaCardShell } from "./MediaCardShell";
import { fileKind, isMediaFile, MEDIA_ACCEPT } from "@/cut/lib/media";
import { patchLibrary, refetchLibrary, useLibrary } from "@/cut/lib/queries";
import {
@@ -111,11 +111,9 @@ import {
syncLinkedLibrary,
} from "@/cut/lib/linkedLibrary";
import { shapeBand } from "@/cut/lib/types";
-import { useRevealFlash } from "@/cut/lib/refReveal";
import { formatTime } from "@/cut/lib/time";
import { cn } from "@/lib/utils";
import { refFromLibrary } from "@/cut/lib/assetRef";
-import { useRefCopy } from "@/cut/lib/refCopy";
import { CopyNameLabel } from "./AssetRefs";
import { FontSpecimen } from "./FontSpecimen";
import {
@@ -1163,17 +1161,14 @@ export function LibraryCard({
onUse?: () => void;
onDragStartExtra?: (e: React.DragEvent) => void;
}) {
- const videoRef = useRef<HTMLVideoElement>(null);
- const { flash, attachReveal } = useRevealFlash("library", a.id);
// ⌘C over the card copies its mention token. A card inside the current
// selection carries the whole set, the same rule its drag follows. A font is
// used from the font menu, so it names nothing a prompt can point a tool
// at.
- const copyRef = useRefCopy(() =>
+ const refs = () =>
(selected && dragGroup?.length ? dragGroup : [a])
.filter((x) => x.type !== "font")
- .map(refFromLibrary),
- );
+ .map(refFromLibrary);
// With one shelf listed, every card is on it — the badge would say nothing.
// Phone recordings all come up from the cloud, so they carry none either.
const bothShelves =
@@ -1223,315 +1218,298 @@ export function LibraryCard({
};
return (
- <div
- ref={(el) => {
- attachReveal(el);
- copyRef(el);
- }}
- data-sel-id={a.id}
- className="group flex flex-col"
+ <MediaCardShell
+ scope="library"
+ id={a.id}
+ refs={refs}
+ // Without a + button there is no project to add it to, so the viewer
+ // opens bare.
+ view={offline ? undefined : () => lightboxItemFromLibrary(a, !onUse)}
+ restTime={posterT}
draggable={!offline}
+ className="group flex flex-col"
onClick={onClick}
- // Double-click plays the asset in the viewer, wherever the card sits:
- // the editor's panel keeps its single click for selection and drag.
- onDoubleClick={
- offline
- ? undefined
- : () =>
- useLightbox
- .getState()
- // Without a + button there is no project to add it to, so the
- // viewer opens bare.
- .open(lightboxItemFromLibrary(a, !onUse))
- }
onDragStart={(e) => {
setLibraryDragData(e, a, dragGroup);
onDragStartExtra?.(e);
}}
- onDragEnd={clearAssetDrag}
- onMouseEnter={() => {
- setHovered(true);
- void videoRef.current?.play().catch(() => {});
- }}
- onMouseLeave={() => {
- const v = videoRef.current;
- if (v) {
- v.pause();
- v.currentTime = posterT;
- }
- }}
+ onHover={() => setHovered(true)}
>
- <div
- ref={tileRef}
- data-drag-object
- className={cn(
- "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 && (font ? "aspect-[16/7]" : "aspect-square"),
- // The sheet is the card, so nothing is drawn around it; the type
- // scales with the tile, which runs from a panel column to a full row.
- font && "@container flex flex-col",
- selected || flash
- ? "border-[#0a84ff] ring-2 ring-[#0a84ff]"
- : font
- ? "border-transparent"
- : "border-border",
- )}
- style={tileStyle}
- >
- {font ? (
- // A pangram set in the face, and under a hairline the name it goes by
- // with the file behind it. Nothing to load from a shelf that isn't
- // answering, so that card shows the kind mark instead.
- <div className="flex size-full min-h-0 flex-col">
- <div className="grid min-h-0 flex-1 place-items-center px-3 pt-3 @[220px]:px-5 @[220px]:pt-5">
- {offline ? (
- <Type className="size-6 text-white/35" />
- ) : (
- <FontSpecimen
- assetId={a.id}
- fitHeight
- pad={0}
- className="size-full"
- />
- )}
- </div>
- <div className="mx-3 flex items-center justify-between gap-2 border-t border-white/10 py-2 @[220px]:mx-5 @[220px]:py-3">
- <CopyNameLabel
- name={a.name}
- label={a.title}
- dark
- mention={false}
- className="min-w-0 text-[11px] font-medium @[220px]:text-[13px]"
- />
- {fontMeta && (
- <span
- data-drag-omit
- className="shrink-0 text-[10px] tabular-nums @[220px]:text-[11px]"
- style={{ color: SPECIMEN_META }}
- >
- {fontMeta}
- </span>
- )}
- </div>
- </div>
- ) : offline ? (
- // Nothing to load from a shelf that isn't answering, so the card
- // shows what it knows: the kind of file, its name, its length.
- <span className="grid size-full place-items-center">
- {a.type === "audio" ? (
- <Music className="size-6 text-muted-foreground/50" />
- ) : a.type === "image" ? (
- <ImageIcon className="size-6 text-muted-foreground/50" />
- ) : (
- <Film className="size-6 text-muted-foreground/50" />
+ {({ flash, videoRef }) => (
+ <>
+ <div
+ ref={tileRef}
+ data-drag-object
+ className={cn(
+ "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 && (font ? "aspect-[16/7]" : "aspect-square"),
+ // The sheet is the card, so nothing is drawn around it; the type
+ // scales with the tile, which runs from a panel column to a full row.
+ font && "@container flex flex-col",
+ selected || flash
+ ? "border-[#0a84ff] ring-2 ring-[#0a84ff]"
+ : font
+ ? "border-transparent"
+ : "border-border",
)}
- </span>
- ) : a.type === "video" ? (
- seen && (
- <>
- <video
+ style={tileStyle}
+ >
+ {font ? (
+ // A pangram set in the face, and under a hairline the name it goes by
+ // with the file behind it. Nothing to load from a shelf that isn't
+ // answering, so that card shows the kind mark instead.
+ <div className="flex size-full min-h-0 flex-col">
+ <div className="grid min-h-0 flex-1 place-items-center px-3 pt-3 @[220px]:px-5 @[220px]:pt-5">
+ {offline ? (
+ <Type className="size-6 text-white/35" />
+ ) : (
+ <FontSpecimen
+ assetId={a.id}
+ fitHeight
+ pad={0}
+ className="size-full"
+ />
+ )}
+ </div>
+ <div className="mx-3 flex items-center justify-between gap-2 border-t border-white/10 py-2 @[220px]:mx-5 @[220px]:py-3">
+ <CopyNameLabel
+ name={a.name}
+ label={a.title}
+ dark
+ mention={false}
+ className="min-w-0 text-[11px] font-medium @[220px]:text-[13px]"
+ />
+ {fontMeta && (
+ <span
+ data-drag-omit
+ className="shrink-0 text-[10px] tabular-nums @[220px]:text-[11px]"
+ style={{ color: SPECIMEN_META }}
+ >
+ {fontMeta}
+ </span>
+ )}
+ </div>
+ </div>
+ ) : offline ? (
+ // Nothing to load from a shelf that isn't answering, so the card
+ // shows what it knows: the kind of file, its name, its length.
+ <span className="grid size-full place-items-center">
+ {a.type === "audio" ? (
+ <Music className="size-6 text-muted-foreground/50" />
+ ) : a.type === "image" ? (
+ <ImageIcon className="size-6 text-muted-foreground/50" />
+ ) : (
+ <Film className="size-6 text-muted-foreground/50" />
+ )}
+ </span>
+ ) : a.type === "video" ? (
+ seen && (
+ <>
+ <video
+ crossOrigin={MEDIA_CORS}
+ ref={videoRef}
+ src={`${libraryMediaUrl(a.fileName, a.residency)}#t=${posterT}`}
+ poster={libraryPosterUrl(a)}
+ muted
+ loop
+ playsInline
+ preload="metadata"
+ className="size-full object-cover"
+ // Whichever lands first: a clip with a stored poster paints
+ // from that, one without paints its own first frame.
+ onLoadedMetadata={() => setPainted(true)}
+ onLoadedData={() => setPainted(true)}
+ onError={() => setPainted(true)}
+ />
+ {/* The source's own cover — a video's thumbnail where it came
+ from — is the face at rest: the element's poster gives way as
+ soon as the browser has the frame at #t, and that frame is
+ whatever the clip happens to hold a second in. Hovering
+ uncovers the video, which is what plays. */}
+ {libraryPosterUrl(a) && (
+ // eslint-disable-next-line @next/next/no-img-element -- library media file, not Next-optimizable
+ <img
+ crossOrigin={MEDIA_CORS}
+ src={libraryPosterUrl(a)}
+ alt=""
+ aria-hidden
+ className="pointer-events-none absolute inset-0 size-full object-cover transition-opacity group-hover:opacity-0"
+ onLoad={() => setPainted(true)}
+ onError={() => setPainted(true)}
+ />
+ )}
+ </>
+ )
+ ) : a.type === "image" ? (
+ // eslint-disable-next-line @next/next/no-img-element -- library media file, not Next-optimizable
+ <img
crossOrigin={MEDIA_CORS}
- ref={videoRef}
- src={`${libraryMediaUrl(a.fileName, a.residency)}#t=${posterT}`}
- poster={libraryPosterUrl(a)}
- muted
- loop
- playsInline
- preload="metadata"
+ src={libraryMediaUrl(a.fileName, a.residency)}
+ alt={a.name}
+ loading="lazy"
className="size-full object-cover"
- // Whichever lands first: a clip with a stored poster paints
- // from that, one without paints its own first frame.
- onLoadedMetadata={() => setPainted(true)}
- onLoadedData={() => setPainted(true)}
+ onLoad={() => setPainted(true)}
onError={() => setPainted(true)}
/>
- {/* The source's own cover — a video's thumbnail where it came
- from — is the face at rest: the element's poster gives way as
- soon as the browser has the frame at #t, and that frame is
- whatever the clip happens to hold a second in. Hovering
- uncovers the video, which is what plays. */}
- {libraryPosterUrl(a) && (
- // eslint-disable-next-line @next/next/no-img-element -- library media file, not Next-optimizable
- <img
- crossOrigin={MEDIA_CORS}
- src={libraryPosterUrl(a)}
- alt=""
- aria-hidden
- className="pointer-events-none absolute inset-0 size-full object-cover transition-opacity group-hover:opacity-0"
- onLoad={() => setPainted(true)}
- onError={() => setPainted(true)}
- />
- )}
- </>
- )
- ) : a.type === "image" ? (
- // eslint-disable-next-line @next/next/no-img-element -- library media file, not Next-optimizable
- <img
- crossOrigin={MEDIA_CORS}
- src={libraryMediaUrl(a.fileName, a.residency)}
- alt={a.name}
- loading="lazy"
- className="size-full object-cover"
- onLoad={() => setPainted(true)}
- onError={() => setPainted(true)}
- />
- ) : (
- <AudioCardFace
- url={libraryMediaUrl(a.fileName, a.residency)}
- duration={a.duration}
- // On hover the + button takes the pill's corner.
- durationClassName={
- !!onUse && "transition-opacity group-hover:opacity-0"
- }
- />
- )}
- {!offline && !font && a.type !== "audio" && !painted && (
- <Skeleton
- data-drag-omit
- aria-hidden
- className="pointer-events-none absolute inset-0 rounded-none"
- />
- )}
- {a.type !== "audio" &&
- !font &&
- (a.type === "video" || sizeBytes != null) && (
- // Length and size share one pill in the corner: on a card this narrow
- // two of them collide. The length reads at rest, the size takes over
- // on hover, where the + button is what the pointer is there for.
- <span
- data-drag-omit
- className={cn(
- "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",
- a.type !== "video" &&
- "opacity-0 transition-opacity group-hover:opacity-100",
- )}
- >
- {a.type === "video" && (
- <span className={cn(sizeBytes != null && "group-hover:hidden")}>
- {formatTime(a.duration)}
- </span>
- )}
- {sizeBytes != null && (
+ ) : (
+ <AudioCardFace
+ url={libraryMediaUrl(a.fileName, a.residency)}
+ duration={a.duration}
+ // On hover the + button takes the pill's corner.
+ durationClassName={
+ !!onUse && "transition-opacity group-hover:opacity-0"
+ }
+ />
+ )}
+ {!offline && !font && a.type !== "audio" && !painted && (
+ <Skeleton
+ data-drag-omit
+ aria-hidden
+ className="pointer-events-none absolute inset-0 rounded-none"
+ />
+ )}
+ {a.type !== "audio" &&
+ !font &&
+ (a.type === "video" || sizeBytes != null) && (
+ // Length and size share one pill in the corner: on a card this narrow
+ // two of them collide. The length reads at rest, the size takes over
+ // on hover, where the + button is what the pointer is there for.
<span
+ data-drag-omit
className={cn(
- a.type === "video" && "hidden group-hover:inline",
+ "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",
+ a.type !== "video" &&
+ "opacity-0 transition-opacity group-hover:opacity-100",
)}
>
- {formatBytes(sizeBytes)}
+ {a.type === "video" && (
+ <span className={cn(sizeBytes != null && "group-hover:hidden")}>
+ {formatTime(a.duration)}
+ </span>
+ )}
+ {sizeBytes != null && (
+ <span
+ className={cn(
+ a.type === "video" && "hidden group-hover:inline",
+ )}
+ >
+ {formatBytes(sizeBytes)}
+ </span>
+ )}
</span>
)}
- </span>
- )}
- {a.type === "audio" && sizeBytes != null && (
- // Clear of the play circle, matching the face's duration pill.
- <span className="absolute bottom-3 left-12 rounded-md bg-[#2b4e42] px-1.5 py-0.5 font-mono text-[10px] text-[#d6eddf] tabular-nums opacity-0 transition-opacity group-hover:opacity-100">
- {formatBytes(sizeBytes)}
- </span>
- )}
- {onUse && (
- <button
- aria-label="Add to timeline"
- title="Add to timeline"
- 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 font's bottom edge is its footer, so its + takes the corner
- // the name used to sit in.
- a.type === "audio"
- ? "right-1.5 bottom-1.5"
- : font
- ? "top-1.5 left-1.5"
- : "bottom-1.5 left-1.5",
+ {a.type === "audio" && sizeBytes != null && (
+ // Clear of the play circle, matching the face's duration pill.
+ <span className="absolute bottom-3 left-12 rounded-md bg-[#2b4e42] px-1.5 py-0.5 font-mono text-[10px] text-[#d6eddf] tabular-nums opacity-0 transition-opacity group-hover:opacity-100">
+ {formatBytes(sizeBytes)}
+ </span>
)}
- onClick={(e) => {
- e.stopPropagation();
- onUse();
- }}
- >
- <Plus className="size-3.5" />
- </button>
- )}
- {bothShelves && (
- <ShelfBadge
- residency={a.residency}
- offline={offline}
- className={cn(
- "absolute top-2 right-2 transition-opacity",
- offline ? "text-muted-foreground" : "text-white/85",
- // The actions menu takes this corner on hover.
- "group-hover:opacity-0",
+ {onUse && (
+ <button
+ aria-label="Add to timeline"
+ title="Add to timeline"
+ 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 font's bottom edge is its footer, so its + takes the corner
+ // the name used to sit in.
+ a.type === "audio"
+ ? "right-1.5 bottom-1.5"
+ : font
+ ? "top-1.5 left-1.5"
+ : "bottom-1.5 left-1.5",
+ )}
+ onClick={(e) => {
+ e.stopPropagation();
+ onUse();
+ }}
+ >
+ <Plus className="size-3.5" />
+ </button>
)}
- />
- )}
- <DropdownMenu>
- <DropdownMenuTrigger
- aria-label="More actions"
- title="More actions"
- className={cn(
- "absolute top-1.5 right-1.5 grid size-6 place-items-center rounded-full text-white opacity-0 transition-opacity group-hover:opacity-100 data-[state=open]:opacity-100",
- // A black scrim disappears into the charcoal sheet.
- font
- ? "bg-white/10 hover:bg-white/20"
- : "bg-black/40 hover:bg-black/60",
+ {bothShelves && (
+ <ShelfBadge
+ residency={a.residency}
+ offline={offline}
+ className={cn(
+ "absolute top-2 right-2 transition-opacity",
+ offline ? "text-muted-foreground" : "text-white/85",
+ // The actions menu takes this corner on hover.
+ "group-hover:opacity-0",
+ )}
+ />
)}
- onClick={(e) => e.stopPropagation()}
- >
- <Ellipsis className="size-3.5" />
- </DropdownMenuTrigger>
- <DropdownMenuContent
- // Hung off the card's right edge rather than laid over the media:
- // the tile stays readable behind the open menu.
- align="start"
- alignOffset={4}
- className="w-44"
- onClick={(e) => e.stopPropagation()}
- >
- <DropdownMenuItem
- onClick={() => downloadLibraryAsset(a)}
- disabled={offline}
- >
- <Download /> Download
- </DropdownMenuItem>
- {a.source?.url && (
- // An imported clip keeps the link it came from, so the post it was
- // cut out of is one click away.
- <DropdownMenuItem
- onClick={() =>
- window.open(a.source!.url, "_blank", "noopener,noreferrer")
- }
+ <DropdownMenu>
+ <DropdownMenuTrigger
+ aria-label="More actions"
+ title="More actions"
+ className={cn(
+ "absolute top-1.5 right-1.5 grid size-6 place-items-center rounded-full text-white opacity-0 transition-opacity group-hover:opacity-100 data-[state=open]:opacity-100",
+ // A black scrim disappears into the charcoal sheet.
+ font
+ ? "bg-white/10 hover:bg-white/20"
+ : "bg-black/40 hover:bg-black/60",
+ )}
+ onClick={(e) => e.stopPropagation()}
>
- <ExternalLink /> Open original
- </DropdownMenuItem>
- )}
- {onDelete && (
- <>
- <DropdownMenuSeparator />
- <DropdownMenuItem variant="destructive" onClick={onDelete}>
- <Trash2 /> Delete
+ <Ellipsis className="size-3.5" />
+ </DropdownMenuTrigger>
+ <DropdownMenuContent
+ // Hung off the card's right edge rather than laid over the media:
+ // the tile stays readable behind the open menu.
+ align="start"
+ alignOffset={4}
+ className="w-44"
+ onClick={(e) => e.stopPropagation()}
+ >
+ <DropdownMenuItem
+ onClick={() => downloadLibraryAsset(a)}
+ disabled={offline}
+ >
+ <Download /> Download
</DropdownMenuItem>
- </>
- )}
- </DropdownMenuContent>
- </DropdownMenu>
- {!font && (
- <CopyNameLabel
- name={a.name}
- label={a.title}
- dark={a.type === "audio"}
- mention={mention}
- 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%-2.75rem)]",
- // The emerald fill is its own backdrop; thumbnails need the scrim pill.
- a.type !== "audio" && "rounded-lg bg-black/55 backdrop-blur-sm",
+ {a.source?.url && (
+ // An imported clip keeps the link it came from, so the post it was
+ // cut out of is one click away.
+ <DropdownMenuItem
+ onClick={() =>
+ window.open(a.source!.url, "_blank", "noopener,noreferrer")
+ }
+ >
+ <ExternalLink /> Open original
+ </DropdownMenuItem>
+ )}
+ {onDelete && (
+ <>
+ <DropdownMenuSeparator />
+ <DropdownMenuItem variant="destructive" onClick={onDelete}>
+ <Trash2 /> Delete
+ </DropdownMenuItem>
+ </>
+ )}
+ </DropdownMenuContent>
+ </DropdownMenu>
+ {!font && (
+ <CopyNameLabel
+ name={a.name}
+ label={a.title}
+ dark={a.type === "audio"}
+ mention={mention}
+ 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%-2.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>
- {caption && (
- <div data-drag-omit className="mt-2 px-0.5 text-xs text-muted-foreground">
- {caption}
- </div>
+ </div>
+ {caption && (
+ <div data-drag-omit className="mt-2 px-0.5 text-xs text-muted-foreground">
+ {caption}
+ </div>
+ )}
+ </>
)}
- </div>
+ </MediaCardShell>
);
}
diff --git a/site/src/cut/components/MediaCardShell.tsx b/site/src/cut/components/MediaCardShell.tsx
new file mode 100644
index 00000000..ac347b46
--- /dev/null
+++ b/site/src/cut/components/MediaCardShell.tsx
@@ -0,0 +1,99 @@
+"use client";
+
+import { useRef, type ReactNode, type RefObject } from "react";
+import type { AssetRef, AssetRefScope } from "@/cut/lib/assetRef";
+import { clearAssetDrag } from "@/cut/lib/assetDrag";
+import { useLightbox, type LightboxItem } from "@/cut/lib/lightbox";
+import { useRevealFlash } from "@/cut/lib/refReveal";
+import { useRefCopy } from "@/cut/lib/refCopy";
+
+/** The card behind every media tile: a Project Files card and a Library card
+ * draw their own faces inside it, and this is the one place that says what a
+ * card does with the pointer. A click picks it, a double-click opens it in the
+ * viewer, a drag carries the pick, hovering plays the clip and leaving puts
+ * the rest frame back, ⌘C copies its mention token, and a chat reveal flashes
+ * it. */
+export function MediaCardShell({
+ scope,
+ id,
+ refs,
+ view,
+ restTime,
+ draggable = true,
+ className,
+ title,
+ onClick,
+ onDragStart,
+ onHover,
+ children,
+}: {
+ scope: AssetRefScope;
+ id: string;
+ /** The refs ⌘C copies over the card — the whole pick when the card is in it. */
+ refs: () => AssetRef[];
+ /** What a double-click opens in the viewer. Unset, the card has nothing to
+ * show — a shelf that isn't answering. */
+ view?: () => LightboxItem;
+ /** The frame the video sits on when the pointer leaves. */
+ restTime: number;
+ draggable?: boolean;
+ className?: string;
+ title?: string;
+ onClick?: (e: React.MouseEvent) => void;
+ onDragStart?: (e: React.DragEvent) => void;
+ /** The pointer has arrived: the card can start what waits for a hover. */
+ onHover?: () => void;
+ /** The face, given its reveal flash and the ref the tile's video takes so
+ * hovering plays it. */
+ children: (state: {
+ flash: boolean;
+ videoRef: RefObject<HTMLVideoElement | null>;
+ }) => ReactNode;
+}) {
+ const { flash, attachReveal } = useRevealFlash(scope, id);
+ const videoRef = useRef<HTMLVideoElement>(null);
+ const copyRef = useRefCopy(refs);
+ return (
+ <div
+ ref={(el) => {
+ attachReveal(el);
+ copyRef(el);
+ }}
+ data-sel-id={id}
+ className={className}
+ title={title}
+ draggable={draggable}
+ onClick={onClick}
+ onDoubleClick={
+ view
+ ? (e) => {
+ // The + button and the actions menu answer their own clicks; a
+ // fast double press on one is two of those, and opens nothing.
+ if (
+ (e.target as HTMLElement).closest(
+ "button,[role='button'],[role='menuitem']",
+ )
+ )
+ return;
+ useLightbox.getState().open(view());
+ }
+ : undefined
+ }
+ onDragStart={onDragStart}
+ onDragEnd={clearAssetDrag}
+ onMouseEnter={() => {
+ onHover?.();
+ void videoRef.current?.play().catch(() => {});
+ }}
+ onMouseLeave={() => {
+ const v = videoRef.current;
+ if (v) {
+ v.pause();
+ v.currentTime = restTime;
+ }
+ }}
+ >
+ {children({ flash, videoRef })}
+ </div>
+ );
+}
diff --git a/site/src/cut/components/SidePanel.tsx b/site/src/cut/components/SidePanel.tsx
index 31cdcd32..78f98f97 100644
--- a/site/src/cut/components/SidePanel.tsx
+++ b/site/src/cut/components/SidePanel.tsx
@@ -43,7 +43,6 @@ import {
useAssetDrop,
type AssetRef,
} from "@/cut/lib/assetRef";
-import { useRefCopy } from "@/cut/lib/refCopy";
import { RefDropZone } from "./RefDropZone";
import { deleteExport, downloadProjectExport, exportFileUrl, revealExport } from "@/cut/lib/exportClient";
import {
@@ -119,7 +118,7 @@ import { useLocalPref } from "@/cut/lib/uiState";
import type { MediaAsset, SidePanelTab } from "@/cut/lib/types";
import { cn } from "@/lib/utils";
import { containerOfName } from "@/cut/lib/exportDelivery";
-import { useRevealEffect, useRevealFlash } from "@/cut/lib/refReveal";
+import { useRevealEffect } from "@/cut/lib/refReveal";
import { usePanelRequestEffect } from "@/cut/lib/panelRequest";
import { CopyNameLabel } from "./AssetRefs";
import { AudioCardFace, AudioPanel } from "./AudioPanel";
@@ -137,6 +136,8 @@ import { StockVideosPanel } from "./StockVideosPanel";
import { STOCK_MUSIC } from "@/cut/lib/stockMusicManifest";
import { STOCK_VIDEOS } from "@/cut/lib/stockVideoManifest";
import { LibraryCard, ShelfBadge } from "./LibraryView";
+import { MediaCardShell } from "./MediaCardShell";
+import { lightboxItemFromAsset } from "@/cut/lib/lightbox";
import { SubTabs } from "./SubTabs";
// Drag a library clip onto a folder tile to file it (side panel, single card).
@@ -1364,18 +1365,16 @@ function AssetCard({
const [saved, setSaved] = useState(false);
// Number of timeline items that would be cascade-deleted; null = no prompt.
const [confirmUses, setConfirmUses] = useState<number | null>(null);
- const videoRef = useRef<HTMLVideoElement>(null);
- const { flash, attachReveal } = useRevealFlash("project", asset.id);
// ⌘C over the card copies its mention token (`@v2`). A card inside the
// current pick carries the whole set, the same rule its drag follows.
- const copyRef = useRefCopy(() => {
+ const refs = () => {
const all = useEditor.getState().assets;
const ids = selected && dragGroup?.length ? dragGroup : [asset.id];
return ids
.map((id) => all.find((x) => x.id === id))
.filter((x): x is MediaAsset => !!x)
.map(refFromAsset);
- });
+ };
// 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>();
@@ -1410,15 +1409,15 @@ function AssetCard({
return (
<>
- <div
- ref={(el) => {
- attachReveal(el);
- copyRef(el);
- }}
- data-sel-id={asset.id}
+ <MediaCardShell
+ scope="project"
+ id={asset.id}
+ refs={refs}
+ // A file still on its way up has nothing to show yet.
+ view={asset.upload ? undefined : () => lightboxItemFromAsset(asset)}
+ restTime={0.1}
className="asset-card group flex flex-col gap-1.5 text-left"
title="Drag onto the timeline, or click + to add"
- draggable
onClick={onSelect}
onDragStart={(e) => {
setAssetDragData(e, asset.id, dragGroup);
@@ -1432,138 +1431,131 @@ function AssetCard({
e.dataTransfer.setData(MEDIA_MOVE_MIME, JSON.stringify(ids));
setObjectDragImage(e, ids.length, ids, onDragLanded);
}}
- onDragEnd={clearAssetDrag}
- onMouseEnter={() => {
- setHovered(true);
- void videoRef.current?.play().catch(() => {});
- }}
- onMouseLeave={() => {
- const v = videoRef.current;
- if (v) {
- v.pause();
- v.currentTime = 0.1;
- }
- }}
+ onHover={() => setHovered(true)}
>
- <div
- ref={tileRef}
- data-drag-object
- className={cn(
- "relative aspect-square overflow-hidden rounded-lg border border-border bg-muted transition-colors group-hover:border-input",
- (flash || selected) && "ring-2 ring-[#0a84ff] ring-offset-1"
- )}
- >
- {asset.type === "video" ? (
- // Native first frame as the poster — full-resolution, no blurry thumb.
- seen && (
- <video crossOrigin={MEDIA_CORS}
- ref={videoRef}
- src={`${asset.url}#t=0.1`}
- poster={asset.thumbs?.[0]}
- 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 crossOrigin={MEDIA_CORS} src={asset.url} alt={asset.name} loading="lazy" className="size-full object-cover" />
- ) : (
- <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 === "video" && (
- <span
- data-drag-omit
- 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>
- )}
- {sizeBytes != null && (
- <span
+ {({ flash, videoRef }) => (
+ <>
+ <div
+ ref={tileRef}
+ data-drag-object
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"
+ "relative aspect-square overflow-hidden rounded-lg border border-border bg-muted transition-colors group-hover:border-input",
+ (flash || selected) && "ring-2 ring-[#0a84ff] ring-offset-1"
)}
>
- {formatBytes(sizeBytes)}
- </span>
- )}
- <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"
- className="grid size-5 scale-75 cursor-pointer place-items-center rounded-full bg-primary text-primary-foreground transition-transform group-hover:scale-100 hover:brightness-110"
- onClick={(e) => {
- e.stopPropagation();
- add();
- }}
- >
- <Plus className="size-3" />
- </span>
- </span>
- <DropdownMenu>
- <DropdownMenuTrigger
- aria-label="More actions"
- title="More actions"
- className="absolute top-1 right-1 grid size-5 place-items-center rounded-full bg-black/45 text-white opacity-0 transition-opacity group-hover:opacity-100 hover:bg-black/65 data-[state=open]:opacity-100"
- onClick={(e) => e.stopPropagation()}
- >
- {saved ? <Check className="size-3" /> : <Ellipsis className="size-3" />}
- </DropdownMenuTrigger>
- <DropdownMenuContent align="end" className="w-48" onClick={(e) => e.stopPropagation()}>
- <DropdownMenuItem onClick={saveToLibrary} disabled={!!asset.upload}>
- <FolderPlus /> Save to library
- </DropdownMenuItem>
- <DropdownMenuItem
- onClick={() => downloadMedia(projectId, asset)}
- disabled={!!asset.upload}
+ {asset.type === "video" ? (
+ // Native first frame as the poster — full-resolution, no blurry thumb.
+ seen && (
+ <video crossOrigin={MEDIA_CORS}
+ ref={videoRef}
+ src={`${asset.url}#t=0.1`}
+ poster={asset.thumbs?.[0]}
+ 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 crossOrigin={MEDIA_CORS} src={asset.url} alt={asset.name} loading="lazy" className="size-full object-cover" />
+ ) : (
+ <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 === "video" && (
+ <span
+ data-drag-omit
+ 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>
+ )}
+ {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",
+ // 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"
+ )}
>
- <Download /> Download
- </DropdownMenuItem>
- {caps.revealInFinder && (
- <DropdownMenuItem
- onClick={() => void revealMedia(projectId, asset.fileName).catch(() => {})}
+ <span
+ role="button"
+ title="Add to timeline"
+ className="grid size-5 scale-75 cursor-pointer place-items-center rounded-full bg-primary text-primary-foreground transition-transform group-hover:scale-100 hover:brightness-110"
+ onClick={(e) => {
+ e.stopPropagation();
+ add();
+ }}
>
- <FolderOpen /> Show in Finder
- </DropdownMenuItem>
+ <Plus className="size-3" />
+ </span>
+ </span>
+ <DropdownMenu>
+ <DropdownMenuTrigger
+ aria-label="More actions"
+ title="More actions"
+ className="absolute top-1 right-1 grid size-5 place-items-center rounded-full bg-black/45 text-white opacity-0 transition-opacity group-hover:opacity-100 hover:bg-black/65 data-[state=open]:opacity-100"
+ onClick={(e) => e.stopPropagation()}
+ >
+ {saved ? <Check className="size-3" /> : <Ellipsis className="size-3" />}
+ </DropdownMenuTrigger>
+ <DropdownMenuContent align="end" className="w-48" onClick={(e) => e.stopPropagation()}>
+ <DropdownMenuItem onClick={saveToLibrary} disabled={!!asset.upload}>
+ <FolderPlus /> Save to library
+ </DropdownMenuItem>
+ <DropdownMenuItem
+ onClick={() => downloadMedia(projectId, asset)}
+ disabled={!!asset.upload}
+ >
+ <Download /> Download
+ </DropdownMenuItem>
+ {caps.revealInFinder && (
+ <DropdownMenuItem
+ onClick={() => void revealMedia(projectId, asset.fileName).catch(() => {})}
+ >
+ <FolderOpen /> Show in Finder
+ </DropdownMenuItem>
+ )}
+ <DropdownMenuSeparator />
+ <DropdownMenuItem variant="destructive" onClick={remove}>
+ <Trash2 /> Delete
+ </DropdownMenuItem>
+ </DropdownMenuContent>
+ </DropdownMenu>
+ {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)]"
+ />
)}
- <DropdownMenuSeparator />
- <DropdownMenuItem variant="destructive" onClick={remove}>
- <Trash2 /> Delete
- </DropdownMenuItem>
- </DropdownMenuContent>
- </DropdownMenu>
- {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)]"
- />
- )}
- {asset.upload && <UploadState asset={asset} />}
- </div>
- {asset.type !== "audio" && (
- <CopyNameLabel name={asset.name} className="text-[11px] text-muted-foreground" />
+ {asset.upload && <UploadState asset={asset} />}
+ </div>
+ {asset.type !== "audio" && (
+ <CopyNameLabel name={asset.name} className="text-[11px] text-muted-foreground" />
+ )}
+ </>
)}
- </div>
+ </MediaCardShell>
<AlertDialog open={confirmUses !== null} onOpenChange={(o) => !o && setConfirmUses(null)}>
<AlertDialogContent aria-describedby={undefined}>
<AlertDialogHeader>
diff --git a/site/src/cut/lib/lightbox.ts b/site/src/cut/lib/lightbox.ts
index e3f300be..5c893fe0 100644
--- a/site/src/cut/lib/lightbox.ts
+++ b/site/src/cut/lib/lightbox.ts
@@ -3,6 +3,7 @@
import { create } from "zustand";
import type { AssetRef } from "./assetRef";
import { libraryMediaUrl, libraryPosterUrl, type LibraryAsset } from "./library";
+import type { MediaAsset } from "./types";
// The asset lightbox: a full-screen viewer opened from stock tiles, generated
// media, chat asset cards, and library cards. Held in its own store so any
@@ -50,6 +51,20 @@ export const lightboxItemFromRef = (ref: AssetRef): LightboxItem => ({
...(ref.scope === "library" ? { libraryId: ref.id } : {}),
});
+/** The lightbox view of a project asset — what a Project Files card opens on
+ * a double-click. It carries the asset's id, so "Use" adds it straight to the
+ * timeline. */
+export const lightboxItemFromAsset = (a: MediaAsset): LightboxItem => ({
+ kind: a.type,
+ src: a.url,
+ name: a.name,
+ prompt: "",
+ assetId: a.id,
+ ...(a.width && a.height ? { ratio: a.width / a.height } : {}),
+ ...(a.duration ? { duration: a.duration } : {}),
+ ...(a.thumbs?.[0] ? { poster: a.thumbs[0] } : {}),
+});
+
/** The lightbox view of a library asset — what a library card opens on a
* double-click. `bare` where the surface has no project to add anything to. */
export const lightboxItemFromLibrary = (