Skip to content

🎥 Make videos programmatically with React

alright

JonnyBurger50b68f29335d
diff --git a/packages/convert/app/components/ConvertUi.tsx b/packages/convert/app/components/ConvertUi.tsx
index c868aa3fa5..58a5f5727e 100644
--- a/packages/convert/app/components/ConvertUi.tsx
+++ b/packages/convert/app/components/ConvertUi.tsx
@@ -16,7 +16,10 @@ import type {Dimensions} from '~/lib/calculate-new-dimensions-from-dimensions';
import {calculateNewDimensionsFromRotateAndScale} from '~/lib/calculate-new-dimensions-from-dimensions';
import {canRotateOrMirror} from '~/lib/can-rotate-or-mirror';
import type {ConvertState, Source} from '~/lib/convert-state';
-import type {ConvertSections, RotateOrMirrorState} from '~/lib/default-ui';
+import type {
+ ConvertSections,
+ RotateOrMirrorOrCropState,
+} from '~/lib/default-ui';
import {getOrderOfSections, isConvertEnabledByDefault} from '~/lib/default-ui';
import {getNewName} from '~/lib/generate-new-name';
import {
@@ -72,7 +75,6 @@ const ConvertUI = ({
input,
mediafox,
crop,
- setCrop,
cropRect,
}: {
readonly setSrc: React.Dispatch<React.SetStateAction<Source | null>>;
@@ -87,9 +89,9 @@ const ConvertUI = ({
readonly action: RouteAction;
readonly name: string;
readonly input: Input;
- readonly enableRotateOrMirror: RotateOrMirrorState;
+ readonly enableRotateOrMirror: RotateOrMirrorOrCropState;
readonly setEnableRotateOrMirror: React.Dispatch<
- React.SetStateAction<RotateOrMirrorState | null>
+ React.SetStateAction<RotateOrMirrorOrCropState | null>
>;
readonly userRotation: number;
readonly setRotation: React.Dispatch<React.SetStateAction<number>>;
@@ -98,7 +100,6 @@ const ConvertUI = ({
readonly setFlipHorizontal: React.Dispatch<React.SetStateAction<boolean>>;
readonly setFlipVertical: React.Dispatch<React.SetStateAction<boolean>>;
readonly crop: boolean;
- readonly setCrop: React.Dispatch<React.SetStateAction<boolean>>;
readonly sampleRate: number | null;
readonly mediafox: MediaFox;
readonly cropRect: CropRectangle;
@@ -439,6 +440,16 @@ const ConvertUI = ({
});
}, [setEnableRotateOrMirror]);
+ const onCropClick = useCallback(() => {
+ setEnableRotateOrMirror((m) => {
+ if (m !== 'crop') {
+ return 'crop';
+ }
+
+ return null;
+ });
+ }, [setEnableRotateOrMirror]);
+
const onResizeClick = useCallback(() => {
setResizeOperation((r) => {
if (r !== null || !dimensions) {
@@ -677,7 +688,7 @@ const ConvertUI = ({
if (section === 'crop') {
return (
<div key="crop">
- <ConvertUiSection active={crop} setActive={setCrop}>
+ <ConvertUiSection active={crop} setActive={onCropClick}>
Crop
</ConvertUiSection>
</div>
diff --git a/packages/convert/app/components/FileAvailable.tsx b/packages/convert/app/components/FileAvailable.tsx
index 3db3ef9a1d..5bc09b8327 100644
--- a/packages/convert/app/components/FileAvailable.tsx
+++ b/packages/convert/app/components/FileAvailable.tsx
@@ -3,7 +3,7 @@ import type {CropRectangle} from 'mediabunny';
import React, {useCallback, useEffect, useMemo, useRef, useState} from 'react';
import {normalizeVideoRotation} from '~/lib/calculate-new-dimensions-from-dimensions';
import type {Source} from '~/lib/convert-state';
-import type {RotateOrMirrorState} from '~/lib/default-ui';
+import type {RotateOrMirrorOrCropState} from '~/lib/default-ui';
import {defaultRotateOrMirorState} from '~/lib/default-ui';
import {isAudioOnly} from '~/lib/is-audio-container';
import type {RouteAction} from '~/seo';
@@ -30,7 +30,9 @@ export const FileAvailable: React.FC<{
const videoThumbnailRef = useRef<VideoThumbnailRef>(null);
const [enableRotateOrMirrow, setEnableRotateOrMirror] =
- useState<RotateOrMirrorState>(() => defaultRotateOrMirorState(routeAction));
+ useState<RotateOrMirrorOrCropState>(() =>
+ defaultRotateOrMirorState(routeAction),
+ );
const probeResult = useProbe({
src,
@@ -58,7 +60,6 @@ export const FileAvailable: React.FC<{
height: Infinity,
};
});
- const [crop, setCrop] = useState<boolean>(false);
const [waveform, setWaveform] = useState<number[]>([]);
@@ -86,7 +87,7 @@ export const FileAvailable: React.FC<{
isAudio={isAudio}
waveform={waveform}
mediaFox={mediaFox}
- crop={crop}
+ crop={enableRotateOrMirrow === 'crop'}
setUnclampedRect={setCropOperation}
unclampedRect={cropOperation}
/>
@@ -122,8 +123,7 @@ export const FileAvailable: React.FC<{
mediaFox ? (
<div className="gap-4">
<ConvertUI
- crop={crop}
- setCrop={setCrop}
+ crop={enableRotateOrMirrow === 'crop'}
mediafox={mediaFox}
inputContainer={probeResult.container}
currentAudioCodec={probeResult.audioCodec ?? null}
diff --git a/packages/convert/app/lib/default-ui.ts b/packages/convert/app/lib/default-ui.ts
index a3b1256e94..5b3b75d59d 100644
--- a/packages/convert/app/lib/default-ui.ts
+++ b/packages/convert/app/lib/default-ui.ts
@@ -1,10 +1,10 @@
import type {RouteAction} from '~/seo';
-export type RotateOrMirrorState = 'rotate' | 'mirror' | null;
+export type RotateOrMirrorOrCropState = 'rotate' | 'mirror' | 'crop' | null;
export const defaultRotateOrMirorState = (
action: RouteAction,
-): RotateOrMirrorState => {
+): RotateOrMirrorOrCropState => {
if (action.type === 'convert') {
return null;
}
diff --git a/packages/convert/app/lib/is-submit-enabled.ts b/packages/convert/app/lib/is-submit-enabled.ts
index ce5ac963a8..c42883578c 100644
--- a/packages/convert/app/lib/is-submit-enabled.ts
+++ b/packages/convert/app/lib/is-submit-enabled.ts
@@ -1,6 +1,6 @@
import type {SupportedConfigs} from '~/components/get-supported-configs';
import {canRotateOrMirror} from './can-rotate-or-mirror';
-import type {RotateOrMirrorState} from './default-ui';
+import type {RotateOrMirrorOrCropState} from './default-ui';
import {isDroppingEverything} from './is-reencoding';
export const isSubmitDisabled = ({
@@ -14,7 +14,7 @@ export const isSubmitDisabled = ({
audioConfigIndexSelection: Record<number, string>;
videoConfigIndexSelection: Record<number, string>;
enableConvert: boolean;
- enableRotateOrMirror: RotateOrMirrorState;
+ enableRotateOrMirror: RotateOrMirrorOrCropState;
}) => {
if (!supportedConfigs) {
return true;