From 58fefb071ed5dda867e2173d9b1e1b8714fa8b95 Mon Sep 17 00:00:00 2001 From: Carla Paiva Date: Sat, 13 Jan 2024 20:48:02 -0300 Subject: [PATCH] feat: moved all shared code to Convert component (#25) --- .../converter.css} | 0 src/components/converter/index.tsx | 57 +++++++++++++++++++ src/components/video-converter/index.tsx | 54 +++++++----------- src/model/format.ts | 9 +++ 4 files changed, 86 insertions(+), 34 deletions(-) rename src/components/{video-converter/video-converter.css => converter/converter.css} (100%) create mode 100644 src/components/converter/index.tsx create mode 100644 src/model/format.ts diff --git a/src/components/video-converter/video-converter.css b/src/components/converter/converter.css similarity index 100% rename from src/components/video-converter/video-converter.css rename to src/components/converter/converter.css diff --git a/src/components/converter/index.tsx b/src/components/converter/index.tsx new file mode 100644 index 0000000..43011f2 --- /dev/null +++ b/src/components/converter/index.tsx @@ -0,0 +1,57 @@ +import { memo, useCallback, useState } from "react" +import DraggerUpload from "../dragger-upload" +import { Button, UploadFile } from "antd" +import convertFiles from "../../services/converter" +import { Format } from "../../model/format" +import './converter.css' + +/** Properties of Converter */ +type ConverterProps = { + /** Define allowed types to be uploaded */ + allowedUploadingFormats: Format[] + /** Formats available to be converted at */ + optionsToConvertTo: Format[] +} + +function Converter(props: ConverterProps): JSX.Element { + const [fileList, setFileList] = useState([]) + + const onUploadDone = useCallback((files: UploadFile[]) => { + setFileList(files) + }, []) + + const downloadBlob = useCallback((blob: Blob) => { + const url = window.URL.createObjectURL(blob); + const a = document.createElement("a") + document.body.appendChild(a) + a.style.display = "none" + a.href = url; + a.target = "_blank" + a.download = 'test.gif'; + a.click(); + window.URL.revokeObjectURL(url); + }, []) + + const convert = useCallback(async () => { + const blobs = await convertFiles(fileList) + + for (const blob of blobs) { + downloadBlob(blob) + } + }, [downloadBlob, fileList]) + + return ( +
+ + +
+ ) +} + + +export default memo(Converter) \ No newline at end of file diff --git a/src/components/video-converter/index.tsx b/src/components/video-converter/index.tsx index bc7ec7c..9500fdc 100644 --- a/src/components/video-converter/index.tsx +++ b/src/components/video-converter/index.tsx @@ -1,42 +1,28 @@ -import { useCallback, useState } from "react" -import DraggerUpload from "../dragger-upload" -import { Button, UploadFile } from "antd" -import './video-converter.css' -import convertFiles from "../../services/converter" +import { Format } from "../../model/format" +import Converter from "../converter" -function VideoConverter(): JSX.Element { - const [fileList, setFileList] = useState([]) +const uploadingFormat: Format[] = [ + { + name: "MP4", + extension: ".mp4", + mimeType: "video/mp4" + } +] - const onUploadDone = useCallback((files: UploadFile[]) => { - setFileList(files) - }, []) +const optionsToConvertTo: Format[] = [ + { + name: "GIF", + extension: ".gif", + mimeType: "image/gif" + } +] - const convert = useCallback(async () => { - const blobs = await convertFiles(fileList) - console.log(blobs) - for (const blob of blobs) { - const url = window.URL.createObjectURL(blob); - const a = document.createElement("a") - document.body.appendChild(a) - a.style.display = "none" - a.href = url; - a.target = "_blank" - a.download = 'test.gif'; - a.click(); - window.URL.revokeObjectURL(url); - } - }, [fileList]) +function VideoConverter(): JSX.Element { return ( -
- - -
+ ) } diff --git a/src/model/format.ts b/src/model/format.ts new file mode 100644 index 0000000..276ec6e --- /dev/null +++ b/src/model/format.ts @@ -0,0 +1,9 @@ +/** Defines a format */ +export type Format = { + /** Name o the format */ + name: string + /** Extension with a dot */ + extension: string + /** Mime type of this format, that allow it to be downloaded */ + mimeType: string +} \ No newline at end of file