Papermark is the open-source DocSend alternative and secure data rooms with built-in analytics and custom domains.
add buttons to and from assistant to document
Marc Seitzb5ef4c8ffc10
diff --git a/components/view/PDFViewer.tsx b/components/view/PDFViewer.tsxindex 935039ce..5c3aa0e0 100644--- a/components/view/PDFViewer.tsx+++ b/components/view/PDFViewer.tsx@@ -3,6 +3,7 @@ import { useEffect, useRef, useState } from "react";import { Document, Page, pdfjs } from "react-pdf";import { Download } from "lucide-react";import { useTeam } from "@/context/team-context";+import Nav from "./nav";pdfjs.GlobalWorkerOptions.workerSrc = `//cdnjs.cloudflare.com/ajax/libs/pdf.js/${pdfjs.version}/pdf.worker.js`;@@ -163,8 +164,9 @@ export default function PDFViewer(props: any) {<NavpageNumber={pageNumber}numPages={numPages}- downloadFile={downloadfile}allowDownload={props.allowDownload}+ assistantEnabled={props.assistantEnabled}+ file={{ name: props.name, url: props.file }}/><divhidden={loading}@@ -216,46 +218,3 @@ export default function PDFViewer(props: any) {</>);}--function Nav({- pageNumber,- numPages,- allowDownload,- downloadFile,-}: {- pageNumber: number;- numPages: number;- allowDownload: boolean;- downloadFile: (e: React.MouseEvent<HTMLButtonElement>) => void;-}) {- return (- <nav className="bg-black">- <div className="mx-auto px-2 sm:px-6 lg:px-8">- <div className="relative flex h-16 items-center justify-between">- <div className="flex flex-1 items-center justify-center sm:items-stretch sm:justify-start">- <div className="flex flex-shrink-0 items-center">- <p className="text-2xl font-bold tracking-tighter text-white">- Papermark- </p>- </div>- </div>- <div className="absolute inset-y-0 right-0 flex items-center pr-2 sm:static sm:inset-auto sm:ml-6 sm:pr-0">- <div className="bg-gray-900 text-white rounded-md px-3 py-2 text-sm font-medium m-1">- <span>{pageNumber}</span>- <span className="text-gray-400"> / {numPages}</span>- </div>- {allowDownload ? (- <div className="bg-gray-900 text-white rounded-md px-2 py-1 text-sm m-1">- <button onClick={downloadFile}>- <Download className="w-8 h-6" />- </button>- </div>- ) : (- ""- )}- </div>- </div>- </div>- </nav>- );-}diff --git a/components/view/PagesViewer.tsx b/components/view/PagesViewer.tsxindex 3ad3f53e..bfd9f30d 100644--- a/components/view/PagesViewer.tsx+++ b/components/view/PagesViewer.tsx@@ -3,6 +3,7 @@ import { useEffect, useRef, useState } from "react";import Image from "next/image";import LoadingSpinner from "../ui/loading-spinner";import BlankImg from "@/public/_static/blank.gif";+import Nav from "./nav";const DEFAULT_PRELOADED_IMAGES_NUM = 10;@@ -11,12 +12,14 @@ export default function PagesViewer({linkId,documentId,viewId,+ assistantEnabled,versionNumber,}: {pages: { file: string; pageNumber: string }[];linkId: string;documentId: string;viewId: string;+ assistantEnabled: boolean;versionNumber: number;}) {const numPages = pages.length;@@ -135,7 +138,11 @@ export default function PagesViewer({return (<>- <Nav pageNumber={pageNumber} numPages={numPages} />+ <Nav+ pageNumber={pageNumber}+ numPages={numPages}+ assistantEnabled={assistantEnabled}+ /><divstyle={{ height: "calc(100vh - 64px)" }}className="flex items-center relative"@@ -193,33 +200,3 @@ export default function PagesViewer({</>);}--function Nav({- pageNumber,- numPages,-}: {- pageNumber: number;- numPages: number;-}) {- return (- <nav className="bg-black">- <div className="mx-auto px-2 sm:px-6 lg:px-8">- <div className="relative flex h-16 items-center justify-between">- <div className="flex flex-1 items-center justify-center sm:items-stretch sm:justify-start">- <div className="flex flex-shrink-0 items-center">- <p className="text-2xl font-bold tracking-tighter text-white">- Papermark- </p>- </div>- </div>- <div className="absolute inset-y-0 right-0 flex items-center pr-2 sm:static sm:inset-auto sm:ml-6 sm:pr-0">- <div className="bg-gray-900 text-white rounded-md px-3 py-2 text-sm font-medium">- <span>{pageNumber}</span>- <span className="text-gray-400"> / {numPages}</span>- </div>- </div>- </div>- </div>- </nav>- );-}diff --git a/components/view/document-view.tsx b/components/view/document-view.tsxindex b7303c76..aded6bdb 100644--- a/components/view/document-view.tsx+++ b/components/view/document-view.tsx@@ -142,6 +142,7 @@ export default function DocumentView({viewId={viewData.viewId}linkId={link.id}documentId={document.id}+ assistantEnabled={document.assistantEnabled}versionNumber={document.versions[0].versionNumber}/>) : (@@ -152,6 +153,7 @@ export default function DocumentView({documentId={document.id}name={document.name}allowDownload={link.allowDownload}+ assistantEnabled={document.assistantEnabled}versionNumber={document.versions[0].versionNumber}/>)diff --git a/components/view/nav.tsx b/components/view/nav.tsxnew file mode 100644index 00000000..4af894d7--- /dev/null+++ b/components/view/nav.tsx@@ -0,0 +1,87 @@+import Link from "next/link";+import { useRouter } from "next/router";+import { Button } from "../ui/button";+import PapermarkSparkle from "../shared/icons/papermark-sparkle";+import { Download } from "lucide-react";++export default function Nav({+ pageNumber,+ numPages,+ allowDownload,+ assistantEnabled,+ file,+}: {+ pageNumber: number;+ numPages: number;+ allowDownload?: boolean;+ assistantEnabled?: boolean;+ file?: { name: string; url: string };+}) {+ const router = useRouter();+ const { linkId } = router.query as { linkId: string };++ async function downloadFile(e: React.MouseEvent<HTMLButtonElement>) {+ if (!allowDownload || !file) return;+ try {+ //get file data+ const response = await fetch(file.url);+ const fileData = await response.blob();++ //create <a/> to download the file+ const a = document.createElement("a");+ a.href = window.URL.createObjectURL(fileData);+ a.download = file.name;+ document.body.appendChild(a);+ a.click();++ //clean up used resources+ document.body.removeChild(a);+ window.URL.revokeObjectURL(a.href);+ } catch (error) {+ console.error("Error downloading file:", error);+ }+ }++ return (+ <nav className="bg-black">+ <div className="mx-auto px-2 sm:px-6 lg:px-8">+ <div className="relative flex h-16 items-center justify-between">+ <div className="flex flex-1 items-stretch justify-start">+ <div className="flex flex-shrink-0 items-center">+ <p className="text-2xl font-bold tracking-tighter text-white">+ Papermark+ </p>+ </div>+ </div>+ <div className="absolute inset-y-0 right-0 flex items-center pr-2 sm:static sm:inset-auto sm:ml-6 sm:pr-0 space-x-2">+ {assistantEnabled ? (+ <Link href={`/view/${linkId}/chat`}>+ <Button+ className="group space-x-1 bg-gradient-to-r from-[#16222A] via-emerald-500 to-[#16222A] duration-200 ease-linear hover:bg-right"+ variant={"special"}+ style={{+ backgroundSize: "200% auto",+ }}+ >+ <PapermarkSparkle className="h-5 w-5 animate-pulse group-hover:animate-none" />{" "}+ <span>AI Assistant</span>+ </Button>+ </Link>+ ) : null}+ {allowDownload ? (+ <div className="bg-gray-900 text-white rounded-md px-2 py-1 text-sm m-1">+ <Button onClick={downloadFile} size="icon">+ <Download className="w-6 h-6" />+ </Button>+ </div>+ ) : null}+ <div className="bg-gray-900 text-white rounded-md h-10 px-4 py-2 items-center flex text-sm font-medium">+ <span>{pageNumber}</span>+ <span className="text-gray-400"> / {numPages}</span>+ </div>+ </div>+ </div>+ </div>+ </nav>+ );+}diff --git a/pages/api/links/[id]/index.ts b/pages/api/links/[id]/index.tsindex 00621d6b..3af5bdc4 100644--- a/pages/api/links/[id]/index.ts+++ b/pages/api/links/[id]/index.ts@@ -29,6 +29,7 @@ export default async function handle(document: {select: {id: true,+ assistantEnabled: true,versions: {where: { isPrimary: true },select: {diff --git a/pages/documents/[id]/chat.tsx b/pages/documents/[id]/chat.tsxindex dcf17ae3..101d7d2c 100644--- a/pages/documents/[id]/chat.tsx+++ b/pages/documents/[id]/chat.tsx@@ -6,6 +6,8 @@ import { CustomUser } from "@/lib/types";import { Chat } from "@/components/chat/chat";import Sparkle from "@/components/shared/icons/sparkle";import { usePlan } from "@/lib/swr/use-billing";+import { Button } from "@/components/ui/button";+import Link from "next/link";export const getServerSideProps = async (context: any) => {const { id } = context.params;@@ -20,10 +22,23 @@ export const getServerSideProps = async (context: any) => {};}+ const userId = (session.user as CustomUser).id;+const document = await prisma.document.findUnique({- where: { id },+ where: {+ id,+ assistantEnabled: true,+ team: {+ users: {+ some: {+ userId: userId,+ },+ },+ },+ },select: {id: true,+ assistantEnabled: true,versions: {where: { isPrimary: true },select: {@@ -44,8 +59,6 @@ export const getServerSideProps = async (context: any) => {};}- const userId = (session.user as CustomUser).id;-// create or fetch threadIdconst res = await fetch(`${process.env.NEXTAUTH_URL}/api/assistants/threads`,@@ -73,8 +86,9 @@ export const getServerSideProps = async (context: any) => {props: {threadId,messages: messages || [],- firstPage: document.versions[0].pages[0].file,+ firstPage: document.versions[0].pages[0]?.file || "",userId,+ documentId: document.id,},};};@@ -84,16 +98,18 @@ export default function ChatPage({messages,firstPage,userId,+ documentId,}: {threadId: string;messages: Message[];firstPage: string;userId: string;+ documentId: string;}) {const { plan } = usePlan();return (<>- <Nav />+ <Nav documentId={documentId} /><ChatinitialMessages={messages}threadId={threadId}@@ -105,7 +121,7 @@ export default function ChatPage({);}-function Nav() {+function Nav({ documentId }: { documentId: string }) {return (<nav className="bg-black fixed top-0 inset-x-0 z-10"><div className="mx-auto px-2 sm:px-6 lg:px-8">@@ -119,10 +135,9 @@ function Nav() {</div></div><div className="absolute inset-y-0 right-0 flex items-center pr-2 sm:static sm:inset-auto sm:ml-6 sm:pr-0">- <div className="bg-gray-900 text-white rounded-md px-3 py-2 text-sm font-medium">- {/* <span>{pageNumber}</span>- <span className="text-gray-400"> / {numPages}</span> */}- </div>+ <Link href={`/documents/${documentId}`}>+ <Button variant="secondary">Back to document</Button>+ </Link></div></div></div>diff --git a/pages/view/[linkId]/chat.tsx b/pages/view/[linkId]/chat.tsxindex 17316a5e..c599b3f4 100644--- a/pages/view/[linkId]/chat.tsx+++ b/pages/view/[linkId]/chat.tsx@@ -6,6 +6,8 @@ import { CustomUser } from "@/lib/types";import { Chat } from "@/components/chat/chat";import Sparkle from "@/components/shared/icons/sparkle";import { usePlan } from "@/lib/swr/use-billing";+import Link from "next/link";+import { Button } from "@/components/ui/button";export const getServerSideProps = async (context: any) => {const { linkId } = context.params;@@ -26,6 +28,7 @@ export const getServerSideProps = async (context: any) => {document: {select: {id: true,+ assistantEnabled: true,versions: {where: { isPrimary: true },select: {@@ -48,6 +51,12 @@ export const getServerSideProps = async (context: any) => {};}+ if (!link.document.assistantEnabled) {+ return {+ notFound: true,+ };+ }+const userId = (session.user as CustomUser).id;// create or fetch threadId@@ -77,8 +86,9 @@ export const getServerSideProps = async (context: any) => {props: {threadId,messages: messages || [],- firstPage: link.document.versions[0].pages[0].file,+ firstPage: link.document.versions[0].pages[0]?.file || "",userId: userId,+ linkId: linkId,},};};@@ -88,17 +98,19 @@ export default function ChatPage({messages,firstPage,userId,+ linkId,}: {threadId: string;messages: Message[];firstPage: string;userId: string;+ linkId: string;}) {const { plan } = usePlan();return (<>- <Nav />+ <Nav linkId={linkId} /><ChatinitialMessages={messages}threadId={threadId}@@ -110,7 +122,7 @@ export default function ChatPage({);}-function Nav() {+function Nav({ linkId }: { linkId: string }) {return (<nav className="bg-black fixed top-0 inset-x-0 z-10"><div className="mx-auto px-2 sm:px-6 lg:px-8">@@ -124,10 +136,9 @@ function Nav() {</div></div><div className="absolute inset-y-0 right-0 flex items-center pr-2 sm:static sm:inset-auto sm:ml-6 sm:pr-0">- <div className="bg-gray-900 text-white rounded-md px-3 py-2 text-sm font-medium">- {/* <span>{pageNumber}</span>- <span className="text-gray-400"> / {numPages}</span> */}- </div>+ <Link href={`/view/${linkId}`}>+ <Button variant="secondary">Back to document</Button>+ </Link></div></div></div>