Skip to content

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

feat: improve upload notificaiton

Marc Seitzaa54ca6d428e
diff --git a/components/upload-notification.tsx b/components/upload-notification.tsx
index b14d3b3b..9bd5b70b 100644
--- a/components/upload-notification.tsx
+++ b/components/upload-notification.tsx
@@ -61,13 +61,20 @@ const ItemRow = memo(
const [hovered, setHovered] = useState(false);
const done =
item.completedEntries + item.failedEntries >= item.totalEntries;
- const progress =
- item.totalEntries > 0
- ? Math.round(
- ((item.completedEntries + item.failedEntries) / item.totalEntries) *
- 100,
+ const progress = done
+ ? 100
+ : item.bytesTotal && item.bytesTotal > 0
+ ? Math.min(
+ Math.round(((item.bytesUploaded ?? 0) / item.bytesTotal) * 100),
+ 99,
)
- : 0;
+ : item.totalEntries > 0
+ ? Math.round(
+ ((item.completedEntries + item.failedEntries) /
+ item.totalEntries) *
+ 100,
+ )
+ : 0;
const indicator = () => {
if (item.cancelled) {
@@ -140,7 +147,17 @@ const ItemRow = memo(
) : (
<FileIcon className="h-5 w-5 shrink-0 text-muted-foreground" />
)}
- <span className="truncate text-sm">{item.name}</span>
+ {done && item.folderHref ? (
+ <a
+ href={item.folderHref}
+ className="truncate text-sm hover:underline"
+ title={`Open ${item.name}`}
+ >
+ {item.name}
+ </a>
+ ) : (
+ <span className="truncate text-sm">{item.name}</span>
+ )}
</div>
<div className="flex shrink-0 items-center gap-2.5">
{!item.cancelled &&
@@ -173,6 +190,8 @@ const ItemRow = memo(
a.failedEntries === b.failedEntries &&
a.totalEntries === b.totalEntries &&
a.cancelled === b.cancelled &&
+ a.bytesUploaded === b.bytesUploaded &&
+ a.folderHref === b.folderHref &&
prev.isBatchDone === next.isBatchDone &&
prev.onCancelItem === next.onCancelItem
);
@@ -352,6 +371,19 @@ export function UploadNotificationDrawer({
const timeLeftLabel = useMemo(() => {
if (!batch || isDone) return null;
const elapsed = Date.now() - batch.startedAt;
+
+ // Prefer byte-level progress for accurate time estimation (critical for single large files)
+ const batchBytesTotal = batch.bytesTotal ?? 0;
+ const batchBytesUploaded = batch.bytesUploaded ?? 0;
+ if (batchBytesTotal > 0 && batchBytesUploaded > 0) {
+ if (elapsed < 2000) return "Calculating...";
+ const rate = batchBytesUploaded / elapsed;
+ const remaining = batchBytesTotal - batchBytesUploaded;
+ const msLeft = remaining / rate;
+ return formatTimeLeft(msLeft);
+ }
+
+ // Fallback to entry-based estimation
const processed = completedEntries + batchFailedEntries;
if (processed === 0 || elapsed < 2000) return "Calculating...";
const rate = processed / elapsed;
diff --git a/components/upload-zone.tsx b/components/upload-zone.tsx
index 690fd2d6..2007b7f1 100644
--- a/components/upload-zone.tsx
+++ b/components/upload-zone.tsx
@@ -74,6 +74,9 @@ export interface UploadItemState {
cancelled?: boolean;
/** Pre-computed link for completed folder items */
folderHref?: string;
+ /** Byte-level upload progress for granular tracking (especially single large files) */
+ bytesUploaded?: number;
+ bytesTotal?: number;
}
export interface UploadBatchState {
@@ -85,6 +88,9 @@ export interface UploadBatchState {
completedEntries: number;
failedEntries: number;
cancelled?: boolean;
+ /** Aggregate byte-level progress across all items */
+ bytesUploaded?: number;
+ bytesTotal?: number;
}
const UPLOAD_CONCURRENCY = 5;
@@ -460,6 +466,8 @@ export default function UploadZone({
: `/documents/tree/${group.folderSlugPath}`;
}
+ const groupBytesTotal = group.files.reduce((sum, f) => sum + f.size, 0);
+
return {
itemId: crypto.randomUUID(),
name: group.name,
@@ -468,6 +476,8 @@ export default function UploadZone({
completedEntries: folderCount,
failedEntries: 0,
folderHref,
+ bytesUploaded: 0,
+ bytesTotal: groupBytesTotal,
};
},
);
@@ -480,6 +490,8 @@ export default function UploadZone({
// Folders created during traversal count as completed entries
completedEntries: items.reduce((s, it) => s + it.completedEntries, 0),
failedEntries: 0,
+ bytesUploaded: 0,
+ bytesTotal: items.reduce((s, it) => s + (it.bytesTotal ?? 0), 0),
};
const dropCancelled = { current: false };
@@ -500,9 +512,26 @@ export default function UploadZone({
let completedCount = batch.completedEntries;
let failedCount = 0;
+ // Per-file byte tracking for granular progress
+ const fileBytesUploaded = new Map<FileWithPaths, number>();
+ const itemFilesMap = new Map<UploadItemState, FileWithPaths[]>();
+ for (const [file, item] of fileToItem) {
+ fileBytesUploaded.set(file, 0);
+ const files = itemFilesMap.get(item) ?? [];
+ files.push(file);
+ itemFilesMap.set(item, files);
+ }
+
const emitUpdate = () => {
onUploadBatchUpdate(batchId, {
- items: items.map((it) => ({ ...it })),
+ items: items.map((it) => {
+ const filesInItem = itemFilesMap.get(it) ?? [];
+ let uploaded = 0;
+ for (const f of filesInItem) {
+ uploaded += fileBytesUploaded.get(f) ?? 0;
+ }
+ return { ...it, bytesUploaded: uploaded };
+ }),
completedEntries: completedCount,
failedEntries: failedCount,
});
@@ -542,11 +571,8 @@ export default function UploadZone({
const { complete } = await resumableUpload({
file,
- onProgress: (bytesUploaded, bytesTotal) => {
- const _progress = Math.min(
- Math.round((bytesUploaded / bytesTotal) * 100),
- 99,
- );
+ onProgress: (bytesUploaded, _bytesTotal) => {
+ fileBytesUploaded.set(file, bytesUploaded);
emitUpdate();
},
onError: () => {
@@ -660,6 +686,10 @@ export default function UploadZone({
completedCount++;
parentItem.completedEntries++;
+ fileBytesUploaded.set(file, file.size);
+ if (!parentItem.folderHref && document.id) {
+ parentItem.folderHref = `/documents/${document.id}`;
+ }
emitUpdate();
analytics.capture("Document Added", {
diff --git a/context/upload-progress-context.tsx b/context/upload-progress-context.tsx
index 364a344c..39610c18 100644
--- a/context/upload-progress-context.tsx
+++ b/context/upload-progress-context.tsx
@@ -34,6 +34,19 @@ const UploadProgressContext = createContext<UploadProgressContextType | null>(
null,
);
+function getActiveUploadByteTotals(items: UploadItemState[]) {
+ let bytesUploaded = 0;
+ let bytesTotal = 0;
+
+ for (const item of items) {
+ if (item.cancelled) continue;
+ bytesUploaded += item.bytesUploaded ?? 0;
+ bytesTotal += item.bytesTotal ?? 0;
+ }
+
+ return { bytesUploaded, bytesTotal };
+}
+
export function UploadProgressProvider({
children,
}: {
@@ -76,10 +89,14 @@ export function UploadProgressProvider({
}
return it;
});
+ const { bytesUploaded, bytesTotal } =
+ getActiveUploadByteTotals(updatedItems);
return {
...prev,
cancelled: true,
completedEntries: prev.completedEntries + addedCompleted,
+ bytesUploaded,
+ bytesTotal,
items: updatedItems,
};
});
@@ -97,18 +114,23 @@ export function UploadProgressProvider({
if (!target || target.cancelled) return prev;
const remaining =
target.totalEntries - target.completedEntries - target.failedEntries;
+ const updatedItems = prev.items.map((it) =>
+ it.itemId === itemId
+ ? ({
+ ...it,
+ cancelled: true,
+ completedEntries: it.totalEntries - it.failedEntries,
+ } as UploadItemState)
+ : it,
+ );
+ const { bytesUploaded, bytesTotal } =
+ getActiveUploadByteTotals(updatedItems);
return {
...prev,
completedEntries: prev.completedEntries + remaining,
- items: prev.items.map((it) =>
- it.itemId === itemId
- ? ({
- ...it,
- cancelled: true,
- completedEntries: it.totalEntries - it.failedEntries,
- } as UploadItemState)
- : it,
- ),
+ bytesUploaded,
+ bytesTotal,
+ items: updatedItems,
};
});
}, []);
@@ -262,6 +284,8 @@ export function useUploadCallbacks() {
completedEntries:
prev.completedEntries + batch.completedEntries,
failedEntries: prev.failedEntries + batch.failedEntries,
+ bytesUploaded: (prev.bytesUploaded ?? 0) + (batch.bytesUploaded ?? 0),
+ bytesTotal: (prev.bytesTotal ?? 0) + (batch.bytesTotal ?? 0),
};
}
return batch;
@@ -296,12 +320,16 @@ export function useUploadCallbacks() {
completedEntries += it.completedEntries;
failedEntries += it.failedEntries;
}
+ const { bytesUploaded, bytesTotal } =
+ getActiveUploadByteTotals(mergedItems);
return {
...prev,
items: mergedItems,
totalEntries,
completedEntries,
failedEntries,
+ bytesUploaded,
+ bytesTotal,
};
});
},