Skip to content

🎥 Make videos programmatically with React

start over progress

Jonny Burgere4e45d2e633a
diff --git a/packages/convert/app/components/ConversionDone.tsx b/packages/convert/app/components/ConversionDone.tsx
index cf132ce889..bdb1168f1b 100644
--- a/packages/convert/app/components/ConversionDone.tsx
+++ b/packages/convert/app/components/ConversionDone.tsx
@@ -4,6 +4,7 @@ import {ConvertState, Source} from '~/lib/convert-state';
import {getNewName} from '~/lib/generate-new-name';
import {ConvertProgress} from './ConvertProgress';
import {CloneIcon} from './icons/clone';
+import {UndoIcon} from './icons/undo';
import {Button} from './ui/button';
export const ConversionDone: React.FC<{
@@ -44,6 +45,10 @@ export const ConversionDone: React.FC<{
});
}, [setSrc, state]);
+ const startOver = useCallback(() => {
+ setState({type: 'idle'});
+ }, [setState]);
+
return (
<>
<ConvertProgress state={state.state} name={name} container={container} />
@@ -62,6 +67,16 @@ export const ConversionDone: React.FC<{
<div className="w-2" />
Use new video as input
</Button>
+ <Button
+ variant="ghost"
+ className="w-full flex flex-row justify-start"
+ type="button"
+ onClick={startOver}
+ >
+ <UndoIcon className="size-4" />
+ <div className="w-2" />
+ Start over
+ </Button>
</>
);
};
diff --git a/packages/convert/app/components/icons/undo.tsx b/packages/convert/app/components/icons/undo.tsx
new file mode 100644
index 0000000000..0bcaa60248
--- /dev/null
+++ b/packages/convert/app/components/icons/undo.tsx
@@ -0,0 +1,10 @@
+import React, {SVGProps} from 'react';
+
+export const UndoIcon: React.FC<SVGProps<SVGSVGElement>> = (props) => (
+ <svg viewBox="0 0 512 512" {...props}>
+ <path
+ fill="currentcolor"
+ d="M48.5 224L40 224c-13.3 0-24-10.7-24-24L16 72c0-9.7 5.8-18.5 14.8-22.2s19.3-1.7 26.2 5.2L98.6 96.6c87.6-86.5 228.7-86.2 315.8 1c87.5 87.5 87.5 229.3 0 316.8s-229.3 87.5-316.8 0c-12.5-12.5-12.5-32.8 0-45.3s32.8-12.5 45.3 0c62.5 62.5 163.8 62.5 226.3 0s62.5-163.8 0-226.3c-62.2-62.2-162.7-62.5-225.3-1L185 183c6.9 6.9 8.9 17.2 5.2 26.2s-12.5 14.8-22.2 14.8L48.5 224z"
+ />
+ </svg>
+);
diff --git a/packages/webcodecs/src/convert-media.ts b/packages/webcodecs/src/convert-media.ts
index 2979b69a21..6b3e4392b6 100644
--- a/packages/webcodecs/src/convert-media.ts
+++ b/packages/webcodecs/src/convert-media.ts
@@ -242,7 +242,7 @@ export const convertMedia = async function <
reject(err);
})
.finally(() => {
- throttledState.stop();
+ throttledState.stopAndGetLastProgress();
});
return getPromiseToImmediatelyReturn().finally(() => {
diff --git a/packages/webcodecs/src/throttled-state-update.ts b/packages/webcodecs/src/throttled-state-update.ts
index c52aee536b..d86f7599fa 100644
--- a/packages/webcodecs/src/throttled-state-update.ts
+++ b/packages/webcodecs/src/throttled-state-update.ts
@@ -10,7 +10,7 @@ export type ConvertMediaProgressFn = (
type ReturnType = {
get: () => ConvertMediaProgress;
update: ConvertMediaProgressFn | null;
- stop: () => void;
+ stopAndGetLastProgress: () => void;
};
export const throttledStateUpdate = ({
@@ -32,16 +32,28 @@ export const throttledStateUpdate = ({
expectedOutputDurationInMs: null,
overallProgress: 0,
};
+
if (!updateFn) {
return {
get: () => currentState,
update: null,
- stop: () => {},
+ stopAndGetLastProgress: () => {},
};
}
- const interval = setInterval(() => {
+ let lastUpdated: ConvertMediaProgress | null = null;
+
+ const callUpdateIfChanged = () => {
+ if (currentState === lastUpdated) {
+ return;
+ }
+
updateFn(currentState);
+ lastUpdated = currentState;
+ };
+
+ const interval = setInterval(() => {
+ callUpdateIfChanged();
}, everyMilliseconds);
const onAbort = () => {
@@ -55,9 +67,10 @@ export const throttledStateUpdate = ({
update: (fn: (prevState: ConvertMediaProgress) => ConvertMediaProgress) => {
currentState = fn(currentState);
},
- stop: () => {
+ stopAndGetLastProgress: () => {
clearInterval(interval);
signal.removeEventListener('abort', onAbort);
+ return currentState;
},
};
};