Skip to content

🎥 Make videos programmatically with React

Browser Studio: Validate Element installation props

Igor Samokhovetsa3cf8b0f1021
diff --git a/packages/browser-studio/src/browser-studio-operations.ts b/packages/browser-studio/src/browser-studio-operations.ts
index 8c32aa3c31..13c91567b6 100644
--- a/packages/browser-studio/src/browser-studio-operations.ts
+++ b/packages/browser-studio/src/browser-studio-operations.ts
@@ -2044,6 +2044,33 @@ export const createBrowserStudioOperations = ({
),
insertElement: async (request) => {
try {
+ const installationMode = request.element.installationMode ?? 'wrapped';
+ const componentOwnsSequence =
+ installationMode === 'component-owned-sequence';
+ if (
+ componentOwnsSequence &&
+ request.element.initialProps !== null &&
+ ['from', 'durationInFrames', 'name'].some((prop) =>
+ Object.hasOwn(request.element.initialProps ?? {}, prop),
+ )
+ ) {
+ throw new Error(
+ 'Component-owned Element initial props must not override from, durationInFrames, or name',
+ );
+ }
+
+ if (
+ componentOwnsSequence &&
+ request.element.initialProps?.style !== undefined &&
+ (request.element.initialProps.style === null ||
+ typeof request.element.initialProps.style !== 'object' ||
+ Array.isArray(request.element.initialProps.style))
+ ) {
+ throw new Error(
+ 'Component-owned Element initial style must be an object',
+ );
+ }
+
const project = getProject();
const plan = await getElementInstallPlanForProject({
installationName: request.installationName,
@@ -2092,9 +2119,6 @@ export const createBrowserStudioOperations = ({
const installedDependencies = await resolveElementDependencies(
request.element.dependencies,
);
- const installationMode = request.element.installationMode ?? 'wrapped';
- const componentOwnsSequence =
- installationMode === 'component-owned-sequence';
const durationInFrames = request.element.durationInFrames ?? null;
const insertion =
await insertJsxElementIntoProjectWithNodePathRemappings({
diff --git a/packages/browser-studio/src/test/browser-studio-project-operations.test.ts b/packages/browser-studio/src/test/browser-studio-project-operations.test.ts
index 7a01842187..273a2b4fd4 100644
--- a/packages/browser-studio/src/test/browser-studio-project-operations.test.ts
+++ b/packages/browser-studio/src/test/browser-studio-project-operations.test.ts
@@ -636,6 +636,74 @@ test('installs component-owned Element timing and initial props', async () => {
expect(composition.match(/\bstyle=/g)).toHaveLength(1);
});
+test('rejects contradictory component-owned Element initial props', async () => {
+ const initialProject = createBlankTemplateProject();
+ let project = initialProject;
+ const operations = createBrowserStudioOperations({
+ dependencyVersions: {},
+ getStaticFiles: null,
+ getProject: () => project,
+ initialElement: null,
+ onProjectChange: (nextProject) => {
+ project = nextProject;
+ },
+ resolveDependencies: null,
+ });
+ const invalidCases: Array<{
+ initialProps: ElementDragData['element']['initialProps'];
+ reason: string;
+ }> = [
+ {
+ initialProps: {from: 10},
+ reason:
+ 'Component-owned Element initial props must not override from, durationInFrames, or name',
+ },
+ {
+ initialProps: {durationInFrames: 10},
+ reason:
+ 'Component-owned Element initial props must not override from, durationInFrames, or name',
+ },
+ {
+ initialProps: {name: 'Override'},
+ reason:
+ 'Component-owned Element initial props must not override from, durationInFrames, or name',
+ },
+ {
+ initialProps: {style: 'color: red'},
+ reason: 'Component-owned Element initial style must be an object',
+ },
+ ];
+
+ for (const {initialProps, reason} of invalidCases) {
+ const response = await operations.insertElement({
+ installationName: null,
+ compositionFile: '/project/src/Composition.tsx',
+ compositionId: 'MyComp',
+ element: {
+ dependencies: [],
+ dimensions: {width: 640, height: 180},
+ displayName: 'Captions',
+ durationInFrames: 90,
+ initialProps,
+ installationMode: 'component-owned-sequence',
+ slug: 'captions',
+ sourceCode: 'export const Captions = () => <div />;\n',
+ },
+ expectedFileState: null,
+ from: 30,
+ overwriteExisting: false,
+ position: {x: 24, y: 48},
+ });
+
+ expect(response).toMatchObject({
+ success: false,
+ type: 'error',
+ reason,
+ });
+ expect(project).toBe(initialProject);
+ }
+});
+
test('installs packages as an undoable project mutation and reports structured failures', async () => {
const initialProject = createBlankTemplateProject();
let project = initialProject;