feat: Add pagination to SSL table

- Implement pagination for the SSL dashboard table, includes adding a dropdown for selecting the number of records per page (10, 30, 50).
This commit is contained in:
Tola Leng
2025-07-27 20:07:59 +07:00
parent c329425ac1
commit e2f41fdef1
4 changed files with 267 additions and 64 deletions
+52
View File
@@ -0,0 +1,52 @@
import { useState, useMemo } from 'react';
import { SSLCertificate } from '@/types/ssl.types';
export type SSLPageSize = 10 | 30 | 50;
interface UseSSLPaginationProps {
certificates: SSLCertificate[];
initialPageSize?: SSLPageSize;
}
export const useSSLPagination = ({
certificates,
initialPageSize = 10
}: UseSSLPaginationProps) => {
const [currentPage, setCurrentPage] = useState(1);
const [pageSize, setPageSize] = useState<SSLPageSize>(initialPageSize);
const { paginatedCertificates, totalPages } = useMemo(() => {
const totalItems = certificates.length;
const pages = Math.ceil(totalItems / pageSize);
const startIndex = (currentPage - 1) * pageSize;
const endIndex = startIndex + pageSize;
return {
paginatedCertificates: certificates.slice(startIndex, endIndex),
totalPages: Math.max(1, pages)
};
}, [certificates, currentPage, pageSize]);
// Reset to first page when page size changes or certificates change
const handlePageSizeChange = (newPageSize: SSLPageSize) => {
setPageSize(newPageSize);
setCurrentPage(1);
};
// Reset to first page if current page exceeds total pages
const handlePageChange = (page: number) => {
const validPage = Math.min(Math.max(1, page), totalPages);
setCurrentPage(validPage);
};
return {
paginatedCertificates,
currentPage,
totalPages,
pageSize,
totalItems: certificates.length,
handlePageChange,
handlePageSizeChange,
};
};