From 5e9b93bea2aaf54a3e1d92d44099723142dff39e Mon Sep 17 00:00:00 2001 From: Szedann <65607498+Szedann@users.noreply.github.com> Date: Sun, 20 Jul 2025 13:26:31 +0200 Subject: [PATCH] feat: search pagination --- .../frontend/src/hooks/usePagination.tsx | 118 +++++++++++++++ .../app/browser/package.$provider.$slug.tsx | 141 +++++++++--------- .../src/routes/app/browser/search.tsx | 37 ++++- .../frontend/src/styles/global.css | 6 +- 4 files changed, 231 insertions(+), 71 deletions(-) create mode 100644 apps/onelauncher/frontend/src/hooks/usePagination.tsx diff --git a/apps/onelauncher/frontend/src/hooks/usePagination.tsx b/apps/onelauncher/frontend/src/hooks/usePagination.tsx new file mode 100644 index 00000000..33d5937e --- /dev/null +++ b/apps/onelauncher/frontend/src/hooks/usePagination.tsx @@ -0,0 +1,118 @@ +import { Button, Show } from '@onelauncher/common/components'; +import { ChevronRightIcon, ChevronLeftIcon } from '@untitled-theme/icons-react'; +import { useState } from 'react'; + +export interface PaginationOptions { + itemsCount: number; + itemsPerPage: number; + initialPage?: number; +}; + +function usePagination(options: PaginationOptions) { + // const [options, setOptions] = useState(initialOptions); + + const [page, setPage] = useState(Math.max(1, options.initialPage || 1)); + + const next = () => setPage(page => page + 1); + const prev = () => setPage(page => page - 1); + + const totalPages = Math.ceil(options.itemsCount / options.itemsPerPage); + + const hasNext = page < totalPages; + const hasPrev = page > 1; + + const offset = (page - 1) * options.itemsPerPage; + + // Exported + function reset(/*options: PaginationOptions*/) { + setPage(1); + } + + function getVisiblePageNumbers(page: number) { + const pages: number[] = []; + + const left = page - 1 > 1 ? page - 1 : null; + const right = page + 1 < totalPages ? page + 1 : null; + + if (left !== null) + pages.push(left); + + if (page !== 1 && page !== totalPages) + pages.push(page); + + if (right !== null) + pages.push(right); + + return pages; + } + + function PaginationBtn(props: { page: number }) { + return ( + + ); + } + + const Navigation = () => ( +