|
| 1 | +import { |
| 2 | + ArrowLeftIcon, |
| 3 | + ArrowRightIcon, |
| 4 | + DoubleArrowLeftIcon, |
| 5 | + DoubleArrowRightIcon, |
| 6 | +} from '@radix-ui/react-icons'; |
| 7 | +import { |
| 8 | + ColumnDef, |
| 9 | + ColumnFiltersState, |
| 10 | + flexRender, |
| 11 | + getCoreRowModel, |
| 12 | + getFilteredRowModel, |
| 13 | + getPaginationRowModel, |
| 14 | + getSortedRowModel, |
| 15 | + SortingState, |
| 16 | + useReactTable, |
| 17 | +} from '@tanstack/react-table'; |
| 18 | +import { useState } from 'react'; |
| 19 | + |
| 20 | +import { Button } from '@/components/ui/button'; |
| 21 | +import { ComboboxMulti } from '@/components/ui/combobox'; |
| 22 | +import { Pagination, PaginationContent, PaginationItem } from '@/components/ui/pagination'; |
| 23 | +import { ScrollArea } from '@/components/ui/scroll-area'; |
| 24 | +import { |
| 25 | + Table, |
| 26 | + TableBody, |
| 27 | + TableCell, |
| 28 | + TableFooter, |
| 29 | + TableHead, |
| 30 | + TableHeader, |
| 31 | + TableRow, |
| 32 | +} from '@/components/ui/table'; |
| 33 | +import { IQuestionAttempt } from '@/types/question-types'; |
| 34 | + |
| 35 | +interface QuestionTableProps<TData, TValue> { |
| 36 | + columns: Array<ColumnDef<TData, TValue>>; |
| 37 | + data: Array<TData>; |
| 38 | + isError: boolean; |
| 39 | +} |
| 40 | + |
| 41 | +export function QuestionAttemptsTable<TValue>({ |
| 42 | + columns, |
| 43 | + data, |
| 44 | + isError, |
| 45 | +}: QuestionTableProps<IQuestionAttempt, TValue>) { |
| 46 | + const [pagination, setPagination] = useState({ |
| 47 | + pageIndex: 0, |
| 48 | + pageSize: 10, |
| 49 | + }); |
| 50 | + |
| 51 | + const [columnFilters, setColumnFilters] = useState<ColumnFiltersState>([]); |
| 52 | + const [sorting, setSorting] = useState<SortingState>([]); |
| 53 | + |
| 54 | + const table = useReactTable({ |
| 55 | + data, |
| 56 | + columns, |
| 57 | + state: { pagination, columnFilters, sorting }, |
| 58 | + filterFns: {}, |
| 59 | + getPaginationRowModel: getPaginationRowModel(), |
| 60 | + getFilteredRowModel: getFilteredRowModel(), |
| 61 | + getCoreRowModel: getCoreRowModel(), |
| 62 | + onColumnFiltersChange: setColumnFilters, |
| 63 | + onPaginationChange: setPagination, |
| 64 | + onSortingChange: setSorting, |
| 65 | + getSortedRowModel: getSortedRowModel(), |
| 66 | + }); |
| 67 | + |
| 68 | + const setLanguages = (languages: Array<string>) => { |
| 69 | + setColumnFilters((columnFilters) => [ |
| 70 | + ...columnFilters.filter((v) => v.id !== 'language'), |
| 71 | + ...languages.map((v) => ({ id: 'language', value: v })), |
| 72 | + ]); |
| 73 | + }; |
| 74 | + |
| 75 | + return ( |
| 76 | + <div className='relative flex max-h-full w-full grow flex-col'> |
| 77 | + <div className='flex items-center py-4'> |
| 78 | + <div className='flex flex-col gap-1'> |
| 79 | + <label className='text-sm font-medium'>Filter by Language</label> |
| 80 | + <ComboboxMulti |
| 81 | + setValuesCallback={setLanguages} |
| 82 | + options={Array.from(new Set(data.map((v) => v.language))).map((v) => ({ |
| 83 | + value: v, |
| 84 | + label: v, |
| 85 | + }))} |
| 86 | + placeholderText='Select a language filter' |
| 87 | + noOptionsText='None of the available languages match your search' |
| 88 | + /> |
| 89 | + </div> |
| 90 | + {/* <Input |
| 91 | + placeholder='Search questions...' |
| 92 | + value={(table.getColumn('title')?.getFilterValue() as string) ?? ''} |
| 93 | + onChange={(event) => table.getColumn('title')?.setFilterValue(event.target.value)} |
| 94 | + className='max-w-sm' |
| 95 | + /> */} |
| 96 | + </div> |
| 97 | + <div className='border-border sticky top-0 rounded-t-md border'> |
| 98 | + <Table> |
| 99 | + <TableHeader> |
| 100 | + {table.getHeaderGroups().map((headerGroup) => ( |
| 101 | + <TableRow |
| 102 | + className='border-border/60 bg-primary-foreground text-primary' |
| 103 | + key={headerGroup.id} |
| 104 | + > |
| 105 | + {headerGroup.headers.map((header) => { |
| 106 | + return ( |
| 107 | + <TableHead key={header.id}> |
| 108 | + {header.isPlaceholder |
| 109 | + ? null |
| 110 | + : flexRender(header.column.columnDef.header, header.getContext())} |
| 111 | + </TableHead> |
| 112 | + ); |
| 113 | + })} |
| 114 | + </TableRow> |
| 115 | + ))} |
| 116 | + </TableHeader> |
| 117 | + </Table> |
| 118 | + </div> |
| 119 | + <ScrollArea className='size-full overflow-x-auto border-x'> |
| 120 | + <Table> |
| 121 | + <TableBody> |
| 122 | + {!isError && table.getRowModel().rows?.length ? ( |
| 123 | + table.getRowModel().rows.map((row) => ( |
| 124 | + <TableRow key={row.id} className='border-border/60 even:bg-secondary/10'> |
| 125 | + {row.getVisibleCells().map((cell) => ( |
| 126 | + <TableCell key={cell.id}> |
| 127 | + {flexRender(cell.column.columnDef.cell, cell.getContext())} |
| 128 | + </TableCell> |
| 129 | + ))} |
| 130 | + </TableRow> |
| 131 | + )) |
| 132 | + ) : ( |
| 133 | + <TableRow> |
| 134 | + <TableCell colSpan={columns.length} className='h-24 text-center'> |
| 135 | + No results. |
| 136 | + </TableCell> |
| 137 | + </TableRow> |
| 138 | + )} |
| 139 | + </TableBody> |
| 140 | + </Table> |
| 141 | + </ScrollArea> |
| 142 | + <div className='sticky bottom-0 rounded-b-md border'> |
| 143 | + <Table> |
| 144 | + <TableFooter> |
| 145 | + <TableRow> |
| 146 | + <TableCell colSpan={columns.length}> |
| 147 | + <Pagination className='flex items-center justify-end space-x-2 p-2'> |
| 148 | + <PaginationContent> |
| 149 | + <PaginationItem> |
| 150 | + <Button |
| 151 | + variant='outline' |
| 152 | + size='sm' |
| 153 | + className='px-2' |
| 154 | + onClick={() => table.firstPage()} |
| 155 | + disabled={!table.getCanPreviousPage()} |
| 156 | + > |
| 157 | + <DoubleArrowLeftIcon /> |
| 158 | + </Button> |
| 159 | + </PaginationItem> |
| 160 | + <PaginationItem className='mr-1'> |
| 161 | + <Button |
| 162 | + variant='outline' |
| 163 | + size='sm' |
| 164 | + className='px-2' |
| 165 | + onClick={() => table.previousPage()} |
| 166 | + disabled={!table.getCanPreviousPage()} |
| 167 | + > |
| 168 | + <ArrowLeftIcon /> |
| 169 | + </Button> |
| 170 | + </PaginationItem> |
| 171 | + <PaginationItem className='text-sm'> |
| 172 | + {table.getState().pagination.pageIndex + 1} of {table.getPageCount()} |
| 173 | + </PaginationItem> |
| 174 | + <PaginationItem className='ml-1'> |
| 175 | + <Button |
| 176 | + variant='outline' |
| 177 | + size='sm' |
| 178 | + className='px-2' |
| 179 | + onClick={() => table.nextPage()} |
| 180 | + disabled={!table.getCanNextPage()} |
| 181 | + > |
| 182 | + <ArrowRightIcon /> |
| 183 | + </Button> |
| 184 | + </PaginationItem> |
| 185 | + <PaginationItem> |
| 186 | + <Button |
| 187 | + variant='outline' |
| 188 | + size='sm' |
| 189 | + className='px-2' |
| 190 | + onClick={() => table.lastPage()} |
| 191 | + disabled={!table.getCanNextPage()} |
| 192 | + > |
| 193 | + <DoubleArrowRightIcon /> |
| 194 | + </Button> |
| 195 | + </PaginationItem> |
| 196 | + </PaginationContent> |
| 197 | + </Pagination> |
| 198 | + </TableCell> |
| 199 | + </TableRow> |
| 200 | + </TableFooter> |
| 201 | + </Table> |
| 202 | + </div> |
| 203 | + </div> |
| 204 | + ); |
| 205 | +} |
0 commit comments