diff --git a/src/hooks/api.ts b/src/hooks/api.ts index cc342fc..3a53daf 100644 --- a/src/hooks/api.ts +++ b/src/hooks/api.ts @@ -1,5 +1,9 @@ import { type Dispatch, type SetStateAction, useState } from "react" +export type Pagination = { page: number; limit: number; offset: number } +export type SetPagination = Dispatch< + SetStateAction<{ page: number; limit: number }> +> export type UsePaginationOptions = Partial<{ page: number limit: number @@ -7,21 +11,16 @@ export type UsePaginationOptions = Partial<{ export function usePagination( options?: UsePaginationOptions, -): [ - { page: number; limit: number; offset: number }, - Dispatch>, -] { +): [Pagination, SetPagination] { const { page = 0, limit = 150 } = options || {} - const [pagination, _setPagination] = useState({ + const [pagination, _setPagination] = useState({ page, limit, offset: page * limit, }) - function setPagination( - value: SetStateAction<{ page: number; limit: number }>, - ) { + const setPagination: SetPagination = value => { _setPagination(({ page: previousPage, limit: previousLimit }) => { let { page, limit } = typeof value === "function" diff --git a/src/hooks/index.ts b/src/hooks/index.ts index cdb8319..7c4d185 100644 --- a/src/hooks/index.ts +++ b/src/hooks/index.ts @@ -1,4 +1,9 @@ -import { usePagination, type UsePaginationOptions } from "./api" +import { + usePagination, + type Pagination, + type SetPagination, + type UsePaginationOptions, +} from "./api" import { useSession, useSessionMetadata, @@ -21,7 +26,9 @@ export { useSearchParams, useSession, useSessionMetadata, + type Pagination, type SessionMetadata, + type SetPagination, type UsePaginationOptions, type UseSessionChildren, type UseSessionChildrenFunction,