Papermark is the open-source DocSend alternative and secure data rooms with built-in analytics and custom domains.
feat: add rename document option to dataroom document card
Cursor Agenta005f62f4eea
diff --git a/components/datarooms/dataroom-document-card.tsx b/components/datarooms/dataroom-document-card.tsxindex cf7f7642..940a0573 100644--- a/components/datarooms/dataroom-document-card.tsx+++ b/components/datarooms/dataroom-document-card.tsx@@ -8,6 +8,7 @@ import { TeamContextType } from "@/context/team-context";import {ArchiveXIcon,BetweenHorizontalStartIcon,+ FilePenIcon,FileSlidersIcon,FolderInputIcon,MoreVertical,@@ -39,6 +40,7 @@ import {import { AddToDataroomModal } from "../documents/add-document-to-dataroom-modal";import { DocumentPreviewButton } from "../documents/document-preview-button";import FileProcessStatusBar from "../documents/file-process-status-bar";+import { EditDataroomDocumentModal } from "./edit-dataroom-document-modal";import { SetUnifiedPermissionsModal } from "./groups/set-unified-permissions-modal";import { MoveToDataroomFolderModal } from "./move-dataroom-folder-modal";@@ -74,6 +76,7 @@ export default function DataroomDocumentCard({const [isFirstClick, setIsFirstClick] = useState<boolean>(false);const [menuOpen, setMenuOpen] = useState<boolean>(false);const [moveFolderOpen, setMoveFolderOpen] = useState<boolean>(false);+ const [renameOpen, setRenameOpen] = useState<boolean>(false);const dropdownRef = useRef<HTMLDivElement | null>(null);const [addDataRoomOpen, setAddDataRoomOpen] = useState<boolean>(false);@@ -92,6 +95,14 @@ export default function DataroomDocumentCard({}}, [moveFolderOpen]);+ useEffect(() => {+ if (!renameOpen) {+ setTimeout(() => {+ document.body.style.pointerEvents = "";+ });+ }+ }, [renameOpen]);+useEffect(() => {function handleClickOutside(event: { target: any }) {if (dropdownRef.current && !dropdownRef.current.contains(event.target)) {@@ -287,6 +298,15 @@ export default function DataroomDocumentCard({</DropdownMenuTrigger><DropdownMenuContent align="end" ref={dropdownRef}><DropdownMenuLabel>Actions</DropdownMenuLabel>+ <DropdownMenuItem+ onClick={(e) => {+ e.stopPropagation();+ setRenameOpen(true);+ }}+ >+ <FilePenIcon className="mr-2 h-4 w-4" />+ Rename+ </DropdownMenuItem><DropdownMenuItemonClick={(e) => {e.stopPropagation();@@ -353,6 +373,15 @@ export default function DataroomDocumentCard({/>)}</div>+ {renameOpen ? (+ <EditDataroomDocumentModal+ open={renameOpen}+ setOpen={setRenameOpen}+ documentId={dataroomDocument.document.id}+ documentName={dataroomDocument.document.name}+ dataroomId={dataroomId}+ />+ ) : null}{addDataRoomOpen ? (<AddToDataroomModalopen={addDataRoomOpen}diff --git a/components/datarooms/edit-dataroom-document-modal.tsx b/components/datarooms/edit-dataroom-document-modal.tsxnew file mode 100644index 00000000..4179876f--- /dev/null+++ b/components/datarooms/edit-dataroom-document-modal.tsx@@ -0,0 +1,138 @@+import { useRouter } from "next/router";++import { useState } from "react";++import { useTeam } from "@/context/team-context";+import { toast } from "sonner";+import { mutate } from "swr";+import { z } from "zod";++import { Button } from "@/components/ui/button";+import {+ Dialog,+ DialogContent,+ DialogDescription,+ DialogFooter,+ DialogHeader,+ DialogTitle,+} from "@/components/ui/dialog";+import { Input } from "@/components/ui/input";+import { Label } from "@/components/ui/label";++export function EditDataroomDocumentModal({+ open,+ setOpen,+ documentId,+ documentName,+ dataroomId,+}: {+ open: boolean;+ setOpen: React.Dispatch<React.SetStateAction<boolean>>;+ documentId: string;+ documentName: string;+ dataroomId: string;+}) {+ const [name, setName] = useState<string>(documentName);+ const [loading, setLoading] = useState<boolean>(false);++ const teamInfo = useTeam();+ const router = useRouter();+ const currentFolderPath = router.query.name as string[] | undefined;++ const editDocumentNameSchema = z.object({+ name: z+ .string()+ .min(1, {+ message: "Please provide a document name.",+ })+ .max(255, {+ message: "Document name is too long.",+ }),+ });++ const handleSubmit = async (event: React.FormEvent) => {+ event.preventDefault();+ event.stopPropagation();++ const validation = editDocumentNameSchema.safeParse({ name });+ if (!validation.success) {+ return toast.error(validation.error.errors[0].message);+ }++ setLoading(true);++ try {+ const response = await fetch(+ `/api/teams/${teamInfo?.currentTeam?.id}/documents/${documentId}/update-name`,+ {+ method: "POST",+ headers: {+ "Content-Type": "application/json",+ },+ body: JSON.stringify({+ name: name.trim(),+ }),+ },+ );++ if (!response.ok) {+ const { error, message } = await response.json();+ setLoading(false);+ toast.error(error || message || "Failed to update document name");+ return;+ }++ toast.success("Document name updated successfully!");++ // Revalidate the dataroom documents cache+ mutate(+ `/api/teams/${teamInfo?.currentTeam?.id}/datarooms/${dataroomId}/documents`,+ );+ // Revalidate folder documents if the document is in a folder+ if (currentFolderPath) {+ mutate(+ `/api/teams/${teamInfo?.currentTeam?.id}/datarooms/${dataroomId}/folders/documents/${currentFolderPath.join("/")}`,+ );+ }+ // Revalidate the dataroom folders tree for sidebar+ mutate(+ `/api/teams/${teamInfo?.currentTeam?.id}/datarooms/${dataroomId}/folders?tree=true`,+ );+ } catch (error) {+ setLoading(false);+ toast.error("Error updating document name. Please try again.");+ return;+ } finally {+ setLoading(false);+ setOpen(false);+ }+ };++ return (+ <Dialog open={open} onOpenChange={setOpen}>+ <DialogContent className="sm:max-w-[425px]">+ <DialogHeader className="text-start">+ <DialogTitle>Rename Document</DialogTitle>+ <DialogDescription>Enter a new document name.</DialogDescription>+ </DialogHeader>+ <form onSubmit={handleSubmit}>+ <Label htmlFor="document-name-update" className="opacity-80">+ Document Name+ </Label>+ <Input+ id="document-name-update"+ value={name}+ placeholder="document-name"+ className="mb-4 mt-1 w-full"+ onChange={(e) => setName(e.target.value)}+ />+ <DialogFooter>+ <Button type="submit" className="h-9 w-full" loading={loading}>+ Update name+ </Button>+ </DialogFooter>+ </form>+ </DialogContent>+ </Dialog>+ );+}