Papermark is the open-source DocSend alternative and secure data rooms with built-in analytics and custom domains.
fix: resolve three bugs in upload visibility feature
Marc Seitz3c827d9499d0
diff --git a/components/viewer-upload-component.tsx b/components/viewer-upload-component.tsxindex 8fb40d11..03e42252 100644--- a/components/viewer-upload-component.tsx+++ b/components/viewer-upload-component.tsx@@ -31,18 +31,22 @@ export function ViewerUploadComponent({const { addPendingUpload, updatePendingUpload } = usePendingUploads();- // Map file names to their pending upload IDs- const pendingUploadIds = useRef<Map<string, string>>(new Map());+ // Map batch index to pending upload ID (index is stable and unique per batch)+ const pendingUploadIds = useRef<Map<number, string>>(new Map());+ const expectedCountRef = useRef(0);+ const completedCountRef = useRef(0);const handleUploadStart = (newUploads: { fileName: string; progress: number }[],) => {setUploads(newUploads);- // Add each file to pending uploads context- newUploads.forEach((upload) => {+ expectedCountRef.current = newUploads.length;+ completedCountRef.current = 0;++ newUploads.forEach((upload, index) => {const pendingId = newId("pending");- pendingUploadIds.current.set(upload.fileName, pendingId);+ pendingUploadIds.current.set(index, pendingId);addPendingUpload({id: pendingId,@@ -61,8 +65,7 @@ export function ViewerUploadComponent({if (updated[index]) {updated[index] = { ...updated[index], progress };- // Update pending upload progress- const pendingId = pendingUploadIds.current.get(updated[index].fileName);+ const pendingId = pendingUploadIds.current.get(index);if (pendingId) {updatePendingUpload(pendingId, { progress });}@@ -71,8 +74,11 @@ export function ViewerUploadComponent({});};- const handleUploadComplete = async (documentData: DocumentData) => {- const pendingId = pendingUploadIds.current.get(documentData.name);+ const handleUploadComplete = async (+ documentData: DocumentData,+ index: number,+ ) => {+ const pendingId = pendingUploadIds.current.get(index);// Update status to processing (file uploaded to S3, now being processed by backend)if (pendingId) {@@ -121,19 +127,25 @@ export function ViewerUploadComponent({});}- // Notify parent component of successful upload- onUploadSuccess?.();+ completedCountRef.current += 1;+ if (completedCountRef.current >= expectedCountRef.current) {+ onUploadSuccess?.();+ }} catch (error) {console.error("Error processing upload:", error);toast.error((error as Error).message || "Failed to upload document");- // Update pending upload with error statusif (pendingId) {updatePendingUpload(pendingId, {status: "error",errorMessage: (error as Error).message || "Failed to upload document",});}++ completedCountRef.current += 1;+ if (completedCountRef.current >= expectedCountRef.current) {+ onUploadSuccess?.();+ }}};diff --git a/components/viewer-upload-zone.tsx b/components/viewer-upload-zone.tsxindex d14ca8a1..6b444f7c 100644--- a/components/viewer-upload-zone.tsx+++ b/components/viewer-upload-zone.tsx@@ -27,7 +27,7 @@ export default function ViewerUploadZone({children: React.ReactNode;onUploadStart: (uploads: { fileName: string; progress: number }[]) => void;onUploadProgress: (index: number, progress: number) => void;- onUploadComplete: (documentData: DocumentData) => void;+ onUploadComplete: (documentData: DocumentData, index: number) => void;onUploadRejected: (rejected: { fileName: string; message: string }[]) => void;viewerData: {id: string;@@ -134,8 +134,7 @@ export default function ViewerUploadZone({numPages: numPages,};- // Complete the upload by calling the provided callback- onUploadComplete(documentData);+ onUploadComplete(documentData, index);onUploadProgress(index, 100); // Mark upload as completediff --git a/context/pending-uploads-context.tsx b/context/pending-uploads-context.tsxindex fde99bf8..ea472350 100644--- a/context/pending-uploads-context.tsx+++ b/context/pending-uploads-context.tsx@@ -133,6 +133,11 @@ export const PendingUploadsProvider = ({upload.id === id ? { ...upload, ...update } : upload,),);+ setPersistedUploads((prev) =>+ prev.map((upload) =>+ upload.id === id ? { ...upload, ...update } : upload,+ ),+ );},[],);