// react import React, { useState, useCallback } from "react"; // next import Link from "next/link"; import { useRouter } from "next/router"; // swr import useSWR, { mutate } from "swr"; // services import issuesService from "services/issues.service"; // react dropzone import { useDropzone } from "react-dropzone"; // fetch key import { ISSUE_ATTACHMENTS, PROJECT_ISSUES_ACTIVITY } from "constants/fetch-keys"; // icons import { FileText, ChevronRight, X, Image as ImageIcon } from "lucide-react"; // components import { Label, WebViewModal } from "components/web-view"; import { DeleteAttachmentModal } from "components/issues"; // types import type { IIssueAttachment } from "types"; type Props = { allowed: boolean; }; const isImage = (fileName: string) => /\.(gif|jpe?g|tiff?|png|webp|bmp)$/i.test(fileName); export const IssueAttachments: React.FC = (props) => { const { allowed } = props; const router = useRouter(); const { workspaceSlug, projectId, issueId } = router.query; const [isOpen, setIsOpen] = useState(false); const [isLoading, setIsLoading] = useState(false); const [deleteAttachment, setDeleteAttachment] = useState(null); const [attachmentDeleteModal, setAttachmentDeleteModal] = useState(false); const onDrop = useCallback( (acceptedFiles: File[]) => { if (!acceptedFiles[0] || !workspaceSlug) return; const formData = new FormData(); formData.append("asset", acceptedFiles[0]); formData.append( "attributes", JSON.stringify({ name: acceptedFiles[0].name, size: acceptedFiles[0].size, }) ); setIsLoading(true); issuesService .uploadIssueAttachment( workspaceSlug as string, projectId as string, issueId as string, formData ) .then((res) => { mutate( ISSUE_ATTACHMENTS(issueId as string), (prevData) => [res, ...(prevData ?? [])], false ); mutate(PROJECT_ISSUES_ACTIVITY(issueId as string)); console.log( "toast", JSON.stringify({ type: "success", title: "Success!", message: "File added successfully.", }) ); setIsOpen(false); setIsLoading(false); }) .catch((err) => { setIsLoading(false); console.log( "toast", JSON.stringify({ type: "error", title: "error!", message: "Something went wrong. please check file type & size (max 5 MB)", }) ); }); }, [issueId, projectId, workspaceSlug] ); const { getRootProps, getInputProps } = useDropzone({ onDrop, maxSize: 5 * 1024 * 1024, disabled: !allowed || isLoading, }); const { data: attachments } = useSWR( workspaceSlug && projectId && issueId ? ISSUE_ATTACHMENTS(issueId as string) : null, workspaceSlug && projectId && issueId ? () => issuesService.getIssueAttachment( workspaceSlug.toString(), projectId.toString(), issueId.toString() ) : null ); return (
setIsOpen(false)} modalTitle="Insert file">
{isLoading ? (

Uploading...

) : ( <>

Upload

)}
{attachments?.map((attachment) => (
{isImage(attachment.attributes.name) ? ( ) : ( )} {attachment.attributes.name} {allowed && ( )}
))}
); };