Skip to content

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

Fefresh Root File: update mutaion and ui

Ashish Viradiyaede3a6820f97
diff --git a/components/documents/documents-list.tsx b/components/documents/documents-list.tsx
index 9b04d7fb..55d54793 100644
--- a/components/documents/documents-list.tsx
+++ b/components/documents/documents-list.tsx
@@ -54,11 +54,15 @@ export function DocumentsList({
documents,
teamInfo,
folderPathName,
+ loading,
+ foldersLoading,
}: {
folders: FolderWithCount[] | undefined;
documents: DocumentWithLinksAndLinkCountAndViewCount[] | undefined;
teamInfo: TeamContextType | null;
folderPathName?: string[];
+ loading: boolean;
+ foldersLoading: boolean;
}) {
const { isMobile } = useMediaQuery();
@@ -418,7 +422,7 @@ export function DocumentsList({
<div className="space-y-4">
{/* Folders list */}
<ul role="list" className="space-y-4">
- {folders
+ {folders && !foldersLoading
? folders.map((folder) => {
return (
<FolderCard
@@ -448,7 +452,7 @@ export function DocumentsList({
{/* Documents list */}
<ul role="list" className="space-y-4">
- {documents
+ {documents && !loading
? documents.map((document) => {
return (
<DocumentCard
@@ -483,7 +487,7 @@ export function DocumentsList({
<HeaderContent />
</Portal>
- {documents && documents.length === 0 && (
+ {documents && documents.length === 0 && !loading && (
<div className="flex items-center justify-center">
<EmptyDocuments />
</div>
@@ -506,7 +510,7 @@ export function DocumentsList({
<div className="space-y-4">
{/* Folders list */}
<ul role="list" className="space-y-4">
- {folders
+ {folders && !foldersLoading
? folders.map((folder) => {
return (
<DroppableFolder
@@ -559,7 +563,7 @@ export function DocumentsList({
{/* Documents list */}
<ul role="list" className="space-y-4">
- {documents
+ {documents && !loading
? documents.map((document) => {
return (
<DraggableItem
@@ -639,7 +643,7 @@ export function DocumentsList({
<HeaderContent />
</Portal>
- {documents && documents.length === 0 && (
+ {documents && documents.length === 0 && !loading && (
<div className="flex items-center justify-center">
<EmptyDocuments />
</div>
@@ -674,4 +678,4 @@ export function DocumentsList({
) : null}
</>
);
-}
+};
diff --git a/components/search-box.tsx b/components/search-box.tsx
index beafa919..3f1abcb7 100644
--- a/components/search-box.tsx
+++ b/components/search-box.tsx
@@ -120,52 +120,48 @@ export function SearchBoxPersisted({
const router = useRouter();
const queryParams = router.query;
- const [value, setValue] = useState(queryParams[urlParam] ?? "");
- const [debouncedValue, setDebouncedValue] = useState(value);
+ const initial =
+ typeof queryParams[urlParam] === "string" ? queryParams[urlParam] : "";
+
+ const [value, setValue] = useState(initial);
+ const [debouncedValue, setDebouncedValue] = useState(initial);
- // Set URL param when debounced value changes
useEffect(() => {
- if (queryParams[urlParam] ?? "" !== debouncedValue)
- if (debouncedValue === "") {
- delete queryParams[urlParam];
- delete queryParams["page"];
- delete queryParams["limit"];
- router.push(
- {
- pathname: router.pathname,
- query: queryParams,
- },
- undefined,
- { shallow: true },
- );
- } else {
- queryParams[urlParam] = debouncedValue;
- router.push(
- {
- pathname: router.pathname,
- query: queryParams,
- },
- undefined,
- { shallow: true },
- );
- }
+ const currentQuery = { ...router.query };
+
+ if (debouncedValue === "") {
+ delete currentQuery[urlParam];
+ delete currentQuery.page;
+ delete currentQuery.limit;
+ } else {
+ currentQuery[urlParam] = debouncedValue;
+ }
+
+ router.push(
+ {
+ pathname: router.pathname,
+ query: currentQuery,
+ },
+ undefined,
+ { shallow: true },
+ );
}, [debouncedValue]);
- // Set value when URL param changes
useEffect(() => {
- const search = queryParams[urlParam];
- // Only update if the value and debouncedValue are synced (the user isn't actively typing)
- if ((search ?? "" !== value) && value === debouncedValue) {
- setValue(search ?? "");
+ const queryValue =
+ typeof queryParams[urlParam] === "string" ? queryParams[urlParam] : "";
+ if (queryValue !== value && value === debouncedValue) {
+ setValue(queryValue);
+ setDebouncedValue(queryValue);
}
}, [queryParams[urlParam]]);
return (
<SearchBox
- value={value as string}
+ value={value}
onChange={setValue}
onChangeDebounced={setDebouncedValue}
{...props}
/>
);
-}
+}
\ No newline at end of file
diff --git a/components/upload-zone.tsx b/components/upload-zone.tsx
index 74d47c58..c3163857 100644
--- a/components/upload-zone.tsx
+++ b/components/upload-zone.tsx
@@ -110,6 +110,13 @@ export default function UploadZone({
const { data: session } = useSession();
const isFreePlan = plan === "free";
const isTrial = !!trial;
+ const queryParams = router.query;
+ const searchQuery = queryParams["search"];
+ const sortQuery = queryParams["sort"];
+ const page = Number(queryParams["page"]) || 1;
+ const pageSize = Number(queryParams["limit"]) || 10;
+ const paginationParams =
+ searchQuery || sortQuery ? `&page=${page}&limit=${pageSize}` : "";
// const maxSize = isFreePlan && !isTrial ? 30 : 350;
// const maxNumPages = isFreePlan && !isTrial ? 100 : 500;
const { limits, canAddDocuments } = useLimits();
@@ -318,7 +325,15 @@ export default function UploadZone({
});
// add the new document to the list
- mutate(`/api/teams/${teamInfo?.currentTeam?.id}/documents`);
+ mutate(
+ `/api/teams/${teamInfo?.currentTeam?.id}/documents?${
+ searchQuery ? `query=${searchQuery}` : ""
+ }${sortQuery ? `&sort=${sortQuery}` : ""}${paginationParams}`.replace(
+ /^\?&/,
+ "?",
+ ),
+ );
+
fileUploadPathName &&
mutate(
`/api/teams/${teamInfo?.currentTeam?.id}/folders/documents/${fileUploadPathName}`,
@@ -671,4 +686,4 @@ export default function UploadZone({
{children}
</div>
);
-}
+}
\ No newline at end of file
diff --git a/lib/swr/use-documents.ts b/lib/swr/use-documents.ts
index 8c055ac4..84b49aa9 100644
--- a/lib/swr/use-documents.ts
+++ b/lib/swr/use-documents.ts
@@ -36,8 +36,7 @@ export default function useDocuments() {
};
}>(
teamId &&
- `/api/teams/${teamId}/documents?${
- searchQuery ? `query=${searchQuery}` : ""
+ `/api/teams/${teamId}/documents?${searchQuery ? `query=${searchQuery}` : ""
}${sortQuery ? `&sort=${sortQuery}` : ""}${paginationParams}`.replace(/^\?&/, '?'),
fetcher,
{
diff --git a/pages/documents/index.tsx b/pages/documents/index.tsx
index d01c68b0..b9ecdafa 100644
--- a/pages/documents/index.tsx
+++ b/pages/documents/index.tsx
@@ -1,22 +1,24 @@
+import { useRouter } from "next/router";
+
import { useTeam } from "@/context/team-context";
import { FolderPlusIcon, PlusIcon } from "lucide-react";
-import { useState } from "react";
import {
ChevronLeftIcon,
ChevronRightIcon,
ChevronsLeftIcon,
ChevronsRightIcon,
} from "lucide-react";
-import { useRouter } from "next/router";
+
+import useDocuments, { useRootFolders } from "@/lib/swr/use-documents";
import { AddDocumentModal } from "@/components/documents/add-document-modal";
import { DocumentsList } from "@/components/documents/documents-list";
+import { DocumentsPagination } from "@/components/documents/documents-pagination";
import SortButton from "@/components/documents/filters/sort-button";
import { AddFolderModal } from "@/components/folders/add-folder-modal";
import AppLayout from "@/components/layouts/app";
import { SearchBoxPersisted } from "@/components/search-box";
import { Button } from "@/components/ui/button";
-import { Separator } from "@/components/ui/separator";
import {
Select,
SelectContent,
@@ -24,9 +26,7 @@ import {
SelectTrigger,
SelectValue,
} from "@/components/ui/select";
-
-import useDocuments, { useRootFolders } from "@/lib/swr/use-documents";
-import { DocumentsPagination } from "@/components/documents/documents-pagination";
+import { Separator } from "@/components/ui/separator";
export default function Documents() {
const router = useRouter();
@@ -35,8 +35,9 @@ export default function Documents() {
const currentPage = Number(queryParams["page"]) || 1;
const pageSize = Number(queryParams["limit"]) || 10;
- const { folders } = useRootFolders();
- const { documents, pagination, isValidating, isFiltered } = useDocuments();
+ const { folders, loading: foldersLoading } = useRootFolders();
+ const { documents, pagination, isValidating, isFiltered, loading } =
+ useDocuments();
const updatePagination = (newPage?: number, newPageSize?: number) => {
const params = new URLSearchParams(window.location.search);
@@ -104,6 +105,8 @@ export default function Documents() {
documents={documents}
folders={displayFolders}
teamInfo={teamInfo}
+ loading={loading}
+ foldersLoading={foldersLoading}
/>
{isFiltered && pagination && (
diff --git a/pages/documents/tree/[...name].tsx b/pages/documents/tree/[...name].tsx
index 66cbb071..bfa15d99 100644
--- a/pages/documents/tree/[...name].tsx
+++ b/pages/documents/tree/[...name].tsx
@@ -16,8 +16,8 @@ export default function DocumentTreePage() {
const router = useRouter();
const { name } = router.query as { name: string[] };
- const { folders } = useFolder({ name });
- const { documents } = useFolderDocuments({ name });
+ const { folders, loading: foldersLoading } = useFolder({ name });
+ const { documents, loading } = useFolderDocuments({ name });
const teamInfo = useTeam();
return (
@@ -65,6 +65,8 @@ export default function DocumentTreePage() {
folders={folders}
teamInfo={teamInfo}
folderPathName={name}
+ loading={loading}
+ foldersLoading={foldersLoading}
/>
</main>
</AppLayout>