import React, { useState } from "react"; import { observer } from "mobx-react-lite"; import { useDropzone } from "react-dropzone"; import { UserCircle2 } from "lucide-react"; import { Transition, Dialog } from "@headlessui/react"; // hooks import { Button, TOAST_TYPE, setToast } from "@plane/ui"; import { MAX_FILE_SIZE } from "@/constants/common"; import { useApplication } from "@/hooks/store"; // services import { FileService } from "@/services/file.service"; // ui // icons // constants type Props = { handleDelete?: () => void; isOpen: boolean; isRemoving: boolean; onClose: () => void; onSuccess: (url: string) => void; value: string | null; }; // services const fileService = new FileService(); export const UserImageUploadModal: React.FC = observer((props) => { const { value, onSuccess, isOpen, onClose, isRemoving, handleDelete } = props; // states const [image, setImage] = useState(null); const [isImageUploading, setIsImageUploading] = useState(false); // store hooks const { config: { envConfig }, } = useApplication(); const onDrop = (acceptedFiles: File[]) => setImage(acceptedFiles[0]); const { getRootProps, getInputProps, isDragActive, fileRejections } = useDropzone({ onDrop, accept: { "image/*": [".png", ".jpg", ".jpeg", ".svg", ".webp"], }, maxSize: envConfig?.file_size_limit ?? MAX_FILE_SIZE, multiple: false, }); const handleClose = () => { setImage(null); setIsImageUploading(false); onClose(); }; const handleSubmit = async () => { if (!image) return; setIsImageUploading(true); const formData = new FormData(); formData.append("asset", image); formData.append("attributes", JSON.stringify({})); fileService .uploadUserFile(formData) .then((res) => { const imageUrl = res.asset; onSuccess(imageUrl); setImage(null); if (value) fileService.deleteUserFile(value); }) .catch((err) => setToast({ type: TOAST_TYPE.ERROR, title: "Error!", message: err?.error ?? "Something went wrong. Please try again.", }) ) .finally(() => setIsImageUploading(false)); }; return (
Upload Image
{image !== null || (value && value !== "") ? ( <> image ) : (
{isDragActive ? "Drop image here to upload" : "Drag & drop image here"}
)}
{fileRejections.length > 0 && (

{fileRejections[0].errors[0].code === "file-too-large" ? "The image size cannot exceed 5 MB." : "Please upload a file in a valid format."}

)}

File formats supported- .jpeg, .jpg, .png, .webp, .svg

{handleDelete && ( )}
); });