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"; // hooks import { useMobxStore } from "@/lib/mobx/store-provider"; // services import fileService from "@/services/file.service"; type Props = { handleDelete?: () => void; isOpen: boolean; isRemoving: boolean; onClose: () => void; onSuccess: (url: string) => void; value: string | null; }; const MAX_FILE_SIZE = 5 * 1024 * 1024; // 5MB 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 { instanceStore: { instance }, } = useMobxStore(); const onDrop = (acceptedFiles: File[]) => setImage(acceptedFiles[0]); const { getRootProps, getInputProps, isDragActive, fileRejections } = useDropzone({ onDrop, accept: { "image/*": [".png", ".jpg", ".jpeg", ".svg", ".webp"], }, maxSize: instance?.config?.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 && ( )}
); });