Skip to content

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

Sort the order of folders in overview tree

Ashish Viradiyacee850432fc8
diff --git a/components/datarooms/folders/sidebar-tree.tsx b/components/datarooms/folders/sidebar-tree.tsx
index bfeae90f..75e41433 100644
--- a/components/datarooms/folders/sidebar-tree.tsx
+++ b/components/datarooms/folders/sidebar-tree.tsx
@@ -9,11 +9,16 @@ import {
useDataroomFoldersTree,
} from "@/lib/swr/use-dataroom";
import { cn } from "@/lib/utils";
+import { sortByIndexThenName } from "@/lib/utils/sort-items-by-index-name";
import { FileTree } from "@/components/ui/nextra-filetree";
import { buildNestedFolderStructure } from "./utils";
+type MixedItem =
+ | (DataroomFolderWithDocuments & { itemType: "folder" })
+ | (DataroomFolderWithDocuments["documents"][0] & { itemType: "document" });
+
const FolderComponent = memo(
({
dataroomId,
@@ -24,30 +29,43 @@ const FolderComponent = memo(
}) => {
const router = useRouter();
- // Memoize the rendering of the current folder's documents
- const documents = useMemo(
- () =>
- folder.documents.map((doc) => (
- <FileTree.File
- key={doc.id}
- name={doc.document.name}
- onToggle={() => router.push(`/documents/${doc.document.id}`)}
- />
- )),
- [folder.documents, dataroomId, router.query.name],
- );
-
- // Recursively render child folders if they exist
- const childFolders = useMemo(
+ const mixedItems = useMemo(() => {
+ const allItems: MixedItem[] = [
+ ...(folder.childFolders || []).map((f) => ({
+ ...f,
+ itemType: "folder" as const,
+ })),
+ ...(folder.documents || []).map((d) => ({
+ ...d,
+ itemType: "document" as const,
+ })),
+ ];
+
+ return sortByIndexThenName(allItems);
+ }, [folder.childFolders, folder.documents]);
+
+ const renderedItems = useMemo(
() =>
- folder.childFolders.map((childFolder) => (
- <FolderComponent
- key={childFolder.id}
- dataroomId={dataroomId}
- folder={childFolder}
- />
- )),
- [folder.childFolders, dataroomId],
+ mixedItems.map((item: MixedItem) => {
+ if (item.itemType === "folder") {
+ return (
+ <FolderComponent
+ key={item.id}
+ dataroomId={dataroomId}
+ folder={item}
+ />
+ );
+ } else {
+ return (
+ <FileTree.File
+ key={item.id}
+ name={item.document.name}
+ onToggle={() => router.push(`/documents/${item.document.id}`)}
+ />
+ );
+ }
+ }),
+ [mixedItems, dataroomId, router],
);
const isActive =
@@ -75,8 +93,7 @@ const FolderComponent = memo(
childActive={isChildActive}
onToggle={handleFolderClick}
>
- {childFolders}
- {documents}
+ {renderedItems}
</FileTree.Folder>
);
},
diff --git a/components/datarooms/sortable/sortable-list.tsx b/components/datarooms/sortable/sortable-list.tsx
index 9ace38a7..6b4ee5ed 100644
--- a/components/datarooms/sortable/sortable-list.tsx
+++ b/components/datarooms/sortable/sortable-list.tsx
@@ -145,12 +145,15 @@ export function DataroomSortableList({
}
// Update local data using SWR's mutate
+ const baseKey = `/api/teams/${teamInfo?.currentTeam?.id}/datarooms/${dataroomId}`;
mutate(
- `/api/teams/${teamInfo?.currentTeam?.id}/datarooms/${dataroomId}/folders${folderPathName ? `/${folderPathName.join(" / ")}` : "?root=true"}`,
+ `${baseKey}/folders${folderPathName ? `/${folderPathName.join("/")}` : "?root=true"}`,
);
mutate(
- `/api/teams/${teamInfo?.currentTeam?.id}/datarooms/${dataroomId}${folderPathName ? `/folders/documents/${folderPathName.join("/")}` : "/documents"}`,
+ `${baseKey}${folderPathName ? `/folders/documents/${folderPathName.join("/")}` : "/documents"}`,
);
+ mutate(`${baseKey}/folders`);
+ mutate(`${baseKey}/folders?include_documents=true`);
setIsReordering(false);
toast.success("Index saved successfully");
} catch (error) {
diff --git a/components/view/viewer/dataroom-viewer.tsx b/components/view/viewer/dataroom-viewer.tsx
index ffa77cd7..ec4ebb34 100644
--- a/components/view/viewer/dataroom-viewer.tsx
+++ b/components/view/viewer/dataroom-viewer.tsx
@@ -11,6 +11,7 @@ import * as SheetPrimitive from "@radix-ui/react-dialog";
import { PanelLeftIcon, XIcon } from "lucide-react";
import { cn } from "@/lib/utils";
+import { sortByIndexThenName } from "@/lib/utils/sort-items-by-index-name";
import { ViewFolderTree } from "@/components/datarooms/folders";
import {
@@ -175,7 +176,8 @@ export default function DataroomViewer({
};
}),
];
- return mixedItems.sort((a, b) => (a.orderIndex || 0) - (b.orderIndex || 0));
+
+ return sortByIndexThenName(mixedItems);
}, [folders, documents, folderId, accessControls, allowDownload]);
const renderItem = (item: FolderOrDocument) => {
diff --git a/lib/swr/use-dataroom.ts b/lib/swr/use-dataroom.ts
index 79eddf18..16329ce6 100644
--- a/lib/swr/use-dataroom.ts
+++ b/lib/swr/use-dataroom.ts
@@ -9,6 +9,7 @@ import useSWR from "swr";
import { LinkWithViews } from "@/lib/types";
import { fetcher } from "@/lib/utils";
+import { sortByIndexThenName } from "@/lib/utils/sort-items-by-index-name";
export type DataroomFolderWithCount = DataroomFolder & {
_count: {
@@ -128,8 +129,7 @@ export function useDataroomItems({
})),
...(documentData || []).map((doc) => ({ ...doc, itemType: "document" })),
];
-
- return allItems.sort((a, b) => (a.orderIndex || 0) - (b.orderIndex || 0));
+ return sortByIndexThenName(allItems);
}, [folderData, documentData]);
return {
@@ -207,6 +207,7 @@ export function useDataroomFolders({
export type DataroomFolderWithDocuments = DataroomFolder & {
childFolders: DataroomFolderWithDocuments[];
documents: {
+ orderIndex: number | null;
id: string;
folderId: string;
document: {
diff --git a/lib/utils/sort-items-by-index-name.ts b/lib/utils/sort-items-by-index-name.ts
index 7f116f87..3515be4f 100644
--- a/lib/utils/sort-items-by-index-name.ts
+++ b/lib/utils/sort-items-by-index-name.ts
@@ -26,6 +26,33 @@ export const sortItemsByIndexAndName = <
});
};
+export const sortByIndexThenName = <
+ T extends {
+ orderIndex: number | null;
+ name?: string;
+ document?: { name: string };
+ },
+>(
+ items: T[],
+): T[] => {
+ return items.sort((a, b) => {
+ if (a.orderIndex !== null && b.orderIndex !== null) {
+ if (a.orderIndex !== b.orderIndex) {
+ return a.orderIndex - b.orderIndex;
+ }
+ }
+ if (a.orderIndex !== null && b.orderIndex === null) {
+ return -1;
+ }
+ if (a.orderIndex === null && b.orderIndex !== null) {
+ return 1;
+ }
+ const nameA = a.name || a.document?.name || "";
+ const nameB = b.name || b.document?.name || "";
+ return nameA.localeCompare(nameB);
+ });
+};
+
// Helper function to extract the numerical part of a string
const getNumber = (str: string): number => {
const match = str.match(/^\d+/);
diff --git a/pages/api/teams/[teamId]/datarooms/[id]/folders/index.ts b/pages/api/teams/[teamId]/datarooms/[id]/folders/index.ts
index 79d29e5b..f59a42ba 100644
--- a/pages/api/teams/[teamId]/datarooms/[id]/folders/index.ts
+++ b/pages/api/teams/[teamId]/datarooms/[id]/folders/index.ts
@@ -138,6 +138,7 @@ export default async function handle(
include: {
documents: {
select: {
+ orderIndex: true,
id: true,
folderId: true,
document: {
@@ -161,6 +162,7 @@ export default async function handle(
include: {
documents: {
select: {
+ orderIndex: true,
id: true,
folderId: true,
document: {