import React, { useCallback, useState } from "react"; import { useRouter } from "next/router"; import { observer } from "mobx-react-lite"; import { useDropzone } from "react-dropzone"; import { Transition, Dialog } from "@headlessui/react"; // mobx store import { useMobxStore } from "lib/mobx/store-provider"; // services import { FileService } from "services/file.service"; // ui import { Button } from "@plane/ui"; // icons import { UserCircle2 } from "lucide-react"; type Props = { value?: string | null; onClose: () => void; isOpen: boolean; onSuccess: (url: string) => void; isRemoving: boolean; handleDelete: () => void; userImage?: boolean; }; // services const fileService = new FileService(); export const ImageUploadModal: React.FC = observer((props) => { const { value, onSuccess, isOpen, onClose, isRemoving, handleDelete, userImage } = props; const [image, setImage] = useState(null); const [isImageUploading, setIsImageUploading] = useState(false); const router = useRouter(); const { workspaceSlug } = router.query; const { workspace: workspaceStore } = useMobxStore(); const { currentWorkspace: workspaceDetails } = workspaceStore; const onDrop = useCallback((acceptedFiles: File[]) => { setImage(acceptedFiles[0]); }, []); const { getRootProps, getInputProps, isDragActive, fileRejections } = useDropzone({ onDrop, accept: { "image/*": [".png", ".jpg", ".jpeg", ".svg", ".webp"], }, maxSize: 5 * 1024 * 1024, }); const handleSubmit = async () => { setIsImageUploading(true); if (!image || !workspaceSlug) return; const formData = new FormData(); formData.append("asset", image); formData.append("attributes", JSON.stringify({})); if (userImage) { fileService .uploadUserFile(formData) .then((res) => { const imageUrl = res.asset; onSuccess(imageUrl); setIsImageUploading(false); setImage(null); if (value) fileService.deleteUserFile(value); }) .catch((err) => { console.error(err); }); } else fileService .uploadFile(workspaceSlug as string, formData) .then((res) => { const imageUrl = res.asset; onSuccess(imageUrl); setIsImageUploading(false); setImage(null); if (value && workspaceDetails) fileService.deleteFile(workspaceDetails.id, value); }) .catch((err) => { console.error(err); }); }; const handleClose = () => { setImage(null); onClose(); }; 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

); });