Skip to content

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

feat: add doc to dataroom

Marc Seitzb0b3f28fa282
diff --git a/components/documents/document-card.tsx b/components/documents/document-card.tsx
index a401383a..0e18c25f 100644
--- a/components/documents/document-card.tsx
+++ b/components/documents/document-card.tsx
@@ -6,6 +6,8 @@ import { useEffect, useRef, useState } from "react";
import { TeamContextType } from "@/context/team-context";
import {
+ BetweenHorizontalStartIcon,
+ FilePlus2Icon,
FolderInputIcon,
Layers2Icon,
MoreVertical,
@@ -33,6 +35,7 @@ import { DocumentWithLinksAndLinkCountAndViewCount } from "@/lib/types";
import { nFormatter, timeAgo } from "@/lib/utils";
import { useCopyToClipboard } from "@/lib/utils/use-copy-to-clipboard";
+import { AddToDataroomModal } from "./move-dataroom-modal";
import { MoveToFolderModal } from "./move-folder-modal";
type DocumentsCardProps = {
@@ -52,6 +55,7 @@ export default function DocumentsCard({
const [isFirstClick, setIsFirstClick] = useState<boolean>(false);
const [menuOpen, setMenuOpen] = useState<boolean>(false);
const [moveFolderOpen, setMoveFolderOpen] = useState<boolean>(false);
+ const [addDataroomOpen, setAddDataroomOpen] = useState<boolean>(false);
const dropdownRef = useRef<HTMLDivElement | null>(null);
/** current folder name */
@@ -142,9 +146,7 @@ export default function DocumentsCard({
toast.promise(
fetch(
`/api/teams/${teamInfo?.currentTeam?.id}/documents/${prismaDocument.id}/duplicate`,
- {
- method: "POST",
- },
+ { method: "POST" },
).then(() => {
mutate(`/api/teams/${teamInfo?.currentTeam?.id}/documents`);
mutate(
@@ -256,6 +258,10 @@ export default function DocumentsCard({
<Layers2Icon className="mr-2 h-4 w-4" />
Duplicate document
</DropdownMenuItem>
+ <DropdownMenuItem onClick={() => setAddDataroomOpen(true)}>
+ <BetweenHorizontalStartIcon className="mr-2 h-4 w-4" />
+ Add to dataroom
+ </DropdownMenuItem>
<DropdownMenuSeparator />
<DropdownMenuItem
onClick={(event) => handleButtonClick(event, prismaDocument.id)}
@@ -281,6 +287,15 @@ export default function DocumentsCard({
documentName={prismaDocument.name}
/>
) : null}
+
+ {addDataroomOpen ? (
+ <AddToDataroomModal
+ open={addDataroomOpen}
+ setOpen={setAddDataroomOpen}
+ documentId={prismaDocument.id}
+ documentName={prismaDocument.name}
+ />
+ ) : null}
</>
);
}
diff --git a/components/documents/move-dataroom-modal.tsx b/components/documents/move-dataroom-modal.tsx
new file mode 100644
index 00000000..618b40a5
--- /dev/null
+++ b/components/documents/move-dataroom-modal.tsx
@@ -0,0 +1,146 @@
+import { useRouter } from "next/router";
+
+import { useState } from "react";
+
+import { useTeam } from "@/context/team-context";
+import { toast } from "sonner";
+import { mutate } from "swr";
+import { select } from "ts-pattern/dist/patterns";
+
+import { SidebarFolderTreeSelection } from "@/components/sidebar-folders";
+import { Button } from "@/components/ui/button";
+import {
+ Dialog,
+ DialogContent,
+ DialogDescription,
+ DialogFooter,
+ DialogHeader,
+ DialogTitle,
+} from "@/components/ui/dialog";
+
+import useDatarooms from "@/lib/swr/use-datarooms";
+
+import { ScrollArea } from "../ui/scroll-area";
+import {
+ Select,
+ SelectContent,
+ SelectGroup,
+ SelectItem,
+ SelectLabel,
+ SelectTrigger,
+ SelectValue,
+} from "../ui/select";
+
+export type TSelectedDataroom = { id: string; name: string } | null;
+
+export function AddToDataroomModal({
+ open,
+ setOpen,
+ documentId,
+ documentName,
+}: {
+ open: boolean;
+ setOpen: React.Dispatch<React.SetStateAction<boolean>>;
+ documentId?: string;
+ documentName?: string;
+}) {
+ const router = useRouter();
+ const [selectedDataroom, setSelectedDataroom] = useState<string | null>(null);
+ const [loading, setLoading] = useState<boolean>(false);
+
+ const teamInfo = useTeam();
+ const teamId = teamInfo?.currentTeam?.id;
+
+ const { datarooms } = useDatarooms();
+
+ const handleSubmit = async (event: any) => {
+ event.preventDefault();
+ event.stopPropagation();
+
+ if (!selectedDataroom) return;
+
+ setLoading(true);
+ try {
+ const response = await fetch(
+ `/api/teams/${teamId}/documents/${documentId}/add-to-dataroom`,
+ {
+ method: "POST",
+ headers: {
+ "Content-Type": "application/json",
+ },
+ body: JSON.stringify({
+ dataroomId: selectedDataroom,
+ }),
+ },
+ );
+
+ if (!response.ok) {
+ const { message } = await response.json();
+ setLoading(false);
+ toast.error(message);
+ return;
+ }
+
+ toast.success("Document added to dataroom successfully!");
+ } catch (error) {
+ console.error("Error adding document to dataroom", error);
+ toast.error("Failed to add document to dataroom. Try again.");
+ } finally {
+ setLoading(false);
+ setOpen(false);
+ }
+ };
+
+ return (
+ <Dialog open={open} onOpenChange={setOpen}>
+ <DialogContent className="sm:max-w-[425px]">
+ <DialogHeader className="text-start">
+ <DialogTitle>
+ <span className="font-bold">{documentName}</span>
+ </DialogTitle>
+ <DialogDescription>
+ Add your document to a dataroom.
+ </DialogDescription>
+ </DialogHeader>
+ <Select onValueChange={(value) => setSelectedDataroom(value)}>
+ <SelectTrigger className="min-w-fit">
+ <SelectValue placeholder="Select a dataroom" />
+ </SelectTrigger>
+ <SelectContent>
+ {datarooms?.map((dataroom) => (
+ <SelectItem key={dataroom.id} value={dataroom.id}>
+ {dataroom.name}
+ </SelectItem>
+ ))}
+ </SelectContent>
+ </Select>
+ <form>
+ <div className="mb-2"></div>
+
+ <DialogFooter>
+ <Button
+ onClick={handleSubmit}
+ className="flex h-9 w-full gap-1"
+ loading={loading}
+ disabled={!selectedDataroom}
+ >
+ {!selectedDataroom ? (
+ "Select a dataroom"
+ ) : (
+ <>
+ Add to{" "}
+ <span className="font-medium">
+ {
+ datarooms?.filter((d) => d.id === selectedDataroom)[0]
+ .name
+ }
+ </span>
+ </>
+ )}
+ </Button>
+ </DialogFooter>
+ </form>
+ </DialogContent>
+ </Dialog>
+ );
+}
diff --git a/pages/api/teams/[teamId]/datarooms/create-from-folder.ts b/pages/api/teams/[teamId]/datarooms/create-from-folder.ts
index d2dcf7ef..4b8403da 100644
--- a/pages/api/teams/[teamId]/datarooms/create-from-folder.ts
+++ b/pages/api/teams/[teamId]/datarooms/create-from-folder.ts
@@ -156,6 +156,7 @@ export default async function handle(
if (
!team.plan.includes("drtrial") &&
+ ["business", "datarooms"].includes(team.plan) &&
limits &&
team._count.datarooms >= limits.datarooms
) {
@@ -171,6 +172,12 @@ export default async function handle(
.json({ message: "Trial data room already exists" });
}
+ if (["free", "pro"].includes(team.plan)) {
+ return res
+ .status(400)
+ .json({ message: "You need a Business plan to create a data room" });
+ }
+
// Fetch the folder structure
const folderContents = await fetchFolderContents(folderId);
diff --git a/pages/api/teams/[teamId]/documents/[id]/add-to-dataroom.ts b/pages/api/teams/[teamId]/documents/[id]/add-to-dataroom.ts
new file mode 100644
index 00000000..e6436f75
--- /dev/null
+++ b/pages/api/teams/[teamId]/documents/[id]/add-to-dataroom.ts
@@ -0,0 +1,91 @@
+import { NextApiRequest, NextApiResponse } from "next";
+
+import { authOptions } from "@/pages/api/auth/[...nextauth]";
+import { getServerSession } from "next-auth/next";
+import { version } from "os";
+
+import { errorhandler } from "@/lib/errorHandler";
+import prisma from "@/lib/prisma";
+import { getTeamWithUsersAndDocument } from "@/lib/team/helper";
+import { CustomUser } from "@/lib/types";
+
+export default async function handle(
+ req: NextApiRequest,
+ res: NextApiResponse,
+) {
+ if (req.method === "POST") {
+ // GET /api/teams/:teamId/documents/:id/add-to-dataroom
+ const session = await getServerSession(req, res, authOptions);
+ if (!session) {
+ return res.status(401).end("Unauthorized");
+ }
+
+ const { teamId, id: docId } = req.query as { teamId: string; id: string };
+ const { dataroomId } = req.body as { dataroomId: string };
+
+ const userId = (session.user as CustomUser).id;
+
+ try {
+ const team = await prisma.team.findUnique({
+ where: {
+ id: teamId,
+ users: {
+ some: {
+ userId,
+ },
+ },
+ documents: {
+ some: {
+ id: {
+ equals: docId,
+ },
+ },
+ },
+ },
+ select: {
+ id: true,
+ plan: true,
+ },
+ });
+
+ if (!team) {
+ return res.status(401).end("Unauthorized");
+ }
+
+ if (team.plan === "free" || team.plan === "pro") {
+ return res.status(403).json({
+ message: "Upgrade your plan to use datarooms.",
+ });
+ }
+
+ try {
+ await prisma.dataroom.update({
+ where: {
+ id: dataroomId,
+ },
+ data: {
+ documents: {
+ create: {
+ documentId: docId,
+ },
+ },
+ },
+ });
+ } catch (error) {
+ return res.status(500).json({
+ message: "Document already exists in dataroom!",
+ });
+ }
+
+ return res.status(200).json({
+ message: "Document added to dataroom!",
+ });
+ } catch (error) {
+ errorhandler(error, res);
+ }
+ } else {
+ // We only allow POST requests
+ res.setHeader("Allow", ["POST"]);
+ return res.status(405).end(`Method ${req.method} Not Allowed`);
+ }
+}