/** * External dependencies */ import { Button, Text } from '@automattic/jetpack-components'; import { Icon, chevronLeft, chevronRight } from '@wordpress/icons'; import classnames from 'classnames'; /** * Internal dependencies */ import useVideos, { useLocalVideos } from '../../hooks/use-videos'; import styles from './style.module.scss'; import { PaginationProps } from './types'; import type React from 'react'; const range = ( start, count ) => { return [ ...Array( count ) ].map( ( _, index ) => index + start ); }; const Ellipsis = () => ( ); /** * Pagination component * * @param {PaginationProps} props - Component props. * @returns {React.ReactNode} - Pagination react component. */ const Pagination: React.FC< PaginationProps > = ( { className, currentPage = 1, perPage, total, minColumns = 7, disabled, onChangePage, } ) => { if ( ! total || ! perPage ) { return null; } const numPages = Math.ceil( total / perPage ); if ( currentPage > numPages ) { onChangePage( numPages ); return null; } if ( currentPage < 1 ) { onChangePage( 1 ); return null; } const PageButton = ( { page }: { page: number } ) => { const isCurrent = page === currentPage; return ( ); }; // Smallest odd integer from minColumns, inclusive, with a minimum of 7 columns let numColumns = Math.max( minColumns, 7 ); numColumns = numColumns % 2 === 0 ? numColumns + 1 : numColumns; let content; if ( numPages <= numColumns ) { content = range( 1, numPages ).map( i => ); } else if ( currentPage < numColumns - 2 ) { content = ( <> { range( 1, numColumns - 2 ).map( i => ( ) ) } ); } else if ( currentPage > numPages - numColumns + 3 ) { content = ( <> { range( numPages - numColumns + 3, numColumns - 2 ).map( i => ( ) ) } ); } else { const numSideColumns = ( numColumns - 5 ) / 2; content = ( <> { range( 1, numSideColumns ).map( i => ( ) ) } { range( currentPage - 1, 3 ).map( i => ( ) ) } { range( numPages - numSideColumns + 1, numSideColumns ).map( i => ( ) ) } ); } return (
{ content }
); }; export const ConnectPagination = ( props: { className: string; disabled?: boolean } ) => { const { setPage, page, itemsPerPage, total, isFetching } = useVideos(); return total <= itemsPerPage ? (
) : ( ); }; export const ConnectLocalPagination = ( props: { className?: string; disabled?: boolean } ) => { const { setPage, page, itemsPerPage, total, isFetching } = useLocalVideos(); return total < itemsPerPage ? (
) : ( ); }; export default Pagination;