Skip to content

Papermark is the open-source DocSend alternative and secure data rooms with built-in analytics and custom domains.

feat: add toast info when downloading individual doc

Marc Seitz83b35825689d
diff --git a/components/view/dataroom/document-card.tsx b/components/view/dataroom/document-card.tsx
index ccba40dc..fad26233 100644
--- a/components/view/dataroom/document-card.tsx
+++ b/components/view/dataroom/document-card.tsx
@@ -101,7 +101,8 @@ export default function DocumentCard({
toast.error("You cannot download dataroom document in preview mode.");
return;
}
- try {
+
+ const downloadPromise = (async () => {
const response = await fetch(`/api/links/download/dataroom-document`, {
method: "POST",
headers: {
@@ -115,8 +116,9 @@ export default function DocumentCard({
});
if (!response.ok) {
- toast.error("Error downloading file");
- return;
+ const errorData = await response.json().catch(() => ({}));
+ const errorMessage = errorData.error || "Failed to download file";
+ throw new Error(errorMessage);
}
// Check if the response is JSON (for direct downloads) or binary (for buffered files)
@@ -144,8 +146,7 @@ export default function DocumentCard({
window.document.body.removeChild(link);
}, 100);
- toast.success("File downloaded successfully");
- return;
+ return "File downloaded successfully";
}
// For all other files, use the iframe method
@@ -168,15 +169,17 @@ export default function DocumentCard({
}
}, 5000);
- toast.success("Download started");
- return;
+ return "Download started";
}
- toast.error("Unexpected response format");
- } catch (error) {
- console.error("Error downloading file:", error);
- toast.error("Error downloading file");
- }
+ throw new Error("Unexpected response format");
+ })();
+
+ toast.promise(downloadPromise, {
+ loading: "Preparing download...",
+ success: (message) => message,
+ error: (err) => err.message || "Failed to download file",
+ });
};
return (
diff --git a/components/view/nav.tsx b/components/view/nav.tsx
index a41aeadf..524d7c11 100644
--- a/components/view/nav.tsx
+++ b/components/view/nav.tsx
@@ -123,7 +123,8 @@ export default function Nav({
return;
}
if (!allowDownload || type === "notion") return;
- try {
+
+ const downloadPromise = (async () => {
const response = await fetch(`/api/links/download`, {
method: "POST",
headers: {
@@ -133,8 +134,9 @@ export default function Nav({
});
if (!response.ok) {
- toast.error("Error downloading file");
- return;
+ const errorData = await response.json().catch(() => ({}));
+ const errorMessage = errorData.error || "Failed to download file";
+ throw new Error(errorMessage);
}
// Check if the response is a PDF file (for watermarked PDFs)
@@ -183,10 +185,17 @@ export default function Nav({
document.body.removeChild(link);
}, 100);
}
- } catch (error) {
- console.error("Error downloading file:", error);
- toast.error("Error downloading file");
- }
+
+ return "File downloaded successfully";
+ })();
+
+ toast.promise(downloadPromise, {
+ loading: hasWatermark
+ ? "Preparing download with watermark..."
+ : "Preparing download...",
+ success: (message) => message,
+ error: (err) => err.message || "Failed to download file",
+ });
};
useEffect(() => {
diff --git a/components/view/viewer/download-only-viewer.tsx b/components/view/viewer/download-only-viewer.tsx
index 7ed2be47..2ff59eef 100644
--- a/components/view/viewer/download-only-viewer.tsx
+++ b/components/view/viewer/download-only-viewer.tsx
@@ -196,7 +196,8 @@ export default function DownloadOnlyViewer({
return;
}
if (!allowDownload) return;
- try {
+
+ const downloadPromise = (async () => {
const response = await fetch(`/api/links/download`, {
method: "POST",
headers: {
@@ -206,8 +207,9 @@ export default function DownloadOnlyViewer({
});
if (!response.ok) {
- toast.error("Error downloading file");
- return;
+ const errorData = await response.json().catch(() => ({}));
+ const errorMessage = errorData.error || "Failed to download file";
+ throw new Error(errorMessage);
}
// Check if the response is a PDF file (for watermarked PDFs)
@@ -234,10 +236,15 @@ export default function DownloadOnlyViewer({
const { downloadUrl } = await response.json();
window.open(downloadUrl, "_blank");
}
- } catch (error) {
- console.error("Error downloading file:", error);
- toast.error("Error downloading file");
- }
+
+ return "File downloaded successfully";
+ })();
+
+ toast.promise(downloadPromise, {
+ loading: "Preparing download...",
+ success: (message) => message,
+ error: (err) => err.message || "Failed to download file",
+ });
};
return (