import { Box, ButtonGroup, Center, Flex, GridItem, Icon, Select, SimpleGrid, Skeleton, Spinner, Table, Tbody, Td, Th, Thead, Tr, } from "@chakra-ui/react"; import { UseQueryResult } from "@tanstack/react-query"; import { ColumnDef, flexRender, getCoreRowModel, useReactTable, } from "@tanstack/react-table"; import pluralize from "pluralize"; import { SetStateAction, useMemo } from "react"; import { FiArrowRight } from "react-icons/fi"; import { Button, TableContainer, Text } from "tw-components"; type TWQueryTableProps = { columns: ColumnDef[]; query: UseQueryResult; selectData: (data?: TInputData) => TRowData[]; onRowClick?: (row: TRowData) => void; pagination?: Omit, "data" | "isLoading">; title: string; }; export function TWQueryTable( tableProps: TWQueryTableProps, ) { const data = tableProps.selectData(tableProps.query.data); const isLoading = tableProps.query.isLoading; const isFetching = tableProps.query.isFetching; const isFetched = tableProps.query.isFetched; const table = useReactTable({ data, columns: tableProps.columns, getCoreRowModel: getCoreRowModel(), }); return ( {table.getHeaderGroups().map((headerGroup) => ( {headerGroup.headers.map((header) => ( ))} {/* if the row is clickable we want an arrow to show */} {tableProps.onRowClick && ))} {table.getRowModel().rows.map((row) => { return ( tableProps.onRowClick?.(row.original), _hover: { bg: "blackAlpha.50" }, _dark: { _hover: { bg: "whiteAlpha.50", }, }, } : {})} > {row.getVisibleCells().map((cell) => { return ( ); })} {/* if the row is clickable we want an arrow to show */} {tableProps.onRowClick && ( )} ); })}
{header.isPlaceholder ? null : ( {flexRender( header.column.columnDef.header, header.getContext(), )} )} }
{flexRender( cell.column.columnDef.cell, cell.getContext(), )}
{isLoading && (
Loading {pluralize(tableProps.title, 0, false)}
)} {!isLoading && !isFetching && data.length === 0 && isFetched && (
No {pluralize(tableProps.title, 0, false)} found.
)}
{/* render pagination if pagination is enabled */} {tableProps.pagination && ( )}
); } type PaginationProps = { data?: TInputData; isLoading: boolean; pageSize: number; setPageSize?: (value: SetStateAction) => void; pageSizeOptions?: number[]; page: number; setPage: (value: SetStateAction) => void; selectTotalCount: (data?: TInputData) => number; }; const DEFAULT_PAGE_SIZE_OPTIONS = [25, 50, 100, 250]; const MAX_PAGE_BUTTONS = 7; type PageButton = | { type: "page"; page: number } | { type: "ellipsis"; key: string }; function Pagination(paginationProps: PaginationProps) { const totalCount = paginationProps.selectTotalCount(paginationProps.data); const totalPages = Math.ceil(totalCount / paginationProps.pageSize); const pageSizeOptions = paginationProps.pageSizeOptions || DEFAULT_PAGE_SIZE_OPTIONS; const pagesToRender: PageButton[] = useMemo(() => { // if there is only one page we don't need to render anything if (totalPages <= 1) { return []; } // if we have less than the max number of pages then we can just render all of them if (totalPages <= MAX_PAGE_BUTTONS) { return new Array(totalPages).fill(0).map((_, i) => ({ type: "page", page: i, })); } // otherwise compute the buttons to render const pages = Array(MAX_PAGE_BUTTONS); const currentPage = paginationProps.page; let startPage = Math.max(0, currentPage - 3); let endPage = startPage + MAX_PAGE_BUTTONS - 1; if (endPage > totalPages - 1) { endPage = totalPages - 1; startPage = Math.max(0, endPage - MAX_PAGE_BUTTONS + 1); } for (let i = startPage; i <= endPage; i++) { pages[i - startPage] = { type: "page", page: i }; } // if the the second index is bigger than 2 then we need to show the first page and an ellipsis if (pages[1].page > 2) { pages[0] = { type: "page", page: 0 }; pages[1] = { type: "ellipsis", key: "ellipsis_pre" }; } // if the second to last index is less than the total pages - 2 then we need to show an ellipsis and the last page if (pages[pages.length - 2].page < totalPages - 2) { pages[pages.length - 1] = { type: "page", page: totalPages - 1 }; pages[pages.length - 2] = { type: "ellipsis", key: "ellipsis_post" }; } return pages; }, [totalPages, paginationProps.page]); const buttonStringTemplate = useMemo(() => { const maxPage = `${totalPages}`.length; return new Array(maxPage).fill("0").join(""); }, [totalPages]); return ( {/* filler box on left side for spacing */}
{paginationProps.isLoading ? new Array(MAX_PAGE_BUTTONS).fill("0").map((val, i) => { return ( ); }) : pagesToRender.map((page) => page.type === "page" ? ( ) : ( ), )}
{/* if we let the users set the page size then show a select to do that */} {paginationProps.setPageSize ? ( ) : ( )}
); }