fix(studio): gate timeline drop writes

Route asset, file, and composition drops through the shared edit permission gate before any side effect.
This commit is contained in:
Miguel Angel Simon Sierra
2026-09-19 22:35:01 -04:00
parent dd4add54be
commit c414625d1c
3 changed files with 103 additions and 7 deletions
@@ -16,19 +16,29 @@ afterEach(() => {
});
type DropFn = ReturnType<typeof useTimelineAssetDropOps>["handleTimelineAssetDrop"];
type FileDropFn = ReturnType<typeof useTimelineAssetDropOps>["handleTimelineFileDrop"];
type CompositionDropFn = ReturnType<
typeof useTimelineAssetDropOps
>["handleTimelineCompositionDrop"];
function renderDropHook(
sourceContent: string,
writeProjectFile: (path: string, content: string, expectedContent?: string) => Promise<void>,
timelineElements: TimelineElement[] = [],
checkEditable?: (targets: readonly TimelineElement[]) => boolean,
uploadProjectFiles: (files: Iterable<File>, dir?: string) => Promise<string[]> = vi
.fn()
.mockResolvedValue([]),
) {
vi.stubGlobal(
"fetch",
vi.fn().mockResolvedValue({ ok: true, json: async () => ({ content: sourceContent }) }),
);
let drop: DropFn | null = null;
let fileDrop: FileDropFn | null = null;
let compositionDrop: CompositionDropFn | null = null;
function Harness() {
const { handleTimelineAssetDrop } = useTimelineAssetDropOps({
const handlers = useTimelineAssetDropOps({
projectIdRef: { current: "project" },
activeCompPath: "index.html",
timelineElements,
@@ -36,13 +46,19 @@ function renderDropHook(
writeProjectFile,
recordEdit: vi.fn().mockResolvedValue(undefined),
reloadPreview: vi.fn(),
uploadProjectFiles: vi.fn().mockResolvedValue([]),
uploadProjectFiles,
checkEditable,
});
drop = handleTimelineAssetDrop;
drop = handlers.handleTimelineAssetDrop;
fileDrop = handlers.handleTimelineFileDrop;
compositionDrop = handlers.handleTimelineCompositionDrop;
return null;
}
mountReactHarness(<Harness />);
return () => drop!;
return Object.assign(() => drop!, {
file: () => fileDrop!,
composition: () => compositionDrop!,
});
}
describe("useTimelineAssetDropOps handleTimelineAssetDrop", () => {
@@ -124,4 +140,46 @@ describe("useTimelineAssetDropOps handleTimelineAssetDrop", () => {
expect(written).toContain('id="hidden" data-start="0" data-track-index="1"');
expect(written).toMatch(/<video id="clip"[^>]*data-track-index="1"/);
});
it("refuses an asset drop before reading or writing when editing is blocked", async () => {
const writeProjectFile = vi.fn().mockResolvedValue(undefined);
const checkEditable = vi.fn(() => false);
const getDrop = renderDropHook(
'<main data-composition-id="scene" data-duration="10"></main>',
writeProjectFile,
[],
checkEditable,
);
await act(async () => {
await getDrop()("clip.mp4", { start: 1, track: 0 }, 2);
});
expect(writeProjectFile).not.toHaveBeenCalled();
expect(checkEditable).toHaveBeenCalledWith([
expect.objectContaining({ sourceFile: "index.html", start: 1, track: 0 }),
]);
});
it("refuses file and composition drops before their writes when editing is blocked", async () => {
const writeProjectFile = vi.fn().mockResolvedValue(undefined);
const uploadProjectFiles = vi.fn().mockResolvedValue(["clip.mp4"]);
const getDrop = renderDropHook(
'<main data-composition-id="scene" data-duration="10"></main>',
writeProjectFile,
[],
() => false,
uploadProjectFiles,
);
const fetchMock = vi.mocked(globalThis.fetch);
await act(async () => {
await getDrop.file()([new File(["clip"], "clip.mp4")], { start: 1, track: 0 });
await getDrop.composition()("scene.html", { start: 1, track: 0 });
});
expect(uploadProjectFiles).not.toHaveBeenCalled();
expect(fetchMock).not.toHaveBeenCalled();
expect(writeProjectFile).not.toHaveBeenCalled();
});
});
@@ -38,6 +38,20 @@ function fileDropPlacement(
return index === 0 ? { ...dropped, ...next } : { ...next, track: landedTrack ?? next.track };
}
function timelineDropTarget(
sourceFile: string,
placement: Pick<TimelineElement, "start" | "track">,
): TimelineElement {
return {
id: "timeline-drop",
tag: "div",
start: placement.start,
duration: 0,
track: placement.track,
sourceFile,
};
}
interface UseTimelineAssetDropOpsOptions {
projectIdRef: MutableRefObject<string | null>;
activeCompPath: string | null;
@@ -50,6 +64,7 @@ interface UseTimelineAssetDropOpsOptions {
isRecordingRef?: RefObject<boolean>;
forceReloadSdkSession?: () => void;
observeProjectFileVersion?: (path: string, version: string | null) => void;
checkEditable?: (targets: readonly TimelineElement[]) => boolean;
}
export function useTimelineAssetDropOps({
@@ -64,6 +79,7 @@ export function useTimelineAssetDropOps({
isRecordingRef,
forceReloadSdkSession,
observeProjectFileVersion,
checkEditable,
}: UseTimelineAssetDropOpsOptions) {
// fallow-ignore-next-line complexity
const dropAssetAt = useCallback(
@@ -77,6 +93,10 @@ export function useTimelineAssetDropOps({
showToast("Cannot edit timeline while recording", "error");
return undefined;
}
const targetPath = activeCompPath || "index.html";
if (checkEditable && !checkEditable([timelineDropTarget(targetPath, placement)])) {
return undefined;
}
const pid = projectIdRef.current;
if (!pid) throw new Error("No active project");
@@ -86,7 +106,6 @@ export function useTimelineAssetDropOps({
return undefined;
}
const targetPath = activeCompPath || "index.html";
try {
const originalContent = await readFileContent(pid, targetPath);
@@ -172,6 +191,7 @@ export function useTimelineAssetDropOps({
reloadPreview,
isRecordingRef,
forceReloadSdkSession,
checkEditable,
],
);
@@ -190,6 +210,11 @@ export function useTimelineAssetDropOps({
showToast("Cannot edit timeline while recording", "error");
return;
}
const targetPath = activeCompPath || "index.html";
const initialPlacement = placement ?? { start: 0, track: 0 };
if (checkEditable && !checkEditable([timelineDropTarget(targetPath, initialPlacement)])) {
return;
}
const pid = projectIdRef.current;
if (!pid) return;
const uploaded = await uploadProjectFiles(files);
@@ -215,7 +240,15 @@ export function useTimelineAssetDropOps({
if (index === 0) landedTrack = track;
}
},
[dropAssetAt, projectIdRef, uploadProjectFiles, isRecordingRef, showToast],
[
activeCompPath,
checkEditable,
dropAssetAt,
projectIdRef,
uploadProjectFiles,
isRecordingRef,
showToast,
],
);
const handleTimelineCompositionDrop = useCallback(
@@ -224,9 +257,12 @@ export function useTimelineAssetDropOps({
showToast("Cannot edit timeline while recording", "error");
return;
}
const targetPath = activeCompPath || "index.html";
if (checkEditable && !checkEditable([timelineDropTarget(targetPath, placement)])) {
return;
}
const pid = projectIdRef.current;
if (!pid) throw new Error("No active project");
const targetPath = activeCompPath || "index.html";
try {
await commitTimelineCompositionInsertion({
projectId: pid,
@@ -251,6 +287,7 @@ export function useTimelineAssetDropOps({
},
[
activeCompPath,
checkEditable,
forceReloadSdkSession,
isRecordingRef,
observeProjectFileVersion,
@@ -488,6 +488,7 @@ export function useTimelineEditing({
isRecordingRef,
forceReloadSdkSession,
observeProjectFileVersion,
checkEditable,
});
const handleBlockedTimelineEdit = useBlockedTimelineEditToast(showToast);