Papermark is the open-source DocSend alternative and secure data rooms with built-in analytics and custom domains.
feat: add notion to document upload modal
Marc Seitzbedf466506c5
diff --git a/components/documents/add-document-modal.tsx b/components/documents/add-document-modal.tsxindex 0af6f7db..0eb3bb16 100644--- a/components/documents/add-document-modal.tsx+++ b/components/documents/add-document-modal.tsx@@ -1,38 +1,49 @@+import { Dialog, DialogContent, DialogTrigger } from "@/components/ui/dialog";import {- Dialog,- DialogContent,- DialogDescription,- DialogHeader,- DialogTitle,- DialogTrigger,-} from "@/components/ui/dialog";-import { useState } from "react";+ Card,+ CardContent,+ CardDescription,+ CardHeader,+ CardTitle,+} from "@/components/ui/card";+import { Label } from "@/components/ui/label";+import { Tabs, TabsContent, TabsList, TabsTrigger } from "@/components/ui/tabs";+import { FormEvent, useState } from "react";import { useRouter } from "next/router";import { type PutBlobResult } from "@vercel/blob";import { upload } from "@vercel/blob/client";import DocumentUpload from "@/components/document-upload";import { pdfjs } from "react-pdf";import { copyToClipboard, getExtension } from "@/lib/utils";-import { Button } from "../ui/button";+import { Button } from "@/components/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`;-export function AddDocumentModal({newVersion, children}: {newVersion?: boolean, children: React.ReactNode}) {+export function AddDocumentModal({+ newVersion,+ children,+}: {+ newVersion?: boolean;+ children: React.ReactNode;+}) {const router = useRouter();const plausible = usePlausible();const [uploading, setUploading] = useState<boolean>(false);const [currentFile, setCurrentFile] = useState<File | null>(null);+ const [notionLink, setNotionLink] = useState<string | null>(null);const teamInfo = useTeam();- const handleBrowserUpload = async (event: any) => {+ const handleBrowserUpload = async (+ event: FormEvent<HTMLFormElement>,+ ): Promise<void> => {event.preventDefault();// Check if the file is chosenif (!currentFile) {- alert("Please select a file to upload.");+ toast.error("Please select a file to upload.");return; // prevent form from submitting}@@ -58,7 +69,7 @@ export function AddDocumentModal({newVersion, children}: {newVersion?: boolean,response = await saveNewVersionToDatabase(newBlob,documentId as string,- numPages+ numPages,);}}@@ -66,13 +77,13 @@ export function AddDocumentModal({newVersion, children}: {newVersion?: boolean,if (response) {const document = await response.json();- console.log("document: ", document)+ console.log("document: ", document);if (!newVersion) {// copy the link to the clipboardcopyToClipboard(`${process.env.NEXT_PUBLIC_BASE_URL}/view/${document.links[0].id}`,- "Document uploaded and link copied to clipboard. Redirecting to document page..."+ "Document uploaded and link copied to clipboard. Redirecting to document page...",);// track the event@@ -94,8 +105,6 @@ export function AddDocumentModal({newVersion, children}: {newVersion?: boolean,setUploading(false);}, 2000);}--}} catch (error) {console.error("An error occurred while uploading the file: ", error);@@ -104,7 +113,7 @@ export function AddDocumentModal({newVersion, children}: {newVersion?: boolean,async function saveDocumentToDatabase(blob: PutBlobResult,- numPages?: number+ numPages?: number,) {// create a document in the database with the blob urlconst response = await fetch(@@ -119,7 +128,7 @@ export function AddDocumentModal({newVersion, children}: {newVersion?: boolean,url: blob.url,numPages: numPages,}),- }+ },);if (!response.ok) {@@ -130,7 +139,11 @@ export function AddDocumentModal({newVersion, children}: {newVersion?: boolean,}// create a new version in the database- async function saveNewVersionToDatabase(blob: PutBlobResult, documentId: string, numPages?: number) {+ async function saveNewVersionToDatabase(+ blob: PutBlobResult,+ documentId: string,+ numPages?: number,+ ) {const response = await fetch(`/api/teams/${teamInfo?.currentTeam?.id}/documents/${documentId}/versions`,{@@ -143,7 +156,7 @@ export function AddDocumentModal({newVersion, children}: {newVersion?: boolean,numPages: numPages,type: "pdf",}),- }+ },);if (!response.ok) {@@ -159,46 +172,171 @@ export function AddDocumentModal({newVersion, children}: {newVersion?: boolean,return pdf.numPages;}+ const handleNotionUpload = async (+ event: FormEvent<HTMLFormElement>,+ ): Promise<void> => {+ event.preventDefault();++ // Check if the file is chosen+ if (!notionLink) {+ toast.error("Please enter a Notion link to proceed.");+ return; // prevent form from submitting+ }++ try {+ setUploading(true);++ const response = await fetch(+ `/api/teams/${teamInfo?.currentTeam?.id}/documents`,+ {+ method: "POST",+ headers: {+ "Content-Type": "application/json",+ },+ body: JSON.stringify({+ name: "Notion Link", // TODO: get the title of the notion page+ url: notionLink,+ numPages: 1,+ type: "notion",+ }),+ },+ );++ if (response) {+ const document = await response.json();++ console.log("document: ", document);++ if (!newVersion) {+ // copy the link to the clipboard+ copyToClipboard(+ `${process.env.NEXT_PUBLIC_BASE_URL}/view/${document.links[0].id}`,+ "Notion Page processed and link copied to clipboard. Redirecting to document page...",+ );++ // track the event+ plausible("documentUploaded");+ plausible("notionDocumentUploaded");++ // redirect to the document page+ setTimeout(() => {+ router.push("/documents/" + document.id);+ setUploading(false);+ }, 2000);+ }+ }+ } catch (error) {+ console.error(+ "An error occurred while processing the Notion link: ",+ error,+ );+ }+ };+return (<Dialog><DialogTrigger asChild>{children}</DialogTrigger>- <DialogContent className="text-foreground bg-background">- <DialogHeader>- <DialogTitle>{newVersion ? `Upload a new version` : `Share a document`}</DialogTitle>- <DialogDescription>- <div className="border-b border-border py-2">- <p className="mb-1 text-sm text-muted-foreground">- {newVersion ? `After you upload a new version, the existing links will remain the unchanged but ` : `After you upload the document, a shareable link will be+ <DialogContent+ className="text-foreground bg-transparent border-none shadow-none"+ isDocumentDialog+ >+ <Tabs defaultValue="document">+ <TabsList className="grid w-full grid-cols-2">+ <TabsTrigger value="document">Document</TabsTrigger>+ <TabsTrigger value="notion">Notion Page</TabsTrigger>+ </TabsList>+ <TabsContent value="document">+ <Card>+ <CardHeader className="space-y-3">+ <CardTitle>+ {newVersion ? `Upload a new version` : `Share a document`}+ </CardTitle>+ <CardDescription>+ {newVersion+ ? `After you upload a new version, the existing links will remain the unchanged but `+ : `After you upload the document, a shareable link will begenerated and copied to your clipboard.`}- </p>- </div>- <form- encType="multipart/form-data"- onSubmit={handleBrowserUpload}- className="flex flex-col">- <div className="space-y-12">- <div className="pb-6">- <div className="mt-10 grid grid-cols-1 gap-x-6 gap-y-8 sm:grid-cols-6">- <DocumentUpload- currentFile={currentFile}- setCurrentFile={setCurrentFile}- />+ </CardDescription>+ </CardHeader>+ <CardContent className="space-y-2">+ <form+ encType="multipart/form-data"+ onSubmit={handleBrowserUpload}+ className="flex flex-col"+ >+ <div className="space-y-1">+ <div className="pb-6">+ <div className="grid grid-cols-1 gap-x-6 gap-y-8 sm:grid-cols-6">+ <DocumentUpload+ currentFile={currentFile}+ setCurrentFile={setCurrentFile}+ />+ </div>+ </div>+ </div>++ <div className="flex justify-center">+ <Button+ type="submit"+ className="w-full lg:w-1/2"+ disabled={uploading || !currentFile}+ loading={uploading}+ >+ {uploading ? "Uploading..." : "Upload Document"}+ </Button>+ </div>+ </form>+ </CardContent>+ </Card>+ </TabsContent>+ <TabsContent value="notion">+ <Card>+ <CardHeader className="space-y-3">+ <CardTitle>Share a Notion Page</CardTitle>+ <CardDescription>+ After you submit the Notion link, a shareable link will be+ generated and copied to your clipboard. Just like with a PDF+ document.+ </CardDescription>+ </CardHeader>+ <CardContent className="space-y-2">+ <form+ encType="multipart/form-data"+ onSubmit={handleNotionUpload}+ className="flex flex-col"+ >+ <div className="space-y-1 pb-8">+ <Label htmlFor="notion-link">Notion Page Link</Label>+ <div className="mt-2">+ <input+ type="text"+ name="notion-link"+ id="notion-link"+ placeholder="notion.site/..."+ className="flex w-full rounded-md border-0 py-1.5 text-foreground bg-background shadow-sm ring-1 ring-inset ring-input placeholder:text-muted-foreground focus:ring-2 focus:ring-inset focus:ring-gray-400 sm:text-sm sm:leading-6"+ value={notionLink || ""}+ onChange={(e) => setNotionLink(e.target.value)}+ />+ </div>+ <small className="text-xs text-muted-foreground">+ Your Notion page needs to be shared publicly. Learn more.+ </small>+ </div>+ <div className="flex justify-center">+ <Button+ type="submit"+ className="w-full lg:w-1/2"+ disabled={uploading || !notionLink}+ loading={uploading}+ >+ {uploading ? "Saving..." : "Save Notion Link"}+ </Button></div>- </div>- </div>-- <div className="flex justify-center">- <Button- type="submit"- className="w-full lg:w-1/2"- disabled={uploading || !currentFile}- loading={uploading}>- {uploading ? "Uploading..." : "Upload Document"}- </Button>- </div>- </form>- </DialogDescription>- </DialogHeader>+ </form>+ </CardContent>+ </Card>+ </TabsContent>+ </Tabs></DialogContent></Dialog>);diff --git a/components/ui/card.tsx b/components/ui/card.tsxnew file mode 100644index 00000000..afa13ecf--- /dev/null+++ b/components/ui/card.tsx@@ -0,0 +1,79 @@+import * as React from "react"++import { cn } from "@/lib/utils"++const Card = React.forwardRef<+ HTMLDivElement,+ React.HTMLAttributes<HTMLDivElement>+>(({ className, ...props }, ref) => (+ <div+ ref={ref}+ className={cn(+ "rounded-lg border bg-card text-card-foreground shadow-sm",+ className+ )}+ {...props}+ />+))+Card.displayName = "Card"++const CardHeader = React.forwardRef<+ HTMLDivElement,+ React.HTMLAttributes<HTMLDivElement>+>(({ className, ...props }, ref) => (+ <div+ ref={ref}+ className={cn("flex flex-col space-y-1.5 p-6", className)}+ {...props}+ />+))+CardHeader.displayName = "CardHeader"++const CardTitle = React.forwardRef<+ HTMLParagraphElement,+ React.HTMLAttributes<HTMLHeadingElement>+>(({ className, ...props }, ref) => (+ <h3+ ref={ref}+ className={cn(+ "text-2xl font-semibold leading-none tracking-tight",+ className+ )}+ {...props}+ />+))+CardTitle.displayName = "CardTitle"++const CardDescription = React.forwardRef<+ HTMLParagraphElement,+ React.HTMLAttributes<HTMLParagraphElement>+>(({ className, ...props }, ref) => (+ <p+ ref={ref}+ className={cn("text-sm text-muted-foreground", className)}+ {...props}+ />+))+CardDescription.displayName = "CardDescription"++const CardContent = React.forwardRef<+ HTMLDivElement,+ React.HTMLAttributes<HTMLDivElement>+>(({ className, ...props }, ref) => (+ <div ref={ref} className={cn("p-6 pt-0", className)} {...props} />+))+CardContent.displayName = "CardContent"++const CardFooter = React.forwardRef<+ HTMLDivElement,+ React.HTMLAttributes<HTMLDivElement>+>(({ className, ...props }, ref) => (+ <div+ ref={ref}+ className={cn("flex items-center p-6 pt-0", className)}+ {...props}+ />+))+CardFooter.displayName = "CardFooter"++export { Card, CardHeader, CardFooter, CardTitle, CardDescription, CardContent }diff --git a/components/ui/dialog.tsx b/components/ui/dialog.tsxindex 879dbf44..0556cc9b 100644--- a/components/ui/dialog.tsx+++ b/components/ui/dialog.tsx@@ -1,14 +1,14 @@-"use client"+"use client";-import * as React from "react"-import * as DialogPrimitive from "@radix-ui/react-dialog"-import X from "@/components/shared/icons/x"+import * as React from "react";+import * as DialogPrimitive from "@radix-ui/react-dialog";+import X from "@/components/shared/icons/x";-import { cn } from "@/lib/utils"+import { cn } from "@/lib/utils";-const Dialog = DialogPrimitive.Root+const Dialog = DialogPrimitive.Root;-const DialogTrigger = DialogPrimitive.Trigger+const DialogTrigger = DialogPrimitive.Trigger;const DialogPortal = ({children,@@ -19,8 +19,8 @@ const DialogPortal = ({{children}</div></DialogPrimitive.Portal>-)-DialogPortal.displayName = DialogPrimitive.Portal.displayName+);+DialogPortal.displayName = DialogPrimitive.Portal.displayName;const DialogOverlay = React.forwardRef<React.ElementRef<typeof DialogPrimitive.Overlay>,@@ -30,16 +30,18 @@ const DialogOverlay = React.forwardRef<ref={ref}className={cn("fixed inset-0 z-50 bg-background/80 backdrop-blur-sm transition-all duration-100 data-[state=closed]:animate-out data-[state=closed]:fade-out data-[state=open]:fade-in",- className+ className,)}{...props}/>-))-DialogOverlay.displayName = DialogPrimitive.Overlay.displayName+));+DialogOverlay.displayName = DialogPrimitive.Overlay.displayName;const DialogContent = React.forwardRef<React.ElementRef<typeof DialogPrimitive.Content>,- React.ComponentPropsWithoutRef<typeof DialogPrimitive.Content>+ React.ComponentPropsWithoutRef<typeof DialogPrimitive.Content> & {+ isDocumentDialog?: boolean;+ }>(({ className, children, ...props }, ref) => (<DialogPortal><DialogOverlay />@@ -47,19 +49,24 @@ const DialogContent = React.forwardRef<ref={ref}className={cn("fixed z-50 grid w-full md:w-1/2 gap-4 rounded-t-lg border border-gray-800 bg-background p-6 shadow-lg animate-in data-[state=open]:fade-in-90 data-[state=open]:slide-in-from-bottom-10 sm:max-w-xl sm:rounded-lg sm:zoom-in-90 data-[state=open]:sm:slide-in-from-bottom-0",- className+ className,)}{...props}>{children}- <DialogPrimitive.Close className="absolute right-4 top-4 rounded-sm opacity-70 ring-offset-background transition-opacity hover:opacity-100 focus:outline-none focus:ring-2 focus:ring-ring focus:ring-offset-2 disabled:pointer-events-none data-[state=open]:bg-accent data-[state=open]:text-muted-foreground">+ <DialogPrimitive.Close+ className={cn(+ "absolute rounded-sm opacity-70 ring-offset-background transition-opacity hover:opacity-100 focus:outline-none focus:ring-2 focus:ring-ring focus:ring-offset-2 disabled:pointer-events-none data-[state=open]:bg-accent data-[state=open]:text-muted-foreground",+ props.isDocumentDialog ? "right-8 top-20" : "right-4 top-4",+ )}+ ><X className="h-4 w-4" /><span className="sr-only">Close</span></DialogPrimitive.Close></DialogPrimitive.Content></DialogPortal>-))-DialogContent.displayName = DialogPrimitive.Content.displayName+));+DialogContent.displayName = DialogPrimitive.Content.displayName;const DialogHeader = ({className,@@ -68,12 +75,12 @@ const DialogHeader = ({<divclassName={cn("flex flex-col space-y-1.5 text-center sm:text-left",- className+ className,)}{...props}/>-)-DialogHeader.displayName = "DialogHeader"+);+DialogHeader.displayName = "DialogHeader";const DialogFooter = ({className,@@ -82,12 +89,12 @@ const DialogFooter = ({<divclassName={cn("flex flex-col-reverse sm:flex-row sm:justify-end sm:space-x-2",- className+ className,)}{...props}/>-)-DialogFooter.displayName = "DialogFooter"+);+DialogFooter.displayName = "DialogFooter";const DialogTitle = React.forwardRef<React.ElementRef<typeof DialogPrimitive.Title>,@@ -97,12 +104,12 @@ const DialogTitle = React.forwardRef<ref={ref}className={cn("text-lg font-semibold leading-none tracking-tight",- className+ className,)}{...props}/>-))-DialogTitle.displayName = DialogPrimitive.Title.displayName+));+DialogTitle.displayName = DialogPrimitive.Title.displayName;const DialogDescription = React.forwardRef<React.ElementRef<typeof DialogPrimitive.Description>,@@ -113,8 +120,8 @@ const DialogDescription = React.forwardRef<className={cn("text-sm text-muted-foreground", className)}{...props}/>-))-DialogDescription.displayName = DialogPrimitive.Description.displayName+));+DialogDescription.displayName = DialogPrimitive.Description.displayName;export {Dialog,@@ -124,4 +131,4 @@ export {DialogFooter,DialogTitle,DialogDescription,-}+};diff --git a/components/ui/tabs.tsx b/components/ui/tabs.tsxnew file mode 100644index 00000000..f57fffdb--- /dev/null+++ b/components/ui/tabs.tsx@@ -0,0 +1,53 @@+import * as React from "react"+import * as TabsPrimitive from "@radix-ui/react-tabs"++import { cn } from "@/lib/utils"++const Tabs = TabsPrimitive.Root++const TabsList = React.forwardRef<+ React.ElementRef<typeof TabsPrimitive.List>,+ React.ComponentPropsWithoutRef<typeof TabsPrimitive.List>+>(({ className, ...props }, ref) => (+ <TabsPrimitive.List+ ref={ref}+ className={cn(+ "inline-flex h-10 items-center justify-center rounded-md bg-muted p-1 text-muted-foreground",+ className+ )}+ {...props}+ />+))+TabsList.displayName = TabsPrimitive.List.displayName++const TabsTrigger = React.forwardRef<+ React.ElementRef<typeof TabsPrimitive.Trigger>,+ React.ComponentPropsWithoutRef<typeof TabsPrimitive.Trigger>+>(({ className, ...props }, ref) => (+ <TabsPrimitive.Trigger+ ref={ref}+ className={cn(+ "inline-flex items-center justify-center whitespace-nowrap rounded-sm px-3 py-1.5 text-sm font-medium ring-offset-background transition-all focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-ring focus-visible:ring-offset-2 disabled:pointer-events-none disabled:opacity-50 data-[state=active]:bg-background data-[state=active]:text-foreground data-[state=active]:shadow-sm",+ className+ )}+ {...props}+ />+))+TabsTrigger.displayName = TabsPrimitive.Trigger.displayName++const TabsContent = React.forwardRef<+ React.ElementRef<typeof TabsPrimitive.Content>,+ React.ComponentPropsWithoutRef<typeof TabsPrimitive.Content>+>(({ className, ...props }, ref) => (+ <TabsPrimitive.Content+ ref={ref}+ className={cn(+ "mt-2 ring-offset-background focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-ring focus-visible:ring-offset-2",+ className+ )}+ {...props}+ />+))+TabsContent.displayName = TabsPrimitive.Content.displayName++export { Tabs, TabsList, TabsTrigger, TabsContent }diff --git a/package-lock.json b/package-lock.jsonindex 75dae71d..d8660c25 100644--- a/package-lock.json+++ b/package-lock.json@@ -26,6 +26,7 @@"@radix-ui/react-separator": "^1.0.3","@radix-ui/react-slot": "^1.0.2","@radix-ui/react-switch": "^1.0.3",+ "@radix-ui/react-tabs": "^1.0.4","@react-email/components": "^0.0.11","@stripe/stripe-js": "^2.1.7","@tailwindcss/typography": "^0.5.10",@@ -2287,6 +2288,36 @@}}},+ "node_modules/@radix-ui/react-tabs": {+ "version": "1.0.4",+ "resolved": "https://registry.npmjs.org/@radix-ui/react-tabs/-/react-tabs-1.0.4.tgz",+ "integrity": "sha512-egZfYY/+wRNCflXNHx+dePvnz9FbmssDTJBtgRfDY7e8SE5oIo3Py2eCB1ckAbh1Q7cQ/6yJZThJ++sgbxibog==",+ "dependencies": {+ "@babel/runtime": "^7.13.10",+ "@radix-ui/primitive": "1.0.1",+ "@radix-ui/react-context": "1.0.1",+ "@radix-ui/react-direction": "1.0.1",+ "@radix-ui/react-id": "1.0.1",+ "@radix-ui/react-presence": "1.0.1",+ "@radix-ui/react-primitive": "1.0.3",+ "@radix-ui/react-roving-focus": "1.0.4",+ "@radix-ui/react-use-controllable-state": "1.0.1"+ },+ "peerDependencies": {+ "@types/react": "*",+ "@types/react-dom": "*",+ "react": "^16.8 || ^17.0 || ^18.0",+ "react-dom": "^16.8 || ^17.0 || ^18.0"+ },+ "peerDependenciesMeta": {+ "@types/react": {+ "optional": true+ },+ "@types/react-dom": {+ "optional": true+ }+ }+ },"node_modules/@radix-ui/react-use-callback-ref": {"version": "1.0.1","resolved": "https://registry.npmjs.org/@radix-ui/react-use-callback-ref/-/react-use-callback-ref-1.0.1.tgz",@@ -13680,6 +13711,22 @@"@radix-ui/react-use-size": "1.0.1"}},+ "@radix-ui/react-tabs": {+ "version": "1.0.4",+ "resolved": "https://registry.npmjs.org/@radix-ui/react-tabs/-/react-tabs-1.0.4.tgz",+ "integrity": "sha512-egZfYY/+wRNCflXNHx+dePvnz9FbmssDTJBtgRfDY7e8SE5oIo3Py2eCB1ckAbh1Q7cQ/6yJZThJ++sgbxibog==",+ "requires": {+ "@babel/runtime": "^7.13.10",+ "@radix-ui/primitive": "1.0.1",+ "@radix-ui/react-context": "1.0.1",+ "@radix-ui/react-direction": "1.0.1",+ "@radix-ui/react-id": "1.0.1",+ "@radix-ui/react-presence": "1.0.1",+ "@radix-ui/react-primitive": "1.0.3",+ "@radix-ui/react-roving-focus": "1.0.4",+ "@radix-ui/react-use-controllable-state": "1.0.1"+ }+ },"@radix-ui/react-use-callback-ref": {"version": "1.0.1","resolved": "https://registry.npmjs.org/@radix-ui/react-use-callback-ref/-/react-use-callback-ref-1.0.1.tgz",diff --git a/package.json b/package.jsonindex 313d6c86..8712c2e2 100644--- a/package.json+++ b/package.json@@ -33,6 +33,7 @@"@radix-ui/react-separator": "^1.0.3","@radix-ui/react-slot": "^1.0.2","@radix-ui/react-switch": "^1.0.3",+ "@radix-ui/react-tabs": "^1.0.4","@react-email/components": "^0.0.11","@stripe/stripe-js": "^2.1.7","@tailwindcss/typography": "^0.5.10",