From 5a98080e1b7f3ce36578d333db385125b30b238a Mon Sep 17 00:00:00 2001 From: Diya Bajoria Date: Sat, 14 Nov 2026 23:44:42 +0530 Subject: [PATCH] fix: prevent repeated download clicks --- src/components/Card.tsx | 28 +++++++++++++++++----- src/components/newPdfViewer.tsx | 42 +++++++++++++++++++++++---------- src/context/filterContext.tsx | 9 ++++--- 3 files changed, 57 insertions(+), 22 deletions(-) diff --git a/src/components/Card.tsx b/src/components/Card.tsx index 4ceecb9..614b63e 100644 --- a/src/components/Card.tsx +++ b/src/components/Card.tsx @@ -3,7 +3,7 @@ import React from "react"; import { type IPaper } from "@/interface"; import Image from "next/image"; -import { X, Eye, Download, Check } from "lucide-react"; +import { X, Eye, Download, Check, Loader2 } from "lucide-react"; import { extractBracketContent, extractWithoutBracketContent, @@ -27,6 +27,8 @@ interface CardProps { const Card = ({ paper, onSelect, isSelected, isShow=true }: CardProps) => { const [previewOpen, setPreviewOpen] = React.useState(false); + const [isDownloading, setIsDownloading] = React.useState(false); + const downloadInProgress = React.useRef(false); const paperContextValue = React.useMemo( () => ({ @@ -51,7 +53,15 @@ const Card = ({ paper, onSelect, isSelected, isShow=true }: CardProps) => { }, [previewOpen]); const handleDownload = async (paper: IPaper) => { - await downloadFile(getSecureUrl(paper.file_url), generateFileName(paper)); + if (downloadInProgress.current) return; + downloadInProgress.current = true; + setIsDownloading(true); + try { + await downloadFile(getSecureUrl(paper.file_url), generateFileName(paper)); + } finally { + downloadInProgress.current = false; + setIsDownloading(false); + } }; const handleCheckboxChange = () => { @@ -114,14 +124,20 @@ const Card = ({ paper, onSelect, isSelected, isShow=true }: CardProps) => { }} /> - { e.stopPropagation(); void handleDownload(paper); }} - className="cursor-pointer" - /> + className="cursor-pointer disabled:cursor-wait disabled:opacity-50" + > + {isDownloading ? : } +
diff --git a/src/components/newPdfViewer.tsx b/src/components/newPdfViewer.tsx index 68ead11..fd8560e 100644 --- a/src/components/newPdfViewer.tsx +++ b/src/components/newPdfViewer.tsx @@ -9,7 +9,7 @@ import { useZoom, ZoomPluginPackage, ZoomMode } from '@embedpdf/plugin- import { RenderLayer, RenderPluginPackage } from '@embedpdf/plugin-render/react'; import { ExportPluginPackage } from '@embedpdf/plugin-export/react'; -import { Download, ZoomIn, ZoomOut, Maximize2, Minimize2, BookOpenText, X } from "lucide-react"; +import { Download, Loader2, ZoomIn, ZoomOut, Maximize2, Minimize2, BookOpenText, X } from "lucide-react"; import posthog from "posthog-js"; import { memo, useCallback, useEffect, useMemo, useRef, useState } from "react"; import { downloadFile } from "../lib/utils/download"; @@ -23,6 +23,7 @@ interface ControlProps { toggleFullscreen: () => void; isFullscreen: boolean; onDownload: () => Promise; + isDownloading: boolean; forceMobile?: boolean; isMobile: boolean; isSmall: boolean; @@ -63,7 +64,7 @@ function useBreakpoint() { return {isMobile: width !== null && width < 768, isSmall: width !== null && width < 640}; } -const Controls = memo(function Controls({documentId, toggleFullscreen, isFullscreen, onDownload, +const Controls = memo(function Controls({documentId, toggleFullscreen, isFullscreen, onDownload, isDownloading, forceMobile, isMobile, isSmall, viewerRef, isReadingMode, toggleReadingMode, showReadingCoachmark, dismissReadingCoachmark}: ControlProps) { @@ -208,10 +209,13 @@ const pageChange = useCallback( @@ -464,6 +468,8 @@ export default function PDFViewer({ const { isMobile, isSmall } = useBreakpoint(); const { resolvedTheme } = useTheme(); const [isFullscreen, setIsFullscreen] = useState(false); + const [isDownloading, setIsDownloading] = useState(false); + const downloadInProgress = useRef(false); const [isReadingMode, setIsReadingMode] = useState(false); const [showReadingCoachmark, setShowReadingCoachmark] = useState(false); const viewerRef = useRef(null); @@ -487,15 +493,23 @@ export default function PDFViewer({ resolvedTheme === "light" ? "rgba(17,24,39,0.6)" : "rgba(255,255,255,0.5)"; const handleDownload = useCallback(async () => { - window.dataLayer?.push({ - event: "pdf_download_start", - paper_title: name, - paper_url: url, - }); - posthog.capture("paper_downloaded", { - paper_name: name, - }); - await downloadFile(url, `${name}.pdf`); + if (downloadInProgress.current) return; + downloadInProgress.current = true; + setIsDownloading(true); + try { + window.dataLayer?.push({ + event: "pdf_download_start", + paper_title: name, + paper_url: url, + }); + posthog.capture("paper_downloaded", { + paper_name: name, + }); + await downloadFile(url, `${name}.pdf`); + } finally { + downloadInProgress.current = false; + setIsDownloading(false); + } }, [url, name]); const toggleFullscreen = useCallback(() => { @@ -671,6 +685,7 @@ export default function PDFViewer({ toggleFullscreen={toggleFullscreen} isFullscreen={isFullscreen} onDownload={handleDownload} + isDownloading={isDownloading} forceMobile={true} isMobile={isMobile} isSmall={isSmall} @@ -736,6 +751,7 @@ export default function PDFViewer({ toggleFullscreen={toggleFullscreen} isFullscreen={isFullscreen} onDownload={handleDownload} + isDownloading={isDownloading} forceMobile={false} isMobile={isMobile} isSmall={isSmall} diff --git a/src/context/filterContext.tsx b/src/context/filterContext.tsx index 77548ae..608b12a 100644 --- a/src/context/filterContext.tsx +++ b/src/context/filterContext.tsx @@ -6,6 +6,7 @@ import React, { useState, useCallback, useMemo, + useRef, type ReactNode, } from "react"; import { @@ -111,6 +112,7 @@ export const FilterProvider: React.FC = ({ const [currentPage, setCurrentPage] = useState(1); const [papersPerPage] = useState(12); const [isDownloading, setIsDownloading] = useState(false); + const downloadInProgress = useRef(false); const filtersNotPulled = useCallback(() => { setFiltersPulled(false); @@ -144,12 +146,12 @@ export const FilterProvider: React.FC = ({ const searchParams = useSearchParams(); const handleDownloadSelected = useCallback(async () => { + if (downloadInProgress.current) return; if (selectedPapers.length === 0) { toast.error("No papers selected for download."); return; } - if (isDownloading) return; - + downloadInProgress.current = true; setIsDownloading(true); const toastId = toast.loading( `Preparing ${selectedPapers.length} paper${selectedPapers.length > 1 ? "s" : ""} for download…`, @@ -242,9 +244,10 @@ export const FilterProvider: React.FC = ({ id: toastId, }); } finally { + downloadInProgress.current = false; setIsDownloading(false); } - }, [searchParams, selectedPapers, isDownloading]); + }, [searchParams, selectedPapers]); const handleApplyFilters = useCallback( (