mirror of
https://github.com/R0m1k3/LogiFlow.git
synced 2026-10-12 01:37:40 +02:00
Extract pagination logic into a reusable hook (usePagination) and update components (CustomerOrders, Dashboard, Deliveries, Orders) to utilize it, enhancing UI layout for better overflow handling. Replit-Commit-Author: Agent Replit-Commit-Session-Id: cc8ba926-557c-4bf1-a2ea-ceabbcf38824 Replit-Commit-Checkpoint-Type: full_checkpoint Replit-Commit-Screenshot-Url: https://storage.googleapis.com/screenshot-production-us-central1/1957c339-2757-4d1f-8e92-e9f71a1ce58e/cc8ba926-557c-4bf1-a2ea-ceabbcf38824/0XBiHIk
30 lines
854 B
TypeScript
30 lines
854 B
TypeScript
import { useState, useMemo, useEffect } from 'react';
|
|
|
|
export function usePagination<T>(data: T[], initialItemsPerPage: number = 10) {
|
|
const [currentPage, setCurrentPage] = useState(1);
|
|
const [itemsPerPage, setItemsPerPage] = useState(initialItemsPerPage);
|
|
|
|
const totalItems = data.length;
|
|
const totalPages = Math.ceil(totalItems / itemsPerPage);
|
|
|
|
// Reset to first page when data changes
|
|
useEffect(() => {
|
|
setCurrentPage(1);
|
|
}, [data.length, itemsPerPage]);
|
|
|
|
const paginatedData = useMemo(() => {
|
|
const startIndex = (currentPage - 1) * itemsPerPage;
|
|
const endIndex = startIndex + itemsPerPage;
|
|
return data.slice(startIndex, endIndex);
|
|
}, [data, currentPage, itemsPerPage]);
|
|
|
|
return {
|
|
currentPage,
|
|
setCurrentPage,
|
|
itemsPerPage,
|
|
setItemsPerPage,
|
|
totalPages,
|
|
totalItems,
|
|
paginatedData,
|
|
};
|
|
} |