import type { AgGridReact as AgGridReactType } from '@ag-grid-community/react/lib/agGridReact'; import { useForm } from '@mantine/form'; import { useDisclosure } from '@mantine/hooks'; import { MutableRefObject } from 'react'; import { useContainerQuery } from '/@/renderer/hooks'; import { ListKey } from '/@/renderer/store'; import { ActionIcon } from '/@/shared/components/action-icon/action-icon'; import { Button } from '/@/shared/components/button/button'; import { Flex } from '/@/shared/components/flex/flex'; import { Group } from '/@/shared/components/group/group'; import { NumberInput } from '/@/shared/components/number-input/number-input'; import { Pagination } from '/@/shared/components/pagination/pagination'; import { Popover } from '/@/shared/components/popover/popover'; import { Text } from '/@/shared/components/text/text'; import { TablePagination as TablePaginationType } from '/@/shared/types/types'; interface TablePaginationProps { pageKey: ListKey; pagination: TablePaginationType; setIdPagination?: (id: string, pagination: Partial) => void; setPagination?: (args: { data: Partial; key: ListKey }) => void; tableRef: MutableRefObject; } export const TablePagination = ({ pageKey, pagination, setIdPagination, setPagination, tableRef, }: TablePaginationProps) => { const [isGoToPageOpen, handlers] = useDisclosure(false); const containerQuery = useContainerQuery(); const goToForm = useForm({ initialValues: { pageNumber: undefined, }, }); const handlePagination = (index: number) => { const newPage = index - 1; tableRef.current?.api.paginationGoToPage(newPage); setPagination?.({ data: { currentPage: newPage }, key: pageKey }); setIdPagination?.(pageKey || '', { currentPage: newPage }); }; const handleGoSubmit = goToForm.onSubmit((values) => { handlers.close(); if ( !values.pageNumber || values.pageNumber < 1 || values.pageNumber > pagination.totalPages ) { return; } const newPage = values.pageNumber - 1; tableRef.current?.api.paginationGoToPage(newPage); setPagination?.({ data: { currentPage: newPage }, key: pageKey }); setIdPagination?.(pageKey || '', { currentPage: newPage }); }); const currentPageStartIndex = pagination.currentPage * pagination.itemsPerPage + 1; const currentPageMaxIndex = (pagination.currentPage + 1) * pagination.itemsPerPage; const currentPageStopIndex = currentPageMaxIndex > pagination.totalItems ? pagination.totalItems : currentPageMaxIndex; return ( {containerQuery.isMd ? ( <> Showing {currentPageStartIndex} - {currentPageStopIndex} of{' '} {pagination.totalItems} items ) : containerQuery.isSm ? ( <> {currentPageStartIndex} - {currentPageStopIndex} of{' '} {pagination.totalItems} items ) : ( <> {currentPageStartIndex} - {currentPageStopIndex} of{' '} {pagination.totalItems} )} handlers.close()} opened={isGoToPageOpen} position="bottom-start" trapFocus > handlers.toggle()} radius="sm" size="sm" style={{ height: '26px', padding: '0', width: '26px' }} />
); };