import { useContext, useEffect, useState } from "react"; import { v4 as uuidv4 } from "uuid"; import { faCirclePlus, faFileArrowUp, faFileAudio, faSpinnerThird, } from "@fortawesome/pro-solid-svg-icons"; import { FontAwesomeIcon } from "@fortawesome/react-fontawesome"; import { FileUploader } from "react-drag-drop-files"; import { TransitionDialogue } from "../TransitionDialogue" import { Button } from "../Button"; import { H2, P } from "../Typography"; import { WaveformPlayer } from "../WaveformPlayer"; import { AuthenticationContext } from "~/contexts/Authentication"; import { UploadMedia, UploadMediaResponse } from "./utilities"; // import { ListDropdown } from "../ListDropdown"; // const visiblityOptions = [ // { // name: "private", // value: "private", // }, // { // name: "public", // value: "public", // }, // ]; const FILE_TYPES = ["MP3", "WAV", "FLAC", "OGG"]; type ComponentState = { isOpen: boolean; file: File | undefined; audioUrl: string; uploadState: "init" | "staged" | "uploading" | "uploaded" | "error"; // visibility: string; fileToken: string|undefined; } const initialValues:ComponentState = { isOpen: false, file: undefined, audioUrl: "", uploadState: "init", // visibility: "private", fileToken: undefined, } const getFileName = (file:File)=>{ return file.name.split(".")[0] || ""; } export const UploadAudioButtonDiagloue = ({ onUploaded, }:{ onUploaded: (fileToken:string)=>void; }) => { const { authState } = useContext(AuthenticationContext); const [{isOpen, file, audioUrl, uploadState, fileToken}, setState] = useState(initialValues); const closeModal = () => setState(initialValues); const openModal = () => setState((curr)=>({...curr, isOpen:true})); // const selectVisibility = (value:string) => { // setState((curr)=>({...curr, visibility:value})) // }; const handleFileChange = (file: File) => { setState((curr) => ({ ...curr, file: file, audioUrl: URL.createObjectURL(file), uploadState: "staged", })); }; const handleClear = ()=>{ setState({ ...initialValues, isOpen: true, }); } const submitUpload = ()=>{ if(file && authState && authState.sessionToken){ setState((curr)=>({ ...curr, uploadState: "uploading" })) UploadMedia({ uuid_idempotency_token: uuidv4(), file: file, source: "file", title: getFileName(file), }, authState.sessionToken) .then((res: UploadMediaResponse) => { // console.log(res); if ("media_file_token" in res) { setState((curr)=>({ ...curr, uploadState: "uploaded", fileToken: res.media_file_token, })); }else{ setState((curr)=>({ ...curr, uploadState: "error" })); } }); } }; useEffect(()=>{ if(fileToken){ onUploaded(fileToken); } },[fileToken]) return( <> Upload Audio} >
} /> {file && (
)} {/* */}
{uploadState !== "uploaded" && } {uploadState === "uploaded" && }
); } const DragAndDropZone = ({ file, }: { file: File | undefined; }) => { const fileSize = file && file.size >= 1024 * 1024 ? (file.size / 1024 / 1024).toFixed(2) + " MB" : file ? `${Math.floor(file.size / 1024)} KB` : null; const fileName = file && getFileName(file).toUpperCase(); if (!file) { return (

Upload a file or drop it here

{FILE_TYPES.join(", ").toString()} supported

); } else { return (

{file.name.slice(0, file.name.lastIndexOf("."))}

{`${fileName} file size: ${fileSize} `} Change File

); } };