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!

Add support for 'Results per page' selector

Blue Mouseedbce55f7efe
diff --git a/web/app/ui/Pagination.tsx b/web/app/ui/Pagination.tsx
index f5197299a..738fed979 100644
--- a/web/app/ui/Pagination.tsx
+++ b/web/app/ui/Pagination.tsx
@@ -1,9 +1,10 @@
import React, { memo } from 'react'
-import { ArrowLongLeftIcon, ArrowLongRightIcon } from '@heroicons/react/24/solid'
+import { ChevronLeftIcon, ChevronRightIcon } from 'lucide-react'
import _map from 'lodash/map'
import cx from 'clsx'
-import { useTranslation } from 'react-i18next'
+import { Trans, useTranslation } from 'react-i18next'
import { usePagination, DOTS } from '~/hooks/usePagination'
+import Select from './Select'
interface PaginationProps {
page: number
@@ -12,75 +13,168 @@ interface PaginationProps {
total: number
className?: string
pageSize?: number
+ pageSizeOptions?: number[]
+ onPageSizeChange?: (size: number) => void
}
-const Pagination = ({ page, setPage, pageAmount, total, className, pageSize }: PaginationProps) => {
+const Pagination = ({
+ page,
+ setPage,
+ pageAmount,
+ total,
+ className,
+ pageSize = 10,
+ pageSizeOptions,
+ onPageSizeChange,
+}: PaginationProps) => {
const { t } = useTranslation('common')
const paginationRange = usePagination(total, page, 1, pageSize)
+ const processedPageSizeOptions = _map(pageSizeOptions, (option) => option.toString())
+
+ const start = (page - 1) * pageSize + 1
+ const end = Math.min(start + pageSize - 1, total)
+
return (
- <nav className={cx('flex items-center justify-between border-t-0 border-gray-200 px-4 sm:px-0', className)}>
- <div className='group -mt-px flex w-0 flex-1'>
- {page > 1 && (
- <button
- type='button'
- onClick={() => setPage(page - 1)}
- className='inline-flex items-center pr-1 pt-4 text-sm font-medium text-gray-500 group-hover:text-gray-700 dark:text-gray-300 dark:group-hover:text-gray-400'
- >
- <ArrowLongLeftIcon
- className='mr-3 h-5 w-5 text-gray-500 transition-none group-hover:text-gray-700 dark:text-gray-300 dark:group-hover:text-gray-400'
- aria-hidden='true'
- />
- {t('project.prev')}
- </button>
- )}
+ <div
+ className={cx('flex items-center justify-between border-t border-gray-200 py-3 dark:border-gray-700', className)}
+ >
+ <div className='flex flex-1 justify-between sm:hidden'>
+ <button
+ onClick={() => {
+ if (page === 1) {
+ return
+ }
+
+ setPage(page - 1)
+ }}
+ disabled={page === 1}
+ className={cx(
+ 'relative inline-flex items-center rounded-md border border-gray-300 bg-white px-4 py-2 text-sm font-medium text-gray-700 dark:border-gray-600 dark:bg-slate-800 dark:text-gray-200',
+ {
+ 'cursor-not-allowed': page === 1,
+ 'hover:bg-gray-100 dark:hover:bg-slate-700': page !== 1,
+ },
+ )}
+ >
+ {t('project.prev')}
+ </button>
+ <button
+ onClick={() => {
+ if (page === pageAmount) {
+ return
+ }
+
+ setPage(page + 1)
+ }}
+ disabled={page === pageAmount}
+ className={cx(
+ 'relative ml-3 inline-flex items-center rounded-md border border-gray-300 bg-white px-4 py-2 text-sm font-medium text-gray-700 dark:border-gray-600 dark:bg-slate-800 dark:text-gray-200',
+ {
+ 'cursor-not-allowed': page === pageAmount,
+ 'hover:bg-gray-100 dark:hover:bg-slate-700': page !== pageAmount,
+ },
+ )}
+ >
+ {t('project.next')}
+ </button>
</div>
- <div className='hidden md:-mt-px md:flex'>
- {_map(paginationRange, (item, index) => {
- if (item === DOTS) {
- return (
- <span
- className='inline-flex items-center border-t-2 border-transparent px-4 pt-4 text-sm font-medium text-gray-500 dark:text-gray-300'
- key={item + index}
- >
- {DOTS}
- </span>
- )
- }
+ <div className='hidden w-full sm:flex sm:flex-wrap sm:items-center sm:justify-between sm:gap-y-4'>
+ <p className='text-sm text-gray-700 dark:text-gray-200'>
+ <Trans t={t} i18nKey='common.pagination' values={{ start, end, total }}>
+ <span className='font-medium' />
+ </Trans>
+ </p>
+ <div className='flex items-center gap-4'>
+ {pageSizeOptions && onPageSizeChange && (
+ <Select<string>
+ label={t('common.resultsPerPage')}
+ title={pageSize.toString()}
+ items={processedPageSizeOptions}
+ onSelect={(item) => onPageSizeChange(Number(item))}
+ labelClassName='!overflow-visible'
+ />
+ )}
+ <nav className='isolate inline-flex -space-x-px rounded-md shadow-sm' aria-label='Pagination'>
+ <button
+ onClick={() => {
+ if (page === 1) {
+ return
+ }
- return (
+ setPage(page - 1)
+ }}
+ disabled={page === 1}
+ className={cx(
+ 'relative inline-flex items-center rounded-l-md border border-gray-300 px-2 py-2 text-gray-400 focus:z-20 dark:border-gray-800',
+ {
+ 'cursor-not-allowed': page === 1,
+ 'hover:bg-gray-100 dark:hover:bg-slate-700': page !== 1,
+ },
+ )}
+ >
+ <span className='sr-only'>{t('project.prev')}</span>
+ <ChevronLeftIcon className='h-5 w-5' aria-hidden='true' />
+ </button>
+ {_map(paginationRange, (item, index) => {
+ if (item === DOTS) {
+ return (
+ <button
+ key={item + index}
+ onClick={() => {
+ const lastShownPage = paginationRange[index - 1] as number
+ const nextShownPage = paginationRange[index + 1] as number
+ const middlePage = Math.floor((lastShownPage + nextShownPage) / 2)
+
+ setPage(middlePage)
+ }}
+ className='relative inline-flex items-center border border-gray-300 px-4 py-2 text-sm font-semibold text-gray-700 hover:bg-gray-100 dark:border-gray-800 dark:text-gray-200 dark:hover:bg-slate-700'
+ >
+ {DOTS}
+ </button>
+ )
+ }
+
+ return (
+ <button
+ key={item}
+ onClick={() => setPage(item)}
+ className={cx(
+ 'relative inline-flex items-center border border-gray-300 px-4 py-2 text-sm font-semibold focus:z-20 dark:border-gray-800',
+ {
+ 'z-10 bg-slate-800 text-white dark:bg-slate-700': item === page,
+ 'text-gray-900 hover:bg-gray-100 dark:text-gray-200 dark:hover:bg-slate-700': item !== page,
+ },
+ )}
+ >
+ {item}
+ </button>
+ )
+ })}
<button
- key={item}
- type='button'
- onClick={() => setPage(item)}
- className={cx({
- 'inline-flex items-center border-t-2 border-slate-900 px-4 pt-4 text-sm font-medium text-slate-900 dark:border-gray-50 dark:text-gray-50':
- item === page,
- 'inline-flex items-center border-t-2 border-transparent px-4 pt-4 text-sm font-medium text-gray-500 hover:border-gray-300 hover:text-gray-700 dark:text-gray-300 dark:hover:text-gray-400':
- item !== page,
- })}
+ onClick={() => {
+ if (page === pageAmount) {
+ return
+ }
+
+ setPage(page + 1)
+ }}
+ disabled={page === pageAmount}
+ className={cx(
+ 'relative inline-flex items-center rounded-r-md border border-gray-300 px-2 py-2 text-gray-400 focus:z-20 dark:border-gray-800',
+ {
+ 'cursor-not-allowed': page === pageAmount,
+ 'hover:bg-gray-100 dark:hover:bg-slate-700': page !== pageAmount,
+ },
+ )}
>
- {item}
+ <span className='sr-only'>{t('project.next')}</span>
+ <ChevronRightIcon className='h-5 w-5' aria-hidden='true' />
</button>
- )
- })}
- </div>
- <div className='group -mt-px flex w-0 flex-1 justify-end'>
- {page !== pageAmount && (
- <button
- type='button'
- onClick={() => setPage(page + 1)}
- className='inline-flex items-center pl-1 pt-4 text-sm font-medium text-gray-500 group-hover:text-gray-700 dark:text-gray-300 dark:group-hover:text-gray-400'
- >
- {t('project.next')}
- <ArrowLongRightIcon
- className='ml-3 h-5 w-5 text-gray-500 transition-none group-hover:text-gray-700 dark:text-gray-300 dark:group-hover:text-gray-400'
- aria-hidden='true'
- />
- </button>
- )}
+ </nav>
+ </div>
</div>
- </nav>
+ </div>
)
}
diff --git a/web/app/ui/Select.tsx b/web/app/ui/Select.tsx
index 3c7155e0f..c77531bff 100644
--- a/web/app/ui/Select.tsx
+++ b/web/app/ui/Select.tsx
@@ -48,13 +48,15 @@ function Select<T>({
<Listbox id={id || ''} value={title} onChange={onSelect}>
{({ open }) => (
<>
- <Label className='block whitespace-pre-line text-sm font-medium text-gray-700 dark:text-gray-100'>
- {label}
- </Label>
- <div className={cx('relative mt-1', className)}>
+ {label ? (
+ <Label className='mb-1 block whitespace-pre-line text-sm font-medium text-gray-700 dark:text-gray-100'>
+ {label}
+ </Label>
+ ) : null}
+ <div className={cx('relative', className)}>
<ListboxButton
className={cx(
- 'relative w-full cursor-default rounded-md border border-gray-300 bg-white py-2 pl-3 pr-10 text-left shadow-sm focus:border-indigo-500 focus:outline-none focus:ring-1 focus:ring-indigo-500 dark:border-gray-800 dark:bg-slate-800 dark:text-gray-50 sm:text-sm',
+ 'relative w-full rounded-md border border-gray-300 bg-white py-2 pl-3 pr-10 text-left shadow-sm focus:border-indigo-500 focus:outline-none focus:ring-1 focus:ring-indigo-500 dark:border-gray-800 dark:bg-slate-800 dark:text-gray-50 sm:text-sm',
buttonClassName,
)}
>