Skip to content

📈 Open source, privacy-first web analytics — a cookie-less Google Analytics alternative with built-in error tracking and performance monitoring. Self-host it or use our cloud. Get started for free!

added dots

kruzhambus23d28e97a8db
diff --git a/src/hooks/usePagination.jsx b/src/hooks/usePagination.jsx
new file mode 100644
index 000000000..c1fe96e32
--- /dev/null
+++ b/src/hooks/usePagination.jsx
@@ -0,0 +1,73 @@
+import { useMemo } from 'react'
+import { ENTRIES_PER_PAGE_DASHBOARD } from 'redux/constants'
+
+export const DOTS = '...'
+
+const range = (start, end) => {
+ const length = end - start + 1
+ return Array.from({ length }, (_, idx) => idx + start)
+}
+
+export const usePagination = (
+ totalCount,
+ currentPage,
+ siblingCount = 1,
+ pageSize = ENTRIES_PER_PAGE_DASHBOARD,
+) => {
+ const paginationRange = useMemo(() => {
+ const totalPageCount = Math.ceil(totalCount / pageSize)
+
+ // Pages count is determined as siblingCount + firstPage + lastPage + currentPage + 2*DOTS
+ const totalPageNumbers = siblingCount + 5
+
+ /*
+ If the number of pages is less than the page numbers we want to show in our
+ paginationComponent, we return the range [1..totalPageCount]
+ */
+ if (totalPageNumbers >= totalPageCount) {
+ return range(1, totalPageCount)
+ }
+
+ const leftSiblingIndex = Math.max(currentPage - siblingCount, 1)
+ const rightSiblingIndex = Math.min(
+ currentPage + siblingCount,
+ totalPageCount,
+ )
+
+ /*
+ We do not want to show dots if there is only one position left
+ after/before the left/right page count as that would lead to a change if our Pagination
+ component size which we do not want
+ */
+ const shouldShowLeftDots = leftSiblingIndex > 2
+ const shouldShowRightDots = rightSiblingIndex < totalPageCount - 2
+
+ const firstPageIndex = 1
+ const lastPageIndex = totalPageCount
+
+ if (!shouldShowLeftDots && shouldShowRightDots) {
+ const leftItemCount = 3 + 2 * siblingCount
+ const leftRange = range(1, leftItemCount)
+
+ return [...leftRange, DOTS, totalPageCount]
+ }
+
+ if (shouldShowLeftDots && !shouldShowRightDots) {
+ const rightItemCount = 3 + 2 * siblingCount
+ const rightRange = range(
+ totalPageCount - rightItemCount + 1,
+ totalPageCount,
+ )
+ return [firstPageIndex, DOTS, ...rightRange]
+ }
+
+ if (shouldShowLeftDots && shouldShowRightDots) {
+ const middleRange = range(leftSiblingIndex, rightSiblingIndex)
+ return [firstPageIndex, DOTS, ...middleRange, DOTS, lastPageIndex]
+ }
+
+ return []
+ }, [totalCount, pageSize, siblingCount, currentPage])
+
+ return paginationRange
+}
diff --git a/src/pages/Dashboard/Dashboard.jsx b/src/pages/Dashboard/Dashboard.jsx
index 0739dc0da..724b2303b 100644
--- a/src/pages/Dashboard/Dashboard.jsx
+++ b/src/pages/Dashboard/Dashboard.jsx
@@ -282,7 +282,7 @@ const Dashboard = ({
{
pageAmount > 1 && (
- <Pagination page={dashboardPaginationPage} setPage={(page) => setDashboardPaginationPage(page)} pageAmount={pageAmount || 0} />
+ <Pagination page={dashboardPaginationPage} setPage={(page) => setDashboardPaginationPage(page)} pageAmount={pageAmount || 0} total={total} />
)
}
</div>
diff --git a/src/ui/Pagination.jsx b/src/ui/Pagination.jsx
index 3b559aae8..556b3d7e9 100644
--- a/src/ui/Pagination.jsx
+++ b/src/ui/Pagination.jsx
@@ -4,9 +4,13 @@ import _map from 'lodash/map'
import cx from 'clsx'
import { useTranslation } from 'react-i18next'
import PropTypes from 'prop-types'
+import { usePagination, DOTS } from 'hooks/usePagination'
-const Pagination = ({ page, setPage, pageAmount }) => {
+const Pagination = ({
+ page, setPage, pageAmount, total,
+}) => {
const { t } = useTranslation('common')
+ const paginationRange = usePagination(total, page)
return (
<nav className='border-t border-gray-200 px-4 flex items-center justify-between sm:px-0'>
@@ -26,40 +30,26 @@ const Pagination = ({ page, setPage, pageAmount }) => {
</div>
<div className='hidden md:-mt-px md:flex'>
{
- _map(Array(pageAmount), (_, index) => {
- // if (pageAmount > 6 && Math.ceil(pageAmount / 2) + page === index && page < pageAmount - 3 && page !== pageAmount) {
- // return (
- // <span className='border-transparent text-gray-500 border-t-2 pt-4 px-4 inline-flex items-center text-sm font-medium' key={index}>
- // true
- // </span>
- // )
- // }
-
- // if (pageAmount > 6 && Math.ceil(pageAmount / 2) + page === index && page < pageAmount - 3 && page !== pageAmount) {
- // return (
- // <span className='border-transparent text-gray-500 border-t-2 pt-4 px-4 inline-flex items-center text-sm font-medium' key={index}>
- // false
- // </span>
- // )
- // }
-
- // if (index >= pageAmount && index < pageAmount - 3) {
- // return (
- // <div className='sr-only' key={index} />
- // )
- // }
+ _map(paginationRange, (item, index) => {
+ if (item === DOTS) {
+ return (
+ <span className='border-transparent text-gray-500 border-t-2 pt-4 px-4 inline-flex items-center text-sm font-medium' key={item + index}>
+ { DOTS }
+ </span>
+ )
+ }
return (
<button
- key={index}
+ key={item}
type='button'
- onClick={() => setPage(index + 1)}
+ onClick={() => setPage(item)}
className={cx({
- 'border-indigo-500 text-indigo-600 border-t-2 pt-4 px-4 inline-flex items-center text-sm font-medium': index + 1 === page,
- 'border-transparent text-gray-500 hover:text-gray-700 hover:border-gray-300 border-t-2 pt-4 px-4 inline-flex items-center text-sm font-medium': index + 1 !== page,
+ 'border-indigo-500 text-indigo-600 border-t-2 pt-4 px-4 inline-flex items-center text-sm font-medium': item === page,
+ 'border-transparent text-gray-500 hover:text-gray-700 hover:border-gray-300 border-t-2 pt-4 px-4 inline-flex items-center text-sm font-medium': item !== page,
})}
>
- {index + 1}
+ {item}
</button>
)
})
@@ -87,6 +77,7 @@ Pagination.propTypes = {
page: PropTypes.number.isRequired,
setPage: PropTypes.func.isRequired,
pageAmount: PropTypes.number.isRequired,
+ total: PropTypes.number.isRequired,
}
export default memo(Pagination)