Skip to content

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

feat: successive uploads

Marc Seitz8e0af6e0db64
diff --git a/components/view/viewer/dataroom-viewer.tsx b/components/view/viewer/dataroom-viewer.tsx
index 7edf58cf..4d704dc0 100644
--- a/components/view/viewer/dataroom-viewer.tsx
+++ b/components/view/viewer/dataroom-viewer.tsx
@@ -1,6 +1,6 @@
import { useRouter } from "next/router";
-import { useMemo, useState } from "react";
+import { useEffect, useMemo, useState } from "react";
import React from "react";
import { ViewerChatPanel } from "@/ee/features/ai/components/viewer-chat-panel";
@@ -190,8 +190,12 @@ export default function DataroomViewer({
);
// Get pending uploads (in-flight + persisted from server)
- const { getPendingUploadsForFolder, getAllUploads, hasUploads } =
- usePendingUploads();
+ const {
+ getPendingUploadsForFolder,
+ getAllUploads,
+ hasUploads,
+ updatePendingUpload,
+ } = usePendingUploads();
const pendingUploadsForFolder = getPendingUploadsForFolder(folderId);
const allUploads = getAllUploads();
@@ -365,7 +369,7 @@ export default function DataroomViewer({
const filteredPendingUploads = useMemo(
() =>
pendingUploadsForFolder.filter((u) => {
- if (u.status !== "complete") return true;
+ if (!u.documentId) return true;
return !mixedItems.some(
(item) => "versions" in item && item.id === u.documentId,
);
@@ -373,6 +377,29 @@ export default function DataroomViewer({
[pendingUploadsForFolder, mixedItems],
);
+ // Fallback reconciliation: if the document is already visible and ready,
+ // mark its pending upload as complete even if realtime status was missed.
+ useEffect(() => {
+ allUploads.forEach((upload) => {
+ if (upload.status !== "processing" || !upload.documentId) return;
+
+ const matchingDocument = documents.find((doc) => doc.id === upload.documentId);
+ if (!matchingDocument) return;
+
+ const primaryVersion = matchingDocument.versions[0];
+ if (!primaryVersion) return;
+
+ const needsProcessing = ["pdf", "docs", "slides"].includes(
+ primaryVersion.type,
+ );
+ const isReady = !needsProcessing || primaryVersion.hasPages;
+
+ if (isReady) {
+ updatePendingUpload(upload.id, { status: "complete" });
+ }
+ });
+ }, [allUploads, documents, updatePendingUpload]);
+
const renderItem = (item: FolderOrDocument) => {
if ("versions" in item) {
const isProcessing =
diff --git a/components/viewer-upload-component.tsx b/components/viewer-upload-component.tsx
index 3d4d6c83..de628a4d 100644
--- a/components/viewer-upload-component.tsx
+++ b/components/viewer-upload-component.tsx
@@ -23,7 +23,7 @@ export function ViewerUploadComponent({
onUploadSuccess?: () => void;
}) {
const [uploads, setUploads] = useState<
- { fileName: string; progress: number }[]
+ { uploadId: string; fileName: string; progress: number }[]
>([]);
const [rejectedFiles, setRejectedFiles] = useState<
{ fileName: string; message: string }[]
@@ -31,24 +31,35 @@ export function ViewerUploadComponent({
const { addPendingUpload, updatePendingUpload } = usePendingUploads();
- // 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);
+ // Map each active upload item to its pending upload record
+ const pendingUploadIds = useRef<Map<string, string>>(new Map());
+ const activeUploadIds = useRef<Set<string>>(new Set());
+ const failedCountRef = useRef(0);
+
+ const finalizeSessionIfIdle = () => {
+ if (activeUploadIds.current.size > 0) return;
+ const hasFailures = failedCountRef.current > 0;
+ failedCountRef.current = 0;
+
+ if (!hasFailures) {
+ onUploadSuccess?.();
+ }
+ };
const handleUploadStart = (
- newUploads: { fileName: string; progress: number }[],
+ newUploads: { uploadId: string; fileName: string; progress: number }[],
) => {
- setUploads(newUploads);
- setRejectedFiles([]);
-
- expectedCountRef.current = newUploads.length;
- completedCountRef.current = 0;
- pendingUploadIds.current.clear();
+ const isStartingFreshSession = activeUploadIds.current.size === 0;
+ if (isStartingFreshSession) {
+ failedCountRef.current = 0;
+ setRejectedFiles([]);
+ }
+ setUploads((prev) => [...prev, ...newUploads]);
- newUploads.forEach((upload, index) => {
+ newUploads.forEach((upload) => {
+ activeUploadIds.current.add(upload.uploadId);
const pendingId = newId("pending");
- pendingUploadIds.current.set(index, pendingId);
+ pendingUploadIds.current.set(upload.uploadId, pendingId);
addPendingUpload({
id: pendingId,
@@ -61,26 +72,37 @@ export function ViewerUploadComponent({
});
};
- const handleUploadProgress = (index: number, progress: number) => {
+ const handleUploadProgress = (uploadId: string, progress: number) => {
setUploads((prev) => {
- const updated = [...prev];
- if (updated[index]) {
- updated[index] = { ...updated[index], progress };
-
- const pendingId = pendingUploadIds.current.get(index);
- if (pendingId) {
- updatePendingUpload(pendingId, { progress });
- }
+ const updated = prev.map((upload) =>
+ upload.uploadId === uploadId ? { ...upload, progress } : upload,
+ );
+
+ const pendingId = pendingUploadIds.current.get(uploadId);
+ if (pendingId) {
+ updatePendingUpload(pendingId, { progress });
}
+
return updated;
});
};
+ const settleUpload = (uploadId: string, failed: boolean) => {
+ if (failed) {
+ failedCountRef.current += 1;
+ }
+
+ activeUploadIds.current.delete(uploadId);
+ pendingUploadIds.current.delete(uploadId);
+ setUploads((prev) => prev.filter((upload) => upload.uploadId !== uploadId));
+ finalizeSessionIfIdle();
+ };
+
const handleUploadComplete = async (
documentData: DocumentData,
- index: number,
+ uploadId: string,
) => {
- const pendingId = pendingUploadIds.current.get(index);
+ const pendingId = pendingUploadIds.current.get(uploadId);
// Update status to processing (file uploaded to S3, now being processed by backend)
if (pendingId) {
@@ -132,14 +154,7 @@ export function ViewerUploadComponent({
});
}
- completedCountRef.current += 1;
- if (completedCountRef.current >= expectedCountRef.current) {
- setUploads([]);
- pendingUploadIds.current.clear();
- expectedCountRef.current = 0;
- completedCountRef.current = 0;
- onUploadSuccess?.();
- }
+ settleUpload(uploadId, false);
} catch (error) {
console.error("Error processing upload:", error);
toast.error((error as Error).message || "Failed to upload document");
@@ -151,14 +166,7 @@ export function ViewerUploadComponent({
});
}
- completedCountRef.current += 1;
- if (completedCountRef.current >= expectedCountRef.current) {
- setUploads([]);
- pendingUploadIds.current.clear();
- expectedCountRef.current = 0;
- completedCountRef.current = 0;
- onUploadSuccess?.();
- }
+ settleUpload(uploadId, true);
}
};
@@ -175,9 +183,9 @@ export function ViewerUploadComponent({
>
{isUploading ? (
<div className="space-y-3">
- {uploads.map((upload, index) => (
+ {uploads.map((upload) => (
<div
- key={index}
+ key={upload.uploadId}
className="flex items-center gap-3 rounded-lg border border-gray-200 bg-gray-50 p-3 dark:border-gray-700 dark:bg-gray-800"
>
<FileUp className="h-5 w-5 shrink-0 text-muted-foreground" />
diff --git a/components/viewer-upload-zone.tsx b/components/viewer-upload-zone.tsx
index 6b444f7c..a249ce43 100644
--- a/components/viewer-upload-zone.tsx
+++ b/components/viewer-upload-zone.tsx
@@ -1,4 +1,4 @@
-import { useCallback, useRef, useState } from "react";
+import { useCallback } from "react";
import { DocumentStorageType } from "@prisma/client";
import { FileRejection, useDropzone } from "react-dropzone";
@@ -7,6 +7,7 @@ import { toast } from "sonner";
import { VIEWER_ACCEPTED_FILE_TYPES } from "@/lib/constants";
import { DocumentData } from "@/lib/documents/create-document";
import { viewerUpload } from "@/lib/files/viewer-tus-upload";
+import { newId } from "@/lib/id-helper";
import { cn } from "@/lib/utils";
import { getSupportedContentType } from "@/lib/utils/get-content-type";
import { getPagesCount } from "@/lib/utils/get-page-number-count";
@@ -23,11 +24,14 @@ export default function ViewerUploadZone({
viewerData,
teamId,
maxFileSize = 30, // 30 MB default
+ disabled = false,
}: {
children: React.ReactNode;
- onUploadStart: (uploads: { fileName: string; progress: number }[]) => void;
- onUploadProgress: (index: number, progress: number) => void;
- onUploadComplete: (documentData: DocumentData, index: number) => void;
+ onUploadStart: (
+ uploads: { uploadId: string; fileName: string; progress: number }[],
+ ) => void;
+ onUploadProgress: (uploadId: string, progress: number) => void;
+ onUploadComplete: (documentData: DocumentData, uploadId: string) => void;
onUploadRejected: (rejected: { fileName: string; message: string }[]) => void;
viewerData: {
id: string;
@@ -36,10 +40,8 @@ export default function ViewerUploadZone({
};
teamId: string;
maxFileSize?: number;
+ disabled?: boolean;
}) {
- const [progress, setProgress] = useState<number>(0);
- const uploadProgress = useRef<number[]>([]);
-
const onDropRejected = useCallback(
(rejectedFiles: FileRejection[]) => {
const rejected = rejectedFiles.map(({ file, errors }) => {
@@ -58,14 +60,20 @@ export default function ViewerUploadZone({
const onDrop = useCallback(
async (acceptedFiles: File[]) => {
- const newUploads = acceptedFiles.map((file) => ({
+ const trackedFiles = acceptedFiles.map((file) => ({
+ uploadId: newId("pending"),
+ file,
+ }));
+
+ const newUploads = trackedFiles.map(({ uploadId, file }) => ({
+ uploadId,
fileName: file.name,
progress: 0,
}));
onUploadStart(newUploads);
- const uploadPromises = acceptedFiles.map(async (file, index) => {
+ const uploadPromises = trackedFiles.map(async ({ uploadId, file }) => {
// count the number of pages in the file
let numPages = 1;
if (file.type === "application/pdf") {
@@ -76,18 +84,11 @@ export default function ViewerUploadZone({
const { complete } = await viewerUpload({
file,
onProgress: (bytesUploaded, bytesTotal) => {
- uploadProgress.current[index] = (bytesUploaded / bytesTotal) * 100;
- onUploadProgress(
- index,
- Math.min(Math.round(uploadProgress.current[index]), 99),
+ const progress = Math.min(
+ Math.round((bytesUploaded / bytesTotal) * 100),
+ 99,
);
-
- const _progress = uploadProgress.current.reduce(
- (acc, progress) => acc + progress,
- 0,
- );
-
- setProgress(Math.round(_progress / acceptedFiles.length));
+ onUploadProgress(uploadId, progress);
},
onError: (error) => {
console.error("Upload error:", error);
@@ -134,9 +135,9 @@ export default function ViewerUploadZone({
numPages: numPages,
};
- onUploadComplete(documentData, index);
+ onUploadComplete(documentData, uploadId);
- onUploadProgress(index, 100); // Mark upload as complete
+ onUploadProgress(uploadId, 100); // Mark upload as complete
return uploadResult;
});
@@ -158,6 +159,7 @@ export default function ViewerUploadZone({
maxSize: maxFileSize * 1024 * 1024,
onDrop,
onDropRejected,
+ disabled,
});
return (