import { t } from "@lingui/core/macro"; import { env } from "next-runtime-env"; import { useRef, useState } from "react"; import { HiOutlinePaperClip } from "react-icons/hi"; import { HiCheckBadge } from "react-icons/hi2"; import { twMerge } from "tailwind-merge"; import Button from "~/components/Button"; import { useModal } from "~/providers/modal"; import { usePopup } from "~/providers/popup"; import { api } from "~/utils/api"; import { invalidateCard } from "~/utils/cardInvalidation"; export function AttachmentUpload({ cardPublicId }: { cardPublicId: string }) { const { openModal } = useModal(); const { showPopup } = usePopup(); const utils = api.useUtils(); const [uploading, setUploading] = useState(false); const [isDragging, setIsDragging] = useState(false); const inputRef = useRef(null); const uploadFile = async (file: File) => { setUploading(true); try { const baseUrl = env("NEXT_PUBLIC_BASE_URL") ?? ""; const response = await fetch( `${baseUrl}/api/upload/attachment?cardPublicId=${encodeURIComponent(cardPublicId)}`, { method: "POST", headers: { "Content-Type": file.type, "x-original-filename": encodeURIComponent(file.name), }, body: file, }, ); if (!response.ok) { throw new Error("Upload failed"); } await invalidateCard(utils, cardPublicId); showPopup({ header: t`Attachment uploaded`, message: t`Your file has been uploaded successfully.`, icon: "success", }); } catch { showPopup({ header: t`Upload failed`, message: t`Failed to upload attachment. Please try again.`, icon: "error", }); setUploading(false); } }; const handleFileSelect = async ( event: React.ChangeEvent, ) => { const file = event.target.files?.[0]; if (!file) return; // Reset input event.target.value = ""; await uploadFile(file); }; const handleDragOver = (e: React.DragEvent) => { e.preventDefault(); e.stopPropagation(); if (!uploading) { setIsDragging(true); } }; const handleDragLeave = (e: React.DragEvent) => { e.preventDefault(); e.stopPropagation(); setIsDragging(false); }; const handleDrop = async (e: React.DragEvent) => { e.preventDefault(); e.stopPropagation(); setIsDragging(false); if (uploading) return; const files = Array.from(e.dataTransfer.files); if (files.length === 0) return; // Upload the first file (or could upload all files) await uploadFile(files[0] ?? new File([], "")); }; return (
); }