Skip to content

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

feat: improvements

Marc Seitze554654eba50
diff --git a/components/documents/document-preview-modal.tsx b/components/documents/document-preview-modal.tsx
index db4c10f2..914fef7c 100644
--- a/components/documents/document-preview-modal.tsx
+++ b/components/documents/document-preview-modal.tsx
@@ -26,7 +26,6 @@ export function DocumentPreviewModal({
document: documentData,
loading,
error,
- mutate,
} = useDocumentPreview(documentId, isOpen);
const handleClose = () => {
@@ -44,8 +43,6 @@ export function DocumentPreviewModal({
e.stopPropagation();
};
- console.log("error", error);
-
return (
<Dialog open={isOpen} onOpenChange={handleClose}>
<DialogContent
@@ -81,19 +78,7 @@ export function DocumentPreviewModal({
<div className="flex h-full w-full items-center justify-center">
<div className="text-center">
<p className="text-red-400">
- {(() => {
- try {
- const errorData = JSON.parse((error as Error).message);
- return (
- errorData.message || "Failed to load document preview"
- );
- } catch {
- return (
- (error as Error).message ||
- "Failed to load document preview"
- );
- }
- })()}
+ {(error as Error).message || "Failed to load document preview"}
</p>
</div>
</div>
diff --git a/components/documents/preview-viewers/preview-image-viewer.tsx b/components/documents/preview-viewers/preview-image-viewer.tsx
index 44a1ddb3..6a786bff 100644
--- a/components/documents/preview-viewers/preview-image-viewer.tsx
+++ b/components/documents/preview-viewers/preview-image-viewer.tsx
@@ -1,25 +1,8 @@
import { useState } from "react";
+import { DocumentPreviewData } from "@/lib/types/document-preview";
import { cn } from "@/lib/utils";
-interface DocumentPreviewData {
- documentId: string;
- documentName: string;
- documentType: string;
- fileType: string;
- isVertical: boolean;
- numPages: number;
- pages?: {
- file: string;
- pageNumber: string;
- embeddedLinks: string[];
- pageLinks: { href: string; coords: string }[];
- metadata: { width: number; height: number; scaleFactor: number };
- }[];
- file?: string;
- sheetData?: any;
-}
-
interface PreviewImageViewerProps {
documentData: DocumentPreviewData;
onClose: () => void;
diff --git a/components/documents/preview-viewers/preview-pages-viewer.tsx b/components/documents/preview-viewers/preview-pages-viewer.tsx
index 9c9a7358..c8eed104 100644
--- a/components/documents/preview-viewers/preview-pages-viewer.tsx
+++ b/components/documents/preview-viewers/preview-pages-viewer.tsx
@@ -1,29 +1,12 @@
-import { useEffect, useState } from "react";
+import { useCallback, useEffect, useState } from "react";
import { ChevronLeftIcon, ChevronRightIcon } from "lucide-react";
+import { DocumentPreviewData } from "@/lib/types/document-preview";
import { cn } from "@/lib/utils";
import { Button } from "@/components/ui/button";
-interface DocumentPreviewData {
- documentId: string;
- documentName: string;
- documentType: string;
- fileType: string;
- isVertical: boolean;
- numPages: number;
- pages?: {
- file: string;
- pageNumber: string;
- embeddedLinks: string[];
- pageLinks: { href: string; coords: string }[];
- metadata: { width: number; height: number; scaleFactor: number };
- }[];
- file?: string;
- sheetData?: any;
-}
-
interface PreviewPagesViewerProps {
documentData: DocumentPreviewData;
onClose: () => void;
@@ -39,15 +22,19 @@ export function PreviewPagesViewer({
const { pages, numPages, documentName, isVertical } = documentData;
- if (!pages || pages.length === 0) {
- return (
- <div className="flex h-full w-full items-center justify-center">
- <p className="text-gray-400">No pages available for preview</p>
- </div>
- );
- }
+ const goToNextPage = useCallback(() => {
+ if (currentPage < numPages) {
+ setCurrentPage(currentPage + 1);
+ setImageLoaded(imageCache[currentPage + 1] || false);
+ }
+ }, [currentPage, numPages, imageCache]);
- const currentPageData = pages[currentPage - 1];
+ const goToPreviousPage = useCallback(() => {
+ if (currentPage > 1) {
+ setCurrentPage(currentPage - 1);
+ setImageLoaded(imageCache[currentPage - 1] || false);
+ }
+ }, [currentPage, numPages, imageCache]);
useEffect(() => {
const handleKeyDown = (e: KeyboardEvent) => {
@@ -66,21 +53,17 @@ export function PreviewPagesViewer({
document.addEventListener("keydown", handleKeyDown);
return () => document.removeEventListener("keydown", handleKeyDown);
- }, [currentPage, onClose]);
+ }, [goToPreviousPage, goToNextPage, onClose]);
- const goToNextPage = () => {
- if (currentPage < numPages) {
- setCurrentPage(currentPage + 1);
- setImageLoaded(imageCache[currentPage + 1] || false);
- }
- };
+ if (!pages || pages.length === 0) {
+ return (
+ <div className="flex h-full w-full items-center justify-center">
+ <p className="text-gray-400">No pages available for preview</p>
+ </div>
+ );
+ }
- const goToPreviousPage = () => {
- if (currentPage > 1) {
- setCurrentPage(currentPage - 1);
- setImageLoaded(imageCache[currentPage - 1] || false);
- }
- };
+ const currentPageData = pages[currentPage - 1];
const handleImageLoad = () => {
setImageLoaded(true);
diff --git a/components/documents/preview-viewers/preview-viewer.tsx b/components/documents/preview-viewers/preview-viewer.tsx
index b20bb946..491393c8 100644
--- a/components/documents/preview-viewers/preview-viewer.tsx
+++ b/components/documents/preview-viewers/preview-viewer.tsx
@@ -1,24 +1,8 @@
+import { DocumentPreviewData } from "@/lib/types/document-preview";
+
import { PreviewImageViewer } from "./preview-image-viewer";
import { PreviewPagesViewer } from "./preview-pages-viewer";
-interface DocumentPreviewData {
- documentId: string;
- documentName: string;
- documentType: string;
- fileType: string;
- isVertical: boolean;
- numPages: number;
- pages?: {
- file: string;
- pageNumber: string;
- embeddedLinks: string[];
- pageLinks: { href: string; coords: string }[];
- metadata: { width: number; height: number; scaleFactor: number };
- }[];
- file?: string;
- sheetData?: any;
-}
-
interface PreviewViewerProps {
documentData: DocumentPreviewData;
onClose: () => void;
@@ -77,6 +61,8 @@ export function PreviewViewer({ documentData, onClose }: PreviewViewerProps) {
};
return (
- <div className="relative h-full w-full bg-gray-900">{renderViewer()}</div>
+ <div className="relative h-full w-full rounded-lg bg-gray-900">
+ {renderViewer()}
+ </div>
);
}
diff --git a/components/ui/dialog.tsx b/components/ui/dialog.tsx
index 55e882ea..5cc0e01c 100644
--- a/components/ui/dialog.tsx
+++ b/components/ui/dialog.tsx
@@ -25,7 +25,7 @@ const DialogPortal = ({
<div
className={cn(
"fixed inset-0 z-50 flex items-end justify-center sm:items-center",
- isPreviewDialog ? "items-center" : "items-end",
+ isPreviewDialog && "items-center",
)}
>
{children}
diff --git a/lib/swr/use-document-preview.ts b/lib/swr/use-document-preview.ts
index a26fa939..f192fb21 100644
--- a/lib/swr/use-document-preview.ts
+++ b/lib/swr/use-document-preview.ts
@@ -1,25 +1,8 @@
import useSWRImmutable from "swr/immutable";
+import { DocumentPreviewData } from "@/lib/types/document-preview";
import { fetcher } from "@/lib/utils";
-interface DocumentPreviewData {
- documentId: string;
- documentName: string;
- documentType: string;
- fileType: string;
- isVertical: boolean;
- numPages: number;
- pages?: {
- file: string;
- pageNumber: string;
- embeddedLinks: string[];
- pageLinks: { href: string; coords: string }[];
- metadata: { width: number; height: number; scaleFactor: number };
- }[];
- file?: string;
- sheetData?: any;
-}
-
export function useDocumentPreview(documentId: string, isOpen: boolean) {
const {
data: document,
diff --git a/lib/types/document-preview.ts b/lib/types/document-preview.ts
new file mode 100644
index 00000000..e66dc1da
--- /dev/null
+++ b/lib/types/document-preview.ts
@@ -0,0 +1,17 @@
+export interface DocumentPreviewData {
+ documentId: string;
+ documentName: string;
+ documentType: string;
+ fileType: string;
+ isVertical: boolean;
+ numPages: number;
+ pages?: {
+ file: string;
+ pageNumber: string;
+ embeddedLinks: string[];
+ pageLinks: { href: string; coords: string }[];
+ metadata: { width: number; height: number; scaleFactor: number };
+ }[];
+ file?: string;
+ sheetData?: any;
+}