Skip to content

Commit 2b47ce6

Browse files
committed
feat(studio): sidebar asset card (unwired)
What: AssetCard — the sidebar asset tile (thumbnail, hover preview, click policy, context menu incl. add-at-playhead), unwired. Why: standalone sidebar concept; AssetsTab adopts it in the app-shell swap. How: new file against the coexistence layer (assetHelpers, AssetContextMenu, assetClickBehavior, assetPreviewStore); one TEMP(studio-dnd) entry until AssetsTab wires it. Test plan: tsc --noEmit; fallow audit clean.
1 parent c39b84f commit 2b47ce6

2 files changed

Lines changed: 322 additions & 0 deletions

File tree

‎.fallowrc.jsonc‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,9 @@
5555
"packages/studio/src/hooks/gsapRuntimePreview.ts",
5656
// TEMP(studio-dnd): shipped unwired ahead of the NLE integration;
5757
// the app-shell swap PR (studio-dnd/pr22) wires the consumers and removes this block.
58+
"packages/studio/src/components/sidebar/AssetCard.tsx",
59+
// TEMP(studio-dnd): shipped unwired ahead of the NLE integration;
60+
// the app-shell swap PR (studio-dnd/pr22) wires the consumers and removes this block.
5861
"packages/studio/src/components/EditorShell.tsx",
5962
"packages/studio/src/components/nle/TimelinePane.tsx",
6063
"packages/studio/src/components/nle/useTimelineEditCallbacks.ts",
@@ -659,6 +662,9 @@
659662
// complexity pre-dates the computed-timeline work. Exempted at file level
660663
// rather than refactored as scope creep.
661664
"ignore": [
665+
// TEMP(studio-dnd): coexistence-window complexity flare (inherited/CRAP-no-coverage);
666+
// removed by studio-dnd/pr22 when the final config lands.
667+
"packages/studio/src/components/sidebar/AssetCard.tsx",
662668
// TEMP(studio-dnd): coexistence-window complexity flare (inherited/CRAP-no-coverage);
663669
// removed by studio-dnd/pr22 when the final config lands.
664670
"packages/studio/src/components/editor/DomEditSelectionChrome.tsx",
Lines changed: 316 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,316 @@
1+
/**
2+
* AssetCard and FontRow — visual asset tile / row components for the Assets panel.
3+
* Extracted from AssetsTab.tsx to keep that file under the 600-line CI gate.
4+
*/
5+
import { useState, useEffect, useRef, useCallback } from "react";
6+
import { VideoFrameThumbnail } from "../ui/VideoFrameThumbnail";
7+
import { VIDEO_EXT, IMAGE_EXT } from "../../utils/mediaTypes";
8+
import { TIMELINE_ASSET_MIME } from "../../utils/timelineAssetDrop";
9+
import { ContextMenu } from "./AssetContextMenu";
10+
import { usePlayerStore } from "../../player/store/playerStore";
11+
import { useAssetPreviewStore } from "../../utils/assetPreviewStore";
12+
import { findClipForAsset, isPointerClick } from "../../utils/assetClickBehavior";
13+
import { basename, ext, truncateMiddle, formatDuration } from "./assetHelpers";
14+
15+
/** Drag payload writer shared by the asset tile and the font row: copy effect
16+
* plus the timeline-asset MIME and a plain-text path fallback. */
17+
function writeAssetDragData(e: React.DragEvent, asset: string): void {
18+
e.dataTransfer.effectAllowed = "copy";
19+
e.dataTransfer.setData(TIMELINE_ASSET_MIME, JSON.stringify({ path: asset }));
20+
e.dataTransfer.setData("text/plain", asset);
21+
}
22+
23+
/** Open the row/tile context menu at the pointer, shared by asset tile + font row. */
24+
function openAssetContextMenu(
25+
e: React.MouseEvent,
26+
setContextMenu: (menu: { x: number; y: number }) => void,
27+
): void {
28+
e.preventDefault();
29+
setContextMenu({ x: e.clientX, y: e.clientY });
30+
}
31+
32+
/**
33+
* Lazily probe a video/audio URL for its duration via a hidden HTMLVideoElement
34+
* (`preload="metadata"`). The manifest only covers ~/.media assets, so project
35+
* assets in assets/ have no manifest entry — this fills the gap.
36+
* Returns `undefined` until the probe completes; `null` if it failed.
37+
*/
38+
function useProbedDuration(src: string, skip: boolean): number | null | undefined {
39+
const [duration, setDuration] = useState<number | null | undefined>(undefined);
40+
useEffect(() => {
41+
if (skip) return;
42+
let cancelled = false;
43+
44+
function probe(attempt: number) {
45+
if (cancelled) return;
46+
const vid = document.createElement("video");
47+
vid.preload = "metadata";
48+
vid.muted = true;
49+
vid.onloadedmetadata = () => {
50+
const d = Number.isFinite(vid.duration) && vid.duration > 0 ? vid.duration : null;
51+
if (!cancelled) setDuration(d);
52+
vid.onloadedmetadata = null;
53+
vid.onerror = null;
54+
vid.src = "";
55+
};
56+
vid.onerror = () => {
57+
vid.onloadedmetadata = null;
58+
vid.onerror = null;
59+
vid.src = "";
60+
if (!cancelled) {
61+
if (attempt < 1) setTimeout(() => probe(attempt + 1), 50);
62+
else setDuration(null);
63+
}
64+
};
65+
vid.src = src;
66+
}
67+
68+
probe(0);
69+
return () => {
70+
cancelled = true;
71+
};
72+
}, [src, skip]);
73+
return duration;
74+
}
75+
76+
export interface AssetCardProps {
77+
projectId: string;
78+
asset: string;
79+
used: boolean;
80+
duration?: number;
81+
onCopy: (path: string) => void;
82+
isCopied: boolean;
83+
onDelete?: (path: string) => void;
84+
onRename?: (oldPath: string, newPath: string) => void;
85+
onAddAssetToTimeline?: (path: string) => void;
86+
}
87+
88+
/**
89+
* Thumbnail card for images and video assets. Renders in a 2-col grid.
90+
*
91+
* Click behaviour (CapCut-style):
92+
* - Already added → selects the clip on the timeline (setSelectedElementId).
93+
* - Not yet added → opens the asset preview overlay over the canvas.
94+
* Drag behaviour is preserved: a pointer movement exceeding DRAG_THRESHOLD_PX
95+
* before pointerup is treated as drag-start, not a click.
96+
*/
97+
// fallow-ignore-next-line complexity
98+
export function AssetCard({
99+
projectId,
100+
asset,
101+
used,
102+
duration,
103+
onCopy,
104+
isCopied,
105+
onDelete,
106+
onRename,
107+
onAddAssetToTimeline,
108+
}: AssetCardProps) {
109+
const [contextMenu, setContextMenu] = useState<{ x: number; y: number } | null>(null);
110+
const [hovered, setHovered] = useState(false);
111+
const fullName = asset.split("/").pop() ?? asset;
112+
const name = basename(asset);
113+
const extension = ext(asset);
114+
const serveUrl = `/api/projects/${projectId}/preview/${asset}`;
115+
const isVideo = VIDEO_EXT.test(asset);
116+
const isImage = IMAGE_EXT.test(asset);
117+
const probedDuration = useProbedDuration(serveUrl, !isVideo || duration != null);
118+
const resolvedDuration = duration ?? probedDuration ?? undefined;
119+
const durationLabel = formatDuration(resolvedDuration ?? 0);
120+
121+
// Drag-threshold click gate: track pointer-down position so we can ignore
122+
// pointer-up events that followed a real drag gesture.
123+
const pointerDownRef = useRef<{ x: number; y: number } | null>(null);
124+
125+
const setSelectedElementId = usePlayerStore((s) => s.setSelectedElementId);
126+
const elements = usePlayerStore((s) => s.elements);
127+
const setPreviewAsset = useAssetPreviewStore((s) => s.setPreviewAsset);
128+
129+
const handlePointerDown = useCallback((e: React.PointerEvent) => {
130+
pointerDownRef.current = { x: e.clientX, y: e.clientY };
131+
}, []);
132+
133+
const handlePointerUp = useCallback(
134+
(e: React.PointerEvent) => {
135+
const origin = pointerDownRef.current;
136+
pointerDownRef.current = null;
137+
if (!origin) return;
138+
if (!isPointerClick(e.clientX - origin.x, e.clientY - origin.y)) return;
139+
// Treat as click
140+
if (used) {
141+
const clip = findClipForAsset(elements, asset);
142+
if (clip) {
143+
setSelectedElementId(clip.key ?? clip.id);
144+
return;
145+
}
146+
}
147+
// Not added (or no matching clip found) → preview overlay
148+
setPreviewAsset(asset, projectId);
149+
},
150+
[used, elements, asset, projectId, setSelectedElementId, setPreviewAsset],
151+
);
152+
153+
return (
154+
<>
155+
<div
156+
draggable
157+
onPointerDown={handlePointerDown}
158+
onPointerUp={handlePointerUp}
159+
onDragStart={(e) => writeAssetDragData(e, asset)}
160+
onContextMenu={(e) => openAssetContextMenu(e, setContextMenu)}
161+
onPointerEnter={() => setHovered(true)}
162+
onPointerLeave={() => setHovered(false)}
163+
className={`flex flex-col gap-1 cursor-pointer rounded-md p-1 transition-colors ${
164+
isCopied ? "bg-studio-accent/10" : "hover:bg-neutral-800/40"
165+
}`}
166+
>
167+
{/* Thumbnail */}
168+
<div className="w-full aspect-video rounded overflow-hidden bg-neutral-900 relative">
169+
{isImage && (
170+
<img
171+
src={serveUrl}
172+
alt={name}
173+
loading="lazy"
174+
className="w-full h-full object-cover"
175+
onError={(e) => {
176+
(e.target as HTMLImageElement).style.display = "none";
177+
}}
178+
/>
179+
)}
180+
{isVideo && (
181+
<>
182+
<VideoFrameThumbnail src={serveUrl} />
183+
{hovered && (
184+
<video
185+
src={serveUrl}
186+
autoPlay
187+
muted
188+
loop
189+
playsInline
190+
className="absolute inset-0 w-full h-full object-cover"
191+
/>
192+
)}
193+
</>
194+
)}
195+
{!isImage && !isVideo && (
196+
<div className="w-full h-full flex items-center justify-center">
197+
<span className="text-[10px] font-medium text-neutral-600">{extension}</span>
198+
</div>
199+
)}
200+
201+
{/* "Added" badge — top-left */}
202+
{used && (
203+
<span className="absolute top-1 left-1 text-[9px] font-semibold leading-none px-1.5 py-[3px] rounded bg-neutral-950/80 text-panel-text-1">
204+
Added
205+
</span>
206+
)}
207+
208+
{/* Duration badge — top-right, media only */}
209+
{durationLabel && (
210+
<span className="absolute top-1 right-1 text-[9px] font-medium leading-none px-1.5 py-[3px] rounded bg-neutral-950/80 text-panel-text-2 tabular-nums">
211+
{durationLabel}
212+
</span>
213+
)}
214+
</div>
215+
216+
{/* Filename caption */}
217+
<span
218+
className={`text-[10px] leading-tight text-center block w-full ${
219+
used ? "text-panel-text-2" : "text-panel-text-4"
220+
}`}
221+
title={fullName}
222+
>
223+
{truncateMiddle(fullName, 22)}
224+
</span>
225+
</div>
226+
227+
{contextMenu && (
228+
<ContextMenu
229+
x={contextMenu.x}
230+
y={contextMenu.y}
231+
asset={asset}
232+
onClose={() => setContextMenu(null)}
233+
onCopy={onCopy}
234+
onDelete={onDelete}
235+
onRename={onRename}
236+
onAddAtPlayhead={onAddAssetToTimeline}
237+
/>
238+
)}
239+
</>
240+
);
241+
}
242+
243+
export interface FontRowProps {
244+
asset: string;
245+
used: boolean;
246+
onCopy: (path: string) => void;
247+
isCopied: boolean;
248+
onDelete?: (path: string) => void;
249+
onRename?: (oldPath: string, newPath: string) => void;
250+
onAddAssetToTimeline?: (path: string) => void;
251+
}
252+
253+
/**
254+
* Compact row for font assets (no meaningful thumbnail; show ext badge + name).
255+
*/
256+
export function FontRow({
257+
asset,
258+
used,
259+
onCopy,
260+
isCopied,
261+
onDelete,
262+
onRename,
263+
onAddAssetToTimeline,
264+
}: FontRowProps) {
265+
const [contextMenu, setContextMenu] = useState<{ x: number; y: number } | null>(null);
266+
const name = basename(asset);
267+
const extension = ext(asset);
268+
269+
return (
270+
<>
271+
<div
272+
draggable
273+
onClick={() => onCopy(asset)}
274+
onDragStart={(e) => writeAssetDragData(e, asset)}
275+
onContextMenu={(e) => openAssetContextMenu(e, setContextMenu)}
276+
className={`px-2.5 py-1.5 flex items-center gap-2.5 cursor-pointer transition-colors ${
277+
isCopied
278+
? "bg-studio-accent/10 border-l-2 border-studio-accent"
279+
: "border-l-2 border-transparent hover:bg-neutral-800/50"
280+
}`}
281+
>
282+
<div className="w-[50px] h-[32px] rounded overflow-hidden bg-neutral-900 flex-shrink-0 flex items-center justify-center">
283+
<span className="text-[9px] font-medium text-neutral-700">{extension}</span>
284+
</div>
285+
<div className="min-w-0 flex-1">
286+
<span
287+
className={`text-xs font-medium truncate block ${used ? "text-panel-text-1" : "text-panel-text-3"}`}
288+
>
289+
{name}
290+
</span>
291+
<div className="flex items-center gap-1.5">
292+
<span className="text-[10px] text-neutral-600 truncate">{extension}</span>
293+
{used && (
294+
<span className="text-[9px] font-medium text-panel-accent bg-panel-accent/10 px-1.5 py-px rounded">
295+
in use
296+
</span>
297+
)}
298+
</div>
299+
</div>
300+
</div>
301+
302+
{contextMenu && (
303+
<ContextMenu
304+
x={contextMenu.x}
305+
y={contextMenu.y}
306+
asset={asset}
307+
onClose={() => setContextMenu(null)}
308+
onCopy={onCopy}
309+
onDelete={onDelete}
310+
onRename={onRename}
311+
onAddAtPlayhead={onAddAssetToTimeline}
312+
/>
313+
)}
314+
</>
315+
);
316+
}

0 commit comments

Comments
 (0)