Redpanda UIRedpanda UI
Hooks

useTokenPagination

Forward-only token-based pagination that bridges gRPC/Connect APIs to TanStack Table.

Installation

useTokenPagination adapts token-based pagination (the nextPageToken model used by gRPC/Connect APIs) to TanStack Table's offset-based pagination state. It's forward-only: there is no page token for going back, so canPreviousPage is always false. After each fetch, report the next token via onPageResolved, and wire paginationState / onPaginationChange into <DataTable>.

Usage

import { useTokenPagination } from "@/hooks/use-token-pagination";
import { DataTable } from "@/components/redpanda-ui/data-table";

function TokenPaginatedTable() {
  const pagination = useTokenPagination({ pageSize: 20 });

  const { data } = useQuery({
    queryKey: ["rows", pagination.currentPageToken, pagination.pageSize],
    queryFn: async () => {
      const result = await fetchRows({
        pageToken: pagination.currentPageToken,
        pageSize: pagination.pageSize,
      });
      pagination.onPageResolved(result); // report result.nextPageToken
      return result;
    },
  });

  return (
    <DataTable
      columns={columns}
      data={data?.rows ?? []}
      pageCount={pagination.pageCount}
      pagination={pagination.paginationState}
      onPaginationChange={pagination.onPaginationChange}
      tableOptions={{ manualPagination: true }}
    />
  );
}

API

useTokenPagination({ pageSize? })

OptionTypeDefaultDescription
pageSizenumber10Initial rows per page.

Returns

PropertyTypeDescription
currentPageTokenstring | undefinedToken for the current fetch; undefined on page 0.
pageSizenumberCurrent page size.
onPageResolved(r: { nextPageToken?: string }) => voidCall after each fetch to record the next token.
paginationStatePaginationStatePass to <DataTable>'s pagination prop.
onPaginationChangeOnChangeFn<PaginationState>Pass to <DataTable>'s onPaginationChange prop.
pageCountnumberPage count derived from hasNextPage.
canNextPagebooleanWhether a next page is available.
canPreviousPagefalseAlways false — backward navigation is unsupported.
reset() => voidReset to page 0.

For offset/URL-driven pagination, see useDataTableSearchParams.

Built by malinskibeniamin. The source code is available on GitHub.

On this page