Skip to content

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

bugfix: deleted document still shown until refresh

Ashish Viradiya28262a43d1a5
diff --git a/components/documents/actions/delete-documents-modal.tsx b/components/documents/actions/delete-documents-modal.tsx
index 12875f91..4872aaa8 100644
--- a/components/documents/actions/delete-documents-modal.tsx
+++ b/components/documents/actions/delete-documents-modal.tsx
@@ -138,9 +138,33 @@ function DeleteItemsModal({
await mutate(
`/api/teams/${teamInfo?.currentTeam?.id}/folders${parentFolderPath}`,
);
- await mutate(
- `/api/teams/${teamInfo?.currentTeam?.id}/${folderPathName ? `folders/documents/${folderPathName.join("/")}` : "documents"}`,
- );
+
+ if (folderPathName && folderPathName.length > 0) {
+ await mutate(
+ `/api/teams/${
+ teamInfo?.currentTeam?.id
+ }/folders/documents/${folderPathName.join("/")}`,
+ );
+ } else {
+ const { search, sort, page, limit } = router.query;
+ const queryParts = [];
+ if (search) queryParts.push(`query=${search}`);
+ if (sort) queryParts.push(`sort=${sort}`);
+
+ const pageNum = Number(page) || 1;
+ const limitNum = Number(limit) || 10;
+
+ const paginationParams =
+ search || sort ? `&page=${pageNum}&limit=${limitNum}` : "";
+
+ if (paginationParams) queryParts.push(paginationParams.substring(1));
+ const queryString =
+ queryParts.length > 0 ? `?${queryParts.join("&")}` : "";
+
+ await mutate(
+ `/api/teams/${teamInfo?.currentTeam?.id}/documents${queryString}`,
+ );
+ }
setDeleting(false);
@@ -295,4 +319,4 @@ export function useDeleteDocumentsAndFoldersModal({
}),
[setShowDeleteItemsModal, DeleteItemsModalCallback],
);
-}
+}
\ No newline at end of file
diff --git a/components/documents/document-card.tsx b/components/documents/document-card.tsx
index 09b2f353..4b09926a 100644
--- a/components/documents/document-card.tsx
+++ b/components/documents/document-card.tsx
@@ -131,23 +131,51 @@ export default function DocumentsCard({
return;
}
+ const page = Number(queryParams["page"]) || 1;
+ const pageSize = Number(queryParams["limit"]) || 10;
+
+ const queryParts = [];
+ if (searchQuery) queryParts.push(`query=${searchQuery}`);
+ if (sortQuery) queryParts.push(`sort=${sortQuery}`);
+
+ const paginationParams =
+ searchQuery || sortQuery ? `&page=${page}&limit=${pageSize}` : "";
+ if (paginationParams) queryParts.push(paginationParams.substring(1));
+ const queryString = queryParts.length > 0 ? `?${queryParts.join("&")}` : "";
+
const endpoint = currentFolderPath
? `/folders/documents/${currentFolderPath.join("/")}`
- : "/documents";
+ : `/documents${queryString}`;
toast.promise(
fetch(`/api/teams/${teamInfo?.currentTeam?.id}/documents/${documentId}`, {
method: "DELETE",
}).then(() => {
- mutate(`/api/teams/${teamInfo?.currentTeam?.id}${endpoint}`, null, {
- populateCache: (_, docs) => {
- return docs.filter(
- (doc: DocumentWithLinksAndLinkCountAndViewCount) =>
- doc.id !== documentId,
- );
+ mutate(
+ `/api/teams/${teamInfo?.currentTeam?.id}${endpoint}`,
+ (currentData: any) => {
+ if (!currentData) return currentData;
+
+ if (Array.isArray(currentData)) {
+ return currentData.filter(
+ (doc: DocumentWithLinksAndLinkCountAndViewCount) =>
+ doc.id !== documentId,
+ );
+ } else if (currentData.documents) {
+ return {
+ ...currentData,
+ documents: currentData.documents.filter(
+ (doc: DocumentWithLinksAndLinkCountAndViewCount) =>
+ doc.id !== documentId,
+ ),
+ };
+ }
+ return currentData;
+ },
+ {
+ revalidate: false,
},
- revalidate: false,
- });
+ );
}),
{
loading: "Deleting document...",
@@ -386,4 +414,4 @@ export default function DocumentsCard({
) : null}
</>
);
-}
+}
\ No newline at end of file
diff --git a/components/layouts/breadcrumb.tsx b/components/layouts/breadcrumb.tsx
index 25147c4b..87b5086b 100644
--- a/components/layouts/breadcrumb.tsx
+++ b/components/layouts/breadcrumb.tsx
@@ -1,7 +1,7 @@
import Link from "next/link";
import { useRouter } from "next/router";
-import { useMemo } from "react";
+import React, { useMemo } from "react";
import { BreadcrumbComponent as DataroomBreadcrumb } from "@/components/datarooms/dataroom-breadcrumb";
import {
@@ -51,7 +51,7 @@ const SingleDocumentBreadcrumb = () => {
</DropdownMenuTrigger>
<DropdownMenuContent align="start">
{folders.slice(0, -1).map((folder, index) => (
- <DropdownMenuItem key={index}>
+ <DropdownMenuItem key={`${folder.path}-${index}`}>
<Link
href={`/documents/tree${folder.path}`}
className="w-full"
@@ -78,8 +78,8 @@ const SingleDocumentBreadcrumb = () => {
</>
) : (
folders?.map((folder, index) => (
- <>
- <BreadcrumbItem key={`item-${index}`}>
+ <React.Fragment key={`${folder.path}-${index}`}>
+ <BreadcrumbItem>
<BreadcrumbLink asChild>
<Link
href={`/documents/tree${folder.path}`}
@@ -89,8 +89,8 @@ const SingleDocumentBreadcrumb = () => {
</Link>
</BreadcrumbLink>
</BreadcrumbItem>
- <BreadcrumbSeparator key={`sep-${index}`} />
- </>
+ <BreadcrumbSeparator />
+ </React.Fragment>
))
)}
{document && (
@@ -261,7 +261,7 @@ const DocumentsBreadcrumb = () => {
</DropdownMenuTrigger>
<DropdownMenuContent align="start">
{folders.slice(0, -2).map((folder, index) => (
- <DropdownMenuItem key={index}>
+ <DropdownMenuItem key={`${folder.path}-${index}`}>
<Link
href={`/documents/tree${folder.path}`}
className="w-full"
@@ -293,7 +293,7 @@ const DocumentsBreadcrumb = () => {
</>
) : (
folders?.map((folder, index) => (
- <>
+ <React.Fragment key={`${folder.path}-${index}`}>
<BreadcrumbItem key={`item-${index}`}>
{index === folders.length - 1 ? (
<BreadcrumbPage className="max-w-[200px] truncate">
@@ -311,7 +311,7 @@ const DocumentsBreadcrumb = () => {
)}
</BreadcrumbItem>
{index < folders.length - 1 && <BreadcrumbSeparator />}
- </>
+ </React.Fragment>
))
)}
</BreadcrumbList>
@@ -479,4 +479,4 @@ export const AppBreadcrumb = () => {
}, [path, id]);
return breadcrumb;
-};
+};
\ No newline at end of file
diff --git a/lib/documents/move-documents.ts b/lib/documents/move-documents.ts
index bd003a85..8a5c42ed 100644
--- a/lib/documents/move-documents.ts
+++ b/lib/documents/move-documents.ts
@@ -23,11 +23,16 @@ export const moveDocumentToFolder = async ({
// Optimistically update the UI by removing the documents from current folder
mutate(
key,
- (documents: any) => {
- if (!documents) return documents;
+ (data: any) => {
+ if (!data) return data;
- // Filter out the documents that are being moved
- const updatedDocuments = documents.filter(
+ if (data && data.documents) {
+ const updatedDocuments = data.documents.filter(
+ (doc: any) => !documentIds.includes(doc.id),
+ );
+ return { ...data, documents: updatedDocuments };
+ }
+ const updatedDocuments = data.filter(
(doc: any) => !documentIds.includes(doc.id),
);