Papermark is the open-source DocSend alternative and secure data rooms with built-in analytics and custom domains.
feat: add agreement to document and dataroom link view
Marc Seitz57c0abe9effe
diff --git a/components/view/access-form/agreement-section.tsx b/components/view/access-form/agreement-section.tsxnew file mode 100644index 00000000..265bee2d--- /dev/null+++ b/components/view/access-form/agreement-section.tsx@@ -0,0 +1,43 @@+import { Dispatch, SetStateAction, useEffect } from "react";++import { Checkbox } from "@/components/ui/checkbox";++import { DEFAULT_ACCESS_FORM_TYPE } from ".";++export default function AgreementSection({+ data,+ setData,+ agreementContent,+}: {+ data: DEFAULT_ACCESS_FORM_TYPE;+ setData: Dispatch<SetStateAction<DEFAULT_ACCESS_FORM_TYPE>>;+ agreementContent: string;+}) {+ const handleCheckChange = (checked: boolean) => {+ setData((prevData) => ({ ...prevData, hasConfirmedAgreement: checked }));+ };++ return (+ <div className="pb-5">+ <div className="relative flex items-start space-x-2 rounded-md shadow-sm">+ <Checkbox+ id="agreement"+ onCheckedChange={handleCheckChange}+ className="mt-0.5 border border-gray-400 focus-visible:outline-none focus-visible:ring-1 focus-visible:ring-gray-300 focus-visible:ring-offset-1 disabled:cursor-not-allowed disabled:opacity-50 data-[state=checked]:border-white data-[state=checked]:bg-black data-[state=checked]:text-white"+ />+ <label className="text-sm font-medium leading-5 text-white peer-disabled:cursor-not-allowed peer-disabled:opacity-70">+ I have reviewed and agree to the terms of this{" "}+ <a+ href={`${agreementContent}`}+ target="_blank"+ rel="noreferrer noopener"+ className=" underline hover:text-gray-200"+ >+ Non-Disclosure Agreement+ </a>+ .+ </label>+ </div>+ </div>+ );+}diff --git a/components/view/access-form/index.tsx b/components/view/access-form/index.tsxindex 59906969..e727738a 100644--- a/components/view/access-form/index.tsx+++ b/components/view/access-form/index.tsx@@ -2,7 +2,9 @@ import { useEffect } from "react";import { Button } from "@/components/ui/button";+import AgreementSection from "./agreement-section";import EmailSection from "./email-section";+import NameSection from "./name-section";import PasswordSection from "./password-section";export const DEFAULT_ACCESS_FORM_DATA = {@@ -13,6 +15,8 @@ export const DEFAULT_ACCESS_FORM_DATA = {export type DEFAULT_ACCESS_FORM_TYPE = {email: string | null;password: string | null;+ hasConfirmedAgreement?: boolean;+ name?: string | null;};export default function AccessForm({@@ -22,6 +26,8 @@ export default function AccessForm({onSubmitHandler,requireEmail,requirePassword,+ requireAgreement,+ agreementContent,isLoading,}: {data: DEFAULT_ACCESS_FORM_TYPE;@@ -30,6 +36,8 @@ export default function AccessForm({onSubmitHandler: React.FormEventHandler<HTMLFormElement>;requireEmail: boolean;requirePassword: boolean;+ requireAgreement?: boolean;+ agreementContent?: string;isLoading: boolean;}) {useEffect(() => {@@ -53,19 +61,30 @@ export default function AccessForm({<div className="mt-10 sm:mx-auto sm:w-full sm:max-w-md"><form className="space-y-4" onSubmit={onSubmitHandler}>+ {requireAgreement && agreementContent ? (+ <NameSection {...{ data, setData }} />+ ) : null}{requireEmail ? <EmailSection {...{ data, setData }} /> : null}{requirePassword ? (<PasswordSection {...{ data, setData }} />) : null}+ {requireAgreement && agreementContent ? (+ <AgreementSection+ {...{ data, setData }}+ agreementContent={agreementContent}+ />+ ) : null}<div className="flex justify-center"><Buttontype="submit"disabled={(requireEmail && !data.email) ||- (requirePassword && !data.password)+ (requirePassword && !data.password) ||+ (requireAgreement && !data.hasConfirmedAgreement) ||+ (requireAgreement && !data.name)}- className="w-1/3 bg-white text-gray-950 hover:bg-white/90"+ className="w-1/3 min-w-fit bg-white text-gray-950 hover:bg-white/90"loading={isLoading}>Continuediff --git a/components/view/access-form/name-section.tsx b/components/view/access-form/name-section.tsxnew file mode 100644index 00000000..98dfd1cd--- /dev/null+++ b/components/view/access-form/name-section.tsx@@ -0,0 +1,59 @@+import { Dispatch, SetStateAction, useEffect } from "react";++import { DEFAULT_ACCESS_FORM_TYPE } from ".";++export default function NameSection({+ data,+ setData,+}: {+ data: DEFAULT_ACCESS_FORM_TYPE;+ setData: Dispatch<SetStateAction<DEFAULT_ACCESS_FORM_TYPE>>;+}) {+ const { name } = data;++ useEffect(() => {+ // Load name from localStorage when the component mounts+ const storedName = window.localStorage.getItem("papermark.name");+ if (storedName) {+ setData((prevData) => ({+ ...prevData,+ name: storedName,+ }));+ }+ }, [setData]);++ const handleNameChange = (e: React.ChangeEvent<HTMLInputElement>) => {+ const newName = e.target.value;+ // Store the new email in localStorage+ window.localStorage.setItem("papermark.name", newName);+ // Update the state+ setData({ ...data, name: newName });+ };++ return (+ <div className="pb-5">+ <div className="relative space-y-2 rounded-md shadow-sm">+ <label+ htmlFor="name"+ className="block text-sm font-medium leading-6 text-white"+ >+ Name+ </label>+ <input+ name="name"+ id="name"+ type="text"+ autoCorrect="off"+ autoComplete="off"+ autoFocus+ className="flex w-full rounded-md border-0 bg-black py-1.5 text-white shadow-sm ring-1 ring-inset ring-gray-600 placeholder:text-gray-400 focus:ring-2 focus:ring-inset focus:ring-gray-300 sm:text-sm sm:leading-6"+ value={name || ""}+ placeholder="Enter your full name"+ onChange={handleNameChange}+ aria-invalid="true"+ data-1p-ignore+ />+ </div>+ </div>+ );+}diff --git a/components/view/dataroom/dataroom-view.tsx b/components/view/dataroom/dataroom-view.tsxindex 1863bd98..5a952b53 100644--- a/components/view/dataroom/dataroom-view.tsx+++ b/components/view/dataroom/dataroom-view.tsx@@ -3,7 +3,6 @@ import { useRouter } from "next/router";import React, { useEffect, useRef, useState } from "react";-import { LinkWithDataroom } from "@/pages/view/d/[linkId]";import { DataroomBrand } from "@prisma/client";import { usePlausible } from "next-plausible";import { ExtendedRecordMap } from "notion-types";@@ -17,6 +16,7 @@ import AccessForm, {} from "@/components/view/access-form";import { useAnalytics } from "@/lib/analytics";+import { LinkWithDataroom } from "@/lib/types";import DataroomViewer from "../DataroomViewer";import PagesViewerNew from "../PagesViewerNew";@@ -72,7 +72,13 @@ export default function DataroomView({token?: string;verifiedEmail?: string;}) {- const { linkType, dataroom, emailProtected, password: linkPassword } = link;+ const {+ linkType,+ dataroom,+ emailProtected,+ password: linkPassword,+ enableAgreement,+ } = link;const plausible = usePlausible();const analytics = useAnalytics();@@ -236,6 +242,8 @@ export default function DataroomView({onSubmitHandler={handleSubmit}requireEmail={emailProtected}requirePassword={!!linkPassword}+ requireAgreement={enableAgreement!}+ agreementContent={link.agreement?.content}isLoading={isLoading}/>);diff --git a/components/view/document-view.tsx b/components/view/document-view.tsxindex 3a0c7601..270562e4 100644--- a/components/view/document-view.tsx+++ b/components/view/document-view.tsx@@ -59,7 +59,12 @@ export default function DocumentView({verifiedEmail?: string;showPoweredByBanner?: boolean;}) {- const { document, emailProtected, password: linkPassword } = link;+ const {+ document,+ emailProtected,+ password: linkPassword,+ enableAgreement,+ } = link;const plausible = usePlausible();const analytics = useAnalytics();@@ -185,6 +190,8 @@ export default function DocumentView({onSubmitHandler={handleSubmit}requireEmail={emailProtected}requirePassword={!!linkPassword}+ requireAgreement={enableAgreement!}+ agreementContent={link.agreement?.content}isLoading={isLoading}/>);diff --git a/lib/types.ts b/lib/types.tsindex 348a3d21..4fa6194c 100644--- a/lib/types.ts+++ b/lib/types.ts@@ -1,4 +1,5 @@import {+ Agreement,Dataroom,DataroomDocument,DataroomFolder,@@ -62,6 +63,7 @@ export interface LinkWithDocument extends Link {type: string;};} | null;+ agreement: Agreement | null;}export interface LinkWithDataroom extends Link {@@ -88,6 +90,7 @@ export interface LinkWithDataroom extends Link {folders: DataroomFolder[];lastUpdatedAt: Date;};+ agreement: Agreement | null;}export interface Geo {diff --git a/pages/api/links/[id]/index.ts b/pages/api/links/[id]/index.tsindex 6955a4e5..06f43a74 100644--- a/pages/api/links/[id]/index.ts+++ b/pages/api/links/[id]/index.ts@@ -45,6 +45,8 @@ export default async function handle(data: true,},},+ enableAgreement: true,+ agreement: true,},});diff --git a/pages/api/links/domains/[...domainSlug].ts b/pages/api/links/domains/[...domainSlug].tsindex aad35167..cb1ebe5e 100644--- a/pages/api/links/domains/[...domainSlug].ts+++ b/pages/api/links/domains/[...domainSlug].ts@@ -55,6 +55,8 @@ export default async function handle(data: true,},},+ enableAgreement: true,+ agreement: true,document: {select: {team: {diff --git a/pages/api/views-dataroom.ts b/pages/api/views-dataroom.tsindex 69e5d6b3..be7ca437 100644--- a/pages/api/views-dataroom.ts+++ b/pages/api/views-dataroom.ts@@ -60,7 +60,12 @@ export default async function handle(viewType: "DATAROOM_VIEW" | "DOCUMENT_VIEW";};- const { email, password } = data as { email: string; password: string };+ const { email, password, name, hasConfirmedAgreement } = data as {+ email: string;+ password: string;+ name?: string;+ hasConfirmedAgreement?: boolean;+ };// Fetch the link to verify the settingsconst link = await prisma.link.findUnique({@@ -77,6 +82,8 @@ export default async function handle(slug: true,allowList: true,denyList: true,+ enableAgreement: true,+ agreementId: true,},});@@ -120,6 +127,12 @@ export default async function handle(}}+ // Check if agreement is required for visiting the link+ if (link.enableAgreement && !hasConfirmedAgreement) {+ res.status(400).json({ message: "Agreement to NDA is required." });+ return;+ }+// Check if email is allowed to visit the linkif (link.allowList && link.allowList.length > 0) {// Extract the domain from the email address@@ -267,10 +280,20 @@ export default async function handle(data: {linkId: linkId,viewerEmail: email,+ viewerName: name,verified: isEmailVerified,dataroomId: dataroomId,viewType: "DATAROOM_VIEW",viewerId: viewer?.id ?? undefined,+ ...(link.enableAgreement &&+ link.agreementId &&+ hasConfirmedAgreement && {+ agreementResponse: {+ create: {+ agreementId: link.agreementId,+ },+ },+ }),},select: { id: true },});@@ -307,12 +330,22 @@ export default async function handle(data: {linkId: linkId,viewerEmail: email,+ viewerName: name,documentId: documentId,verified: isEmailVerified,dataroomViewId: dataroomViewId,dataroomId: dataroomId,viewType: "DOCUMENT_VIEW",viewerId: viewer?.id ?? undefined,+ ...(link.enableAgreement &&+ link.agreementId &&+ hasConfirmedAgreement && {+ agreementResponse: {+ create: {+ agreementId: link.agreementId,+ },+ },+ }),},select: { id: true },});@@ -415,7 +448,7 @@ export default async function handle(return res.status(200).json(returnObject);} catch (error) {log({- message: `Failed to record view for dataroom ${linkId}. \n\n ${error}`,+ message: `Failed to record view for dataroom document ${linkId}. \n\n ${error}`,type: "error",mention: true,});diff --git a/pages/api/views.ts b/pages/api/views.tsindex b0ad5057..f9da60f4 100644--- a/pages/api/views.ts+++ b/pages/api/views.ts@@ -48,7 +48,12 @@ export default async function handle(verifiedEmail: string | null;};- const { email, password } = data as { email: string; password: string };+ const { email, password, name, hasConfirmedAgreement } = data as {+ email: string;+ password: string;+ name?: string;+ hasConfirmedAgreement?: boolean;+ };// Fetch the link to verify the settingsconst link = await prisma.link.findUnique({@@ -65,6 +70,8 @@ export default async function handle(slug: true,allowList: true,denyList: true,+ enableAgreement: true,+ agreementId: true,},});@@ -108,6 +115,12 @@ export default async function handle(}}+ // Check if agreement is required for visiting the link+ if (link.enableAgreement && !hasConfirmedAgreement) {+ res.status(400).json({ message: "Agreement to NDA is required." });+ return;+ }+// Check if email is allowed to visit the linkif (link.allowList && link.allowList.length > 0) {// Extract the domain from the email address@@ -216,8 +229,18 @@ export default async function handle(data: {linkId: linkId,viewerEmail: email,+ viewerName: name,documentId: documentId,verified: isEmailVerified,+ ...(link.enableAgreement &&+ link.agreementId &&+ hasConfirmedAgreement && {+ agreementResponse: {+ create: {+ agreementId: link.agreementId,+ },+ },+ }),},select: { id: true },});