sendou.ink/app/hooks/useSimplePagination.ts
Kalle 35168111a6
Art (#1412)
* Add to nav

* Allow nav items on front page to take full width

* Initial+

* Fix vods page title

* Add page title

* Common art type

* ArtGrid to a different component

* User arts page initial

* Add art tab to user page

* Preview initial

* Fix art counting

* Fix link and onclick overlapping

* Link to user art page to arts they made

* Artist role initial

* Show description

* Make toggle in art page saved in search params

* Add white-space pre-wrap to plus comments

Not sure why it was removed in the first place?

* Commission open / text and edit those

* Add simple pagination

* New art link display logic

* New art initial

* Upload art

* Hide art from side nav too

* Show banner when waiting for approval

* Edit art

* Fix art sub nav link not showing active

* Relocate unvalidated art text

* Delete art

* Extract ImagePreview component

* Eliminate some layout shift

* BigImageDialog extract component + prevent layout shift

* i18n

* Fix unused var

* Fix tests
2023-07-09 11:48:28 +03:00

46 lines
1.1 KiB
TypeScript

import * as React from "react";
export function useSimplePagination<T>({
items,
pageSize,
}: {
items: T[];
pageSize: number;
}) {
const [currentPage, setCurrentPage] = React.useState(1);
const pagesCount = Math.ceil(items.length / pageSize);
const itemsToDisplay = React.useMemo(
() => items.slice((currentPage - 1) * pageSize, currentPage * pageSize),
[currentPage, items, pageSize]
);
const nextPage = React.useCallback(() => {
if (currentPage < pagesCount) {
setCurrentPage((prev) => prev + 1);
window.scrollTo(0, 0);
}
}, [currentPage, pagesCount]);
const previousPage = React.useCallback(() => {
if (currentPage > 1) {
setCurrentPage((prev) => prev - 1);
window.scrollTo(0, 0);
}
}, [currentPage]);
const thereIsNextPage = currentPage < pagesCount;
const thereIsPreviousPage = currentPage > 1;
return {
pagesCount,
currentPage,
itemsToDisplay,
nextPage,
previousPage,
thereIsNextPage,
thereIsPreviousPage,
everythingVisible: items.length === itemsToDisplay.length,
};
}