Skip to content

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

add external link view page

Marc Seitzd9575334789a
diff --git a/components/EmailForm.tsx b/components/EmailForm.tsx
new file mode 100644
index 00000000..6bd582e6
--- /dev/null
+++ b/components/EmailForm.tsx
@@ -0,0 +1,46 @@
+export default function EmailForm({ onSubmitHandler, setEmail }) {
+ return (
+ <>
+ <div className="flex min-h-full flex-1 flex-col justify-center px-6 py-12 lg:px-8">
+ <div className="sm:mx-auto sm:w-full sm:max-w-md">
+ <h2 className="mt-10 text-2xl font-bold leading-9 tracking-tight text-white">
+ Enter your email to view the document
+ </h2>
+ </div>
+
+ <div className="mt-10 sm:mx-auto sm:w-full sm:max-w-md">
+ <form className="space-y-6" onSubmit={onSubmitHandler}>
+ <div>
+ <label
+ htmlFor="email"
+ className="block text-sm font-medium leading-6 text-white"
+ >
+ Email address
+ </label>
+ <div className="mt-2">
+ <input
+ id="email"
+ name="email"
+ type="email"
+ autoComplete="email"
+ onChange={(e) => setEmail(e.target.value)}
+ required
+ className="block w-full rounded-md border-0 bg-white/5 py-1.5 text-white shadow-sm ring-1 ring-inset ring-white/10 focus:ring-2 focus:ring-inset focus:ring-indigo-500 sm:text-sm sm:leading-6"
+ />
+ </div>
+ </div>
+
+ <div>
+ <button
+ type="submit"
+ className="flex w-full justify-center rounded-md bg-indigo-500 px-3 py-1.5 text-sm font-semibold leading-6 text-white shadow-sm hover:bg-indigo-400 focus-visible:outline focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-indigo-500"
+ >
+ View Document
+ </button>
+ </div>
+ </form>
+ </div>
+ </div>
+ </>
+ );
+}
diff --git a/pages/api/views.ts b/pages/api/views.ts
new file mode 100644
index 00000000..f8a0290c
--- /dev/null
+++ b/pages/api/views.ts
@@ -0,0 +1,29 @@
+import { NextApiRequest, NextApiResponse } from "next";
+import prisma from "@/lib/prisma";
+
+export default async function handle(
+ req: NextApiRequest,
+ res: NextApiResponse
+) {
+ // We only allow POST requests
+ if (req.method !== "POST") {
+ res.status(405).json({ message: "Method Not Allowed" });
+ return;
+ }
+
+ const { linkId, email, documentId } = req.body;
+
+ try {
+ await prisma.view.create({
+ data: {
+ linkId: linkId,
+ viewerEmail: email,
+ documentId: documentId,
+ },
+ });
+
+ res.status(200).json({ message: "View recorded" });
+ } catch (error) {
+ res.status(500).json({ message: (error as Error).message });
+ }
+}
diff --git a/pages/view/[linkId].tsx b/pages/view/[linkId].tsx
new file mode 100644
index 00000000..d6bfd99d
--- /dev/null
+++ b/pages/view/[linkId].tsx
@@ -0,0 +1,113 @@
+import { GetServerSideProps } from "next";
+import React, { useState } from "react";
+import prisma from "@/lib/prisma";
+import EmailForm from "@/components/EmailForm";
+import { Document, Page, pdfjs } from "react-pdf";
+import "react-pdf/dist/esm/Page/AnnotationLayer.css";
+
+pdfjs.GlobalWorkerOptions.workerSrc = `//cdnjs.cloudflare.com/ajax/libs/pdf.js/${pdfjs.version}/pdf.worker.min.js`;
+
+const DocumentView = ({
+ document,
+ linkId,
+}: {
+ document: { id: string; file: string; name: string };
+ linkId: string;
+}) => {
+ const [email, setEmail] = useState("");
+ const [submitted, setSubmitted] = useState(false);
+ const [numPages, setNumPages] = useState<Number>(0);
+ const [pageNumber, setPageNumber] = useState(1);
+
+ function onDocumentLoadSuccess({ numPages }: { numPages: number }) {
+ setNumPages(numPages);
+ }
+
+ const handleSubmit = async (event: React.FormEvent) => {
+ event.preventDefault();
+
+ const response = await fetch("/api/views", {
+ method: "POST",
+ body: JSON.stringify({ linkId, email, documentId: document.id }),
+ headers: {
+ "Content-Type": "application/json",
+ },
+ });
+
+ if (response.ok) {
+ setSubmitted(true);
+ } else {
+ // Handle error
+ }
+ };
+
+ if (!submitted) {
+ return <EmailForm onSubmitHandler={handleSubmit} setEmail={setEmail} />;
+ }
+
+ if (document.file.includes(".pdf")) {
+ return (
+ <div>
+ <Document file={document.file} onLoadSuccess={onDocumentLoadSuccess}>
+ {Array.from(new Array(numPages), (el, index) => (
+ <Page
+ key={`page_${index + 1}`}
+ pageNumber={index + 1}
+ renderTextLayer={false}
+ />
+ ))}
+ </Document>
+ </div>
+ );
+ }
+ if (
+ document.file.includes(".docx") ||
+ document.file.includes(".pptx") ||
+ document.file.includes(".doc") ||
+ document.file.includes(".ppt")
+ ) {
+ return (
+ <div className="h-screen bg-gray-900">
+ <iframe
+ className="w-full h-full"
+ src={`https://view.officeapps.live.com/op/embed.aspx?src=${document.file}`}
+ ></iframe>
+ </div>
+ );
+ }
+ return (
+ <div className="h-screen bg-gray-900">
+ <img className="w-full h-full" src={document.file} />
+ </div>
+ );
+};
+
+export const getServerSideProps: GetServerSideProps = async ({ params }) => {
+ const linkId = params?.linkId as string;
+
+ const link = await prisma.link.findUnique({
+ where: { id: linkId },
+ include: { document: true },
+ });
+
+ if (!link || !link.document) {
+ return {
+ notFound: true,
+ };
+ }
+
+ const document = link.document;
+
+ return {
+ props: {
+ document: {
+ id: document.id,
+ file: document.file,
+ name: document.name,
+ },
+ linkId,
+ },
+ };
+};
+
+export default DocumentView;