Papermark is the open-source DocSend alternative and secure data rooms with built-in analytics and custom domains.
feat(onboarding): add link settings step in welcome
Marc Seitz739a6da8473a
diff --git a/components/welcome/containers/link-option-container.tsx b/components/welcome/containers/link-option-container.tsxnew file mode 100644index 00000000..a5c290a8--- /dev/null+++ b/components/welcome/containers/link-option-container.tsx@@ -0,0 +1,166 @@+import { useRouter } from "next/router";++import { Dispatch, SetStateAction, useState } from "react";++import { useTeam } from "@/context/team-context";+import { LinkType } from "@prisma/client";+import { motion } from "framer-motion";+import { toast } from "sonner";++import { DEFAULT_LINK_TYPE } from "@/components/links/link-sheet";+import { LinkOptions } from "@/components/links/link-sheet/link-options";+import { Button } from "@/components/ui/button";++import { STAGGER_CHILD_VARIANTS } from "@/lib/constants";+import {+ convertDataUrlToFile,+ copyToClipboard,+ uploadImage,+} from "@/lib/utils";++export function LinkOptionContainer({+ currentLinkId,+ currentDocId,+ currentDataroomId,+ linkData,+ setLinkData,+}: {+ currentLinkId: string | null;+ currentDocId?: string | null;+ currentDataroomId?: string | null;+ linkData: DEFAULT_LINK_TYPE;+ setLinkData: Dispatch<SetStateAction<DEFAULT_LINK_TYPE>>;+}) {+ const router = useRouter();+ const [isLoading, setIsLoading] = useState<boolean>(false);+ const [showLinkSettings, setShowLinkSettings] = useState<boolean>(true);++ const teamInfo = useTeam();+ const teamId = teamInfo?.currentTeam?.id;++ const handleSubmit = async (event: any) => {+ event.preventDefault();++ setIsLoading(true);++ let blobUrl: string | null =+ linkData.metaImage && linkData.metaImage.startsWith("data:")+ ? null+ : linkData.metaImage;+ if (linkData.metaImage && linkData.metaImage.startsWith("data:")) {+ const blob = convertDataUrlToFile({ dataUrl: linkData.metaImage });+ blobUrl = await uploadImage(blob);+ setLinkData({ ...linkData, metaImage: blobUrl });+ }++ const response = await fetch(`/api/links/${currentLinkId}`, {+ method: "PUT",+ headers: {+ "Content-Type": "application/json",+ },+ body: JSON.stringify({+ ...linkData,+ metaImage: blobUrl,+ targetId: currentDataroomId || currentDocId,+ linkType: currentDataroomId+ ? LinkType.DATAROOM_LINK+ : LinkType.DOCUMENT_LINK,+ teamId,+ }),+ });++ if (!response.ok) {+ const { error } = await response.json();+ toast.error(error);+ setIsLoading(false);+ return;+ }++ if (currentDataroomId) {+ copyToClipboard(+ `${process.env.NEXT_PUBLIC_MARKETING_URL}/view/${currentLinkId}`,+ `Link copied to clipboard. Redirecting to dataroom page...`,+ );+ router.push(`/datarooms/${currentDataroomId}`);+ } else {+ copyToClipboard(+ `${process.env.NEXT_PUBLIC_MARKETING_URL}/view/${currentLinkId}`,+ `Link copied to clipboard. Redirecting to document page...`,+ );+ router.push(`/documents/${currentDocId}`);+ }+ setIsLoading(false);+ };++ return (+ <motion.div+ className="z-10 flex flex-col space-y-10 text-center"+ variants={{+ hidden: { opacity: 0, scale: 0.95 },+ show: {+ opacity: 1,+ scale: 1,+ transition: {+ staggerChildren: 0.2,+ },+ },+ }}+ initial="hidden"+ animate="show"+ exit="hidden"+ transition={{ duration: 0.3, type: "spring" }}+ >+ <motion.div+ variants={STAGGER_CHILD_VARIANTS}+ className="flex flex-col items-center space-y-10 text-center"+ >+ <h1 className="font-display text-3xl font-semibold text-foreground transition-colors sm:text-4xl">+ Share your unique link+ </h1>+ </motion.div>++ <motion.div variants={STAGGER_CHILD_VARIANTS}>+ {showLinkSettings && (+ <main className="max-h-[calc(100dvh-10rem)] min-h-[300px] overflow-y-scroll scrollbar-hide">+ <div className="flex flex-col justify-center">+ <div className="mb-4 flex items-center justify-center">+ <Button onClick={() => setShowLinkSettings(false)}>+ Share {currentDataroomId ? `Dataroom` : `Document`}+ </Button>+ </div>+ <div className="w-full max-w-xs pb-8 sm:max-w-lg">+ <LinkOptions+ data={linkData}+ setData={setLinkData}+ linkType={LinkType.DOCUMENT_LINK}+ />+ </div>+ </div>+ </main>+ )}+ {!showLinkSettings &&+ currentLinkId &&+ (currentDocId || currentDataroomId) && (+ <main className="max-h-[calc(100dvh-10rem)] min-h-[300px] overflow-y-scroll scrollbar-hide">+ <div className="flex flex-col justify-center">+ <div className="relative">+ <div className="flex py-8">+ <div className="flex w-fit focus-within:z-10">+ <p className="block rounded-md border-0 bg-secondary px-4 py-1.5 text-left leading-6 text-secondary-foreground md:min-w-[500px]">+ {`${process.env.NEXT_PUBLIC_MARKETING_URL}/view/${currentLinkId}`}+ </p>+ </div>+ </div>+ </div>+ <div className="mb-4 flex items-center justify-center">+ <Button onClick={handleSubmit} loading={isLoading}>+ Save & Copy Link+ </Button>+ </div>+ </div>+ </main>+ )}+ </motion.div>+ </motion.div>+ );+}diff --git a/components/welcome/containers/upload-container.tsx b/components/welcome/containers/upload-container.tsxnew file mode 100644index 00000000..313ab127--- /dev/null+++ b/components/welcome/containers/upload-container.tsx@@ -0,0 +1,263 @@+import { useRouter } from "next/router";++import { Dispatch, SetStateAction, useState } from "react";++import { useTeam } from "@/context/team-context";+import { motion } from "framer-motion";+import { toast } from "sonner";++import DocumentUpload from "@/components/document-upload";+import { Button } from "@/components/ui/button";++import { useAnalytics } from "@/lib/analytics";+import { STAGGER_CHILD_VARIANTS } from "@/lib/constants";+import { DocumentData, createDocument } from "@/lib/documents/create-document";+import { putFile } from "@/lib/files/put-file";+import { getSupportedContentType } from "@/lib/utils/get-content-type";++export function UploadContainer({+ currentFile,+ setCurrentFile,+ setCurrentBlob,+ setCurrentLinkId,+ setCurrentDocId,+ dataroomId,+}: {+ currentFile: File | null;+ setCurrentFile: Dispatch<SetStateAction<File | null>>;+ setCurrentBlob: Dispatch<SetStateAction<boolean>>;+ setCurrentLinkId: Dispatch<SetStateAction<string | null>>;+ setCurrentDocId?: Dispatch<SetStateAction<string | null>>;+ dataroomId?: string;+}) {+ const router = useRouter();+ const analytics = useAnalytics();+ const [uploading, setUploading] = useState<boolean>(false);++ const teamInfo = useTeam();+ const teamId = teamInfo?.currentTeam?.id as string;++ const handleBrowserUpload = async (event: any) => {+ event.preventDefault();++ // Check if the file is chosen+ if (!currentFile) {+ toast.error("Please select a file to upload.");+ return; // prevent form from submitting+ }++ try {+ setUploading(true);++ const contentType = currentFile.type;+ const supportedFileType = getSupportedContentType(currentFile.type);++ if (!supportedFileType) {+ setUploading(false);+ toast.error(+ "Unsupported file format. Please upload a PDF, Powerpoint, Excel, or Word file.",+ );+ return;+ }++ const { type, data, numPages } = await putFile({+ file: currentFile,+ teamId,+ });++ setCurrentFile(null);+ setCurrentBlob(true);++ const documentData: DocumentData = {+ name: currentFile.name,+ key: data!,+ storageType: type!,+ contentType: contentType,+ supportedFileType: supportedFileType,+ };+ // create a document in the database+ const response = await createDocument({+ documentData,+ teamId,+ numPages,+ createLink: dataroomId ? false : true, // don't create a link if the document is being added to a dataroom+ });++ if (response) {+ const document = await response.json();++ if (dataroomId) {+ // add document to dataroom+ await fetch(+ `/api/teams/${teamId}/datarooms/${dataroomId}/documents`,+ {+ method: "POST",+ headers: {+ "Content-Type": "application/json",+ },+ body: JSON.stringify({ documentId: document.id }),+ },+ );++ analytics.capture("Document Added to Dataroom", {+ documentId: document.id,+ name: document.name,+ numPages: document.numPages,+ path: router.asPath,+ type: document.type,+ teamId: teamInfo?.currentTeam?.id,+ dataroomId: dataroomId,+ });++ // create link to dataroom+ const newLinkResponse = await fetch(`/api/links`, {+ method: "POST",+ headers: {+ "Content-Type": "application/json",+ },+ body: JSON.stringify({+ targetId: dataroomId,+ linkType: "DATAROOM_LINK",+ teamId,+ }),+ });++ if (newLinkResponse.ok) {+ const link = await newLinkResponse.json();+ setCurrentLinkId(link.id);++ analytics.capture("Link Added", {+ linkId: link.id,+ dataroomId: dataroomId,+ customDomain: null,+ teamId: teamInfo?.currentTeam?.id,+ });+ }++ setTimeout(() => {+ setUploading(false);+ }, 2000);+ } else {+ const linkId = document.links[0].id;++ // track the event+ analytics.capture("Document Added", {+ documentId: document.id,+ name: document.name,+ numPages: document.numPages,+ path: router.asPath,+ type: document.type,+ contentType: document.contentType,+ teamId: teamInfo?.currentTeam?.id,+ });+ analytics.capture("Link Added", {+ linkId: linkId,+ documentId: document.id,+ customDomain: null,+ teamId: teamInfo?.currentTeam?.id,+ });++ setTimeout(() => {+ setCurrentDocId && setCurrentDocId(document.id);+ setCurrentLinkId(linkId);+ setUploading(false);+ }, 2000);+ }+ }+ } catch (error) {+ console.error("An error occurred while uploading the file: ", error);+ setCurrentFile(null);+ setUploading(false);+ }+ };++ return (+ <motion.div+ className="z-10 flex flex-col space-y-10 text-center"+ variants={{+ hidden: { opacity: 0, scale: 0.95 },+ show: {+ opacity: 1,+ scale: 1,+ transition: {+ staggerChildren: 0.2,+ },+ },+ }}+ initial="hidden"+ animate="show"+ exit="hidden"+ transition={{ duration: 0.3, type: "spring" }}+ >+ <motion.div+ variants={STAGGER_CHILD_VARIANTS}+ className="flex flex-col items-center space-y-10 text-center"+ >+ <h1 className="font-display text-balance text-3xl font-semibold text-foreground transition-colors sm:text-4xl">+ {dataroomId+ ? `Upload your first document to your data room`+ : `Upload your ${+ router.query.type === "sales-document"+ ? "document"+ : `${router.query.type}`+ }`}+ </h1>+ </motion.div>+ <motion.div variants={STAGGER_CHILD_VARIANTS}>+ <main className="mx-auto mt-8 max-w-md">+ <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}+ />+ </div>+ </div>+ </div>++ <div className="flex justify-center">+ <Button+ type="submit"+ className="w-full"+ loading={uploading}+ disabled={!currentFile}+ >+ {uploading ? "Uploading..." : "Upload Document"}+ </Button>+ </div>+ </form>++ <div className="text-xs text-muted-foreground">+ <span>Use our</span>{" "}+ <Button+ variant="link"+ className="px-0 text-xs font-normal text-muted-foreground underline hover:text-gray-700"+ onClick={async () => {+ const response = await fetch(+ "/_example/papermark-example-document.pdf",+ );+ const blob = await response.blob();+ const file = new File(+ [blob],+ "papermark-example-document.pdf",+ {+ type: "application/pdf",+ },+ );+ setCurrentFile(file);+ }}+ >+ sample document+ </Button>+ </div>+ </main>+ </motion.div>+ </motion.div>+ );+}diff --git a/components/welcome/dataroom-upload.tsx b/components/welcome/dataroom-upload.tsxindex baeaa3e9..6cbe14a6 100644--- a/components/welcome/dataroom-upload.tsx+++ b/components/welcome/dataroom-upload.tsx@@ -1,366 +1,43 @@-import { useRouter } from "next/router";+import { useState } from "react";-import { useEffect, useState } from "react";--import { useTeam } from "@/context/team-context";import { LinkType } from "@prisma/client";-import { motion } from "framer-motion";-import { usePlausible } from "next-plausible";-import { toast } from "sonner";--import DocumentUpload from "@/components/document-upload";-import {- Accordion,- AccordionContent,- AccordionItem,- AccordionTrigger,-} from "@/components/ui/accordion";-import { Button } from "@/components/ui/button";-import { useAnalytics } from "@/lib/analytics";-import { STAGGER_CHILD_VARIANTS } from "@/lib/constants";-import { DocumentData, createDocument } from "@/lib/documents/create-document";-import { putFile } from "@/lib/files/put-file";import {- convertDataUrlToFile,- copyToClipboard,- uploadImage,-} from "@/lib/utils";-import { getSupportedContentType } from "@/lib/utils/get-content-type";+ DEFAULT_LINK_PROPS,+ DEFAULT_LINK_TYPE,+} from "@/components/links/link-sheet";-import Skeleton from "../Skeleton";-import { DEFAULT_LINK_PROPS, DEFAULT_LINK_TYPE } from "../links/link-sheet";-import { LinkOptions } from "../links/link-sheet/link-options";--interface DataroomUploadProps {- dataroomId: string; // Define the dataroomId prop-}+import { LinkOptionContainer } from "./containers/link-option-container";+import { UploadContainer } from "./containers/upload-container";-export default function DataroomUpload({ dataroomId }: DataroomUploadProps) {- const router = useRouter();- const plausible = usePlausible();- const analytics = useAnalytics();- const [uploading, setUploading] = useState<boolean>(false);- const [isLoading, setIsLoading] = useState<boolean>(false);+export default function DataroomUpload({ dataroomId }: { dataroomId: string }) {const [currentFile, setCurrentFile] = useState<File | null>(null);const [currentBlob, setCurrentBlob] = useState<boolean>(false);const [currentLinkId, setCurrentLinkId] = useState<string | null>(null);- const [currentDocId, setCurrentDocId] = useState<string | null>(null);const [linkData, setLinkData] = useState<DEFAULT_LINK_TYPE>(DEFAULT_LINK_PROPS(LinkType.DATAROOM_LINK),);- const teamInfo = useTeam();- const teamId = teamInfo?.currentTeam?.id as string;- console.log("🚀 ~ DataroomUpload ~ teamId:", teamId);-- const handleFileUpload = async (event: any) => {- event.preventDefault();-- if (!currentFile) {- toast.error("Please select a file to upload.");- return;- }-- try {- setUploading(true);-- const contentType = currentFile.type;- const supportedFileType = getSupportedContentType(contentType);-- if (!supportedFileType) {- setUploading(false);- toast.error(- "Unsupported file format. Please upload a PDF or Excel file.",- );- return;- }-- const { type, data, numPages } = await putFile({- file: currentFile,- teamId,- });-- setCurrentFile(null);- setCurrentBlob(true);-- const documentData: DocumentData = {- name: currentFile.name,- key: data!,- storageType: type!,- contentType: contentType,- supportedFileType: supportedFileType,- };-- const response = await createDocument({ documentData, teamId, numPages });-- if (response) {- const document = await response.json();-- // Add document to dataroom- await fetch(`/api/teams/${teamId}/datarooms/${dataroomId}/documents`, {- method: "POST",- headers: {- "Content-Type": "application/json",- },- body: JSON.stringify({ documentId: document.id }),- });-- plausible("documentUploadedToDataroom");- analytics.capture("Document Added to Dataroom", {- documentId: document.id,- name: document.name,- numPages: document.numPages,- path: router.asPath,- type: document.type,- teamId: teamInfo?.currentTeam?.id,- dataroomId: dataroomId,- });- // create link to dataroom- const newLinkResponse = await fetch(`/api/links`, {- method: "POST",- headers: {- "Content-Type": "application/json",- },- body: JSON.stringify({- ...linkData,- targetId: dataroomId,- linkType: "DATAROOM_LINK",- teamId,- }),- });-- if (newLinkResponse.ok) {- const link = await newLinkResponse.json();- setCurrentLinkId(link.id);- }-- setTimeout(() => {- setCurrentDocId(document.id);- setUploading(false);- }, 2000);- }- } catch (error) {- console.error("An error occurred while uploading the file: ", error);- setCurrentFile(null);- setUploading(false);- }- };-- const handleSubmit = async (event: any) => {- event.preventDefault();-- setIsLoading(true);-- let blobUrl: string | null =- linkData.metaImage && linkData.metaImage.startsWith("data:")- ? null- : linkData.metaImage;- if (linkData.metaImage && linkData.metaImage.startsWith("data:")) {- const blob = convertDataUrlToFile({ dataUrl: linkData.metaImage });- blobUrl = await uploadImage(blob);- setLinkData({ ...linkData, metaImage: blobUrl });- }-- const response = await fetch(`/api/links/${currentLinkId}`, {- method: "PUT",- headers: {- "Content-Type": "application/json",- },- body: JSON.stringify({- ...linkData,- metaImage: blobUrl,- targetId: dataroomId,- linkType: LinkType.DATAROOM_LINK,- teamId,- }),- });-- if (!response.ok) {- const { error } = await response.json();- toast.error(error);- setIsLoading(false);- return;- }-- copyToClipboard(- `${process.env.NEXT_PUBLIC_MARKETING_URL}/view/${currentLinkId}`,- "Link copied to clipboard. Redirecting to dataroom page...",+ if (!currentBlob) {+ return (+ <UploadContainer+ currentFile={currentFile}+ setCurrentFile={setCurrentFile}+ setCurrentBlob={setCurrentBlob}+ setCurrentLinkId={setCurrentLinkId}+ dataroomId={dataroomId}+ />);-- router.push(`/datarooms/${dataroomId}`);- setIsLoading(false);- };-- return (- <>- {!currentBlob && (- <motion.div- className="z-10 flex flex-col space-y-10 text-center"- variants={{- hidden: { opacity: 0, scale: 0.95 },- show: {- opacity: 1,- scale: 1,- transition: {- staggerChildren: 0.2,- },- },- }}- initial="hidden"- animate="show"- exit="hidden"- transition={{ duration: 0.3, type: "spring" }}- >- <motion.div- variants={STAGGER_CHILD_VARIANTS}- className="flex flex-col items-center space-y-10 text-center"- >- <h1 className="font-display max-w-lg text-3xl font-semibold text-foreground transition-colors sm:text-4xl">- Upload first document to your data room- </h1>- </motion.div>- <motion.div variants={STAGGER_CHILD_VARIANTS}>- <main className="mt-8">- <form- encType="multipart/form-data"- onSubmit={handleFileUpload}- 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}- />- </div>- </div>- </div>-- <div className="flex justify-center">- <Button- type="submit"- className="w-full"- loading={uploading}- disabled={!currentFile}- >- {uploading ? "Uploading..." : "Upload Document"}- </Button>- </div>- </form>-- <div className="text-xs text-muted-foreground">- <span>Use our</span>{" "}- <Button- variant="link"- className="px-0 text-xs font-normal text-muted-foreground underline hover:text-gray-700"- onClick={async () => {- const response = await fetch(- "/_example/papermark-example-document.pdf",- );- const blob = await response.blob();- const file = new File(- [blob],- "papermark-example-document.pdf",- {- type: "application/pdf",- },- );- setCurrentFile(file);- }}- >- sample document- </Button>- </div>- </main>- </motion.div>- </motion.div>- )}-- {currentBlob && (- <motion.div- className="z-10 flex flex-col space-y-10 text-center"- variants={{- hidden: { opacity: 0, scale: 0.95 },- show: {- opacity: 1,- scale: 1,- transition: {- staggerChildren: 0.2,- },- },- }}- initial="hidden"- animate="show"- exit="hidden"- transition={{ duration: 0.3, type: "spring" }}- >- <motion.div- variants={STAGGER_CHILD_VARIANTS}- className="flex flex-col items-center space-y-10 text-center"- >- <h1 className="font-display text-3xl font-semibold text-foreground transition-colors sm:text-4xl">- Share your dataroom link- </h1>- </motion.div>-- <motion.div variants={STAGGER_CHILD_VARIANTS}>- {!currentLinkId && (- <main className="min-h-[300px]">- <div className="flex flex-col justify-center">- <div className="flex py-8">- <div className="flex w-full focus-within:z-10">- <Skeleton className="h-6 w-full" />- </div>- </div>- </div>- </main>- )}- {currentLinkId && (- <main className="max-h-[calc(100dvh-10rem)] min-h-[300px] overflow-y-scroll scrollbar-hide">- <div className="flex flex-col justify-center">- <div className="relative">- <div className="flex py-8">- <div className="flex w-full max-w-xs focus-within:z-10 sm:max-w-lg">- <p className="block w-full overflow-y-scroll rounded-md border-0 bg-secondary px-4 py-1.5 text-left leading-6 text-secondary-foreground md:min-w-[500px]">- {`${process.env.NEXT_PUBLIC_MARKETING_URL}/view/${currentLinkId}`}- </p>- </div>- </div>- </div>- <div className="w-full max-w-xs pb-8 sm:max-w-lg">- <Accordion type="single" collapsible>- <AccordionItem value="item-1" className="border-none">- <AccordionTrigger className="space-x-2 rounded-lg py-0">- <span className="text-sm font-medium leading-6 text-foreground">- Configure Link Options- </span>- </AccordionTrigger>- <AccordionContent className="text-left first:pt-5">- <LinkOptions- data={linkData}- setData={setLinkData}- linkType={LinkType.DATAROOM_LINK}- />- </AccordionContent>- </AccordionItem>- </Accordion>- </div>- <div className="mb-4 flex items-center justify-center">- <Button onClick={handleSubmit} loading={isLoading}>- Share Dataroom- </Button>- </div>- <div className="text-xs text-muted-foreground">- <span> One link to share multiple files</span>- </div>- </div>- </main>- )}- </motion.div>- </motion.div>- )}- </>- );+ }++ if (currentBlob) {+ return (+ <LinkOptionContainer+ currentLinkId={currentLinkId}+ linkData={linkData}+ setLinkData={setLinkData}+ currentDataroomId={dataroomId}+ />+ );+ }}diff --git a/components/welcome/upload.tsx b/components/welcome/upload.tsxindex 4a83f751..5aaaf234 100644--- a/components/welcome/upload.tsx+++ b/components/welcome/upload.tsx@@ -1,43 +1,15 @@-import { useRouter } from "next/router";-import { useState } from "react";-import { useTeam } from "@/context/team-context";import { LinkType } from "@prisma/client";-import { motion } from "framer-motion";-import { usePlausible } from "next-plausible";-import { toast } from "sonner";--import DocumentUpload from "@/components/document-upload";-import {- Accordion,- AccordionContent,- AccordionItem,- AccordionTrigger,-} from "@/components/ui/accordion";-import { Button } from "@/components/ui/button";-import { useAnalytics } from "@/lib/analytics";-import { STAGGER_CHILD_VARIANTS } from "@/lib/constants";-import { DocumentData, createDocument } from "@/lib/documents/create-document";-import { putFile } from "@/lib/files/put-file";import {- convertDataUrlToFile,- copyToClipboard,- uploadImage,-} from "@/lib/utils";-import { getSupportedContentType } from "@/lib/utils/get-content-type";--import Skeleton from "../Skeleton";-import { DEFAULT_LINK_PROPS, DEFAULT_LINK_TYPE } from "../links/link-sheet";-import { LinkOptions } from "../links/link-sheet/link-options";+ DEFAULT_LINK_PROPS,+ DEFAULT_LINK_TYPE,+} from "@/components/links/link-sheet";+import { LinkOptionContainer } from "@/components/welcome/containers/link-option-container";+import { UploadContainer } from "@/components/welcome/containers/upload-container";export default function Upload() {- const router = useRouter();- const plausible = usePlausible();- const analytics = useAnalytics();- const [uploading, setUploading] = useState<boolean>(false);- const [isLoading, setIsLoading] = useState<boolean>(false);const [currentFile, setCurrentFile] = useState<File | null>(null);const [currentBlob, setCurrentBlob] = useState<boolean>(false);const [currentLinkId, setCurrentLinkId] = useState<string | null>(null);@@ -45,306 +17,27 @@ export default function Upload() {const [linkData, setLinkData] = useState<DEFAULT_LINK_TYPE>(DEFAULT_LINK_PROPS(LinkType.DOCUMENT_LINK),);- const teamInfo = useTeam();-- const teamId = teamInfo?.currentTeam?.id as string;-- const handleBrowserUpload = async (event: any) => {- event.preventDefault();-- // Check if the file is chosen- if (!currentFile) {- toast.error("Please select a file to upload.");- return; // prevent form from submitting- }-- try {- setUploading(true);-- const contentType = currentFile.type;- const supportedFileType = getSupportedContentType(currentFile.type);-- if (!supportedFileType) {- setUploading(false);- toast.error(- "Unsupported file format. Please upload a PDF, Powerpoint, Excel, or Word file.",- );- return;- }-- const { type, data, numPages } = await putFile({- file: currentFile,- teamId,- });-- setCurrentFile(null);- setCurrentBlob(true);-- const documentData: DocumentData = {- name: currentFile.name,- key: data!,- storageType: type!,- contentType: contentType,- supportedFileType: supportedFileType,- };- // create a document in the database- const response = await createDocument({- documentData,- teamId,- numPages,- createLink: true,- });-- if (response) {- const document = await response.json();- const linkId = document.links[0].id;-- // track the event- plausible("documentUploaded");- analytics.capture("Document Added", {- documentId: document.id,- name: document.name,- numPages: document.numPages,- path: router.asPath,- type: document.type,- contentType: document.contentType,- teamId: teamInfo?.currentTeam?.id,- });- analytics.capture("Link Added", {- linkId: document.links[0].id,- documentId: document.id,- customDomain: null,- teamId: teamInfo?.currentTeam?.id,- });- setTimeout(() => {- setCurrentDocId(document.id);- setCurrentLinkId(linkId);- setUploading(false);- }, 2000);- }- } catch (error) {- console.error("An error occurred while uploading the file: ", error);- setCurrentFile(null);- setUploading(false);- }- };-- const handleSubmit = async (event: any) => {- event.preventDefault();-- setIsLoading(true);-- // Upload the image if it's a data URL- let blobUrl: string | null =- linkData.metaImage && linkData.metaImage.startsWith("data:")- ? null- : linkData.metaImage;- if (linkData.metaImage && linkData.metaImage.startsWith("data:")) {- // Convert the data URL to a blob- const blob = convertDataUrlToFile({ dataUrl: linkData.metaImage });- // Upload the blob to vercel storage- blobUrl = await uploadImage(blob);- setLinkData({ ...linkData, metaImage: blobUrl });- }-- const response = await fetch(`/api/links/${currentLinkId}`, {- method: "PUT",- headers: {- "Content-Type": "application/json",- },- body: JSON.stringify({- ...linkData,- metaImage: blobUrl,- targetId: currentDocId,- linkType: LinkType.DOCUMENT_LINK,- teamId: teamId,- }),- });-- if (!response.ok) {- // handle error with toast message- const { error } = await response.json();- toast.error(error);- setIsLoading(false);- return;- }-- copyToClipboard(- `${process.env.NEXT_PUBLIC_MARKETING_URL}/view/${currentLinkId}`,- "Link copied to clipboard. Redirecting to document page...",+ if (!currentBlob) {+ return (+ <UploadContainer+ currentFile={currentFile}+ setCurrentFile={setCurrentFile}+ setCurrentBlob={setCurrentBlob}+ setCurrentLinkId={setCurrentLinkId}+ setCurrentDocId={setCurrentDocId}+ />);-- router.push(`/documents/${currentDocId}`);- setIsLoading(false);- };-- return (- <>- {!currentBlob && (- <motion.div- className="z-10 flex flex-col space-y-10 text-center"- variants={{- hidden: { opacity: 0, scale: 0.95 },- show: {- opacity: 1,- scale: 1,- transition: {- staggerChildren: 0.2,- },- },- }}- initial="hidden"- animate="show"- exit="hidden"- transition={{ duration: 0.3, type: "spring" }}- >- <motion.div- variants={STAGGER_CHILD_VARIANTS}- className="flex flex-col items-center space-y-10 text-center"- >- <h1 className="font-display text-3xl font-semibold text-foreground transition-colors sm:text-4xl">- {`Upload your ${router.query.type === "sales-document" ? "document" : `${router.query.type}`}`}- </h1>- </motion.div>- <motion.div variants={STAGGER_CHILD_VARIANTS}>- <main className="mt-8">- <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}- />- </div>- </div>- </div>-- <div className="flex justify-center">- <Button- type="submit"- className="w-full"- loading={uploading}- disabled={!currentFile}- >- {uploading ? "Uploading..." : "Upload Document"}- </Button>- </div>- </form>-- <div className="text-xs text-muted-foreground">- <span>Use our</span>{" "}- <Button- variant="link"- className="px-0 text-xs font-normal text-muted-foreground underline hover:text-gray-700"- onClick={async () => {- const response = await fetch(- "/_example/papermark-example-document.pdf",- );- const blob = await response.blob();- const file = new File(- [blob],- "papermark-example-document.pdf",- {- type: "application/pdf",- },- );- setCurrentFile(file);- }}- >- sample document- </Button>- </div>- </main>- </motion.div>- </motion.div>- )}-- {currentBlob && (- <motion.div- className="z-10 flex flex-col space-y-10 text-center"- variants={{- hidden: { opacity: 0, scale: 0.95 },- show: {- opacity: 1,- scale: 1,- transition: {- staggerChildren: 0.2,- },- },- }}- initial="hidden"- animate="show"- exit="hidden"- transition={{ duration: 0.3, type: "spring" }}- >- <motion.div- variants={STAGGER_CHILD_VARIANTS}- className="flex flex-col items-center space-y-10 text-center"- >- <h1 className="font-display text-3xl font-semibold text-foreground transition-colors sm:text-4xl">- Share your unique link- </h1>- </motion.div>-- <motion.div variants={STAGGER_CHILD_VARIANTS}>- {!currentLinkId && (- <main className="min-h-[300px]">- <div className="flex flex-col justify-center">- <div className="flex py-8">- <div className="flex w-full focus-within:z-10">- <Skeleton className="h-6 w-full" />- </div>- </div>- </div>- </main>- )}- {currentLinkId && currentDocId && (- <main className="max-h-[calc(100dvh-10rem)] min-h-[300px] overflow-y-scroll scrollbar-hide">- <div className="flex flex-col justify-center">- <div className="relative">- <div className="flex py-8">- <div className="flex w-full max-w-xs focus-within:z-10 sm:max-w-lg">- <p className="block w-full overflow-y-scroll rounded-md border-0 bg-secondary px-4 py-1.5 text-left leading-6 text-secondary-foreground md:min-w-[500px]">- {`${process.env.NEXT_PUBLIC_MARKETING_URL}/view/${currentLinkId}`}- </p>- </div>- </div>- </div>- <div className="w-full max-w-xs pb-8 sm:max-w-lg">- <Accordion type="single" collapsible>- <AccordionItem value="item-1" className="border-none">- <AccordionTrigger className="space-x-2 rounded-lg py-0">- <span className="text-sm font-medium leading-6 text-foreground">- Configure Link Options- </span>- </AccordionTrigger>- <AccordionContent className="text-left first:pt-5">- <LinkOptions- data={linkData}- setData={setLinkData}- linkType={LinkType.DOCUMENT_LINK}- />- </AccordionContent>- </AccordionItem>- </Accordion>- </div>- <div className="mb-4 flex items-center justify-center">- <Button onClick={handleSubmit} loading={isLoading}>- Share Document- </Button>- </div>- </div>- </main>- )}- </motion.div>- </motion.div>- )}- </>- );+ }++ if (currentBlob) {+ return (+ <LinkOptionContainer+ currentLinkId={currentLinkId}+ currentDocId={currentDocId}+ linkData={linkData}+ setLinkData={setLinkData}+ />+ );+ }}