From 353e5d6374c77a2744590a5bfd3329672009b281 Mon Sep 17 00:00:00 2001
From: Mohamed Bassem <me@mbassem.com>
Date: Sun, 22 Dec 2024 16:17:22 +0000
Subject: feat: Add basic pagination to searchBookmarks tRPC

---
 apps/mobile/app/dashboard/search.tsx | 15 +++++++++++----
 1 file changed, 11 insertions(+), 4 deletions(-)

(limited to 'apps/mobile/app/dashboard/search.tsx')

diff --git a/apps/mobile/app/dashboard/search.tsx b/apps/mobile/app/dashboard/search.tsx
index 884345e7..de3d0f46 100644
--- a/apps/mobile/app/dashboard/search.tsx
+++ b/apps/mobile/app/dashboard/search.tsx
@@ -17,10 +17,15 @@ export default function Search() {
 
   const onRefresh = api.useUtils().bookmarks.searchBookmarks.invalidate;
 
-  const { data, error, refetch, isPending } =
-    api.bookmarks.searchBookmarks.useQuery(
+  const { data, error, refetch, isPending, fetchNextPage, isFetchingNextPage } =
+    api.bookmarks.searchBookmarks.useInfiniteQuery(
       { text: query },
-      { placeholderData: keepPreviousData },
+      {
+        placeholderData: keepPreviousData,
+        gcTime: 0,
+        initialCursor: null,
+        getNextPageParam: (lastPage) => lastPage.nextCursor,
+      },
     );
 
   if (error) {
@@ -45,7 +50,9 @@ export default function Search() {
       {!data && <FullPageSpinner />}
       {data && (
         <BookmarkList
-          bookmarks={data.bookmarks}
+          bookmarks={data.pages.flatMap((p) => p.bookmarks)}
+          fetchNextPage={fetchNextPage}
+          isFetchingNextPage={isFetchingNextPage}
           onRefresh={onRefresh}
           isRefreshing={isPending}
         />
-- 
cgit v1.3-1-g0d28

