import React, { useEffect, useMemo, useState } from 'react'
import styles from './_pagination.module.scss'
import Button from '../Button/Button'
import Icon from '../Icons/Icon'
import { useIsMobileView } from '../../hooks/useIsMobileView/useIsMobileView'
import PaginationEllipsis from './PaginationEllipsis/PaginationEllipsis'
import { c } from '../../translations/LibraryTranslationService'
export type PaginationProps = {
/** Total number of pages */
totalPages: number
/** Function to call when a new page is selected */
callout: (page: number) => void
/** The current page passed in by the parent */
currentPage: number
/** Optional prop to add a test id to the Pagination for QA testing */
qaTestId?: string
}
const Pagination = ({
totalPages,
callout,
currentPage,
qaTestId = 'pagination',
}: PaginationProps): React.JSX.Element => {
const [activePage, setActivePage] = useState(currentPage - 1)
const lastPage = totalPages - 1
const updatePage = (page: number) => {
setActivePage(page)
callout(page + 1)
}
const isMobileView = useIsMobileView()
useEffect(() => {
setActivePage(currentPage - 1)
}, [currentPage])
const options = useMemo(
() =>
Array.from({ length: totalPages }, (_, i) => ({
name: (i + 1).toString(),
label: (i + 1).toString(),
})),
[totalPages],
)
const renderPageButton = (page: number) => {
// Digits counts the number of characters in the page number. This is used to help calculate how wide a button should be.
const digits =
(page + 1).toString().length <= 2 ? 1 : (page + 1).toString().length
const width = {
1: '32px',
3: '40px',
4: '48px',
5: '56px',
6: '64px',
}[digits]
return (
)
}
const renderPageButtons = () => {
if (isMobileView) {
return (