export type PaginationPageControlMode = 'select' | 'display' interface PaginationDisplayStateArgs { pageIndex: number pageSize?: number totalCount?: number currentPageCount?: number } interface PaginationControlsStateArgs { pageIndex: number pageSize?: number totalCount?: number canNextPage?: boolean pageControlMode?: PaginationPageControlMode } export function getPaginationDisplayState({ pageIndex, pageSize, totalCount, currentPageCount, }: PaginationDisplayStateArgs) { if (pageSize === undefined) { return { hasTotalCount: totalCount !== undefined, canRenderDisplay: false, start: 0, end: 0, } } const hasTotalCount = totalCount !== undefined const effectiveCurrentPageCount = hasTotalCount ? Math.max(0, Math.min(pageSize, (totalCount ?? 0) - pageIndex * pageSize)) : Math.max(0, currentPageCount ?? 0) const hasRows = effectiveCurrentPageCount > 0 const start = hasRows ? pageIndex * pageSize + 1 : 0 const end = hasTotalCount ? Math.min((pageIndex + 1) * pageSize, totalCount) : hasRows ? start + effectiveCurrentPageCount - 1 : 0 return { hasTotalCount, canRenderDisplay: hasRows, start, end, } } export function getPaginationControlsState({ pageIndex, pageSize, totalCount, canNextPage: canNextPageProp, pageControlMode = 'select', }: PaginationControlsStateArgs) { const pageCount = totalCount !== undefined && pageSize !== undefined ? Math.ceil(totalCount / pageSize) : undefined const effectivePageControlMode = pageControlMode === 'select' && pageCount !== undefined ? 'select' : 'display' const canPreviousPage = pageIndex > 0 const canNextPage = canNextPageProp ?? (pageCount !== undefined ? pageIndex < pageCount - 1 : false) return { currentPage: pageIndex + 1, pageCount, effectivePageControlMode, canPreviousPage, canNextPage, canRenderControls: canPreviousPage || canNextPage, } }