|
| 1 | +import { |
| 2 | + Box, |
| 3 | + Text, |
| 4 | + VStack, |
| 5 | + Badge, |
| 6 | + Table, |
| 7 | + Pagination, |
| 8 | + ButtonGroup, |
| 9 | + IconButton, |
| 10 | +} from '@chakra-ui/react'; |
| 11 | +import { ChevronLeft, ChevronRight, CloudOff } from 'lucide-react'; |
| 12 | +import { formatDistanceToNow } from 'date-fns'; |
| 13 | +import type { RawCheck } from '@/api/types'; |
| 14 | +import { useMemo, useState } from 'react'; |
| 15 | +interface RecentChecksTableProps { |
| 16 | + checks: RawCheck[]; |
| 17 | + pageSize?: number; |
| 18 | +} |
| 19 | + |
| 20 | +export function RecentChecksTable({ checks, pageSize = 10 }: RecentChecksTableProps) { |
| 21 | + const [currentPage, setCurrentPage] = useState(1); |
| 22 | + const pageCount = Math.ceil(checks.length / pageSize); |
| 23 | + const pagedChecks = useMemo(() => { |
| 24 | + const start = (currentPage - 1) * pageSize; |
| 25 | + return checks.slice(start, start + pageSize); |
| 26 | + }, [checks, currentPage, pageSize]); |
| 27 | + |
| 28 | + if (checks.length === 0) { |
| 29 | + return ( |
| 30 | + <VStack color='gray.300' textAlign='center' gap={1} py={5}> |
| 31 | + <CloudOff size={'40px'} /> |
| 32 | + <Text textAlign='center' color='gray.500'> |
| 33 | + No recent checks available |
| 34 | + </Text> |
| 35 | + </VStack> |
| 36 | + ); |
| 37 | + } |
| 38 | + |
| 39 | + return ( |
| 40 | + <VStack gap={2} align='stretch'> |
| 41 | + <Table.ScrollArea borderWidth='1px' rounded='md' flex={1} minH={0} overflow='auto'> |
| 42 | + <Table.Root size='sm' stickyHeader> |
| 43 | + <Table.Header> |
| 44 | + <Table.Row bg='gray.100' _dark={{ bg: 'gray.800' }}> |
| 45 | + <Table.ColumnHeader w='30%'>Time</Table.ColumnHeader> |
| 46 | + <Table.ColumnHeader w='20%'>Status</Table.ColumnHeader> |
| 47 | + <Table.ColumnHeader w='25%'>RTT</Table.ColumnHeader> |
| 48 | + <Table.ColumnHeader w='25%'>Error</Table.ColumnHeader> |
| 49 | + </Table.Row> |
| 50 | + </Table.Header> |
| 51 | + <Table.Body> |
| 52 | + {pagedChecks.map((check, index) => ( |
| 53 | + <Table.Row key={`${check.ts}-${index}`}> |
| 54 | + <Table.Cell w='30%'> |
| 55 | + <Text flex='1' fontSize='sm'> |
| 56 | + {formatDistanceToNow(new Date(check.ts), { addSuffix: true })} |
| 57 | + </Text> |
| 58 | + </Table.Cell> |
| 59 | + <Table.Cell w='20%'> |
| 60 | + <Badge colorPalette={check.status === 'up' ? 'green' : 'red'} size='sm'> |
| 61 | + {check.status.toUpperCase()} |
| 62 | + </Badge> |
| 63 | + </Table.Cell> |
| 64 | + <Table.Cell w='25%'> |
| 65 | + <Text fontSize='sm'>{check.rttMs ? `${check.rttMs}ms` : '-'}</Text> |
| 66 | + </Table.Cell> |
| 67 | + <Table.Cell w='25%'> |
| 68 | + <Text flex='1' fontSize='sm' color='gray.500' lineClamp={1}> |
| 69 | + {check.error || '-'} |
| 70 | + </Text> |
| 71 | + </Table.Cell> |
| 72 | + </Table.Row> |
| 73 | + ))} |
| 74 | + </Table.Body> |
| 75 | + </Table.Root> |
| 76 | + </Table.ScrollArea> |
| 77 | + |
| 78 | + {/* Pagination */} |
| 79 | + {pageCount > 1 && ( |
| 80 | + <Box flexShrink={0}> |
| 81 | + <Pagination.Root |
| 82 | + count={checks.length} |
| 83 | + pageSize={pageSize} |
| 84 | + page={currentPage} |
| 85 | + onPageChange={details => setCurrentPage(details.page)} |
| 86 | + > |
| 87 | + <ButtonGroup variant='ghost' size='sm' w='full' justifyContent='center'> |
| 88 | + <Text fontSize='sm' color='gray.600' _dark={{ color: 'gray.400' }} flex='1'> |
| 89 | + {`Showing ${(currentPage - 1) * pageSize + 1} - ${Math.min( |
| 90 | + currentPage * pageSize, |
| 91 | + checks.length |
| 92 | + )} of ${checks.length} entries`} |
| 93 | + </Text> |
| 94 | + <Pagination.PrevTrigger asChild> |
| 95 | + <IconButton aria-label='Previous page'> |
| 96 | + <ChevronLeft /> |
| 97 | + </IconButton> |
| 98 | + </Pagination.PrevTrigger> |
| 99 | + <Pagination.Items |
| 100 | + render={page => ( |
| 101 | + <IconButton |
| 102 | + key={page.value} |
| 103 | + variant={page.value === currentPage ? 'outline' : 'ghost'} |
| 104 | + size='sm' |
| 105 | + > |
| 106 | + {page.value} |
| 107 | + </IconButton> |
| 108 | + )} |
| 109 | + /> |
| 110 | + <Pagination.NextTrigger asChild> |
| 111 | + <IconButton aria-label='Next page'> |
| 112 | + <ChevronRight /> |
| 113 | + </IconButton> |
| 114 | + </Pagination.NextTrigger> |
| 115 | + </ButtonGroup> |
| 116 | + </Pagination.Root> |
| 117 | + </Box> |
| 118 | + )} |
| 119 | + </VStack> |
| 120 | + ); |
| 121 | +} |
0 commit comments