Skip to content

🎥 Make videos programmatically with React

@remotion/studio: Reuse existing caption save flow

Igor Samokhovets04d119ad4e84
diff --git a/packages/example/e2e/captions-inspector.test.mts b/packages/example/e2e/captions-inspector.test.mts
index bbd3052f8d..2a87f3b7d9 100644
--- a/packages/example/e2e/captions-inspector.test.mts
+++ b/packages/example/e2e/captions-inspector.test.mts
@@ -163,9 +163,6 @@ test.describe('captions inspector', () => {
sourceBeforeFailedImport,
);
await page.getByRole('button', {name: 'Replace captions'}).click();
- await expect(
- page.getByText('Imported 2 captions. Undo is available.'),
- ).toBeVisible();
await expect(defaultCaption).toHaveValue('Imported');
await expect
.poll(() => fs.readFileSync(elementCaptionsFile, 'utf-8'))
diff --git a/packages/studio/src/components/CaptionInspector.tsx b/packages/studio/src/components/CaptionInspector.tsx
index faa8e5a040..d3b83f9657 100644
--- a/packages/studio/src/components/CaptionInspector.tsx
+++ b/packages/studio/src/components/CaptionInspector.tsx
@@ -30,7 +30,7 @@ export const CaptionInspector: React.FC<{
readonly onTextChange: (captions: Caption[]) => void;
readonly onTextSave: ((captions: Caption[]) => void) | null;
readonly onTextCancel: (() => void) | null;
- readonly onReplaceCaptions: ((captions: Caption[]) => Promise<void>) | null;
+ readonly onReplaceCaptions: ((captions: Caption[]) => void) | null;
readonly onToggle: () => void;
readonly readOnly: boolean;
readonly readOnlyTitle: string | null;
@@ -75,13 +75,7 @@ export const CaptionInspector: React.FC<{
return;
}
- await onReplaceCaptions(importedCaptions);
- const captionLabel =
- importedCaptions.length === 1 ? 'caption' : 'captions';
- showNotification(
- `Imported ${importedCaptions.length} ${captionLabel}. Undo is available.`,
- 4000,
- );
+ onReplaceCaptions(importedCaptions);
} catch (error) {
showNotification(
`Could not import ${file.name}: ${error instanceof Error ? error.message : String(error)}`,
diff --git a/packages/studio/src/components/InlineCaptionInspector.tsx b/packages/studio/src/components/InlineCaptionInspector.tsx
index d43935b008..28a5f8c87e 100644
--- a/packages/studio/src/components/InlineCaptionInspector.tsx
+++ b/packages/studio/src/components/InlineCaptionInspector.tsx
@@ -17,7 +17,7 @@ import {StudioServerConnectionCtx} from '../helpers/client-id';
import {CaptionInspector} from './CaptionInspector';
import {
saveInlineCaptionPatches,
- saveSequencePropsOrThrow,
+ saveSequenceProps,
} from './Timeline/save-sequence-prop';
const serializeCaptions = (captions: Caption[]): string => {
@@ -175,9 +175,9 @@ export const InlineCaptionInspector: React.FC<{
);
const replaceCaptions = useCallback(
- async (nextCaptions: Caption[]) => {
+ (nextCaptions: Caption[]) => {
if (!canSave || clientId === null) {
- throw new Error('Caption importing is unavailable');
+ return;
}
setDraftCaptions(nextCaptions);
@@ -186,33 +186,26 @@ export const InlineCaptionInspector: React.FC<{
'captions',
Internals.makeStaticDragOverride(nextCaptions),
);
-
- try {
- await saveSequencePropsOrThrow({
- changes: [
- {
- fileName: validatedLocation.source,
- nodePath,
- fieldKey: 'captions',
- value: nextCaptions,
- defaultValue: null,
- schema: controls.schema,
- },
- ],
- addedKeyframes: null,
- movedKeyframes: null,
- setPropStatuses,
- clientId,
- undoLabel: 'Import captions',
- redoLabel: 'Import captions again',
- });
- savedCaptions.current = nextCaptions;
- } catch (error) {
- setDraftCaptions(savedCaptions.current);
- throw error;
- } finally {
- clearDragOverrides(nodePath);
- }
+ savedCaptions.current = nextCaptions;
+ saveSequenceProps({
+ changes: [
+ {
+ fileName: validatedLocation.source,
+ nodePath,
+ fieldKey: 'captions',
+ value: nextCaptions,
+ defaultValue: null,
+ schema: controls.schema,
+ },
+ ],
+ addedKeyframes: null,
+ movedKeyframes: null,
+ setPropStatuses,
+ clientId,
+ undoLabel: 'Import captions',
+ redoLabel: 'Import captions again',
+ });
+ clearDragOverrides(nodePath);
},
[
canSave,
diff --git a/packages/studio/src/components/Timeline/save-prop-queue.ts b/packages/studio/src/components/Timeline/save-prop-queue.ts
index 4f093e36f9..ccc8ba7c6c 100644
--- a/packages/studio/src/components/Timeline/save-prop-queue.ts
+++ b/packages/studio/src/components/Timeline/save-prop-queue.ts
@@ -54,17 +54,14 @@ export type EnqueueSaveOptions<TResponse> = {
errorLabel: string;
};
-const enqueueSavePropChangeInternal = <TResponse>(
- {
- nodePath,
- setPropStatuses,
- applyOptimistic,
- applyServerResponse,
- apiCall,
- errorLabel,
- }: EnqueueSaveOptions<TResponse>,
- rejectOnError: boolean,
-): Promise<void> => {
+export const enqueueSavePropChange = <TResponse>({
+ nodePath,
+ setPropStatuses,
+ applyOptimistic,
+ applyServerResponse,
+ apiCall,
+ errorLabel,
+}: EnqueueSaveOptions<TResponse>): Promise<void> => {
const q = getQueue(nodePath);
if (q.cancelled) {
@@ -101,10 +98,6 @@ const enqueueSavePropChangeInternal = <TResponse>(
myQueue.cancelled = true;
dropQueue(nodePath, myQueue);
- if (rejectOnError) {
- throw err;
- }
-
showNotification(
`${errorLabel}: ${err instanceof Error ? err.message : String(err)}`,
4000,
@@ -115,15 +108,3 @@ const enqueueSavePropChangeInternal = <TResponse>(
myQueue.chain = next;
return next;
};
-
-export const enqueueSavePropChange = <TResponse>(
- options: EnqueueSaveOptions<TResponse>,
-): Promise<void> => {
- return enqueueSavePropChangeInternal(options, false);
-};
-
-export const enqueueSavePropChangeOrThrow = <TResponse>(
- options: EnqueueSaveOptions<TResponse>,
-): Promise<void> => {
- return enqueueSavePropChangeInternal(options, true);
-};
diff --git a/packages/studio/src/components/Timeline/save-sequence-prop.ts b/packages/studio/src/components/Timeline/save-sequence-prop.ts
index e7bef9caf5..8cd844e3ce 100644
--- a/packages/studio/src/components/Timeline/save-sequence-prop.ts
+++ b/packages/studio/src/components/Timeline/save-sequence-prop.ts
@@ -16,10 +16,7 @@ import {
type MoveEffectKeyframeChange,
type MoveSequenceKeyframeChange,
} from './call-move-keyframe';
-import {
- enqueueSavePropChange,
- enqueueSavePropChangeOrThrow,
-} from './save-prop-queue';
+import {enqueueSavePropChange} from './save-prop-queue';
export type SetPropStatuses = (
nodePath: SequencePropsSubscriptionKey,
@@ -105,18 +102,15 @@ export const saveInlineCaptionPatches = ({
});
};
-const saveSequencePropsInternal = (
- {
- changes,
- addedKeyframes,
- movedKeyframes,
- setPropStatuses,
- clientId,
- undoLabel,
- redoLabel,
- }: SaveSequencePropsOptions,
- rejectOnError: boolean,
-): Promise<void> => {
+export const saveSequenceProps = ({
+ changes,
+ addedKeyframes,
+ movedKeyframes,
+ setPropStatuses,
+ clientId,
+ undoLabel,
+ redoLabel,
+}: SaveSequencePropsOptions): Promise<void> => {
const keyframesToAdd = addedKeyframes === null ? [] : addedKeyframes;
const sequenceKeyframes =
movedKeyframes === null ? [] : movedKeyframes.sequenceKeyframes;
@@ -142,10 +136,7 @@ const saveSequencePropsInternal = (
throw new Error('Expected a sequence prop change');
}
- const enqueue = rejectOnError
- ? enqueueSavePropChangeOrThrow
- : enqueueSavePropChange;
- return enqueue({
+ return enqueueSavePropChange({
nodePath: change.nodePath,
setPropStatuses,
applyOptimistic: (prev) =>
@@ -265,15 +256,3 @@ const saveSequencePropsInternal = (
redoLabel,
}).then(() => undefined);
};
-
-export const saveSequenceProps = (
- options: SaveSequencePropsOptions,
-): Promise<void> => {
- return saveSequencePropsInternal(options, false);
-};
-
-export const saveSequencePropsOrThrow = (
- options: SaveSequencePropsOptions,
-): Promise<void> => {
- return saveSequencePropsInternal(options, true);
-};