Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 22 additions & 6 deletions src/components/Card.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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(
() => ({
Expand All @@ -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 = () => {
Expand Down Expand Up @@ -114,14 +124,20 @@ const Card = ({ paper, onSelect, isSelected, isShow=true }: CardProps) => {
}}
/>

<Download
size={20}
<button
type="button"
disabled={isDownloading}
aria-busy={isDownloading}
aria-label={isDownloading ? "Downloading paper" : "Download paper"}
title={isDownloading ? "Downloading paper…" : "Download paper"}
onClick={(e) => {
e.stopPropagation();
void handleDownload(paper);
}}
className="cursor-pointer"
/>
className="cursor-pointer disabled:cursor-wait disabled:opacity-50"
>
{isDownloading ? <Loader2 size={20} className="animate-spin" /> : <Download size={20} />}
</button>
</div>

<div className="flex items-center justify-between gap-2 px-4 pb-4 font-play">
Expand Down
42 changes: 29 additions & 13 deletions src/components/newPdfViewer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand All @@ -23,6 +23,7 @@ interface ControlProps {
toggleFullscreen: () => void;
isFullscreen: boolean;
onDownload: () => Promise<void>;
isDownloading: boolean;
forceMobile?: boolean;
isMobile: boolean;
isSmall: boolean;
Expand Down Expand Up @@ -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) {

Expand Down Expand Up @@ -208,10 +209,13 @@ const pageChange = useCallback(

<Button
onClick={onDownload}
disabled={isDownloading}
aria-busy={isDownloading}
aria-label={isDownloading ? "Downloading PDF" : "Download PDF"}
className="h-10 w-10 rounded p-0 text-white bg-[#6536c1] transition hover:bg-[#7d4fc7]"
title="Download PDF"
title={isDownloading ? "Downloading PDF…" : "Download PDF"}
>
<Download size={24} />
{isDownloading ? <Loader2 size={24} className="animate-spin" /> : <Download size={24} />}
</Button>

<ShareButton isFullscreen={isFullscreen} viewerRef={viewerRef} />
Expand Down Expand Up @@ -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<HTMLDivElement>(null);
Expand All @@ -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(() => {
Expand Down Expand Up @@ -671,6 +685,7 @@ export default function PDFViewer({
toggleFullscreen={toggleFullscreen}
isFullscreen={isFullscreen}
onDownload={handleDownload}
isDownloading={isDownloading}
forceMobile={true}
isMobile={isMobile}
isSmall={isSmall}
Expand Down Expand Up @@ -736,6 +751,7 @@ export default function PDFViewer({
toggleFullscreen={toggleFullscreen}
isFullscreen={isFullscreen}
onDownload={handleDownload}
isDownloading={isDownloading}
forceMobile={false}
isMobile={isMobile}
isSmall={isSmall}
Expand Down
9 changes: 6 additions & 3 deletions src/context/filterContext.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import React, {
useState,
useCallback,
useMemo,
useRef,
type ReactNode,
} from "react";
import {
Expand Down Expand Up @@ -111,6 +112,7 @@ export const FilterProvider: React.FC<FilterProviderProps> = ({
const [currentPage, setCurrentPage] = useState(1);
const [papersPerPage] = useState(12);
const [isDownloading, setIsDownloading] = useState<boolean>(false);
const downloadInProgress = useRef(false);

const filtersNotPulled = useCallback(() => {
setFiltersPulled(false);
Expand Down Expand Up @@ -144,12 +146,12 @@ export const FilterProvider: React.FC<FilterProviderProps> = ({

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…`,
Expand Down Expand Up @@ -242,9 +244,10 @@ export const FilterProvider: React.FC<FilterProviderProps> = ({
id: toastId,
});
} finally {
downloadInProgress.current = false;
setIsDownloading(false);
}
}, [searchParams, selectedPapers, isDownloading]);
}, [searchParams, selectedPapers]);

const handleApplyFilters = useCallback(
(
Expand Down
Loading