Skip to content

Commit c9c0f21

Browse files
committed
feat(studio): assets/blocks panel behaviors + preview helpers
What: blocks tab install flow, right-panel and global drag-overlay polish, music beat analysis and clip-content rendering hooks, and the preview-helper utilities backing asset preview. Why: completes the studio NLE stack on top of the visual refresh. How: modified files only (kept as one PR: splitting further would produce sub-150-LOC fragments of interdependent panel glue). Test plan: bunx vitest run studioPreviewHelpers/studioUrlState suites; tsc --noEmit; fallow audit clean.
1 parent a969043 commit c9c0f21

9 files changed

Lines changed: 243 additions & 85 deletions

‎packages/studio/src/components/StudioGlobalDragOverlay.tsx‎

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,7 @@ export function StudioGlobalDragOverlay() {
1717
<polyline points="7 10 12 15 17 10" />
1818
<line x1="12" y1="15" x2="12" y2="3" />
1919
</svg>
20-
<span className="text-sm font-medium text-studio-accent">
21-
Drop files to import into project
22-
</span>
20+
<span className="text-sm font-medium text-studio-accent">Drop to add at the playhead</span>
2321
</div>
2422
</div>
2523
);

‎packages/studio/src/components/StudioRightPanel.tsx‎

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -446,12 +446,14 @@ export function StudioRightPanel({
446446

447447
return (
448448
<>
449+
{/* Vertical resize divider: 3px visible seam, 8px pointer-capture zone via
450+
the absolutely-positioned inner hit area. */}
449451
<div
450452
role="separator"
451453
aria-label="Resize inspector panel"
452454
aria-orientation="vertical"
453455
tabIndex={0}
454-
className="group w-2 flex-shrink-0 cursor-col-resize flex items-center justify-center outline-none focus-visible:bg-studio-accent/20"
456+
className="group relative w-[3px] flex-shrink-0 cursor-col-resize outline-none focus-visible:bg-studio-accent/20"
455457
style={{ touchAction: "none" }}
456458
onPointerDown={(e) => handlePanelResizeStart("right", e)}
457459
onPointerMove={handlePanelResizeMove}
@@ -465,10 +467,13 @@ export function StudioRightPanel({
465467
setRightWidth(Math.max(160, Math.min(600, rightWidth + delta)));
466468
}}
467469
>
468-
<div className="h-[52px] w-px bg-white/12 transition-colors group-hover:bg-white/18 group-active:bg-white/24" />
470+
{/* Expanded hit zone: 8px wide, centered on the 3px seam */}
471+
<div className="absolute inset-y-0 -left-[2.5px] w-2" />
472+
{/* Visible hairline */}
473+
<div className="absolute top-1/2 left-0 h-[52px] w-[3px] -translate-y-1/2 bg-white/12 transition-colors group-hover:bg-white/18 group-active:bg-white/24" />
469474
</div>
470475
<div
471-
className="flex min-w-0 flex-shrink-0 flex-col overflow-hidden border-l border-neutral-800 bg-neutral-900"
476+
className="flex min-w-0 flex-shrink-0 flex-col overflow-hidden rounded-lg border border-neutral-800 bg-neutral-900"
472477
style={{ width: rightWidth }}
473478
>
474479
{captionEditMode ? (

‎packages/studio/src/components/sidebar/BlocksTab.tsx‎

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import {
1010
import { usePlayerStore } from "../../player";
1111
import { formatTime } from "../../player/lib/time";
1212
import { useStudioShellContext } from "../../contexts/StudioContext";
13+
import { TIMELINE_BLOCK_MIME } from "../../utils/timelineAssetDrop";
1314
export interface BlockPreviewInfo {
1415
videoUrl?: string;
1516
posterUrl?: string;
@@ -383,10 +384,16 @@ function BlockCard({
383384
return (
384385
<div
385386
className="group/card rounded-md overflow-hidden cursor-pointer transition-colors bg-neutral-900 hover:bg-neutral-800"
387+
draggable
388+
onDragStart={(e) => {
389+
e.dataTransfer.effectAllowed = "copy";
390+
e.dataTransfer.setData(TIMELINE_BLOCK_MIME, JSON.stringify({ name }));
391+
e.dataTransfer.setData("text/plain", name);
392+
handleLeave(); // cancel the hover-preview timer so it doesn't fire mid-drag
393+
}}
386394
onPointerEnter={handleEnter}
387395
onPointerLeave={handleLeave}
388396
>
389-
{/* Thumbnail */}
390397
<div className="aspect-video w-full overflow-hidden relative">
391398
{hovered && videoUrl ? (
392399
<video

‎packages/studio/src/hooks/useBlockHandlers.ts‎

Lines changed: 55 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
* Block drop/add handlers for the Studio.
33
* Extracted from App.tsx to keep file sizes under the 600-line limit.
44
*/
5-
import { useCallback, useMemo, useState } from "react";
5+
import { useCallback, useMemo, useRef, useState } from "react";
66
import type { TimelineElement } from "../player";
77
import { usePlayerStore } from "../player";
88
import { addBlockToProject } from "../utils/blockInstaller";
@@ -83,17 +83,41 @@ export function useBlockHandlers({
8383
],
8484
);
8585

86+
// Block installs hit the server and end in a full preview reload; without a
87+
// guard, repeat drops while one is in flight stack duplicate installs.
88+
const installingBlockRef = useRef(false);
89+
const runBlockInstall = useCallback(
90+
async <T>(blockName: string, install: () => Promise<T>): Promise<T | null> => {
91+
if (installingBlockRef.current) {
92+
blockCtx.showToast("A block is already installing — one moment…", "info");
93+
return null;
94+
}
95+
installingBlockRef.current = true;
96+
blockCtx.showToast(`Adding ${blockName}…`, "info");
97+
try {
98+
return await install();
99+
} finally {
100+
installingBlockRef.current = false;
101+
}
102+
},
103+
[blockCtx],
104+
);
105+
86106
const handleAddBlock = useCallback(
87107
(blockName: string) => {
88108
if (!projectId) return;
109+
// fallow-ignore-next-line complexity
89110
void (async () => {
90-
const result = await addBlockToProject({
91-
projectId,
92-
blockName,
93-
...blockCtx,
94-
previewIframe: previewIframeRef.current,
95-
currentTime: usePlayerStore.getState().currentTime,
96-
});
111+
const result = await runBlockInstall(blockName, () =>
112+
addBlockToProject({
113+
projectId,
114+
blockName,
115+
...blockCtx,
116+
previewIframe: previewIframeRef.current,
117+
currentTime: usePlayerStore.getState().currentTime,
118+
}),
119+
);
120+
if (result === null) return;
97121
const params = result?.block.type === "hyperframes:block" ? result.block.params : undefined;
98122
if (params?.length) {
99123
setActiveBlockParams({
@@ -107,37 +131,41 @@ export function useBlockHandlers({
107131
}
108132
})();
109133
},
110-
[projectId, blockCtx, previewIframeRef, setRightCollapsed, setRightPanelTab],
134+
[projectId, blockCtx, previewIframeRef, runBlockInstall, setRightCollapsed, setRightPanelTab],
111135
);
112136

113137
const handleTimelineBlockDrop = useCallback(
114138
(blockName: string, placement: { start: number; track: number }) => {
115139
if (!projectId) return;
116-
void addBlockToProject({
117-
projectId,
118-
blockName,
119-
placement,
120-
...blockCtx,
121-
previewIframe: previewIframeRef.current,
122-
currentTime: usePlayerStore.getState().currentTime,
123-
});
140+
void runBlockInstall(blockName, () =>
141+
addBlockToProject({
142+
projectId,
143+
blockName,
144+
placement,
145+
...blockCtx,
146+
previewIframe: previewIframeRef.current,
147+
currentTime: usePlayerStore.getState().currentTime,
148+
}),
149+
);
124150
},
125-
[projectId, blockCtx, previewIframeRef],
151+
[projectId, blockCtx, previewIframeRef, runBlockInstall],
126152
);
127153

128154
const handlePreviewBlockDrop = useCallback(
129155
(blockName: string, position: { left: number; top: number }) => {
130156
if (!projectId) return;
131-
void addBlockToProject({
132-
projectId,
133-
blockName,
134-
visualPosition: position,
135-
...blockCtx,
136-
previewIframe: previewIframeRef.current,
137-
currentTime: usePlayerStore.getState().currentTime,
138-
});
157+
void runBlockInstall(blockName, () =>
158+
addBlockToProject({
159+
projectId,
160+
blockName,
161+
visualPosition: position,
162+
...blockCtx,
163+
previewIframe: previewIframeRef.current,
164+
currentTime: usePlayerStore.getState().currentTime,
165+
}),
166+
);
139167
},
140-
[projectId, blockCtx, previewIframeRef],
168+
[projectId, blockCtx, previewIframeRef, runBlockInstall],
141169
);
142170

143171
return {

‎packages/studio/src/hooks/useMusicBeatAnalysis.ts‎

Lines changed: 72 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { useEffect, useMemo, useRef } from "react";
22
import { usePlayerStore } from "../player/store/playerStore";
3-
import { isMusicTrack } from "../utils/timelineInspector";
3+
import { resolveBeatSourceTrack } from "../utils/timelineInspector";
44
import { analyzeMusicFromUrl } from "@hyperframes/core/beats";
55
import { useFileManagerContextOptional } from "../contexts/FileManagerContext";
66
import { mergeUserBeats } from "../utils/beatEditing";
@@ -52,6 +52,44 @@ async function resolveBeats(
5252
return { ...detected, hasFile: false };
5353
}
5454

55+
/** True when the beats file for a track exists and holds at least one beat. */
56+
async function readHasSavedBeats(io: ProjectIo, beatPath: string): Promise<boolean> {
57+
try {
58+
const content = await io.readOptionalProjectFile(beatPath);
59+
const parsed = content ? parseBeats(content) : null;
60+
return !!(parsed && parsed.times.length > 0);
61+
} catch {
62+
return false;
63+
}
64+
}
65+
66+
type MusicAnalysis = Awaited<ReturnType<typeof analyzeMusicFromUrl>>;
67+
68+
/**
69+
* Analyze a track (memoized per URL) and fold in any saved beat edits. Null on
70+
* decode/analysis failure — the caller then clears state and drops the cache
71+
* entry (only when the effect is still live).
72+
*/
73+
async function loadBeatAnalysis(
74+
musicSrc: string,
75+
beatPath: string,
76+
io: ProjectIo,
77+
): Promise<{ analysis: MusicAnalysis; times: number[]; strengths: number[] } | null> {
78+
let promise = analysisCache.get(musicSrc);
79+
if (!promise) {
80+
promise = analyzeMusicFromUrl(musicSrc);
81+
cacheAnalysis(musicSrc, promise);
82+
}
83+
try {
84+
const analysis = await promise;
85+
const detected = { times: analysis.beatTimes, strengths: analysis.beatStrengths };
86+
const { times, strengths } = await resolveBeats(beatPath, detected, io);
87+
return { analysis, times, strengths };
88+
} catch {
89+
return null;
90+
}
91+
}
92+
5593
export function useMusicBeatAnalysis(): void {
5694
const elements = usePlayerStore((s) => s.elements);
5795
const setBeatAnalysis = usePlayerStore((s) => s.setBeatAnalysis);
@@ -71,9 +109,12 @@ export function useMusicBeatAnalysis(): void {
71109
? { readOptionalProjectFile, writeProjectFile }
72110
: null;
73111

74-
const musicSrc = useMemo(() => {
75-
const el = elements.find((e) => isMusicTrack(e));
76-
return el?.src ?? null;
112+
const { musicSrc, isFallbackTrack } = useMemo(() => {
113+
const resolved = resolveBeatSourceTrack(elements);
114+
return {
115+
musicSrc: resolved?.element.src ?? null,
116+
isFallbackTrack: resolved?.isFallback ?? false,
117+
};
77118
}, [elements]);
78119

79120
// ── Load: decode for strength data, then use the saved beat file if present,
@@ -95,50 +136,45 @@ export function useMusicBeatAnalysis(): void {
95136
const beatPath = beatFilePathForSrc(musicSrc);
96137
const io = ioRef.current;
97138

98-
// Only run expensive audio decode + beat analysis when the user has an
99-
// explicit beats file saved. Without one, skip entirely — no surprise
100-
// green lines on the timeline after dragging unrelated assets.
139+
// For explicitly tagged/named music tracks: only run expensive audio decode
140+
// + beat analysis when the user has an explicit beats file saved. Without
141+
// one, skip entirely — no surprise green lines on the timeline after
142+
// dragging unrelated assets.
143+
//
144+
// For fallback tracks (audio dropped from Finder with no role/music-id):
145+
// always run analysis so the Beat tool becomes usable immediately.
101146
(async () => {
102147
if (!beatPath || !io) return;
103-
let hasSavedBeats = false;
104-
try {
105-
const content = await io.readOptionalProjectFile(beatPath);
106-
const parsed = content ? parseBeats(content) : null;
107-
hasSavedBeats = !!(parsed && parsed.times.length > 0);
108-
} catch {
109-
/* no file */
148+
if (!isFallbackTrack) {
149+
const hasSavedBeats = await readHasSavedBeats(io, beatPath);
150+
if (cancelled) return;
151+
if (!hasSavedBeats) {
152+
setBeatAnalysis(null);
153+
return;
154+
}
110155
}
111156
if (cancelled) return;
112-
if (!hasSavedBeats) {
157+
158+
const result = await loadBeatAnalysis(musicSrc, beatPath, io);
159+
if (cancelled) return;
160+
if (!result) {
113161
setBeatAnalysis(null);
162+
analysisCache.delete(musicSrc);
114163
return;
115164
}
116-
117-
let promise = analysisCache.get(musicSrc);
118-
if (!promise) {
119-
promise = analyzeMusicFromUrl(musicSrc);
120-
cacheAnalysis(musicSrc, promise);
121-
}
122-
try {
123-
const analysis = await promise;
124-
const detected = { times: analysis.beatTimes, strengths: analysis.beatStrengths };
125-
const { times, strengths } = await resolveBeats(beatPath, detected, io);
126-
if (cancelled) return;
127-
setBeatEdits(null);
128-
resetBeatHistory();
129-
setBeatAnalysis({ ...analysis, beatTimes: times, beatStrengths: strengths });
130-
} catch {
131-
if (!cancelled) {
132-
setBeatAnalysis(null);
133-
analysisCache.delete(musicSrc);
134-
}
135-
}
165+
setBeatEdits(null);
166+
resetBeatHistory();
167+
setBeatAnalysis({
168+
...result.analysis,
169+
beatTimes: result.times,
170+
beatStrengths: result.strengths,
171+
});
136172
})();
137173

138174
return () => {
139175
cancelled = true;
140176
};
141-
}, [musicSrc, setBeatAnalysis, setBeatEdits, resetBeatHistory]);
177+
}, [musicSrc, isFallbackTrack, setBeatAnalysis, setBeatEdits, resetBeatHistory]);
142178

143179
// ── Persist: register a debounced writer fired by every beat edit/undo/redo.
144180
// Flushes any pending write on cleanup so the last edit is never lost. ──

‎packages/studio/src/hooks/useRenderClipContent.ts‎

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ import { createElement } from "react";
33
import { CompositionThumbnail, VideoThumbnail } from "../player";
44
import type { TimelineElement } from "../player";
55
import { AudioWaveform } from "../player/components/AudioWaveform";
6+
import { ImageThumbnail } from "../player/components/ImageThumbnail";
7+
import { encodePreviewPath, resolveMediaPreviewUrl } from "../player/components/thumbnailUtils";
68

79
export function normalizeCompositionSrc(
810
compSrc: string,
@@ -51,8 +53,16 @@ function trimFractions(el: TimelineElement): { start?: number; end?: number } {
5153
*/
5254
function renderAudioClip(el: TimelineElement, pid: string, labelColor: string): ReactNode {
5355
const srcRelative = resolvePreviewRelative(el.src, pid);
54-
const audioUrl = srcRelative ? `/api/projects/${pid}/preview/${srcRelative}` : (el.src ?? "");
55-
const waveformUrl = srcRelative ? `/api/projects/${pid}/waveform/${srcRelative}` : undefined;
56+
// Encode each path segment (spaces, parens, U+202F, unicode) so the URL matches
57+
// what the assets panel loads — a raw segment 404s. resolvePreviewRelative
58+
// returns the DECODED path, so it must be re-encoded here.
59+
const encodedRelative = srcRelative ? encodePreviewPath(srcRelative) : null;
60+
const audioUrl = encodedRelative
61+
? `/api/projects/${pid}/preview/${encodedRelative}`
62+
: (el.src ?? "");
63+
const waveformUrl = encodedRelative
64+
? `/api/projects/${pid}/waveform/${encodedRelative}`
65+
: undefined;
5666
const { start, end } = trimFractions(el);
5767
return createElement(AudioWaveform, {
5868
audioUrl,
@@ -100,7 +110,7 @@ export function useRenderClipContent({
100110
// instead of capturing the master at a time when the comp is fading in.
101111
if (compSrc) {
102112
return createElement(CompositionThumbnail, {
103-
previewUrl: `/api/projects/${pid}/preview/comp/${compSrc}`,
113+
previewUrl: `/api/projects/${pid}/preview/comp/${encodePreviewPath(compSrc)}`,
104114
label: "",
105115
labelColor: style.label,
106116

@@ -138,9 +148,17 @@ export function useRenderClipContent({
138148
!/(backdrop|background|overlay|scrim|mask)/i.test(el.id);
139149

140150
if ((el.tag === "video" || el.tag === "img") && el.src) {
141-
const mediaSrc = el.src.startsWith("http")
142-
? el.src
143-
: `/api/projects/${pid}/preview/${el.src}`;
151+
const mediaSrc = resolveMediaPreviewUrl(el.src, pid);
152+
// Still images can't be decoded by VideoThumbnail's <video> extractor
153+
// (the error event fires and the shimmer never resolves) — render the
154+
// image itself as the strip.
155+
if (el.tag === "img") {
156+
return createElement(ImageThumbnail, {
157+
imageSrc: mediaSrc,
158+
label: "",
159+
labelColor: style.label,
160+
});
161+
}
144162
return createElement(VideoThumbnail, {
145163
videoSrc: mediaSrc,
146164
label: "",

0 commit comments

Comments
 (0)