diff --git a/src/component/partial/pagination.tsx b/src/component/partial/pagination.tsx index 38340a9..357eaf9 100644 --- a/src/component/partial/pagination.tsx +++ b/src/component/partial/pagination.tsx @@ -1,18 +1,12 @@ import type { FC } from 'react'; -export type PaginationProps = { - submitPage: (page: number) => void; - currentPage: number; - totalPages: number; - maxPages: number; -}; - -export const Pagination: FC = ({ submitPage, currentPage, totalPages, maxPages }: PaginationProps) => { - if (totalPages <= 1 || maxPages <= 1) { - return
; +const calculatePages = (currentPage: number, totalPages: number, maxPages: number) => { + if (totalPages <= 1 || maxPages <= 1 || currentPage > totalPages) { + return []; } const pages = [currentPage]; + for (let i = 1; ; i++) { if (currentPage - i >= 1) { pages.push(currentPage - i); @@ -33,6 +27,23 @@ export const Pagination: FC = ({ submitPage, currentPage, total pages.sort((a, b) => a - b); + return pages; +}; + +export type PaginationProps = { + submitPage: (page: number) => void; + currentPage: number; + totalPages: number; + maxPages: number; +}; + +export const Pagination: FC = ({ submitPage, currentPage, totalPages, maxPages }: PaginationProps) => { + const pages = calculatePages(currentPage, totalPages, maxPages); + + if (pages.length === 0) { + return
; + } + return (
    {currentPage > 2 ? (