Skip to content

🎥 Make videos programmatically with React

`@remotion/studio`: Check Unwrap eligibility on click and offer agent refactoring

JonnyBurgeraa18666842aa
diff --git a/packages/studio/src/components/InspectorPanel/SequenceSelectionInspector.tsx b/packages/studio/src/components/InspectorPanel/SequenceSelectionInspector.tsx
index c0a847dd93..96a8903192 100644
--- a/packages/studio/src/components/InspectorPanel/SequenceSelectionInspector.tsx
+++ b/packages/studio/src/components/InspectorPanel/SequenceSelectionInspector.tsx
@@ -369,6 +369,8 @@ const SequenceSourceQuickActions: React.FC<{
sourceLocation={validatedLocation}
/>
<SequenceUnwrapAction
+ sequence={track.sequence}
+ sourceLocation={validatedLocation}
nodePathInfo={selection.nodePathInfo}
sourceActionsDisabled={sourceActionsDisabled || isTransition}
/>
diff --git a/packages/studio/src/components/InspectorPanel/SequenceUnwrapAction.tsx b/packages/studio/src/components/InspectorPanel/SequenceUnwrapAction.tsx
index d256f9d948..3719cde311 100644
--- a/packages/studio/src/components/InspectorPanel/SequenceUnwrapAction.tsx
+++ b/packages/studio/src/components/InspectorPanel/SequenceUnwrapAction.tsx
@@ -1,9 +1,10 @@
-import React, {useCallback, useContext, useEffect, useState} from 'react';
-import {Internals} from 'remotion';
+import React, {useCallback, useContext, useState} from 'react';
+import {Internals, type TSequence} from 'remotion';
import type {SequenceNodePathInfo} from '../../helpers/get-timeline-sequence-sort-key';
import {UnwrapIcon} from '../../icons/unwrap';
+import {SetSelectedModalContext} from '../../state/modals';
import {showNotification} from '../Notifications/NotificationCenter';
-import {unwrapNode} from '../unwrap-node-api';
+import {unwrapSequenceFromSource} from '../Timeline/unwrap-sequence-from-source';
import {InspectorQuickAction} from './common';
const iconStyle: React.CSSProperties = {
@@ -14,57 +15,59 @@ const iconStyle: React.CSSProperties = {
export const SequenceUnwrapAction: React.FC<{
readonly nodePathInfo: SequenceNodePathInfo;
+ readonly sequence: TSequence;
readonly sourceActionsDisabled: boolean;
-}> = ({nodePathInfo, sourceActionsDisabled}) => {
+ readonly sourceLocation: {readonly source: string; readonly line: number};
+}> = ({nodePathInfo, sequence, sourceActionsDisabled, sourceLocation}) => {
const {canvasContent} = useContext(Internals.CompositionManager);
- const [canUnwrap, setCanUnwrap] = useState(false);
+ const {setSelectedModal} = useContext(SetSelectedModalContext);
const [busy, setBusy] = useState(false);
- const {absolutePath, nodePath} = nodePathInfo.sequenceSubscriptionKey;
const compositionId =
canvasContent?.type === 'composition' ? canvasContent.compositionId : null;
- useEffect(() => {
- let cancelled = false;
- setCanUnwrap(false);
- if (sourceActionsDisabled || compositionId === null) return;
- unwrapNode({fileName: absolutePath, nodePath, compositionId, dryRun: true})
- .then((result) => {
- if (!cancelled) setCanUnwrap(result.success && result.canUnwrap);
- })
- .catch(() => undefined);
- return () => {
- cancelled = true;
- };
- }, [absolutePath, nodePath, compositionId, sourceActionsDisabled]);
-
const onUnwrap = useCallback(async () => {
- if (busy || !canUnwrap || sourceActionsDisabled || compositionId === null)
+ if (
+ busy ||
+ sourceActionsDisabled ||
+ compositionId === null ||
+ sequence.type !== 'sequence'
+ ) {
return;
+ }
+
setBusy(true);
try {
- const result = await unwrapNode({
- fileName: absolutePath,
- nodePath,
+ await unwrapSequenceFromSource({
compositionId,
- dryRun: false,
+ displayName:
+ sequence.displayName || sequence.controls?.componentName || null,
+ nodePath: nodePathInfo.sequenceSubscriptionKey,
+ sourceLocation,
+ setSelectedModal,
});
- if (!result.success) showNotification(result.reason, 4000);
} catch (error) {
showNotification((error as Error).message, 4000);
} finally {
setBusy(false);
}
}, [
- absolutePath,
- nodePath,
- compositionId,
busy,
- canUnwrap,
sourceActionsDisabled,
+ compositionId,
+ sequence,
+ nodePathInfo,
+ sourceLocation,
+ setSelectedModal,
]);
- if (!canUnwrap || sourceActionsDisabled || compositionId === null)
+ if (
+ sourceActionsDisabled ||
+ compositionId === null ||
+ sequence.type !== 'sequence'
+ ) {
return null;
+ }
+
return (
<InspectorQuickAction
disabled={busy}
diff --git a/packages/studio/src/components/Modals.tsx b/packages/studio/src/components/Modals.tsx
index d7cfc770c9..f25a7adf16 100644
--- a/packages/studio/src/components/Modals.tsx
+++ b/packages/studio/src/components/Modals.tsx
@@ -42,7 +42,7 @@ import {SettingsModal} from './SettingsModal';
import {SvgImportDialog} from './SvgImportDialog';
import {TranscriptionModalWithOptionalWhisper} from './Transcription/TranscriptionModalWithOptionalWhisper';
import {VideoMattingModalWithOptionalPackage} from './VideoMatting/VideoMattingModalWithOptionalPackage';
-import {WrapRefactorModal} from './WrapRefactorModal';
+import {WrapperRefactorModal} from './WrapperRefactorModal';
export const Modals: React.FC<{
readonly readOnlyStudio: boolean;
@@ -433,8 +433,10 @@ export const Modals: React.FC<{
{modalContextType && modalContextType.type === 'generate-with-agent' ? (
<GenerateWithAgentModal state={modalContextType} />
) : null}
- {modalContextType && modalContextType.type === 'wrap-refactor' ? (
- <WrapRefactorModal state={modalContextType} />
+ {modalContextType &&
+ (modalContextType.type === 'wrap-refactor' ||
+ modalContextType.type === 'unwrap-refactor') ? (
+ <WrapperRefactorModal state={modalContextType} />
) : null}
{modalContextType &&
modalContextType.type === 'html-in-canvas-unavailable' ? (
diff --git a/packages/studio/src/components/SelectedOutlineElement.tsx b/packages/studio/src/components/SelectedOutlineElement.tsx
index 4ece70f617..91c90be5a6 100644
--- a/packages/studio/src/components/SelectedOutlineElement.tsx
+++ b/packages/studio/src/components/SelectedOutlineElement.tsx
@@ -321,7 +321,13 @@ const SelectedOutlineElementUnmemoized: React.FC<
const canRotate = !sourceEditDisabled && outline.path === null;
const outlineElement =
contextMenuTarget.sequence.refForOutline?.current ?? null;
- const unwrapMenuItem = await getSequenceUnwrapMenuItem({
+ const unwrapMenuItem = getSequenceUnwrapMenuItem({
+ sequence: contextMenuTarget.sequence,
+ sourceLocation:
+ originalLocation?.source && originalLocation.line !== null
+ ? {source: originalLocation.source, line: originalLocation.line}
+ : null,
+ setSelectedModal,
canEditSource:
!sourceEditDisabled && previewServerState.type === 'connected',
compositionId:
diff --git a/packages/studio/src/components/Timeline/TimelineSequence.tsx b/packages/studio/src/components/Timeline/TimelineSequence.tsx
index fbc8364fc4..17068b1d0d 100644
--- a/packages/studio/src/components/Timeline/TimelineSequence.tsx
+++ b/packages/studio/src/components/Timeline/TimelineSequence.tsx
@@ -1653,7 +1653,7 @@ const useTimelineSequenceState = (props: TimelineSequenceProps) => {
validatedLocation?.source,
]);
const getContextMenuItems = useCallback(
- async (event: MouseEvent) => {
+ (event: MouseEvent) => {
const contextMenuTarget =
event.target instanceof Element
? event.target.closest<HTMLElement>(`[${TIMELINE_MARQUEE_ITEM_ATTR}]`)
@@ -1686,7 +1686,10 @@ const useTimelineSequenceState = (props: TimelineSequenceProps) => {
});
}
- const unwrapMenuItem = await getSequenceUnwrapMenuItem({
+ const unwrapMenuItem = getSequenceUnwrapMenuItem({
+ sequence: s,
+ sourceLocation: validatedLocation,
+ setSelectedModal,
canEditSource: previewInteractive && Boolean(validatedLocation?.source),
compositionId:
canvasContent?.type === 'composition'
@@ -1808,7 +1811,7 @@ const useTimelineSequenceState = (props: TimelineSequenceProps) => {
sequencesRef,
setPropStatuses,
setSelectedModal,
- validatedLocation?.source,
+ validatedLocation,
],
);
const {frozenFrame} = s;
diff --git a/packages/studio/src/components/Timeline/TimelineSequenceItem.tsx b/packages/studio/src/components/Timeline/TimelineSequenceItem.tsx
index 2c11738842..f2760339e4 100644
--- a/packages/studio/src/components/Timeline/TimelineSequenceItem.tsx
+++ b/packages/studio/src/components/Timeline/TimelineSequenceItem.tsx
@@ -1379,7 +1379,7 @@ const TimelineSequenceItemInner: React.FC<{
}, [canRotate, nodePathInfo, selectItem, setManuallyEnabled]);
const getContextMenuItems = useCallback(
- async (event: MouseEvent) => {
+ (event: MouseEvent) => {
const contextMenuTarget =
event.currentTarget instanceof HTMLElement ? event.currentTarget : null;
if (assetContextMenu !== null) {
@@ -1410,7 +1410,10 @@ const TimelineSequenceItemInner: React.FC<{
});
}
- const unwrapMenuItem = await getSequenceUnwrapMenuItem({
+ const unwrapMenuItem = getSequenceUnwrapMenuItem({
+ sequence,
+ sourceLocation: validatedLocation,
+ setSelectedModal,
canEditSource: previewInteractive && Boolean(validatedLocation?.source),
compositionId:
canvasContent?.type === 'composition'
@@ -1609,7 +1612,7 @@ const TimelineSequenceItemInner: React.FC<{
sequencesRef,
setSelectedModal,
setPropStatuses,
- validatedLocation?.source,
+ validatedLocation,
],
);
const canDropEffect =
diff --git a/packages/studio/src/components/Timeline/get-sequence-unwrap-menu-item.ts b/packages/studio/src/components/Timeline/get-sequence-unwrap-menu-item.ts
index 849c03e0cd..06e3492c3d 100644
--- a/packages/studio/src/components/Timeline/get-sequence-unwrap-menu-item.ts
+++ b/packages/studio/src/components/Timeline/get-sequence-unwrap-menu-item.ts
@@ -1,42 +1,40 @@
-import type {SequencePropsSubscriptionKey} from 'remotion';
+import type {SequencePropsSubscriptionKey, TSequence} from 'remotion';
+import type {SetSelectedModalContextType} from '../../state/modals';
import type {ComboboxValue} from '../NewComposition/ComboBox';
import {showNotification} from '../Notifications/NotificationCenter';
-import {unwrapNode} from '../unwrap-node-api';
+import {unwrapSequenceFromSource} from './unwrap-sequence-from-source';
-export const getSequenceUnwrapMenuItem = async ({
+export const getSequenceUnwrapMenuItem = ({
canEditSource,
compositionId,
isTransition,
nodePath,
+ sequence,
+ sourceLocation,
+ setSelectedModal,
}: {
readonly canEditSource: boolean;
readonly compositionId: string | null;
readonly isTransition: boolean;
readonly nodePath: SequencePropsSubscriptionKey | null;
-}): Promise<ComboboxValue | null> => {
+ readonly sequence: TSequence;
+ readonly sourceLocation: {
+ readonly source: string;
+ readonly line: number;
+ } | null;
+ readonly setSelectedModal: SetSelectedModalContextType['setSelectedModal'];
+}): ComboboxValue | null => {
if (
!canEditSource ||
compositionId === null ||
isTransition ||
- nodePath === null
+ nodePath === null ||
+ sourceLocation === null ||
+ sequence.type !== 'sequence'
) {
return null;
}
- const request = {
- fileName: nodePath.absolutePath,
- compositionId,
- nodePath: nodePath.nodePath,
- };
- try {
- const result = await unwrapNode({...request, dryRun: true});
- if (!result.success || !result.canUnwrap) {
- return null;
- }
- } catch {
- return null;
- }
-
return {
type: 'item',
id: 'unwrap-sequence',
@@ -48,13 +46,14 @@ export const getSequenceUnwrapMenuItem = async ({
subMenu: null,
disabled: false,
onClick: () => {
- unwrapNode({...request, dryRun: false})
- .then((result) => {
- if (!result.success) {
- showNotification(result.reason, 4000);
- }
- })
- .catch((error: Error) => showNotification(error.message, 4000));
+ unwrapSequenceFromSource({
+ compositionId,
+ displayName:
+ sequence.displayName || sequence.controls?.componentName || null,
+ nodePath,
+ sourceLocation,
+ setSelectedModal,
+ }).catch((error: Error) => showNotification(error.message, 4000));
},
};
};
diff --git a/packages/studio/src/components/Timeline/unwrap-sequence-from-source.ts b/packages/studio/src/components/Timeline/unwrap-sequence-from-source.ts
new file mode 100644
index 0000000000..6d0d586775
--- /dev/null
+++ b/packages/studio/src/components/Timeline/unwrap-sequence-from-source.ts
@@ -0,0 +1,41 @@
+import type {SequencePropsSubscriptionKey} from 'remotion';
+import type {SetSelectedModalContextType} from '../../state/modals';
+import {unwrapNode} from '../unwrap-node-api';
+
+export const unwrapSequenceFromSource = async ({
+ compositionId,
+ displayName,
+ nodePath,
+ sourceLocation,
+ setSelectedModal,
+}: {
+ readonly compositionId: string;
+ readonly displayName: string | null;
+ readonly nodePath: SequencePropsSubscriptionKey;
+ readonly sourceLocation: {readonly source: string; readonly line: number};
+ readonly setSelectedModal: SetSelectedModalContextType['setSelectedModal'];
+}): Promise<void> => {
+ const request = {
+ fileName: nodePath.absolutePath,
+ compositionId,
+ nodePath: nodePath.nodePath,
+ };
+ const eligibility = await unwrapNode({...request, dryRun: true});
+ if (!eligibility.success) {
+ throw new Error(eligibility.reason);
+ }
+
+ if (!eligibility.canUnwrap) {
+ setSelectedModal({
+ type: 'unwrap-refactor',
+ displayName,
+ location: sourceLocation,
+ });
+ return;
+ }
+
+ const result = await unwrapNode({...request, dryRun: false});
+ if (!result.success) {
+ throw new Error(result.reason);
+ }
+};
diff --git a/packages/studio/src/components/WrapRefactorModal.tsx b/packages/studio/src/components/WrapperRefactorModal.tsx
similarity index 69%
rename from packages/studio/src/components/WrapRefactorModal.tsx
rename to packages/studio/src/components/WrapperRefactorModal.tsx
index 82df0e3104..786c7415f0 100644
--- a/packages/studio/src/components/WrapRefactorModal.tsx
+++ b/packages/studio/src/components/WrapperRefactorModal.tsx
@@ -37,9 +37,9 @@ const text: React.CSSProperties = {
marginBottom: 16,
};
-type State = Extract<ModalState, {type: 'wrap-refactor'}>;
+type State = Extract<ModalState, {type: 'wrap-refactor' | 'unwrap-refactor'}>;
-export const WrapRefactorModal: React.FC<{readonly state: State}> = ({
+export const WrapperRefactorModal: React.FC<{readonly state: State}> = ({
state,
}) => {
const {previewServerState} = useContext(StudioServerConnectionCtx);
@@ -70,38 +70,44 @@ export const WrapRefactorModal: React.FC<{readonly state: State}> = ({
? `${state.displayName} (${location})`
: location;
const wrapperImportPath =
- state.wrapper === 'HtmlInCanvasMotionBlur'
+ state.type === 'wrap-refactor' && state.wrapper === 'HtmlInCanvasMotionBlur'
? '@remotion/motion-blur'
: 'remotion';
const onSendToChatGPT = useCallback(() => {
requestCodexAnnotation({
target: annotationTarget.current,
initialComment: markupSkillAvailable
- ? '$remotion-markup Wrap this item.'
- : 'Wrap this item.',
+ ? `$remotion-markup ${state.type === 'unwrap-refactor' ? 'Unwrap' : 'Wrap'} this item.`
+ : `${state.type === 'unwrap-refactor' ? 'Unwrap' : 'Wrap'} this item.`,
metadata: {
- action: 'Wrap item',
- wrapper: state.wrapper,
- import: wrapperImportPath,
+ action: state.type === 'unwrap-refactor' ? 'Unwrap item' : 'Wrap item',
+ ...(state.type === 'wrap-refactor'
+ ? {wrapper: state.wrapper, import: wrapperImportPath}
+ : {instruction: 'Remove this wrapper and keep its children.'}),
...(state.displayName ? {selection: state.displayName} : {}),
source: location,
},
});
- }, [
- location,
- markupSkillAvailable,
- state.displayName,
- state.wrapper,
- wrapperImportPath,
- ]);
+ }, [location, markupSkillAvailable, state, wrapperImportPath]);
return (
<DismissableModal panelStyle={panelStyle}>
- <ModalHeader title="Wrap item" />
+ <ModalHeader
+ title={state.type === 'unwrap-refactor' ? 'Unwrap item' : 'Wrap item'}
+ />
<div ref={annotationTarget} style={container}>
<div style={text}>
- Studio could not safely wrap this item in
- {` <${state.wrapper}>`} with an automated code change.
+ {state.type === 'unwrap-refactor' ? (
+ <>
+ Studio could not automatically remove this wrapper. Remove it
+ while keeping its children.
+ </>
+ ) : (
+ <>
+ Studio could not safely wrap this item in {` <${state.wrapper}>`}{' '}
+ with an automated code change.
+ </>
+ )}
</div>
{canSuggestAgent || canAnnotate ? (
<AgentPrompt
@@ -112,8 +118,16 @@ export const WrapRefactorModal: React.FC<{readonly state: State}> = ({
</ModalButton>
) : null
}
- availableText="You can wrap it using an agent:"
- promptDetails={` Wrap ${target} in <${state.wrapper}> from '${wrapperImportPath}'`}
+ availableText={
+ state.type === 'unwrap-refactor'
+ ? 'You can unwrap it using an agent:'
+ : 'You can wrap it using an agent:'
+ }
+ promptDetails={
+ state.type === 'unwrap-refactor'
+ ? ` Unwrap ${target}: remove the wrapper and keep its children.`
+ : ` Wrap ${target} in <${state.wrapper}> from '${wrapperImportPath}'`
+ }
skillId="remotion-markup"
/>
) : (
diff --git a/packages/studio/src/state/modals.ts b/packages/studio/src/state/modals.ts
index b82413f5c4..ccf0df7337 100644
--- a/packages/studio/src/state/modals.ts
+++ b/packages/studio/src/state/modals.ts
@@ -322,6 +322,14 @@ export type ModalState =
};
wrapper: NodeWrapper;
}
+ | {
+ type: 'unwrap-refactor';
+ displayName: string | null;
+ location: {
+ source: string;
+ line: number;
+ };
+ }
| {
type: 'browser-studio-experimental-notice';
}