Papermark is the open-source DocSend alternative and secure data rooms with built-in analytics and custom domains.
fix: optimistic SWR updates for dataroom document rename
Cursor Agent6d18b9e79984
diff --git a/components/datarooms/edit-dataroom-document-modal.tsx b/components/datarooms/edit-dataroom-document-modal.tsxindex 4179876f..c69ac382 100644--- a/components/datarooms/edit-dataroom-document-modal.tsx+++ b/components/datarooms/edit-dataroom-document-modal.tsx@@ -7,6 +7,11 @@ import { toast } from "sonner";import { mutate } from "swr";import { z } from "zod";+import {+ type DataroomFolderDocument,+ type DataroomFolderWithDocuments,+} from "@/lib/swr/use-dataroom";+import { Button } from "@/components/ui/button";import {Dialog,@@ -19,6 +24,41 @@ import {import { Input } from "@/components/ui/input";import { Label } from "@/components/ui/label";+function updateDocNameInDocuments(+ _: null,+ docs: DataroomFolderDocument[] | undefined,+ docId: string,+ newName: string,+): DataroomFolderDocument[] | undefined {+ if (!docs) return docs;+ return docs.map((doc) =>+ doc.document.id === docId+ ? { ...doc, document: { ...doc.document, name: newName } }+ : doc,+ );+}++function updateDocNameInFolderTree(+ _: null,+ folders: DataroomFolderWithDocuments[] | undefined,+ docId: string,+ newName: string,+): DataroomFolderWithDocuments[] | undefined {+ if (!folders) return folders;+ const updateFolder = (+ folder: DataroomFolderWithDocuments,+ ): DataroomFolderWithDocuments => ({+ ...folder,+ documents: folder.documents.map((doc) =>+ doc.document.id === docId+ ? { ...doc, document: { ...doc.document, name: newName } }+ : doc,+ ),+ childFolders: folder.childFolders.map(updateFolder),+ });+ return folders.map(updateFolder);+}+export function EditDataroomDocumentModal({open,setOpen,@@ -61,16 +101,20 @@ export function EditDataroomDocumentModal({setLoading(true);+ const trimmedName = name.trim();+ const teamId = teamInfo?.currentTeam?.id;+ const baseKey = `/api/teams/${teamId}/datarooms/${dataroomId}`;+try {const response = await fetch(- `/api/teams/${teamInfo?.currentTeam?.id}/documents/${documentId}/update-name`,+ `/api/teams/${teamId}/documents/${documentId}/update-name`,{method: "POST",headers: {"Content-Type": "application/json",},body: JSON.stringify({- name: name.trim(),+ name: trimmedName,}),},);@@ -84,20 +128,34 @@ export function EditDataroomDocumentModal({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+ mutate(`${baseKey}/documents`, null, {+ populateCache: (_, docs) =>+ updateDocNameInDocuments(_, docs, documentId, trimmedName),+ revalidate: false,+ });+if (currentFolderPath) {mutate(- `/api/teams/${teamInfo?.currentTeam?.id}/datarooms/${dataroomId}/folders/documents/${currentFolderPath.join("/")}`,+ `${baseKey}/folders/documents/${currentFolderPath.join("/")}`,+ null,+ {+ populateCache: (_, docs) =>+ updateDocNameInDocuments(_, docs, documentId, trimmedName),+ revalidate: false,+ },);}- // Revalidate the dataroom folders tree for sidebar- mutate(- `/api/teams/${teamInfo?.currentTeam?.id}/datarooms/${dataroomId}/folders?tree=true`,- );++ mutate(`${baseKey}/folders`, null, {+ populateCache: (_, folders) =>+ updateDocNameInFolderTree(_, folders, documentId, trimmedName),+ revalidate: false,+ });+ mutate(`${baseKey}/folders?include_documents=true`, null, {+ populateCache: (_, folders) =>+ updateDocNameInFolderTree(_, folders, documentId, trimmedName),+ revalidate: false,+ });} catch (error) {setLoading(false);toast.error("Error updating document name. Please try again.");