import { useCallback, useState } from "react"; import { useRouter } from "next/router"; // react-dropzone import { useDropzone } from "react-dropzone"; // services import fileService from "services/file.service"; // hooks import useToast from "hooks/use-toast"; import useWorkspaceDetails from "hooks/use-workspace-details"; // icons import { getFileIcon } from "components/icons"; import { X } from "lucide-react"; // helpers import { getFileExtension } from "helpers/attachment.helper"; // types import { ICustomAttribute } from "types"; // constants import { MAX_FILE_SIZE } from "constants/workspace"; type Props = { attributeDetails: ICustomAttribute; className?: string; issueId: string; projectId: string; value: string | undefined; onChange: (val: string | undefined) => void; }; export const CustomFileAttribute: React.FC = (props) => { const { attributeDetails, className = "", onChange, value } = props; const [isUploading, setIsUploading] = useState(false); const router = useRouter(); const { workspaceSlug } = router.query; const { workspaceDetails } = useWorkspaceDetails(); const { setToastAlert } = useToast(); const onDrop = useCallback( (acceptedFiles: File[]) => { if (!acceptedFiles[0] || !workspaceSlug) return; const extension = getFileExtension(acceptedFiles[0].name); if (!attributeDetails.extra_settings?.file_formats?.includes(`.${extension}`)) { setToastAlert({ type: "error", title: "Error!", message: `File format not accepted. Accepted file formats- ${attributeDetails.extra_settings?.file_formats?.join( ", " )}`, }); return; } const formData = new FormData(); formData.append("asset", acceptedFiles[0]); formData.append( "attributes", JSON.stringify({ name: acceptedFiles[0].name, size: acceptedFiles[0].size, }) ); setIsUploading(true); fileService .uploadFile(workspaceSlug.toString(), formData) .then((res) => { const imageUrl = res.asset; onChange(imageUrl); if (value && value !== "" && workspaceDetails) fileService.deleteFile(workspaceDetails.id, value); }) .finally(() => setIsUploading(false)); }, [ attributeDetails.extra_settings?.file_formats, onChange, setToastAlert, value, workspaceDetails, workspaceSlug, ] ); const handleRemoveFile = () => { if (!workspaceDetails || !value || value === "") return; onChange(undefined); fileService.deleteFile(workspaceDetails.id, value); }; const { getRootProps, getInputProps, isDragActive, isDragReject, fileRejections } = useDropzone({ onDrop, maxSize: MAX_FILE_SIZE, multiple: false, disabled: isUploading, }); const fileError = fileRejections.length > 0 ? `Invalid file type or size (max ${MAX_FILE_SIZE / 1024 / 1024} MB)` : null; return (
{value && value !== "" && (
{getFileIcon(getFileExtension(value))} {value.split("/")[value.split("/").length - 1].split("-")[1]}
)}
{isDragActive ? "Drop here..." : fileError ? fileError : isUploading ? "Uploading..." : `Upload ${value && value !== "" ? "new " : ""}file`}
); };