Papermark is the open-source DocSend alternative and secure data rooms with built-in analytics and custom domains.
feat: update dataroom view with new sorted index
Marc Seitzb936dad8528d
diff --git a/components/view/DataroomViewer.tsx b/components/view/DataroomViewer.tsxindex dd20580b..0e65de99 100644--- a/components/view/DataroomViewer.tsx+++ b/components/view/DataroomViewer.tsx@@ -31,11 +31,14 @@ import DocumentCard from "./dataroom/document-card";import FolderCard from "./dataroom/folder-card";import DataroomNav from "./dataroom/nav-dataroom";+type FolderOrDocument = DataroomFolder | DataroomDocument;+type DataroomDocument = {dataroomDocumentId: string;folderId: string | null;id: string;name: string;+ orderIndex: number | null;versions: {id: string;type: string;@@ -123,6 +126,42 @@ export default function DataroomViewer({[folderId, folders],);+ // create a mixedItems array with folders and documents of the current folder and memoize it+ const mixedItems = useMemo(() => {+ const mixedItems: FolderOrDocument[] = [+ ...(folders || [])+ .filter((folder) => folder.parentId === folderId)+ .map((folder) => ({ ...folder, itemType: "folder" })),+ ...(documents || [])+ .filter((doc) => doc.folderId === folderId)+ .map((doc) => ({ ...doc, itemType: "document" })),+ ];++ return mixedItems.sort((a, b) => (a.orderIndex || 0) - (b.orderIndex || 0));+ }, [folders, documents, folderId]);++ const renderItem = (item: FolderOrDocument) => {+ if ("versions" in item) {+ return (+ <DocumentCard+ key={item.id}+ document={item}+ setViewType={setViewType}+ setDocumentData={setDocumentData}+ />+ );+ }++ return (+ <FolderCard+ key={item.id}+ folder={item}+ dataroomId={dataroom?.id}+ setFolderId={setFolderId}+ />+ );+ };+return (<><DataroomNav@@ -149,7 +188,7 @@ export default function DataroomViewer({</div>{/* Detail view */}- <ScrollArea className="h-full flex-grow" showScrollbar>+ <div className="h-full flex-grow"><div className="mb-10 mt-4 space-y-8 p-3 md:mx-5 md:mt-5 lg:mx-7 lg:mt-8 xl:mx-10"><div className="flex items-center gap-x-2">{/* sidebar for mobile */}@@ -219,45 +258,11 @@ export default function DataroomViewer({</BreadcrumbList></Breadcrumb></div>- <div className="space-y-4">- {/* Folders list */}- <ul role="list" className="space-y-4">- {folders- ? folders- .filter((folder) => folder.parentId === folderId)- .map((folder) => {- return (- <FolderCard- key={folder.id}- folder={folder}- dataroomId={dataroom?.id}- setFolderId={setFolderId}- />- );- })- : null}- </ul>-- {/* Documents list */}- <ul role="list" className="space-y-4">- {documents- ? documents- .filter((doc) => doc.folderId === folderId)- .map((document: DataroomDocument) => {- return (- <DocumentCard- key={document.id}- document={document}- setViewType={setViewType}- setDocumentData={setDocumentData}- />- );- })- : null}- </ul>- </div>+ <ul role="list" className="space-y-4">+ {mixedItems.map(renderItem)}+ </ul></div>- </ScrollArea>+ </div></div></div></>diff --git a/lib/api/links/link-data.ts b/lib/api/links/link-data.tsnew file mode 100644index 00000000..146e726e--- /dev/null+++ b/lib/api/links/link-data.ts@@ -0,0 +1,151 @@+import prisma from "@/lib/prisma";++export async function fetchDataroomLinkData({ linkId }: { linkId: string }) {+ const linkData = await prisma.link.findUnique({+ where: { id: linkId },+ select: {+ dataroom: {+ select: {+ id: true,+ name: true,+ teamId: true,+ documents: {+ select: {+ id: true,+ folderId: true,+ updatedAt: true,+ orderIndex: true,+ document: {+ select: {+ id: true,+ name: true,+ versions: {+ where: { isPrimary: true },+ select: {+ id: true,+ versionNumber: true,+ type: true,+ hasPages: true,+ file: true,+ isVertical: true,+ },+ take: 1,+ },+ },+ },+ },+ orderBy: [+ { orderIndex: "asc" },+ {+ document: {+ name: "asc",+ },+ },+ ],+ },+ folders: {+ orderBy: [+ { orderIndex: "asc" },+ {+ name: "asc",+ },+ ],+ },+ },+ },+ },+ });++ if (!linkData?.dataroom) {+ throw new Error("Dataroom not found");+ }++ // Helper function to extract the numerical part of a string+ const getNumber = (str: string): number => {+ const match = str.match(/^\d+/);+ return match ? parseInt(match[0], 10) : 0;+ };++ // Sort documents by orderIndex and/or name considering the numerical part+ linkData.dataroom.documents.sort((a, b) => {+ // First, compare by orderIndex if both items have it+ if (a.orderIndex && b.orderIndex) {+ if (a.orderIndex !== b.orderIndex) {+ return a.orderIndex - b.orderIndex;+ }+ }++ // If orderIndex is not available or equal, use name-based sorting+ const numA = getNumber(a.document.name);+ const numB = getNumber(b.document.name);++ if (numA !== numB) {+ return numA - numB;+ }++ // If numerical parts are the same, fall back to lexicographical order+ return a.document.name.localeCompare(b.document.name);+ });++ const brand = await prisma.dataroomBrand.findFirst({+ where: {+ dataroomId: linkData.dataroom.id,+ },+ select: {+ logo: true,+ banner: true,+ brandColor: true,+ },+ });++ return { linkData, brand };+}++export async function fetchDocumentLinkData({ linkId }: { linkId: string }) {+ const linkData = await prisma.link.findUnique({+ where: { id: linkId },+ select: {+ document: {+ select: {+ id: true,+ name: true,+ assistantEnabled: true,+ teamId: true,+ ownerId: true,+ team: {+ select: { plan: true },+ },+ versions: {+ where: { isPrimary: true },+ select: {+ id: true,+ versionNumber: true,+ type: true,+ hasPages: true,+ file: true,+ isVertical: true,+ },+ take: 1,+ },+ },+ },+ },+ });++ if (!linkData?.document) {+ throw new Error("Document not found");+ }++ const brand = await prisma.brand.findFirst({+ where: {+ teamId: linkData.document.teamId,+ },+ select: {+ logo: true,+ brandColor: true,+ accentColor: true,+ },+ });++ return { linkData, brand };+}diff --git a/lib/types.ts b/lib/types.tsindex e7e1ff0d..e6e7970c 100644--- a/lib/types.ts+++ b/lib/types.ts@@ -74,6 +74,7 @@ export interface LinkWithDataroom extends Link {documents: {id: string;folderId: string | null;+ orderIndex: number | null;document: {id: string;name: string;diff --git a/pages/api/links/[id]/index.ts b/pages/api/links/[id]/index.tsindex 3cbd532f..243ba7a8 100644--- a/pages/api/links/[id]/index.ts+++ b/pages/api/links/[id]/index.ts@@ -1,13 +1,12 @@import { NextApiRequest, NextApiResponse } from "next";-import {- Brand,- DataroomBrand,- DataroomDocument,- Document,-} from "@prisma/client";+import { Brand, DataroomBrand } from "@prisma/client";import { getServerSession } from "next-auth/next";+import {+ fetchDataroomLinkData,+ fetchDocumentLinkData,+} from "@/lib/api/links/link-data";import prisma from "@/lib/prisma";import { CustomUser } from "@/lib/types";import { generateEncrpytedPassword } from "@/lib/utils";@@ -72,130 +71,13 @@ export default async function handle(let linkData: any;if (linkType === "DOCUMENT_LINK") {- linkData = await prisma.link.findUnique({- where: { id: id },- select: {- document: {- select: {- id: true,- name: true,- assistantEnabled: true,- teamId: true,- ownerId: true,- team: {- select: {- plan: true,- },- },- versions: {- where: { isPrimary: true },- select: {- id: true,- versionNumber: true,- type: true,- hasPages: true,- file: true,- isVertical: true,- },- take: 1,- },- },- },- },- });-- brand = await prisma.brand.findFirst({- where: {- teamId: linkData.document.teamId,- },- select: {- logo: true,- brandColor: true,- accentColor: true,- },- });+ const data = await fetchDocumentLinkData({ linkId: id });+ linkData = data.linkData;+ brand = data.brand;} else if (linkType === "DATAROOM_LINK") {- linkData = await prisma.link.findUnique({- where: { id: id },- select: {- dataroom: {- select: {- id: true,- name: true,- teamId: true,- documents: {- select: {- id: true,- folderId: true,- updatedAt: true,- document: {- select: {- id: true,- name: true,- versions: {- where: { isPrimary: true },- select: {- id: true,- versionNumber: true,- type: true,- hasPages: true,- file: true,- isVertical: true,- },- take: 1,- },- },- },- },- orderBy: {- document: {- name: "asc",- },- },- },- folders: {- orderBy: {- name: "asc",- },- },- },- },- },- });-- // Sort documents by name considering the numerical part- linkData.dataroom.documents.sort(- (- a: DataroomDocument & { document: Document },- b: DataroomDocument & { document: Document },- ) => {- const getNumber = (str: string): number => {- const match = str.match(/^\d+/);- return match ? parseInt(match[0], 10) : 0;- };-- const numA = getNumber(a.document.name);- const numB = getNumber(b.document.name);-- if (numA !== numB) {- return numA - numB;- }-- // If numerical parts are the same, fall back to lexicographical order- return a.document.name.localeCompare(b.document.name);- },- );-- brand = await prisma.dataroomBrand.findFirst({- where: {- dataroomId: linkData.dataroom.id,- },- select: {- logo: true,- banner: true,- brandColor: true,- },- });+ const data = await fetchDataroomLinkData({ linkId: id });+ linkData = data.linkData;+ brand = data.brand;}const returnLink = {diff --git a/pages/api/links/domains/[...domainSlug].ts b/pages/api/links/domains/[...domainSlug].tsindex 4d29213b..64fcf75c 100644--- a/pages/api/links/domains/[...domainSlug].ts+++ b/pages/api/links/domains/[...domainSlug].ts@@ -1,12 +1,11 @@import { NextApiRequest, NextApiResponse } from "next";-import {- Brand,- DataroomBrand,- DataroomDocument,- Document,-} from "@prisma/client";+import { Brand, DataroomBrand } from "@prisma/client";+import {+ fetchDataroomLinkData,+ fetchDocumentLinkData,+} from "@/lib/api/links/link-data";import prisma from "@/lib/prisma";import { log } from "@/lib/utils";@@ -126,125 +125,13 @@ export default async function handle(let linkData: any;if (linkType === "DOCUMENT_LINK") {- linkData = await prisma.link.findUnique({- where: { id: link.id },- select: {- document: {- select: {- id: true,- name: true,- teamId: true,- ownerId: true,- team: { select: { plan: true } },- versions: {- where: { isPrimary: true },- select: {- id: true,- versionNumber: true,- hasPages: true,- type: true,- file: true,- isVertical: true,- },- take: 1,- },- },- },- },- });-- brand = await prisma.brand.findFirst({- where: {- teamId: linkData.document.teamId,- },- select: {- logo: true,- brandColor: true,- accentColor: true,- },- });+ const data = await fetchDocumentLinkData({ linkId: link.id });+ linkData = data.linkData;+ brand = data.brand;} else if (linkType === "DATAROOM_LINK") {- linkData = await prisma.link.findUnique({- where: { id: link.id },- select: {- dataroom: {- select: {- id: true,- name: true,- teamId: true,- documents: {- select: {- id: true,- folderId: true,- updatedAt: true,- document: {- select: {- id: true,- name: true,- versions: {- where: { isPrimary: true },- select: {- id: true,- versionNumber: true,- type: true,- hasPages: true,- file: true,- isVertical: true,- },- take: 1,- },- },- },- },- orderBy: {- document: {- name: "asc",- },- },- },- folders: {- orderBy: {- name: "asc",- },- },- },- },- },- });-- // Sort documents by name considering the numerical part- linkData.dataroom.documents.sort(- (- a: DataroomDocument & { document: Document },- b: DataroomDocument & { document: Document },- ) => {- const getNumber = (str: string): number => {- const match = str.match(/^\d+/);- return match ? parseInt(match[0], 10) : 0;- };-- const numA = getNumber(a.document.name);- const numB = getNumber(b.document.name);-- if (numA !== numB) {- return numA - numB;- }-- // If numerical parts are the same, fall back to lexicographical order- return a.document.name.localeCompare(b.document.name);- },- );-- brand = await prisma.dataroomBrand.findFirst({- where: {- dataroomId: linkData.dataroom.id,- },- select: {- logo: true,- banner: true,- brandColor: true,- },- });+ const data = await fetchDataroomLinkData({ linkId: link.id });+ linkData = data.linkData;+ brand = data.brand;}// remove document and domain from linkdiff --git a/pages/view/[linkId]/index.tsx b/pages/view/[linkId]/index.tsxindex 900f2c92..136e0021 100644--- a/pages/view/[linkId]/index.tsx+++ b/pages/view/[linkId]/index.tsx@@ -114,6 +114,7 @@ export const getStaticProps = async (context: GetStaticPropsContext) => {...document.document,dataroomDocumentId: document.id,folderId: document.folderId,+ orderIndex: document.orderIndex,versions: [versionWithoutTypeAndFile],};diff --git a/pages/view/domains/[domain]/[slug]/index.tsx b/pages/view/domains/[domain]/[slug]/index.tsxindex 144b87b5..5d8328b3 100644--- a/pages/view/domains/[domain]/[slug]/index.tsx+++ b/pages/view/domains/[domain]/[slug]/index.tsx@@ -118,6 +118,7 @@ export const getStaticProps = async (context: GetStaticPropsContext) => {...document.document,dataroomDocumentId: document.id,folderId: document.folderId,+ orderIndex: document.orderIndex,versions: [versionWithoutTypeAndFile],};