Skip to content

🎥 Make videos programmatically with React

`@remotion/studio`: Determine saving spinner from code value mismatch

Jonny Burger2f919ee57da7
diff --git a/packages/studio-server/src/preview-server/routes/can-update-sequence-props.ts b/packages/studio-server/src/preview-server/routes/can-update-sequence-props.ts
index 689db534b4..4f9788776d 100644
--- a/packages/studio-server/src/preview-server/routes/can-update-sequence-props.ts
+++ b/packages/studio-server/src/preview-server/routes/can-update-sequence-props.ts
@@ -14,7 +14,7 @@ import * as recast from 'recast';
import {parseAst} from '../../codemods/parse-ast';
type CanUpdatePropStatus =
- | {canUpdate: true}
+ | {canUpdate: true; codeValue: unknown}
| {canUpdate: false; reason: 'computed'};
export const isStaticValue = (node: Expression): boolean => {
@@ -47,6 +47,61 @@ export const isStaticValue = (node: Expression): boolean => {
}
};
+export const extractStaticValue = (node: Expression): unknown => {
+ switch (node.type) {
+ case 'NumericLiteral':
+ case 'StringLiteral':
+ case 'BooleanLiteral':
+ return (node as {value: unknown}).value;
+ case 'NullLiteral':
+ return null;
+ case 'UnaryExpression': {
+ const un = node as UnaryExpression;
+ if (un.argument.type === 'NumericLiteral') {
+ const val = (un.argument as {value: number}).value;
+ return un.operator === '-' ? -val : val;
+ }
+
+ return undefined;
+ }
+
+ case 'ArrayExpression':
+ return (
+ node as Extract<Expression, {type: 'ArrayExpression'}>
+ ).elements.map((el) => {
+ if (el === null || el.type === 'SpreadElement') {
+ return undefined;
+ }
+
+ return extractStaticValue(el);
+ });
+ case 'ObjectExpression': {
+ const obj = node as ObjectExpression;
+ const result: Record<string, unknown> = {};
+ for (const prop of obj.properties) {
+ if (prop.type === 'ObjectProperty') {
+ const p = prop as ObjectProperty;
+ const key =
+ p.key.type === 'Identifier'
+ ? (p.key as {name: string}).name
+ : p.key.type === 'StringLiteral' ||
+ p.key.type === 'NumericLiteral'
+ ? String((p.key as {value: unknown}).value)
+ : undefined;
+ if (key !== undefined) {
+ result[key] = extractStaticValue(p.value as Expression);
+ }
+ }
+ }
+
+ return result;
+ }
+
+ default:
+ return undefined;
+ }
+};
+
const getPropsStatus = (
jsxElement: JSXOpeningElement,
): Record<string, CanUpdatePropStatus> => {
@@ -69,12 +124,15 @@ const getPropsStatus = (
const {value} = attr as JSXAttribute;
if (!value) {
- props[name] = {canUpdate: true};
+ props[name] = {canUpdate: true, codeValue: true};
continue;
}
if (value.type === 'StringLiteral') {
- props[name] = {canUpdate: true};
+ props[name] = {
+ canUpdate: true,
+ codeValue: (value as {value: string}).value,
+ };
continue;
}
@@ -88,7 +146,10 @@ const getPropsStatus = (
continue;
}
- props[name] = {canUpdate: true};
+ props[name] = {
+ canUpdate: true,
+ codeValue: extractStaticValue(expression),
+ };
continue;
}
diff --git a/packages/studio-server/src/test/update-sequence-props.test.ts b/packages/studio-server/src/test/update-sequence-props.test.ts
index e5cbe06f61..02613b647e 100644
--- a/packages/studio-server/src/test/update-sequence-props.test.ts
+++ b/packages/studio-server/src/test/update-sequence-props.test.ts
@@ -2,7 +2,10 @@ import {expect, test} from 'bun:test';
import path from 'node:path';
import type {Expression} from '@babel/types';
import {parseAst} from '../codemods/parse-ast';
-import {isStaticValue} from '../preview-server/routes/can-update-sequence-props';
+import {
+ extractStaticValue,
+ isStaticValue,
+} from '../preview-server/routes/can-update-sequence-props';
const parseExpression = (code: string): Expression => {
const ast = parseAst(`a = ${code}`);
@@ -40,6 +43,22 @@ test('Computed values should be detected as computed', () => {
expect(isStaticValue(parseExpression('`template ${"x"}`'))).toBe(false);
});
+test('extractStaticValue should extract values from AST nodes', () => {
+ expect(extractStaticValue(parseExpression('42'))).toBe(42);
+ expect(extractStaticValue(parseExpression('"hello"'))).toBe('hello');
+ expect(extractStaticValue(parseExpression('true'))).toBe(true);
+ expect(extractStaticValue(parseExpression('false'))).toBe(false);
+ expect(extractStaticValue(parseExpression('null'))).toBe(null);
+ expect(extractStaticValue(parseExpression('-1'))).toBe(-1);
+ expect(extractStaticValue(parseExpression('[1, 2, 3]'))).toEqual([1, 2, 3]);
+ expect(extractStaticValue(parseExpression('{a: 1, b: "c"}'))).toEqual({
+ a: 1,
+ b: 'c',
+ });
+ expect(extractStaticValue(parseExpression('[]'))).toEqual([]);
+ expect(extractStaticValue(parseExpression('{}'))).toEqual({});
+});
+
test('canUpdateSequenceProps should flag computed props', async () => {
const {computeSequencePropsStatus} =
await import('../preview-server/routes/can-update-sequence-props');
@@ -56,7 +75,10 @@ test('canUpdateSequenceProps should flag computed props', async () => {
throw new Error('Expected canUpdate to be true');
}
- expect(result.props.durationInFrames).toEqual({canUpdate: true});
- expect(result.props.hueShift).toEqual({canUpdate: true});
+ expect(result.props.durationInFrames).toEqual({
+ canUpdate: true,
+ codeValue: 60,
+ });
+ expect(result.props.hueShift).toEqual({canUpdate: true, codeValue: 30});
expect(result.props.seed).toEqual({canUpdate: false, reason: 'computed'});
});
diff --git a/packages/studio-shared/src/api-requests.ts b/packages/studio-shared/src/api-requests.ts
index 1eb5e64aa8..484acf6ba8 100644
--- a/packages/studio-shared/src/api-requests.ts
+++ b/packages/studio-shared/src/api-requests.ts
@@ -216,7 +216,7 @@ export type UnsubscribeFromSequencePropsRequest = {
};
export type CanUpdateSequencePropStatus =
- | {canUpdate: true}
+ | {canUpdate: true; codeValue: unknown}
| {canUpdate: false; reason: 'computed'};
export type CanUpdateSequencePropsResponse =
diff --git a/packages/studio/src/components/Timeline/TimelineExpandedSection.tsx b/packages/studio/src/components/Timeline/TimelineExpandedSection.tsx
index c1a7c47586..08e6fed934 100644
--- a/packages/studio/src/components/Timeline/TimelineExpandedSection.tsx
+++ b/packages/studio/src/components/Timeline/TimelineExpandedSection.tsx
@@ -62,11 +62,10 @@ const TimelineFieldRow: React.FC<{
readonly onDragEnd: () => void;
readonly propStatus: CanUpdateSequencePropStatus | null;
}> = ({field, onSave, onDragValueChange, onDragEnd, propStatus}) => {
- const [saving, setSaving] = useState(false);
-
- const onSavingChange = useCallback((s: boolean) => {
- setSaving(s);
- }, []);
+ const saving =
+ propStatus !== null &&
+ propStatus.canUpdate &&
+ JSON.stringify(field.currentValue) !== JSON.stringify(propStatus.codeValue);
return (
<div style={{...fieldRow, height: field.rowHeight}}>
@@ -78,7 +77,6 @@ const TimelineFieldRow: React.FC<{
field={field}
propStatus={propStatus}
onSave={onSave}
- onSavingChange={onSavingChange}
onDragValueChange={onDragValueChange}
onDragEnd={onDragEnd}
canUpdate={propStatus?.canUpdate ?? false}
diff --git a/packages/studio/src/components/Timeline/TimelineSchemaField.tsx b/packages/studio/src/components/Timeline/TimelineSchemaField.tsx
index c2635fad0f..039cc749b6 100644
--- a/packages/studio/src/components/Timeline/TimelineSchemaField.tsx
+++ b/packages/studio/src/components/Timeline/TimelineSchemaField.tsx
@@ -31,17 +31,9 @@ const TimelineNumberField: React.FC<{
readonly field: SchemaFieldInfo;
readonly canUpdate: boolean;
readonly onSave: (key: string, value: unknown) => Promise<void>;
- readonly onSavingChange: (saving: boolean) => void;
readonly onDragValueChange: (key: string, value: unknown) => void;
readonly onDragEnd: () => void;
-}> = ({
- field,
- canUpdate,
- onSave,
- onSavingChange,
- onDragValueChange,
- onDragEnd,
-}) => {
+}> = ({field, canUpdate, onSave, onDragValueChange, onDragEnd}) => {
const [dragValue, setDragValue] = useState<number | null>(null);
const dragging = useRef(false);
@@ -56,23 +48,20 @@ const TimelineNumberField: React.FC<{
useEffect(() => {
setDragValue(null);
- onSavingChange(false);
onDragEnd();
- }, [field.currentValue, onSavingChange, onDragEnd]);
+ }, [field.currentValue, onDragEnd]);
const onValueChangeEnd = useCallback(
(newVal: number) => {
if (canUpdate && newVal !== field.currentValue) {
- onSavingChange(true);
onSave(field.key, newVal).catch(() => {
- onSavingChange(false);
setDragValue(null);
});
} else {
setDragValue(null);
}
},
- [canUpdate, onSave, onSavingChange, field.key, field.currentValue],
+ [canUpdate, onSave, field.key, field.currentValue],
);
const onTextChange = useCallback(
@@ -81,15 +70,13 @@ const TimelineNumberField: React.FC<{
const parsed = Number(newVal);
if (!Number.isNaN(parsed) && parsed !== field.currentValue) {
setDragValue(parsed);
- onSavingChange(true);
onSave(field.key, parsed).catch(() => {
- onSavingChange(false);
setDragValue(null);
});
}
}
},
- [canUpdate, onSave, onSavingChange, field.key, field.currentValue],
+ [canUpdate, onSave, field.key, field.currentValue],
);
return (
@@ -112,20 +99,11 @@ const TimelineNumberField: React.FC<{
export const TimelineFieldValue: React.FC<{
readonly field: SchemaFieldInfo;
readonly onSave: (key: string, value: unknown) => Promise<void>;
- readonly onSavingChange: (saving: boolean) => void;
readonly onDragValueChange: (key: string, value: unknown) => void;
readonly onDragEnd: () => void;
readonly canUpdate: boolean;
readonly propStatus: CanUpdateSequencePropStatus | null;
-}> = ({
- field,
- onSave,
- onSavingChange,
- onDragValueChange,
- onDragEnd,
- propStatus,
- canUpdate,
-}) => {
+}> = ({field, onSave, onDragValueChange, onDragEnd, propStatus, canUpdate}) => {
const wrapperStyle: React.CSSProperties | undefined =
canUpdate === null || canUpdate === false
? notEditableBackground
@@ -154,7 +132,6 @@ export const TimelineFieldValue: React.FC<{
field={field}
canUpdate={canUpdate}
onSave={onSave}
- onSavingChange={onSavingChange}
onDragValueChange={onDragValueChange}
onDragEnd={onDragEnd}
/>