import { AspectRatio, Box, BoxProps, Center, Icon, Image, LayoutProps, Link, Stack, } from "@chakra-ui/react"; import { useImageFileOrUrl } from "hooks/useImageFileOrUrl"; import React, { useCallback } from "react"; import { Accept, DropEvent, FileRejection, useDropzone } from "react-dropzone"; import { AiFillEye, AiOutlineFileAdd } from "react-icons/ai"; import { FiImage, FiUpload } from "react-icons/fi"; import { Button, Text } from "tw-components"; interface IFileInputProps extends BoxProps { accept?: Accept; setValue: (file: File) => void; isDisabled?: boolean; value?: string | File; showUploadButton?: true; maxContainerWidth?: LayoutProps["maxW"]; renderPreview?: (fileUrl: string) => React.ReactNode; } export const FileInput: React.FC = ({ setValue, isDisabled, accept, showUploadButton, value, children, maxContainerWidth, renderPreview, ...restBoxProps }) => { const onDrop = useCallback< ( acceptedFiles: T[], fileRejections: FileRejection[], event: DropEvent, ) => void >( (droppedFiles) => { if (droppedFiles && droppedFiles[0]) { setValue(droppedFiles[0]); } }, [setValue], ); const { getRootProps, getInputProps, open } = useDropzone({ onDrop, accept, }); const file: File | null = value instanceof File ? value : null; const fileUrl = useImageFileOrUrl(value); const helperText = accept && Object.keys(accept).filter((k) => k.split("/")[0] !== "image").length === 1 ? "image" : "file"; // Don't display non image file types const noDisplay = file && !file.type.includes("image"); const fileType = file ? file.type.includes("image") ? "Image" : file.type.includes("audio") ? "Audio" : file.type.includes("video") ? "Video" : file.type.includes("csv") ? "CSV" : file.type.includes("text") || file.type.includes("pdf") ? "Text" : file.type.includes("model") || file.name.endsWith(".glb") || file.name.endsWith(".usdz") ? "3D Model" : "Media" : null; if (children) { return ( {children} ); } return ( {isDisabled ? (
Upload Disabled
) : (
{renderPreview ? ( renderPreview(fileUrl) ) : noDisplay ? ( {fileType} uploaded ) : fileUrl ? ( ) : ( Upload {helperText} )}
)}
{showUploadButton || noDisplay ? ( {showUploadButton && ( )} {noDisplay && ( )} ) : null}
); };