diff --git a/src/renderer/App.tsx b/src/renderer/App.tsx index a06567d..52de271 100644 --- a/src/renderer/App.tsx +++ b/src/renderer/App.tsx @@ -26,6 +26,7 @@ import { AddressDetails } from './components/AddressDetails'; * @todo make the sidebar collapsible * @todo make the app responsive on all screen sizes(remove 550px set height on pages/tables) * @todo Accounts.tsx: add a button to copy/see the private key + * @todo sometimes transactions do not get into blocks!!! */ export default function App() { diff --git a/src/renderer/components/Pagination.tsx b/src/renderer/components/Pagination.tsx index cdc85a1..4d9ca81 100644 --- a/src/renderer/components/Pagination.tsx +++ b/src/renderer/components/Pagination.tsx @@ -4,7 +4,7 @@ interface PaginationProps { paginate: (pageNumber: number) => void; } -const ITEMS_PER_PAGE = 10; +const ITEMS_PER_PAGE = 20; export const Pagination = ({ currentPage, diff --git a/src/renderer/components/TransactionsTable.tsx b/src/renderer/components/TransactionsTable.tsx index 67c71f1..1a92a97 100644 --- a/src/renderer/components/TransactionsTable.tsx +++ b/src/renderer/components/TransactionsTable.tsx @@ -8,7 +8,7 @@ export const TransactionsTable = ({ transactions: TransactionExtended[]; }) => { return ( -
+
diff --git a/src/renderer/constants.ts b/src/renderer/constants.ts new file mode 100644 index 0000000..a148d45 --- /dev/null +++ b/src/renderer/constants.ts @@ -0,0 +1 @@ +export const ITEMS_PER_PAGE = 20; diff --git a/src/renderer/hooks/usePagination.ts b/src/renderer/hooks/usePagination.ts new file mode 100644 index 0000000..6aac4d8 --- /dev/null +++ b/src/renderer/hooks/usePagination.ts @@ -0,0 +1,34 @@ +import { useState } from 'react'; + +interface PaginationResult { + currentItems: T[]; + currentPage: number; + paginate: (pageNumber: number) => void; +} + +/** + * A custom hook for handling pagination. + * + * @param {T[]} items - The array of items to paginate. + * @param {number} itemsPerPage - The maximum number of items per page. + * + * @returns {PaginationResult} An object with the following properties: + * - currentItems: The items for the current page. + * - currentPage: The current page number. + * - paginate: A function to change the current page. + */ +export function usePagination( + items: T[], + itemsPerPage: number +): PaginationResult { + const [currentPage, setCurrentPage] = useState(1); + + const indexOfLastItem = currentPage * itemsPerPage; + const indexOfFirstItem = indexOfLastItem - itemsPerPage; + + const currentItems = items.slice(indexOfFirstItem, indexOfLastItem); + + const paginate = (pageNumber: number) => setCurrentPage(pageNumber); + + return { currentItems, currentPage, paginate }; +} diff --git a/src/renderer/pages/Blocks.tsx b/src/renderer/pages/Blocks.tsx index b1ea366..b1b2759 100644 --- a/src/renderer/pages/Blocks.tsx +++ b/src/renderer/pages/Blocks.tsx @@ -9,8 +9,8 @@ import { Pagination, } from 'renderer/components'; import { Block, Address, Hash, isAddress } from 'viem'; - -const BLOCKS_PER_PAGE = 10; +import { ITEMS_PER_PAGE } from 'renderer/constants'; +import { usePagination } from 'renderer/hooks/usePagination'; export const Blocks = ({ blocks }: { blocks: Block[] }) => { const [intervalMining, setIntervalMining] = useState(0); @@ -19,15 +19,14 @@ export const Blocks = ({ blocks }: { blocks: Block[] }) => { const [blockTimestampInterval, setBlockTimestampInterval] = useState(0); const [coinbase, setCoinbase] = useState(''); - const [currentPage, setCurrentPage] = useState(1); let { blockHash } = useParams(); - const indexOfLastBlock = currentPage * BLOCKS_PER_PAGE; - const indexOfFirstBlock = indexOfLastBlock - BLOCKS_PER_PAGE; - const currentBlocks = blocks.slice(indexOfFirstBlock, indexOfLastBlock); - - const paginate = (pageNumber: number) => setCurrentPage(pageNumber); + const { + currentItems: currentBlocks, + currentPage, + paginate, + } = usePagination(blocks, ITEMS_PER_PAGE); const handleSetIntervalMining = async () => { try { @@ -199,7 +198,7 @@ export const Blocks = ({ blocks }: { blocks: Block[] }) => { ) : (
-
+
@@ -219,7 +218,7 @@ export const Blocks = ({ blocks }: { blocks: Block[] }) => { )} - {currentBlocks.map((block) => ( + {currentBlocks.map((block: Block) => (
diff --git a/src/renderer/pages/Transactions.tsx b/src/renderer/pages/Transactions.tsx index dbfe43c..e09dfed 100644 --- a/src/renderer/pages/Transactions.tsx +++ b/src/renderer/pages/Transactions.tsx @@ -1,29 +1,22 @@ import { Pagination, TransactionDetails } from 'renderer/components'; import { useParams } from 'react-router-dom'; -import { useState } from 'react'; import { TransactionsTable } from 'renderer/components/TransactionsTable'; import { TransactionExtended } from 'renderer/utils'; - -const TRANSACTIONS_PER_PAGE = 10; +import { ITEMS_PER_PAGE } from 'renderer/constants'; +import { usePagination } from 'renderer/hooks/usePagination'; export const Transactions = ({ transactions, }: { transactions: TransactionExtended[]; }) => { - const [currentPage, setCurrentPage] = useState(1); - let { txHash } = useParams(); - const indexOfLastTransaction = currentPage * TRANSACTIONS_PER_PAGE; - const indexOfFirstTransaction = - indexOfLastTransaction - TRANSACTIONS_PER_PAGE; - const currentTransactions = transactions.slice( - indexOfFirstTransaction, - indexOfLastTransaction - ); - - const paginate = (pageNumber: number) => setCurrentPage(pageNumber); + const { + currentItems: currentTransactions, + currentPage, + paginate, + } = usePagination(transactions, ITEMS_PER_PAGE); return (