Skip to content

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

Fix: sort=lastViewed return 500 error

Ashish Viradiyab6c0d1d3df07
diff --git a/lib/swr/use-documents.ts b/lib/swr/use-documents.ts
index 1d17cbd3..69606949 100644
--- a/lib/swr/use-documents.ts
+++ b/lib/swr/use-documents.ts
@@ -21,6 +21,12 @@ export default function useDocuments() {
const paginationParams =
searchQuery || sortQuery ? `&page=${page}&limit=${pageSize}` : "";
+ const queryParts = [];
+ if (searchQuery) queryParts.push(`query=${searchQuery}`);
+ if (sortQuery) queryParts.push(`sort=${sortQuery}`);
+ if (paginationParams) queryParts.push(paginationParams.substring(1));
+ const queryString = queryParts.length > 0 ? `?${queryParts.join('&')}` : '';
+
const { data, isValidating, error } = useSWR<{
documents: DocumentWithLinksAndLinkCountAndViewCount[];
pagination?: {
@@ -30,13 +36,7 @@ export default function useDocuments() {
pageSize: number;
};
}>(
- teamId &&
- `/api/teams/${teamId}/documents?${
- searchQuery ? `query=${searchQuery}` : ""
- }${sortQuery ? `&sort=${sortQuery}` : ""}${paginationParams}`.replace(
- /^\?&/,
- "?",
- ),
+ teamId && `/api/teams/${teamId}/documents${queryString}`,
fetcher,
{
revalidateOnFocus: false,
@@ -62,7 +62,7 @@ export function useFolderDocuments({ name }: { name: string[] }) {
DocumentWithLinksAndLinkCountAndViewCount[]
>(
teamInfo?.currentTeam?.id &&
- name &&
+ name.length > 0 &&
`/api/teams/${teamInfo?.currentTeam?.id}/folders/documents/${name.join("/")}`,
fetcher,
{
@@ -91,7 +91,7 @@ export function useFolder({ name }: { name: string[] }) {
const { data: folders, error } = useSWR<FolderWithCount[]>(
teamInfo?.currentTeam?.id &&
- name &&
+ name.length > 0 &&
`/api/teams/${teamInfo?.currentTeam?.id}/folders/${name.join("/")}`,
fetcher,
{
diff --git a/pages/api/teams/[teamId]/documents/index.ts b/pages/api/teams/[teamId]/documents/index.ts
index 231e687a..ca3a60c3 100644
--- a/pages/api/teams/[teamId]/documents/index.ts
+++ b/pages/api/teams/[teamId]/documents/index.ts
@@ -117,14 +117,13 @@ export default async function handle(
path: true,
},
},
- ...(query &&
- sort === "lastViewed" && {
- views: {
- select: { viewedAt: true },
- orderBy: { viewedAt: "desc" },
- take: 1,
- },
- }),
+ ...(sort === "lastViewed" && {
+ views: {
+ select: { viewedAt: true },
+ orderBy: { viewedAt: "desc" },
+ take: 1,
+ },
+ }),
_count: {
select: {
links: true,