aboutsummaryrefslogtreecommitdiffstats
path: root/apps/web/components/dashboard/bookmarks/UpdatableBookmarksGrid.tsx
blob: a344320e5fa5390209f9540bba65dfae6e39a228 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
"use client";

import { api } from "@/lib/trpc";

import type {
  ZGetBookmarksRequest,
  ZGetBookmarksResponse,
} from "@hoarder/trpc/types/bookmarks";

import BookmarksGrid from "./BookmarksGrid";

export default function UpdatableBookmarksGrid({
  query,
  bookmarks: initialBookmarks,
  showEditorCard = false,
}: {
  query: ZGetBookmarksRequest;
  bookmarks: ZGetBookmarksResponse;
  showEditorCard?: boolean;
  itemsPerPage?: number;
}) {
  const { data, fetchNextPage, hasNextPage, isFetchingNextPage } =
    api.bookmarks.getBookmarks.useInfiniteQuery(query, {
      initialData: () => ({
        pages: [initialBookmarks],
        pageParams: [query.cursor],
      }),
      initialCursor: null,
      getNextPageParam: (lastPage) => lastPage.nextCursor,
    });

  return (
    <BookmarksGrid
      bookmarks={data!.pages.flatMap((b) => b.bookmarks)}
      hasNextPage={hasNextPage}
      fetchNextPage={() => fetchNextPage()}
      isFetchingNextPage={isFetchingNextPage}
      showEditorCard={showEditorCard}
    />
  );
}