mirror of
https://github.com/Unleash/unleash.git
synced 2025-06-04 01:18:20 +02:00
This PR adds support for projects as a first class citizen, and toggling features on in different environments.
46 lines
1.0 KiB
TypeScript
46 lines
1.0 KiB
TypeScript
import { useEffect, useState } from 'react';
|
|
import { paginate } from '../utils/paginate';
|
|
|
|
const usePagination = (data: any[], limit: number) => {
|
|
const [paginatedData, setPaginatedData] = useState([[]]);
|
|
const [pageIndex, setPageIndex] = useState(0);
|
|
|
|
useEffect(() => {
|
|
const result = paginate(data, limit);
|
|
setPaginatedData(result);
|
|
}, [data, limit]);
|
|
|
|
const nextPage = () => {
|
|
if (pageIndex < paginatedData.length - 1) {
|
|
setPageIndex(prev => prev + 1);
|
|
}
|
|
};
|
|
|
|
const prevPage = () => {
|
|
if (pageIndex > 0) {
|
|
setPageIndex(prev => prev - 1);
|
|
}
|
|
};
|
|
|
|
const lastPage = () => {
|
|
setPageIndex(paginatedData.length - 1);
|
|
};
|
|
|
|
const firstPage = () => {
|
|
setPageIndex(0);
|
|
};
|
|
|
|
return {
|
|
page: paginatedData[pageIndex] || [],
|
|
pages: paginatedData,
|
|
nextPage,
|
|
prevPage,
|
|
lastPage,
|
|
firstPage,
|
|
setPageIndex,
|
|
pageIndex,
|
|
};
|
|
};
|
|
|
|
export default usePagination;
|