Skip to content
Open
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
13 changes: 10 additions & 3 deletions src/context/filterContext.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,7 @@ export const FilterProvider: React.FC<FilterProviderProps> = ({
new Set(selectedPapers.map((paper) => paper._id)),
).map((id) => selectedPapers.find((paper) => paper._id === id)) as IPaper[];

let successCount = 0;
await Promise.all(
uniquePapers.map(async (paper) => {
try {
Expand All @@ -157,11 +158,17 @@ export const FilterProvider: React.FC<FilterProviderProps> = ({
const blob = await response.blob();
const filename = generateFileName(paper);
zip.file(filename, blob);
successCount++;
} catch (err) {
console.error(`Failed to fetch ${paper.file_url}`, err);
}
}),
);
);

if (successCount === 0) {
toast.error("Failed to download any papers. Please try again.");
return;
}

function getDownloadName(
params: ReadonlyURLSearchParams,
Expand All @@ -177,13 +184,13 @@ export const FilterProvider: React.FC<FilterProviderProps> = ({
const a = document.createElement("a");
a.href = url;

a.download = getDownloadName(searchParams, "subject");
a.download = `${getDownloadName(searchParams, "subject")}.zip`;

document.body.appendChild(a);
a.click();
a.remove();
URL.revokeObjectURL(url);
toast.success("Download Initiated");
toast.success(`Downloaded ${successCount} of ${uniquePapers.length} papers`);
}, [searchParams, selectedPapers]);

const handleApplyFilters = useCallback(
Expand Down