Skip to content

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

Merge branch 'main' into feat/versions-full

Marc Seitz4c9e5c954b65
diff --cc components/documents/add-document-modal.tsx
index fe121bed,243df320..9ed61543
--- a/components/documents/add-document-modal.tsx
+++ b/components/documents/add-document-modal.tsx
@@@ -15,7 -15,7 +15,8 @@@ import { pdfjs } from "react-pdf"
import { copyToClipboard, getExtension } from "@/lib/utils";
import { Button } from "../ui/button";
import { usePlausible } from "next-plausible";
+import { toast } from "sonner";
+ import { useTeam } from "@/context/team-context";
pdfjs.GlobalWorkerOptions.workerSrc = `//cdnjs.cloudflare.com/ajax/libs/pdf.js/${pdfjs.version}/pdf.worker.js`;
diff --cc components/documents/stats-chart.tsx
index 53efa153,ba475da1..a626ffc1
--- a/components/documents/stats-chart.tsx
+++ b/components/documents/stats-chart.tsx
@@@ -5,12 -5,12 +5,12 @@@ import BarChartComponent from "../chart
export default function StatsChart({
documentId,
- totalPages = 0,
+ totalPagesMax = 0,
}: {
documentId: string;
- totalPages?: number;
+ totalPagesMax?: number;
}) {
- const { stats, error } = useStats();
+ const { stats, loading, error } = useStats();
if (error && error.status === 404) {
return <ErrorPage statusCode={404} />;
@@@ -29,26 -29,14 +29,28 @@@
const swrData = stats?.duration;
- durationData.data = durationData.data.flatMap((item) => {
- return swrData.data
- .filter((data) => data.pageNumber === item.pageNumber)
- .map((data) => ({
- versionNumber: data.versionNumber,
- pageNumber: item.pageNumber,
- avg_duration: data.avg_duration,
- }));
- durationData.data = durationData.data.map((item) => {
- const swrItem = swrData?.data.find(
- (data) => data.pageNumber === item.pageNumber
- );
- return swrItem ? swrItem : item;
-- });
++ if (swrData) {
++ durationData.data = durationData.data.flatMap((item) => {
++ return swrData.data
++ .filter((data) => data.pageNumber === item.pageNumber)
++ .map((data) => ({
++ versionNumber: data.versionNumber,
++ pageNumber: item.pageNumber,
++ avg_duration: data.avg_duration,
++ }));
++ });
++ }
+
+ console.log("durationData.data", durationData.data);
+
+ // durationData.data = durationData.data.map((item) => {
+ // const swrItem = swrData.data.find(
+ // (data) => data.pageNumber === item.pageNumber
+ // );
+ // return swrItem ? swrItem : item;
+ // });
- return (
+ return stats && stats.views.length > 0 ? (
<div className="p-5">
<BarChartComponent data={durationData.data} />
</div>
diff --cc components/view/document-view.tsx
index 65f2d7d7,23b7af87..be3d0dfc
--- a/components/view/document-view.tsx
+++ b/components/view/document-view.tsx
@@@ -149,27 -110,31 +110,33 @@@ export default function DocumentView(
);
}
- if (
- extension.includes(".png") ||
- extension.includes(".jpeg") ||
- extension.includes(".gif") ||
- extension.includes(".jpg")
- ) {
- return (
- <div className="h-screen bg-gray-900">
- <img className="w-full h-full" src={viewData.file} />
- </div>
- );
- }
return (
<div className="bg-gray-950">
- <PDFViewer
- file={viewData.file}
- viewId={viewData.viewId}
- linkId={link.id}
- documentId={document.id}
- versionNumber={document.versions[0].versionNumber}
- />
+ {submitted ? (
+ viewData.pages ? (
+ <PagesViewer
+ pages={viewData.pages}
+ viewId={viewData.viewId}
+ linkId={link.id}
+ documentId={document.id}
++ versionNumber={document.versions[0].versionNumber}
+ />
+ ) : (
+ <PDFViewer
+ file={viewData.file}
+ viewId={viewData.viewId}
+ linkId={link.id}
+ documentId={document.id}
+ name={document.name}
+ allowDownload={link.allowDownload}
++ versionNumber={document.versions[0].versionNumber}
+ />
+ )
+ ) : (
+ <div className="h-screen flex items-center justify-center">
+ <LoadingSpinner className="h-20 w-20" />
+ </div>
+ )}
</div>
);
}
diff --cc pages/api/links/[id]/index.ts
index f1c09bb0,c33e2907..887c0179
--- a/pages/api/links/[id]/index.ts
+++ b/pages/api/links/[id]/index.ts
@@@ -22,17 -22,9 +22,18 @@@ export default async function handle
id: true,
expiresAt: true,
emailProtected: true,
+ allowDownload: true,
password: true,
- document: { select: { id: true, name: true } },
+ document: {
+ select: {
+ id: true,
+ versions: {
+ where: { isPrimary: true },
+ select: { versionNumber: true },
+ take: 1,
+ }
+ }
+ },
},
});
diff --cc pages/api/links/domains/[...domainSlug].ts
index 2e6a4441,4499b60c..3451a53d
--- a/pages/api/links/domains/[...domainSlug].ts
+++ b/pages/api/links/domains/[...domainSlug].ts
@@@ -21,17 -25,9 +25,19 @@@ export default async function handle
id: true,
expiresAt: true,
emailProtected: true,
+ allowDownload: true,
password: true,
- document: { select: { id: true, team: { select: { plan: true } } } },
+ document: {
+ select: {
+ id: true,
++ team: { select: { plan: true } },
+ versions: {
+ where: { isPrimary: true },
+ select: { versionNumber: true },
+ take: 1,
+ },
+ },
+ },
},
});
diff --cc pages/documents/[id].tsx
index ec30582d,27f0a109..70b92e99
--- a/pages/documents/[id].tsx
+++ b/pages/documents/[id].tsx
@@@ -8,16 -8,153 +8,155 @@@ import LinkSheet from "@/components/lin
import Image from "next/image";
import LinksTable from "@/components/links/links-table";
import VisitorsTable from "@/components/visitors/visitors-table";
- import { useState } from "react";
+ import { useEffect, useRef, useState } from "react";
import { Button } from "@/components/ui/button";
import LoadingSpinner from "@/components/ui/loading-spinner";
+import { AddDocumentModal } from "@/components/documents/add-document-modal";
+import FileUp from "@/components/shared/icons/file-up";
+ import { toast } from "sonner";
+ import {
+ DropdownMenu,
+ DropdownMenuContent,
+ DropdownMenuItem,
+ DropdownMenuLabel,
+ DropdownMenuTrigger,
+ } from "@/components/ui/dropdown-menu";
+ 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, primaryVersion, error } = useDocument();
+ const { document: prismaDocument, primaryVersion, error } = useDocument();
+ const router = useRouter();
const [isLinkSheetOpen, setIsLinkSheetOpen] = useState<boolean>(false);
+ const [isEditingName, setIsEditingName] = useState<boolean>(false);
+ const [isFirstClick, setIsFirstClick] = useState<boolean>(false);
+ const [menuOpen, setMenuOpen] = useState<boolean>(false);
+
+ const nameRef = useRef<HTMLHeadingElement>(null);
+ const enterPressedRef = useRef<boolean>(false);
+ const dropdownRef = useRef<HTMLDivElement | null>(null);
+
+ const teamInfo = useTeam();
+
+ const handleNameSubmit = async () => {
+ if (enterPressedRef.current) {
+ enterPressedRef.current = false;
+ return;
+ }
+ if (nameRef.current && isEditingName) {
+ const newName = nameRef.current.innerText;
+
+ if (newName !== prismaDocument!.name) {
+ const response = await fetch(
+ `/api/teams/${teamInfo?.currentTeam?.id}/documents/${
+ prismaDocument!.id
+ }/update-name`,
+ {
+ method: "POST",
+ headers: {
+ "Content-Type": "application/json",
+ },
+ body: JSON.stringify({
+ name: newName,
+ }),
+ }
+ );
+
+ if (response.ok) {
+ const { message } = await response.json();
+ toast.success(message);
+ } else {
+ const { message } = await response.json();
+ toast.error(message);
+ }
+ }
+ setIsEditingName(false);
+ }
+ };
+
+ useEffect(() => {
+ function handleClickOutside(event: { target: any }) {
+ if (dropdownRef.current && !dropdownRef.current.contains(event.target)) {
+ setMenuOpen(false);
+ setIsFirstClick(false);
+ }
+ }
+
+ document.addEventListener("mousedown", handleClickOutside);
+ return () => {
+ document.removeEventListener("mousedown", handleClickOutside);
+ };
+ }, []);
+
+ const handleDeleteDocument = async (documentId: string) => {
+ // Prevent the first click from deleting the document
+ if (!isFirstClick) {
+ setIsFirstClick(true);
+ return;
+ }
+
+ const response = await fetch(
+ `/api/teams/${teamInfo?.currentTeam?.id}/documents/${documentId}`,
+ {
+ method: "DELETE",
+ }
+ );
+
+ if (response.ok) {
+ setIsFirstClick(false);
+ setMenuOpen(false);
+ router.push("/documents");
+ toast.success("Document deleted successfully.");
+ } else {
+ const { message } = await response.json();
+ toast.error(message);
+ }
+ };
+
+ const handleMenuStateChange = (open: boolean) => {
+ if (isFirstClick) {
+ setMenuOpen(true); // Keep the dropdown open on the first click
+ return;
+ }
+
+ // If the menu is closed, reset the isFirstClick state
+ if (!open) {
+ setIsFirstClick(false);
+ setMenuOpen(false); // Ensure the dropdown is closed
+ } else {
+ setMenuOpen(true); // Open the dropdown
+ }
+ };
+
+ const handleButtonClick = (event: any, documentId: string) => {
+ event.stopPropagation();
+ event.preventDefault();
+
+ if (isFirstClick) {
+ handleDeleteDocument(documentId);
+ setIsFirstClick(false);
+ setMenuOpen(false); // Close the dropdown after deleting
+ } else {
+ setIsFirstClick(true);
+ }
+ };
+
+ const preventEnterAndSubmit = (
+ event: React.KeyboardEvent<HTMLHeadingElement>
+ ) => {
+ if (event.key === "Enter") {
+ event.preventDefault(); // Prevent the default line break
+ setIsEditingName(true);
+ enterPressedRef.current = true;
+ handleNameSubmit(); // Handle the submit
+ if (nameRef.current) {
+ nameRef.current.blur(); // Remove focus from the h2 element
+ }
+ }
+ };
if (error && error.status === 404) {
return <ErrorPage statusCode={404} />;
@@@ -41,27 -178,65 +180,70 @@@
className=""
/>
</div>
- <h2 className="leading-7 text-2xl text-foreground font-semibold tracking-tight">
- {document.name}
- </h2>
+ <div className="flex flex-col">
+ <h2
+ className="leading-7 text-2xl text-foreground font-semibold tracking-tight hover:cursor-text"
+ ref={nameRef}
+ contentEditable={true}
+ onFocus={() => setIsEditingName(true)}
+ onBlur={handleNameSubmit}
+ onKeyDown={preventEnterAndSubmit}
+ title="Click to edit"
+ dangerouslySetInnerHTML={{ __html: prismaDocument.name }}
+ />
+ {isEditingName && (
+ <p className="text-sm text-muted-foreground mt-1">
+ {`You are editing the document name. Press <Enter> to save.`}
+ </p>
+ )}
+ </div>
</div>
</div>
- <div className="flex items-center space-x-4">
+ <div className="flex items-center gap-x-4">
+ <AddDocumentModal newVersion>
+ <button title="Upload a new version">
+ <FileUp className="w-6 h-6" />
+ </button>
+ </AddDocumentModal>
+ <DropdownMenu
+ open={menuOpen}
+ onOpenChange={handleMenuStateChange}
+ >
+ <DropdownMenuTrigger asChild>
+ <Button variant="ghost" className="h-8 w-8 p-0">
+ <span className="sr-only">Open menu</span>
+ <MoreVertical className="h-4 w-4" />
+ </Button>
+ </DropdownMenuTrigger>
+ <DropdownMenuContent align="end" ref={dropdownRef}>
+ <DropdownMenuLabel>Actions</DropdownMenuLabel>
+ <DropdownMenuItem
+ className="text-destructive focus:bg-destructive focus:text-destructive-foreground"
+ onClick={(event) =>
+ handleButtonClick(event, prismaDocument.id)
+ }
+ >
+ {isFirstClick ? "Really delete?" : "Delete document"}
+ </DropdownMenuItem>
+ </DropdownMenuContent>
+ </DropdownMenu>
<Button onClick={() => setIsLinkSheetOpen(true)}>
Create Link
</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 */}
- {document.numPages !== null && (
+ {prismaDocument.numPages !== null && (
<StatsChart
- documentId={document.id}
+ documentId={prismaDocument.id}
- totalPages={primaryVersion.numPages!}
+ totalPagesMax={primaryVersion.numPages!}
/>
)}
<StatsCard />