🎥 Make videos programmatically with React
progress
Jonny Burger30c755320051
diff --git a/packages/convert/app/components/DropZone.tsx b/packages/convert/app/components/DropZone.tsxnew file mode 100644index 0000000000..00483c2d28--- /dev/null+++ b/packages/convert/app/components/DropZone.tsx@@ -0,0 +1,110 @@+import React, {useCallback, useState} from 'react';+import {handleDrop} from '~/lib/upload-report';+import type {UploadProgress} from '~/lib/upload-with-progress';++const Container: React.FC<React.HTMLAttributes<HTMLDivElement>> = ({+ children,+}) => {+ return (+ <div className="relative overflow-hidden flex h-40 border-black rounded-md mt-8 justify-center items-center border-solid border-2">+ {children}+ </div>+ );+};++type State =+ | {+ type: 'idle';+ }+ | {+ type: 'waiting-for-presign';+ }+ | {type: 'progress'; progress: UploadProgress}+ | {+ type: 'error';+ err: Error;+ }+ | {type: 'done'; url: string};++export const DropZone: React.FC = () => {+ const [progress, setProgress] = useState<State>({type: 'idle'});++ const selectVideo = useCallback(async (file: File) => {+ setProgress({type: 'waiting-for-presign'});+ const url = await handleDrop({+ file,+ onProgress: (p) => setProgress({type: 'progress', progress: p}),+ });+ setProgress({type: 'done', url});+ }, []);++ const onDrop: React.DragEventHandler = useCallback(+ async (e) => {+ const firstItem = e.dataTransfer?.files?.[0];+ if (!firstItem) {+ return;+ }++ e.preventDefault();++ await selectVideo(firstItem);+ },+ [selectVideo],+ );++ const onPick: React.ChangeEventHandler<HTMLInputElement> = useCallback(+ async (e) => {+ const firstItem = e.target.files?.[0];+ if (!firstItem) {+ return;+ }++ await selectVideo(firstItem);+ },+ [selectVideo],+ );++ if (progress.type === 'done') {+ return (+ <Container>+ <div className="text-muted-foreground font-medium text-sm">+ Video uploaded!+ </div>+ </Container>+ );+ }++ if (progress.type === 'progress') {+ return (+ <Container>+ <div className="text-muted-foreground font-medium text-sm">+ Uploading {Math.round(progress.progress.progress * 100)}%+ </div>+ </Container>+ );+ }++ return (+ <Container+ onDrop={onDrop}+ data-disabled={progress.type === 'waiting-for-presign'}+ className="data-[disabled=true]:opacity-80"+ >+ <input+ type="file"+ onChange={onPick}+ disabled={progress.type === 'waiting-for-presign'}+ className="appearance-none w-full h-full bg-red-50 cursor-pointer opacity-0"+ />+ <div className="pointer-events-none inset-0 absolute flex items-center justify-center">+ <div className="text-muted-foreground font-medium text-sm">+ {progress.type === 'waiting-for-presign' ? (+ <div>Starting Upload...</div>+ ) : (+ <div>Drag a video or click here</div>+ )}+ </div>+ </div>+ </Container>+ );+};diff --git a/packages/convert/app/lib/upload-report.ts b/packages/convert/app/lib/upload-report.tsindex ec7c02971d..991458a65e 100644--- a/packages/convert/app/lib/upload-report.ts+++ b/packages/convert/app/lib/upload-report.ts@@ -1,6 +1,13 @@+import type {OnUploadProgress} from './upload-with-progress';import {uploadWithProgress} from './upload-with-progress';-export const handleDrop = async (file: File) => {+export const handleDrop = async ({+ file,+ onProgress,+}: {+ file: File;+ onProgress: OnUploadProgress;+}) => {const presignedResponse = await fetch('https://www.remotion.pro/api/upload', {method: 'POST',body: JSON.stringify({@@ -17,8 +24,8 @@ export const handleDrop = async (file: File) => {await uploadWithProgress({file,url: presignedUrl,- onProgress: (uploadProgress) => {- console.log(uploadProgress);- },+ onProgress,});++ return readUrl;};diff --git a/packages/convert/app/routes/report._index.tsx b/packages/convert/app/routes/report._index.tsxindex b2e761a62b..5fa456e223 100644--- a/packages/convert/app/routes/report._index.tsx+++ b/packages/convert/app/routes/report._index.tsx@@ -1,25 +1,13 @@-import React, {useCallback, useState} from 'react';+import React, {useState} from 'react';+import {DropZone} from '~/components/DropZone';import {Page} from '~/components/Page';import {Button} from '~/components/ui/button';import {Card} from '~/components/ui/card';import {Label} from '~/components/ui/label';import {RadioGroup, RadioGroupItem} from '~/components/ui/radio';import {Textarea} from '~/components/ui/textarea';-import {handleDrop} from '~/lib/upload-report';const Report: React.FC = () => {- const onDrop: React.DragEventHandler = useCallback(async (e) => {- const firstItem = e.dataTransfer?.files?.[0];- if (!firstItem) {- return;- }-- e.preventDefault();-- await handleDrop(firstItem);- }, []);-- const [file, setFile] = useState<File | null>(null);const [description, setDescription] = useState('');return (@@ -27,12 +15,7 @@ const Report: React.FC = () => {<div className="m-auto max-w-[800px] w-full"><Card className="mx-4 px-8 py-8 mt-12 pt-8"><h1 className="text-3xl font-brand font-black">Report a video</h1>- <input- onDrop={onDrop}- type="file"- placeholder="Video URL"- className="mt-10"- />+ <DropZone /><br /><h2 className="font-brand mt-5 font-bold">Which product has an issue with this video?diff --git a/packages/convert/app/seo.ts b/packages/convert/app/seo.tsindex bb9e62197c..b49609f6ee 100644--- a/packages/convert/app/seo.ts+++ b/packages/convert/app/seo.ts@@ -48,6 +48,9 @@ export type RouteAction =| {type: 'generic-resize';}+ | {+ type: 'report';+ }| {type: 'resize-format';format: MediaParserContainer | ConvertMediaContainer;@@ -93,6 +96,10 @@ export const getHeaderTitle = (routeAction: RouteAction) => {return `Fast ${renderHumanReadableContainer(routeAction.format)} resizing in the browser`;}+ if (routeAction.type === 'report') {+ return `Report bad videos to Remotion`;+ }+throw new Error(`Invalid route action ${routeAction satisfies never}`);};@@ -133,6 +140,10 @@ export const getPageTitle = (routeAction: RouteAction) => {return `Online ${renderHumanReadableContainer(routeAction.format)} Resizer - Remotion Convert`;}+ if (routeAction.type === 'report') {+ return `Report bad videos to Remotion`;+ }+throw new Error(`Invalid route action ${routeAction satisfies never}`);};@@ -179,6 +190,10 @@ export const getDescription = (routeAction: RouteAction) => {)} resizer, powered by WebCodecs. No upload required, no watermarks, no limits.`;}+ if (routeAction.type === 'report') {+ return `Report bad videos to Remotion. We will investigate and fix them.`;+ }+throw new Error(`Invalid route action ${routeAction satisfies never}`);};@@ -215,6 +230,10 @@ export const makeSlug = (routeAction: RouteAction) => {return '/resize';}+ if (routeAction.type === 'report') {+ return '/report';+ }+if (routeAction.type === 'resize-format') {return `/resize/${routeAction.format}`;}