Papermark is the open-source DocSend alternative and secure data rooms with built-in analytics and custom domains.
feat: add error message handling and skipped files download
Marc Seitz4246385c2573
diff --git a/components/upload-notification.tsx b/components/upload-notification.tsxindex 88646693..c6bd3d68 100644--- a/components/upload-notification.tsx+++ b/components/upload-notification.tsx@@ -1,12 +1,16 @@-import { memo, useMemo, useState } from "react";+import { memo, useCallback, useMemo, useState } from "react";import {+ AlertTriangleIcon,BanIcon,CheckIcon,ChevronDownIcon,+ ChevronRightIcon,ChevronUpIcon,+ DownloadIcon,FileIcon,FolderIcon,+ XCircleIcon,XIcon,} from "lucide-react";@@ -18,6 +22,8 @@ import {DrawerTitle,} from "@/components/ui/drawer";+import { usePlan } from "@/lib/swr/use-billing";+import { Gauge } from "./ui/gauge";import { ButtonTooltip } from "./ui/tooltip";import { RejectedFile, UploadBatchState, UploadItemState } from "./upload-zone";@@ -166,6 +172,85 @@ const ItemRow = memo(},);+function CollapsibleSection({+ icon,+ label,+ count,+ colorClass,+ bgClass,+ cta,+ items,+ onDownload,+}: {+ icon: React.ReactNode;+ label: string;+ count: number;+ colorClass: string;+ bgClass: string;+ cta?: { label: string; href: string };+ items: RejectedFile[];+ onDownload?: () => void;+}) {+ const [open, setOpen] = useState(false);++ return (+ <div className={`border-t border-gray-200 dark:border-gray-700`}>+ <button+ onClick={() => setOpen(!open)}+ className={`flex w-full items-center justify-between px-4 py-2 ${bgClass}`}+ >+ <div className="flex items-center gap-2">+ {open ? (+ <ChevronDownIcon className={`h-3.5 w-3.5 ${colorClass}`} />+ ) : (+ <ChevronRightIcon className={`h-3.5 w-3.5 ${colorClass}`} />+ )}+ {icon}+ <span className={`text-xs font-medium ${colorClass}`}>+ {count} {label}+ </span>+ </div>+ <div className="flex items-center gap-2">+ {onDownload ? (+ <ButtonTooltip content="Download list" sideOffset={4}>+ <span+ role="button"+ onClick={(e) => {+ e.stopPropagation();+ onDownload();+ }}+ className={`rounded p-1 ${colorClass} hover:bg-black/5 dark:hover:bg-white/10`}+ >+ <DownloadIcon className="h-3.5 w-3.5" />+ </span>+ </ButtonTooltip>+ ) : null}+ {cta ? (+ <a+ href={cta.href}+ onClick={(e) => e.stopPropagation()}+ className={`rounded px-2.5 py-1 text-xs font-medium ${colorClass} hover:underline`}+ >+ {cta.label}+ </a>+ ) : null}+ </div>+ </button>+ {open+ ? items.map((rejected, index) => (+ <div+ key={index}+ className={`flex items-center justify-between px-4 py-2 text-sm ${colorClass} hover:bg-gray-50 dark:hover:bg-gray-800/50`}+ >+ <span className="w-56 truncate">{rejected.fileName}</span>+ <span className="shrink-0 text-xs">{rejected.message}</span>+ </div>+ ))+ : null}+ </div>+ );+}+export function UploadNotificationDrawer({open,onOpenChange,@@ -178,6 +263,7 @@ export function UploadNotificationDrawer({onCancelItem,}: UploadNotificationDrawerProps) {const [expanded, setExpanded] = useState(true);+ const { isDatarooms } = usePlan();const totalEntries = batch?.totalEntries ?? 0;const completedEntries = batch?.completedEntries ?? 0;@@ -192,6 +278,70 @@ export function UploadNotificationDrawer({const itemCount = batch?.items.length ?? 0;const isPreparing = batch === null || itemCount === 0;+ // js-combine-iterations: single pass to partition rejected files+ const { skippedFiles, failedFiles } = useMemo(() => {+ const skipped: RejectedFile[] = [];+ const failed: RejectedFile[] = [];+ for (const rf of rejectedFiles) {+ if (rf.reason === "plan-limit" || rf.reason === "max-files") {+ skipped.push(rf);+ } else {+ failed.push(rf);+ }+ }+ return { skippedFiles: skipped, failedFiles: failed };+ }, [rejectedFiles]);++ const skippedCta = useMemo(() => {+ if (skippedFiles.length === 0) return undefined;+ const hasPlanLimit = skippedFiles.some((f) => f.reason === "plan-limit");+ const hasMaxFiles = skippedFiles.some((f) => f.reason === "max-files");++ if (hasPlanLimit) {+ return { label: "Upgrade", href: "/settings/billing" };+ }+ if (hasMaxFiles && isDatarooms) {+ return {+ label: "Contact support",+ href: "mailto:[email protected]",+ };+ }+ return { label: "Upgrade", href: "/settings/billing" };+ }, [skippedFiles, isDatarooms]);++ const skippedLabel = useMemo(() => {+ const hasPlanLimit = skippedFiles.some((f) => f.reason === "plan-limit");+ return hasPlanLimit+ ? "not uploaded (plan limit)"+ : "not uploaded (upload limit)";+ }, [skippedFiles]);++ const handleDownloadSkipped = useCallback(() => {+ const allNames: string[] = [];+ for (const rf of skippedFiles) {+ if (rf.skippedFileNames?.length) {+ allNames.push(...rf.skippedFileNames);+ } else {+ allNames.push(rf.fileName);+ }+ }+ if (allNames.length === 0) return;++ const content = allNames.join("\n");+ const blob = new Blob([content], { type: "text/plain" });+ const url = URL.createObjectURL(blob);+ const a = document.createElement("a");+ a.href = url;+ a.download = "skipped-files.txt";+ a.click();+ URL.revokeObjectURL(url);+ }, [skippedFiles]);++ const hasDownloadableSkipped = useMemo(+ () => skippedFiles.some((rf) => rf.skippedFileNames?.length || rf.fileName),+ [skippedFiles],+ );+const timeLeftLabel = useMemo(() => {if (!batch || isDone) return null;const elapsed = Date.now() - batch.startedAt;@@ -211,7 +361,8 @@ export function UploadNotificationDrawer({}};- const cancelledItemCount = batch?.items.filter((it) => it.cancelled).length ?? 0;+ const cancelledItemCount =+ batch?.items.filter((it) => it.cancelled).length ?? 0;const headerTitle = isPreparing? "Preparing upload..."@@ -256,23 +407,23 @@ export function UploadNotificationDrawer({</div></DrawerHeader>- {expanded && (+ {expanded ? (<>- {!isDone && !isPreparing && timeLeftLabel && (+ {!isDone && !isPreparing && timeLeftLabel ? (<div className="flex items-center justify-between border-b border-gray-200 bg-blue-50/60 px-4 py-2 dark:border-gray-700 dark:bg-blue-950/30"><span className="text-xs text-muted-foreground">{timeLeftLabel}</span>- {onCancel && (+ {onCancel ? (<buttononClick={onCancel}className="text-xs font-medium text-primary hover:underline">Cancel</button>- )}+ ) : null}</div>- )}+ ) : null}<div className="flex w-full flex-1 flex-col overflow-y-auto">{batch?.items.map((item) => (@@ -285,26 +436,37 @@ export function UploadNotificationDrawer({))}</div>- {rejectedFiles.length > 0 && (- <div className="border-t border-gray-200 dark:border-gray-700">- <div className="px-4 py-1.5 text-xs font-medium text-destructive">- {rejectedFiles.length} failed- </div>- {rejectedFiles.map((rejected, index) => (- <div- key={index}- className="flex items-center justify-between px-4 py-2 text-sm text-red-500 hover:bg-gray-50 dark:hover:bg-gray-800/50"- >- <span className="w-56 truncate">{rejected.fileName}</span>- <span className="shrink-0 text-xs">- {rejected.message}- </span>- </div>- ))}- </div>- )}+ {skippedFiles.length > 0 ? (+ <CollapsibleSection+ icon={+ <AlertTriangleIcon className="h-3.5 w-3.5 text-amber-600 dark:text-amber-400" />+ }+ label={skippedLabel}+ count={skippedFiles.length}+ colorClass="text-amber-600 dark:text-amber-400"+ bgClass="bg-amber-50/60 dark:bg-amber-950/30"+ cta={skippedCta}+ items={skippedFiles}+ onDownload={+ hasDownloadableSkipped ? handleDownloadSkipped : undefined+ }+ />+ ) : null}++ {failedFiles.length > 0 ? (+ <CollapsibleSection+ icon={+ <XCircleIcon className="h-3.5 w-3.5 text-destructive" />+ }+ label="failed"+ count={failedFiles.length}+ colorClass="text-destructive"+ bgClass="bg-red-50/60 dark:bg-red-950/30"+ items={failedFiles}+ />+ ) : null}</>- )}+ ) : null}</DrawerContent></Drawer></div>diff --git a/components/upload-zone.tsx b/components/upload-zone.tsxindex cd2c0cd8..690fd2d6 100644--- a/components/upload-zone.tsx+++ b/components/upload-zone.tsx@@ -58,6 +58,9 @@ interface FileWithPaths extends File {export interface RejectedFile {fileName: string;message: string;+ reason?: "error" | "plan-limit" | "max-files";+ /** Individual file paths skipped due to limits — used for downloadable list */+ skippedFileNames?: string[];}export interface UploadItemState {@@ -284,6 +287,7 @@ export default function UploadZone({{fileName: `${rejectedFiles.length} files selected`,message: `Maximum ${maxFiles} files per upload`,+ reason: "max-files",},]);return;@@ -369,7 +373,8 @@ export default function UploadZone({setRejectedFiles((prev) => [...skippedFiles.map((f) => ({fileName: f.name,- message: "Skipped (document limit reached)",+ message: "Document limit reached",+ reason: "plan-limit" as const,})),...prev,]);@@ -771,6 +776,7 @@ export default function UploadZone({: Infinity;const fileLimit = Math.min(maxFilesPerUpload, planDocumentLimit);let collectedFileCount = 0;+ const skippedPerTopLevel = new Map<string, string[]>();// Early check: skip folder traversal (and folder creation) if document limit is already reachedif (fileLimit <= 0) {@@ -811,6 +817,7 @@ export default function UploadZone({dataroomParentPath?: string,folderCounter?: { count: number },folderPathCapture?: { value?: string },+ topLevelName?: string,): Promise<FileWithPaths[]> => {/*** Summary of this function:@@ -826,12 +833,7 @@ export default function UploadZone({}if (entry.isDirectory) {- /**- * Let's create the folder.- * Fact that reader can skip: For Consistency, child files will only be pushed if folder successfully gets created.- */try {- // An empty folder name can cause the unexpected url problems.if (entry.name.trim() === "") {setRejectedFiles((prev) => [{@@ -906,6 +908,7 @@ export default function UploadZone({undefined,folderCounter,folderPathCapture,+ topLevelName,)),);}@@ -975,6 +978,7 @@ export default function UploadZone({resolvedDataroomPath,folderCounter,folderPathCapture,+ topLevelName,)),);}@@ -998,6 +1002,11 @@ export default function UploadZone({}if (collectedFileCount >= fileLimit) {+ if (topLevelName) {+ const list = skippedPerTopLevel.get(topLevelName) ?? [];+ list.push(entry.fullPath.startsWith("/") ? entry.fullPath.substring(1) : entry.fullPath);+ skippedPerTopLevel.set(topLevelName, list);+ }return files;}@@ -1082,6 +1091,7 @@ export default function UploadZone({dataroomId ? folderPathName : undefined,counter,pathCapture,+ entry.name,).then((files) => {for (const f of files) {f.topLevelItemName = entry.name;@@ -1117,6 +1127,19 @@ export default function UploadZone({fileLimitTruncatedRef.current = true;}+ if (skippedPerTopLevel.size > 0) {+ const skippedEntries: RejectedFile[] = [];+ for (const [name, paths] of skippedPerTopLevel) {+ skippedEntries.push({+ fileName: `${name}: ${paths.length} file${paths.length !== 1 ? "s" : ""} not uploaded`,+ message: "Document limit reached",+ reason: "plan-limit",+ skippedFileNames: paths,+ });+ }+ setRejectedFiles((prev) => [...skippedEntries, ...prev]);+ }+return filesToBePassedToOnDrop;},[