Skip to content

🎥 Make videos programmatically with React

codemod footer

Jonny Burgerf76561cbc036
diff --git a/packages/studio/src/components/NewComposition/CodemodFooter.tsx b/packages/studio/src/components/NewComposition/CodemodFooter.tsx
index e153d3ec2c..99eb14eb2f 100644
--- a/packages/studio/src/components/NewComposition/CodemodFooter.tsx
+++ b/packages/studio/src/components/NewComposition/CodemodFooter.tsx
@@ -22,7 +22,20 @@ const buttonStyle: React.CSSProperties = {
export const CodemodFooter: React.FC<{
valid: boolean;
codemod: RecastCodemod;
-}> = ({codemod, valid}) => {
+ loadingNotification: React.ReactNode;
+ succeessNotifcation: React.ReactNode;
+ errorNotification: string;
+ genericSubmitLabel: string;
+ submitLabel: (options: {relativeRootPath: string}) => string;
+}> = ({
+ codemod,
+ valid,
+ loadingNotification,
+ succeessNotifcation,
+ errorNotification,
+ genericSubmitLabel,
+ submitLabel,
+}) => {
const [submitting, setSubmitting] = useState(false);
const {setSelectedModal} = useContext(ModalsContext);
const [canApplyCodemod, setCanApplyCodemod] = useState<SimpleDiff | null>(
@@ -53,44 +66,66 @@ export const CodemodFooter: React.FC<{
const trigger = useCallback(() => {
setSubmitting(true);
setSelectedModal(null);
- const notification = showNotification('Duplicating...', null);
+ const notification = showNotification(loadingNotification, null);
applyCodemod({
codemod,
dryRun: false,
+ signal: new AbortController().signal,
})
.then(() => {
- notification.replaceContent(`Deleted composition`, 2000);
+ notification.replaceContent(succeessNotifcation, 2000);
})
.catch((err) => {
notification.replaceContent(
- `Could not duplicate composition: ${err.message}`,
+ `${errorNotification}: ${err.message}`,
2000,
);
});
- }, [codemod, setSelectedModal]);
-
- const getCanApplyCodemod = useCallback(async () => {
- const res = await applyCodemod({
- codemod,
- dryRun: true,
- });
+ }, [
+ codemod,
+ errorNotification,
+ loadingNotification,
+ setSelectedModal,
+ succeessNotifcation,
+ ]);
+
+ const getCanApplyCodemod = useCallback(
+ async (signal: AbortSignal) => {
+ const res = await applyCodemod({
+ codemod,
+ dryRun: true,
+ signal,
+ });
- if (!res.success) {
- throw new Error(res.reason);
- }
+ if (!res.success) {
+ throw new Error(res.reason);
+ }
- setCanApplyCodemod(res.diff);
- }, [codemod]);
+ setCanApplyCodemod(res.diff);
+ },
+ [codemod],
+ );
// TODO: Don't hide modal
useEffect(() => {
- getCanApplyCodemod()
+ const abortController = new AbortController();
+ let aborted = false;
+ getCanApplyCodemod(abortController.signal)
.then(() => undefined)
.catch((err) => {
+ if (aborted) {
+ return;
+ }
+
setSelectedModal(null);
showNotification(`Cannot duplicate composition: ${err.message}`, 3000);
});
+
+ return () => {
+ aborted = true;
+ abortController.abort();
+ };
}, [canApplyCodemod, getCanApplyCodemod, setSelectedModal]);
const disabled =
@@ -131,7 +166,9 @@ export const CodemodFooter: React.FC<{
backgroundColor: disabled ? BLUE_DISABLED : BLUE,
}}
>
- {projectInfo ? `Remove from ${projectInfo.relativeRootFile}` : 'Remove'}
+ {projectInfo && projectInfo.relativeRootFile
+ ? submitLabel({relativeRootPath: projectInfo.relativeRootFile})
+ : genericSubmitLabel}
<ShortcutHint keyToPress="↵" cmdOrCtrl />
</Button>
</Row>
diff --git a/packages/studio/src/components/NewComposition/DeleteComposition.tsx b/packages/studio/src/components/NewComposition/DeleteComposition.tsx
index a510b55b6d..31bf95e2ed 100644
--- a/packages/studio/src/components/NewComposition/DeleteComposition.tsx
+++ b/packages/studio/src/components/NewComposition/DeleteComposition.tsx
@@ -50,7 +50,17 @@ const DeleteCompositionLoaded: React.FC<{
remain in your code.
</div>
<div style={{...content, borderTop: '1px solid black'}}>
- <CodemodFooter codemod={codemod} valid />
+ <CodemodFooter
+ errorNotification={`Could not delete composition`}
+ loadingNotification={'Deleting'}
+ succeessNotifcation={`Deleted ${unresolved.id}`}
+ genericSubmitLabel={`Delete`}
+ submitLabel={({relativeRootPath}) =>
+ `Delete from ${relativeRootPath}`
+ }
+ codemod={codemod}
+ valid
+ />
</div>
</form>
</>
diff --git a/packages/studio/src/components/NewComposition/DuplicateComposition.tsx b/packages/studio/src/components/NewComposition/DuplicateComposition.tsx
index 9e6a733503..fa2caf5987 100644
--- a/packages/studio/src/components/NewComposition/DuplicateComposition.tsx
+++ b/packages/studio/src/components/NewComposition/DuplicateComposition.tsx
@@ -1,34 +1,21 @@
-import type {ProjectInfo, SimpleDiff} from '@remotion/studio-shared';
+import type {RecastCodemod} from '@remotion/studio-shared';
import type {ChangeEventHandler} from 'react';
-import React, {
- useCallback,
- useContext,
- useEffect,
- useMemo,
- useState,
-} from 'react';
+import React, {useCallback, useContext, useMemo, useState} from 'react';
import {Internals} from 'remotion';
-import {ShortcutHint} from '../../error-overlay/remotion-overlay/ShortcutHint';
-import {BLUE, BLUE_DISABLED} from '../../helpers/colors';
-import {useKeybinding} from '../../helpers/use-keybinding';
import {
validateCompositionDimension,
validateCompositionName,
} from '../../helpers/validate-new-comp-data';
-import {ModalsContext} from '../../state/modals';
-import {Button} from '../Button';
-import {Flex, Row, Spacing} from '../layout';
+import {Spacing} from '../layout';
import {NewCompHeader} from '../ModalHeader';
-import {showNotification} from '../Notifications/NotificationCenter';
import {label, optionRow, rightRow} from '../RenderModal/layout';
import {
ResolveCompositionBeforeModal,
ResolvedCompositionContext,
} from '../RenderModal/ResolveCompositionBeforeModal';
-import {applyCodemod, getProjectInfo} from '../RenderQueue/actions';
+import {CodemodFooter} from './CodemodFooter';
import type {ComboboxValue} from './ComboBox';
import {Combobox} from './ComboBox';
-import {CodemodDiffPreview} from './DiffPreview';
import {DismissableModal} from './DismissableModal';
import {InputDragger} from './InputDragger';
import {NewCompDuration} from './NewCompDuration';
@@ -47,11 +34,6 @@ const comboBoxStyle: React.CSSProperties = {
width: 190,
};
-const buttonStyle: React.CSSProperties = {
- backgroundColor: BLUE,
- color: 'white',
-};
-
type CompType = 'composition' | 'still';
const DuplicateCompositionLoaded: React.FC<{
@@ -74,7 +56,6 @@ const DuplicateCompositionLoaded: React.FC<{
const [selectedFrameRate, setFrameRate] = useState<string>(
String(resolved.result.fps),
);
- const [projectInfo, setProjectInfo] = useState<ProjectInfo | null>(null);
const {compositions} = useContext(Internals.CompositionManager);
const [type, setType] = useState<CompType>(initialCompType);
const [newId, setName] = useState(() => {
@@ -103,25 +84,6 @@ const DuplicateCompositionLoaded: React.FC<{
height: String(resolved.result.height),
}));
- useEffect(() => {
- const controller = new AbortController();
-
- getProjectInfo(controller.signal)
- .then((info) => {
- setProjectInfo(info.projectInfo);
- })
- .catch((err) => {
- showNotification(
- `Could not get project info: ${err.message}. Unable to duplicate composition`,
- 3000,
- );
- });
-
- return () => {
- controller.abort();
- };
- }, []);
-
const [durationInFrames, setDurationInFrames] = useState('150');
const onTypeChanged = useCallback((newType: CompType) => {
@@ -220,81 +182,22 @@ const DuplicateCompositionLoaded: React.FC<{
];
}, [onTypeChanged]);
- const {registerKeybinding} = useKeybinding();
-
- const {setSelectedModal} = useContext(ModalsContext);
- const [canApplyCodemod, setCanApplyCodemod] = useState<SimpleDiff | null>(
- null,
- );
- const [submitting, setSubmitting] = useState(false);
-
const valid =
compNameErrMessage === null &&
compWidthErrMessage === null &&
- compHeightErrMessage === null &&
- projectInfo !== null;
- const enableSubmit = !submitting && valid && canApplyCodemod;
-
- const getCanApplyCodemod = useCallback(async () => {
- const res = await applyCodemod({
- codemod: {
- type: 'duplicate-composition',
- idToDuplicate: resolved.result.id,
- newDurationInFrames: null,
- newFps: null,
- newHeight: null,
- newId,
- newWidth: null,
- tag: type === 'still' ? 'Still' : 'Composition',
- },
- dryRun: true,
- });
-
- if (!res.success) {
- throw new Error(res.reason);
- }
-
- setCanApplyCodemod(res.diff);
- }, [newId, resolved.result.id, type]);
-
- useEffect(() => {
- getCanApplyCodemod()
- .then(() => undefined)
- .catch((err) => {
- setSelectedModal(null);
- showNotification(`Cannot duplicate composition: ${err.message}`, 3000);
- });
- }, [canApplyCodemod, getCanApplyCodemod, setSelectedModal]);
-
- const trigger = useCallback(() => {
- setSubmitting(true);
- setSelectedModal(null);
- const notification = showNotification('Duplicating...', null);
-
- applyCodemod({
- codemod: {
- type: 'duplicate-composition',
- idToDuplicate: resolved.result.id,
- newDurationInFrames: hadDurationDefined
- ? Number(durationInFrames)
- : null,
- newFps: hadFpsDefined ? Number(selectedFrameRate) : null,
- newHeight: hadDimensionsDefined ? Number(size.height) : null,
- newWidth: hadDimensionsDefined ? Number(size.width) : null,
- newId,
- tag: type === 'still' ? 'Still' : 'Composition',
- },
- dryRun: false,
- })
- .then(() => {
- notification.replaceContent(`Created "${newId}" composition`, 2000);
- })
- .catch((err) => {
- notification.replaceContent(
- `Could not duplicate composition: ${err.message}`,
- 2000,
- );
- });
+ compHeightErrMessage === null;
+
+ const codemod: RecastCodemod = useMemo(() => {
+ return {
+ type: 'duplicate-composition',
+ idToDuplicate: resolved.result.id,
+ newDurationInFrames: hadDurationDefined ? Number(durationInFrames) : null,
+ newFps: hadFpsDefined ? Number(selectedFrameRate) : null,
+ newHeight: hadDimensionsDefined ? Number(size.height) : null,
+ newWidth: hadDimensionsDefined ? Number(size.width) : null,
+ newId,
+ tag: type === 'still' ? 'Still' : 'Composition',
+ };
}, [
durationInFrames,
hadDimensionsDefined,
@@ -303,36 +206,14 @@ const DuplicateCompositionLoaded: React.FC<{
newId,
resolved.result.id,
selectedFrameRate,
- setSelectedModal,
size.height,
size.width,
type,
]);
- useEffect(() => {
- if (!enableSubmit) {
- return;
- }
-
- const enter = registerKeybinding({
- callback() {
- trigger();
- },
- commandCtrlKey: true,
- key: 'Enter',
- event: 'keydown',
- preventDefault: true,
- triggerIfInputFieldFocused: true,
- keepRegisteredWhenNotHighestContext: false,
- });
- return () => {
- enter.unregister();
- };
- }, [enableSubmit, registerKeybinding, trigger, valid]);
-
return (
<>
- <NewCompHeader title={'Duplicate ' + resolved.result.id} />
+ <NewCompHeader title={`Duplicate ${resolved.result.id}`} />
<form>
<div style={content}>
{initialCompType === 'composition' ? (
@@ -471,23 +352,15 @@ const DuplicateCompositionLoaded: React.FC<{
) : null}
</div>
<div style={{...content, borderTop: '1px solid black'}}>
- <Row align="center">
- {canApplyCodemod ? (
- <CodemodDiffPreview diff={canApplyCodemod} />
- ) : null}
- <Flex />
- <Button
- onClick={trigger}
- disabled={!enableSubmit}
- style={{
- ...buttonStyle,
- backgroundColor: enableSubmit ? BLUE : BLUE_DISABLED,
- }}
- >
- {projectInfo ? `Add to ${projectInfo.relativeRootFile}` : 'Add'}
- <ShortcutHint keyToPress="↵" cmdOrCtrl />
- </Button>
- </Row>
+ <CodemodFooter
+ loadingNotification={'Duplicating...'}
+ errorNotification={'Could not duplicate composition'}
+ succeessNotifcation={`Duplicated ${unresolved.id}`}
+ genericSubmitLabel={'Duplicate'}
+ submitLabel={({relativeRootPath}) => `Add to ${relativeRootPath}`}
+ codemod={codemod}
+ valid={valid}
+ />
</div>
</form>
</>
diff --git a/packages/studio/src/components/RenderQueue/actions.ts b/packages/studio/src/components/RenderQueue/actions.ts
index 4d7fbf7ec7..8c49dc38a8 100644
--- a/packages/studio/src/components/RenderQueue/actions.ts
+++ b/packages/studio/src/components/RenderQueue/actions.ts
@@ -344,15 +344,17 @@ export const copyToClipboard = ({
export const applyCodemod = ({
codemod,
dryRun,
+ signal,
}: {
codemod: RecastCodemod;
dryRun: boolean;
+ signal: AbortController['signal'];
}) => {
const body: ApplyCodemodRequest = {
codemod,
dryRun,
};
- return callApi('/api/apply-codemod', body);
+ return callApi('/api/apply-codemod', body, signal);
};
export const removeRenderJob = (job: RenderJob) => {