Skip to content

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

feat: add processing progress status to document

Marc Seitz778f915863fa
diff --git a/components/documents/process-status-bar.tsx b/components/documents/process-status-bar.tsx
new file mode 100644
index 00000000..465ad022
--- /dev/null
+++ b/components/documents/process-status-bar.tsx
@@ -0,0 +1,37 @@
+import { Progress } from "@/components/ui/progress";
+import ErrorPage from "next/error";
+import { useDocumentProcessingStatus } from "@/lib/swr/use-document";
+import { useEffect, useState } from "react";
+
+export default function ProcessStatusBar({ documentVersionId }: { documentVersionId: string }) {
+ const { status, loading, error } =
+ useDocumentProcessingStatus(documentVersionId);
+
+ const [progress, setProgress] = useState<number>(0);
+ const [text, setText] = useState<string>("");
+
+ useEffect(() => {
+ if (status) {
+ const progress = (status.currentPageCount / status.totalPages) * 100;
+ setProgress(progress);
+ if (progress === 100) {
+ setText("Processing complete");
+ } else {
+ setText(
+ `${status.currentPageCount} / ${status.totalPages} pages processed`
+ );
+ }
+ }
+ }, [status]);
+
+
+ if (error && error.status === 404) {
+ return <ErrorPage statusCode={404} />;
+ }
+
+ if (loading) {
+ return <Progress value={0} />
+ }
+
+ return <Progress value={progress} text={text} className="w-[60%]" />;
+}
diff --git a/components/ui/progress.tsx b/components/ui/progress.tsx
new file mode 100644
index 00000000..f174a1f6
--- /dev/null
+++ b/components/ui/progress.tsx
@@ -0,0 +1,49 @@
+import * as React from "react"
+import * as ProgressPrimitive from "@radix-ui/react-progress"
+
+import { cn } from "@/lib/utils"
+
+const Progress = React.forwardRef<
+ React.ElementRef<typeof ProgressPrimitive.Root>,
+ React.ComponentPropsWithoutRef<typeof ProgressPrimitive.Root> & {
+ text?: string;
+ }
+>(({ className, value, text, ...props }, ref) => {
+
+const textColor = `linear-gradient(to right,
+ text-background ${value || 0}%,
+ text-foreground ${value || 0}%
+)`;
+
+
+return (
+ <ProgressPrimitive.Root
+ ref={ref}
+ className={cn(
+ "relative h-4 w-full overflow-hidden rounded-full bg-secondary",
+ className
+ )}
+ {...props}
+ >
+ <ProgressPrimitive.Indicator
+ className="h-full w-full flex-1 bg-primary transition-all"
+ style={{ transform: `translateX(-${100 - (value || 0)}%)` }}
+ />
+ {text ? (
+ <div className="absolute inset-0 flex py-2 items-center justify-center">
+ <div className="overflow-hidden absolute inset-0 flex items-center justify-center">
+ <span className="text-xs text-foreground">{text}</span>
+ </div>
+ <div
+ className="absolute inset-0 flex items-center justify-center"
+ style={{ clipPath: `inset(0 ${100 - (value || 0)}% 0 0)` }}
+ >
+ <span className="text-xs text-background">{text}</span>
+ </div>
+ </div>
+ ) : null}
+ </ProgressPrimitive.Root>
+);});
+Progress.displayName = ProgressPrimitive.Root.displayName
+
+export { Progress }
diff --git a/lib/swr/use-document.ts b/lib/swr/use-document.ts
index f4b3fb80..bfe9c088 100644
--- a/lib/swr/use-document.ts
+++ b/lib/swr/use-document.ts
@@ -94,3 +94,27 @@ export function useDocumentVisits() {
error,
};
}
+
+interface DocumentProcessingStatus {
+ currentPageCount: number;
+ totalPages: number;
+ hasPages: boolean;
+}
+
+export function useDocumentProcessingStatus(documentVersionId: string) {
+ const teamInfo = useTeam();
+
+ const { data: status, error } = useSWR<DocumentProcessingStatus>(
+ `/api/teams/${teamInfo?.currentTeam?.id}/documents/document-processing-status?documentVersionId=${documentVersionId}`,
+ fetcher,
+ {
+ refreshInterval: 3000, // refresh every 3 seconds
+ }
+ );
+
+ return {
+ status: status,
+ loading: !error && !status,
+ error: error,
+ };
+}
\ No newline at end of file
diff --git a/package-lock.json b/package-lock.json
index b3a060ce..388dbfc7 100644
--- a/package-lock.json
+++ b/package-lock.json
@@ -21,6 +21,7 @@
"@radix-ui/react-dialog": "^1.0.5",
"@radix-ui/react-dropdown-menu": "^2.0.6",
"@radix-ui/react-label": "^2.0.2",
+ "@radix-ui/react-progress": "^1.0.3",
"@radix-ui/react-select": "^1.2.2",
"@radix-ui/react-separator": "^1.0.3",
"@radix-ui/react-slot": "^1.0.2",
@@ -2272,6 +2273,30 @@
}
}
},
+ "node_modules/@radix-ui/react-progress": {
+ "version": "1.0.3",
+ "resolved": "https://registry.npmjs.org/@radix-ui/react-progress/-/react-progress-1.0.3.tgz",
+ "integrity": "sha512-5G6Om/tYSxjSeEdrb1VfKkfZfn/1IlPWd731h2RfPuSbIfNUgfqAwbKfJCg/PP6nuUCTrYzalwHSpSinoWoCag==",
+ "dependencies": {
+ "@babel/runtime": "^7.13.10",
+ "@radix-ui/react-context": "1.0.1",
+ "@radix-ui/react-primitive": "1.0.3"
+ },
+ "peerDependencies": {
+ "@types/react": "*",
+ "@types/react-dom": "*",
+ "react": "^16.8 || ^17.0 || ^18.0",
+ "react-dom": "^16.8 || ^17.0 || ^18.0"
+ },
+ "peerDependenciesMeta": {
+ "@types/react": {
+ "optional": true
+ },
+ "@types/react-dom": {
+ "optional": true
+ }
+ }
+ },
"node_modules/@radix-ui/react-roving-focus": {
"version": "1.0.4",
"resolved": "https://registry.npmjs.org/@radix-ui/react-roving-focus/-/react-roving-focus-1.0.4.tgz",
@@ -12580,6 +12605,16 @@
"@radix-ui/react-slot": "1.0.2"
}
},
+ "@radix-ui/react-progress": {
+ "version": "1.0.3",
+ "resolved": "https://registry.npmjs.org/@radix-ui/react-progress/-/react-progress-1.0.3.tgz",
+ "integrity": "sha512-5G6Om/tYSxjSeEdrb1VfKkfZfn/1IlPWd731h2RfPuSbIfNUgfqAwbKfJCg/PP6nuUCTrYzalwHSpSinoWoCag==",
+ "requires": {
+ "@babel/runtime": "^7.13.10",
+ "@radix-ui/react-context": "1.0.1",
+ "@radix-ui/react-primitive": "1.0.3"
+ }
+ },
"@radix-ui/react-roving-focus": {
"version": "1.0.4",
"resolved": "https://registry.npmjs.org/@radix-ui/react-roving-focus/-/react-roving-focus-1.0.4.tgz",
diff --git a/package.json b/package.json
index df63199b..dd2eb99d 100644
--- a/package.json
+++ b/package.json
@@ -28,6 +28,7 @@
"@radix-ui/react-dialog": "^1.0.5",
"@radix-ui/react-dropdown-menu": "^2.0.6",
"@radix-ui/react-label": "^2.0.2",
+ "@radix-ui/react-progress": "^1.0.3",
"@radix-ui/react-select": "^1.2.2",
"@radix-ui/react-separator": "^1.0.3",
"@radix-ui/react-slot": "^1.0.2",
diff --git a/pages/api/teams/[teamId]/documents/document-processing-status.ts b/pages/api/teams/[teamId]/documents/document-processing-status.ts
new file mode 100644
index 00000000..eab5c6cf
--- /dev/null
+++ b/pages/api/teams/[teamId]/documents/document-processing-status.ts
@@ -0,0 +1,27 @@
+import { NextApiRequest, NextApiResponse } from "next";
+import prisma from "@/lib/prisma";
+
+export default async function handler(req: NextApiRequest, res: NextApiResponse) {
+ const { documentVersionId } = req.query as { documentVersionId: string };
+
+ const documentVersion = await prisma.documentVersion.findUnique({
+ where: { id: documentVersionId },
+ select: {
+ numPages: true,
+ hasPages: true,
+ _count: { select: { pages: true } }
+ },
+ });
+
+ if (!documentVersion) {
+ return res.status(404).end();
+ }
+
+ const status = {
+ currentPageCount: documentVersion._count.pages,
+ totalPages: documentVersion.numPages,
+ hasPages: documentVersion.hasPages,
+ };
+
+ res.status(200).json(status);
+}
diff --git a/pages/documents/[id].tsx b/pages/documents/[id].tsx
index 99327aca..27f0a109 100644
--- a/pages/documents/[id].tsx
+++ b/pages/documents/[id].tsx
@@ -22,6 +22,7 @@ import {
import { useRouter } from "next/router";
import MoreVertical from "@/components/shared/icons/more-vertical";
import { useTeam } from "@/context/team-context";
+import ProcessStatusBar from "@/components/documents/process-status-bar";
export default function DocumentPage() {
const { document: prismaDocument, primaryVersion, error } = useDocument();
@@ -224,6 +225,13 @@ export default function DocumentPage() {
</Button>
</div>
</div>
+ {/* Progress bar */}
+ {!primaryVersion.hasPages ? (
+ <div className="flex flex-col items-start justify-between gap-x-8 gap-y-4 p-4 sm:flex-row sm:items-center sm:m-4">
+ <ProcessStatusBar documentVersionId={primaryVersion.id} />
+ </div>
+ ) : null}
+
{/* Stats */}
{prismaDocument.numPages !== null && (
<StatsChart