|
1 | 1 | "use client"; |
2 | 2 |
|
| 3 | +import { useState } from "react"; |
3 | 4 | import { Columns } from "@/types"; |
4 | 5 | import { SetStateAction } from "react"; |
5 | 6 | import { |
@@ -27,8 +28,25 @@ interface BrowseProps { |
27 | 28 | records: Columns; |
28 | 29 | } |
29 | 30 |
|
| 31 | +const ROWS_PER_PAGE = 100; |
| 32 | + |
30 | 33 | export default function Browse(props: BrowseProps) { |
31 | 34 | const { setSelectedTable, tables, selectedTable, records } = props; |
| 35 | + const [currentPage, setCurrentPage] = useState(1); |
| 36 | + |
| 37 | + const startIndex = (currentPage - 1) * ROWS_PER_PAGE; |
| 38 | + const endIndex = startIndex + ROWS_PER_PAGE; |
| 39 | + |
| 40 | + const totalPages = Math.ceil( |
| 41 | + // @ts-ignore |
| 42 | + records[selectedTable]?.[Object.keys(records[selectedTable])[0]]?.length / |
| 43 | + ROWS_PER_PAGE |
| 44 | + ); |
| 45 | + |
| 46 | + const handleChangePage = (page: number) => { |
| 47 | + setCurrentPage(page); |
| 48 | + }; |
| 49 | + |
32 | 50 | return ( |
33 | 51 | <div className="w-full"> |
34 | 52 | <div className="px-2 flex gap-2 items-center"> |
@@ -62,35 +80,83 @@ export default function Browse(props: BrowseProps) { |
62 | 80 | </div> |
63 | 81 | <div className="mt-2 h-[420px] overflow-auto"> |
64 | 82 | {selectedTable && records[selectedTable] && ( |
65 | | - <Table> |
66 | | - <TableHeader> |
67 | | - <TableRow> |
68 | | - {Object.keys(records[selectedTable]).map((column, index) => ( |
69 | | - <TableHead key={index}>{column}</TableHead> |
70 | | - ))} |
71 | | - </TableRow> |
72 | | - </TableHeader> |
73 | | - <TableBody> |
74 | | - {records[selectedTable][ |
75 | | - Object.keys(records[selectedTable])[0] |
76 | | - ].map((_, rowIndex) => ( |
77 | | - <TableRow key={rowIndex}> |
78 | | - {Object.keys(records[selectedTable]).map( |
79 | | - (column, columnIndex) => ( |
80 | | - <TableCell |
81 | | - key={columnIndex} |
82 | | - className="max-w-[150px] truncate hover:max-w-full" |
83 | | - > |
84 | | - <span>{records[selectedTable][column][rowIndex]}</span> |
85 | | - </TableCell> |
86 | | - ) |
87 | | - )} |
| 83 | + <> |
| 84 | + <Table> |
| 85 | + <TableHeader> |
| 86 | + <TableRow> |
| 87 | + {Object.keys(records[selectedTable]).map((column, index) => ( |
| 88 | + <TableHead key={index}>{column}</TableHead> |
| 89 | + ))} |
88 | 90 | </TableRow> |
89 | | - ))} |
90 | | - </TableBody> |
91 | | - </Table> |
| 91 | + </TableHeader> |
| 92 | + <TableBody> |
| 93 | + {records[selectedTable][Object.keys(records[selectedTable])[0]] |
| 94 | + .slice(startIndex, endIndex) |
| 95 | + .map((_, rowIndex) => ( |
| 96 | + <TableRow key={rowIndex}> |
| 97 | + {Object.keys(records[selectedTable]).map( |
| 98 | + (column, columnIndex) => ( |
| 99 | + <TableCell |
| 100 | + key={columnIndex} |
| 101 | + className="max-w-[150px] truncate" |
| 102 | + > |
| 103 | + <span> |
| 104 | + { |
| 105 | + records[selectedTable][column][ |
| 106 | + rowIndex + startIndex |
| 107 | + ] |
| 108 | + } |
| 109 | + </span> |
| 110 | + </TableCell> |
| 111 | + ) |
| 112 | + )} |
| 113 | + </TableRow> |
| 114 | + ))} |
| 115 | + </TableBody> |
| 116 | + </Table> |
| 117 | + </> |
92 | 118 | )} |
93 | 119 | </div> |
| 120 | + <Pagination |
| 121 | + currentPage={currentPage} |
| 122 | + totalPages={totalPages} |
| 123 | + onPageChange={handleChangePage} |
| 124 | + /> |
94 | 125 | </div> |
95 | 126 | ); |
96 | 127 | } |
| 128 | + |
| 129 | +interface PaginationProps { |
| 130 | + currentPage: number; |
| 131 | + totalPages: number; |
| 132 | + onPageChange: (page: number) => void; |
| 133 | +} |
| 134 | + |
| 135 | +const Pagination: React.FC<PaginationProps> = ({ |
| 136 | + currentPage, |
| 137 | + totalPages, |
| 138 | + onPageChange, |
| 139 | +}) => { |
| 140 | + // @ts-ignore |
| 141 | + const pageNumbers = [...Array(totalPages).keys()].map((num) => num + 1); |
| 142 | + |
| 143 | + return ( |
| 144 | + <ul className="flex overflow-x-scroll"> |
| 145 | + {pageNumbers.map((page) => ( |
| 146 | + <li |
| 147 | + key={page} |
| 148 | + className={`grow ${ |
| 149 | + currentPage === page ? "bg-primary text-background" : "bg-secondary" |
| 150 | + }`} |
| 151 | + > |
| 152 | + <button |
| 153 | + className={`page-link p-2 text-center w-full`} |
| 154 | + onClick={() => onPageChange(page)} |
| 155 | + > |
| 156 | + {page} |
| 157 | + </button> |
| 158 | + </li> |
| 159 | + ))} |
| 160 | + </ul> |
| 161 | + ); |
| 162 | +}; |
0 commit comments