Skip to content

:hedgehog: PostHog is the leading platform for building self-driving products. Our developer tools – AI observability, analytics, session replay, flags, experiments, error tracking, logs, and more – capture all the context agents need to diagnose problems, uncover opportunities, and ship fixes. Steer it all from Slack, web, desktop, or the MCP.

4.0 KB
Raw Blame
import './PaginationControl.scss'

import clsx from 'clsx'

import { IconChevronLeft, IconChevronRight } from '@posthog/icons'

import { LemonButton } from 'lib/lemon-ui/LemonButton'
import { LemonSelect } from 'lib/lemon-ui/LemonSelect'

import { PaginationState } from './types'

export interface PaginationControlProps<T> extends PaginationState<T> {
    nouns?: [string, string]
    bordered?: boolean
}

export function PaginationControl<T>({
    pagination,
    currentPage,
    setCurrentPage,
    pageCount,
    dataSourcePage,
    entryCount,
    entryCountIsLowerBound,
    currentStartIndex,
    currentEndIndex,
    nouns = ['entry', 'entries'],
    bordered = false,
}: PaginationControlProps<T>): JSX.Element | null {
    /** Whether pages previous and next are available. */
    const isPreviousAvailable: boolean =
        currentPage !== null ? currentPage > 1 : !!(pagination?.controlled && pagination.onBackward)
    // A capped count does not tell where the rows end, so the next handler decides whether another page exists.
    const lastPage: number = entryCountIsLowerBound ? Infinity : (pageCount as number)
    const isNextAvailable: boolean =
        currentPage !== null && pageCount !== null && !entryCountIsLowerBound
            ? currentPage < pageCount
            : !!(pagination?.controlled && pagination.onForward)
    /** Whether there's reason to show pagination. */
    const showPagination: boolean = isPreviousAvailable || isNextAvailable || pagination?.hideOnSinglePage === false

    const currentPageSize = dataSourcePage.length

    return showPagination ? (
        <div className={clsx('PaginationControl', bordered && 'PaginationControl--bordered')}>
            <span>
                {currentPageSize === 0
                    ? `No ${nouns[1]}`
                    : entryCount === null
                      ? `${currentPageSize} ${currentPageSize === 1 ? nouns[0] : nouns[1]} on this page`
                      : currentPageSize === 1
                        ? `${currentEndIndex} of ${entryCount}${entryCountIsLowerBound ? '+' : ''} ${entryCount === 1 ? nouns[0] : nouns[1]}`
                        : `${currentStartIndex + 1}-${currentEndIndex} of ${entryCount}${entryCountIsLowerBound ? '+' : ''} ${nouns[1]}`}
            </span>
            {pagination && !pagination.controlled && pagination.showPageSelector && pageCount && currentPage ? (
                <LemonSelect
                    size="small"
                    aria-label="Go to page"
                    data-attr="pagination-go-to-page"
                    value={currentPage}
                    onChange={setCurrentPage}
                    options={Array.from({ length: pageCount }, (_, index) => ({
                        value: index + 1,
                        label: `Page ${index + 1} of ${pageCount}`,
                    }))}
                />
            ) : null}
            <LemonButton
                icon={<IconChevronLeft />}
                aria-label="Previous page"
                disabledReason={!isPreviousAvailable ? 'No previous page' : undefined}
                size="small"
                onClick={() => {
                    pagination?.controlled && pagination.onBackward?.()
                    if ((pagination?.controlled && currentPage) || !pagination?.controlled) {
                        setCurrentPage(Math.max(1, Math.min(lastPage, currentPage as number) - 1))
                    }
                }}
            />
            <LemonButton
                icon={<IconChevronRight />}
                aria-label="Next page"
                disabledReason={!isNextAvailable ? 'No next page' : undefined}
                size="small"
                onClick={() => {
                    pagination?.controlled && pagination.onForward?.()
                    if ((pagination?.controlled && currentPage) || !pagination?.controlled) {
                        setCurrentPage(Math.min(lastPage, (currentPage as number) + 1))
                    }
                }}
            />
        </div>
    ) : null
}