|
| 1 | +import React, { useState } from 'react'; |
| 2 | +import { Card, CardHeader, CardTitle, CardContent } from '@/components/ui/card'; |
| 3 | +import { CircleHelp } from 'lucide-react'; |
| 4 | +import { |
| 5 | + createColumnHelper, |
| 6 | + useReactTable, |
| 7 | + getCoreRowModel, |
| 8 | +} from '@tanstack/react-table'; |
| 9 | +import AddressAdditionalInfo from '@/features/Address/AddressAdditionalDialog'; |
| 10 | +import { Chain } from '@/types/Chain'; |
| 11 | +import { mapToTransactionList } from '@/lib/address'; |
| 12 | +import { |
| 13 | + TransactionRow, |
| 14 | + OtterscanTransaction, |
| 15 | + OtterscanReceipt, |
| 16 | +} from '@/types/AddressList'; |
| 17 | +import AmountColumn from '@/features/Address/column/AmountColumn'; |
| 18 | +import FromColumn from '@/features/Address/column/FromColumn'; |
| 19 | +import ToColumn from '@/features/Address/column/ToColumn'; |
| 20 | +import HashColumn from '@/features/Address/column/HashColumn'; |
| 21 | +import MethodColumn from '@/features/Address/column/MethodColumn'; |
| 22 | +import MethodHeader from '@/features/Address/column/MethodHeader'; |
| 23 | +import AgeColumn from '@/features/Address/column/AgeColumn'; |
| 24 | +import AgeHeader from '@/features/Address/column/AgeHeader'; |
| 25 | +import TxFeeHeader from '@/features/Address/column/TxFeeHeader'; |
| 26 | +import TxFeeColumn from '@/features/Address/column/TxFeeColumn'; |
| 27 | +import BlockColumn from '@/features/Address/column/BlockColumn'; |
| 28 | +import AddressDataTable from '@/features/Address/AddressDataTable'; |
| 29 | +import DownloadListButton from '@/features/Address/DownloadListButton'; |
| 30 | +import TransactionsPagination from '@/features/Txs/TransactionsPagination'; |
| 31 | +import { Alert, AlertDescription } from '@/components/ui/alert'; |
| 32 | +import { MAX_PAGE_SIZE } from '@/constants/pagination'; |
| 33 | + |
| 34 | +type TransactionsPaginatedListProps = { |
| 35 | + address: string; |
| 36 | + chain: Chain; |
| 37 | + txs: OtterscanTransaction[]; |
| 38 | + receipts: OtterscanReceipt[]; |
| 39 | + isLastPage: boolean; |
| 40 | + isFirstPage: boolean; |
| 41 | + currentPageIndex: number; // 1-indexed |
| 42 | + totalPages: number | null; // null if unknown |
| 43 | + totalTxs: number; |
| 44 | +}; |
| 45 | + |
| 46 | +const TransactionsPaginatedList: React.FC<TransactionsPaginatedListProps> = ({ |
| 47 | + address, |
| 48 | + chain, |
| 49 | + txs, |
| 50 | + receipts, |
| 51 | + isLastPage, |
| 52 | + isFirstPage, |
| 53 | + currentPageIndex, |
| 54 | + totalPages, |
| 55 | + totalTxs, |
| 56 | +}) => { |
| 57 | + const [isDate, setIsDate] = useState<boolean>(false); |
| 58 | + const [isGasPrice, setIsGasPrice] = useState<boolean>(false); |
| 59 | + const [hoverId, setHoverId] = useState<string>(''); |
| 60 | + const chainId = chain?.id ?? 0; |
| 61 | + const data = React.useMemo(() => { |
| 62 | + return mapToTransactionList(txs, receipts); |
| 63 | + }, [txs, receipts]); |
| 64 | + |
| 65 | + const columnHelper = createColumnHelper<TransactionRow>(); |
| 66 | + const [openToolTipIndex, setOpenTooltipIndex] = useState<number | null>(); |
| 67 | + |
| 68 | + const columns = [ |
| 69 | + columnHelper.accessor('detail', { |
| 70 | + cell: (info: any) => ( |
| 71 | + <AddressAdditionalInfo |
| 72 | + rowIndex={info.row.index} |
| 73 | + openToolTipIndex={openToolTipIndex!} |
| 74 | + setOpenTooltipIndex={setOpenTooltipIndex} |
| 75 | + chain={chain} |
| 76 | + txHash={info.row.getValue('hash')} |
| 77 | + method={info.row.getValue('method')} |
| 78 | + /> |
| 79 | + ), |
| 80 | + header: () => <CircleHelp className="h-4 w-4" />, |
| 81 | + }), |
| 82 | + columnHelper.accessor('hash', { |
| 83 | + cell: (info: any) => <HashColumn info={info} chainId={chainId!} />, |
| 84 | + header: 'Transaction Hash', |
| 85 | + }), |
| 86 | + columnHelper.accessor('method', { |
| 87 | + cell: (info: any) => <MethodColumn info={info} />, |
| 88 | + header: () => <MethodHeader />, |
| 89 | + }), |
| 90 | + columnHelper.accessor('blockNumber', { |
| 91 | + cell: (info: any) => <BlockColumn info={info} />, |
| 92 | + header: 'Block', |
| 93 | + }), |
| 94 | + columnHelper.accessor('age', { |
| 95 | + cell: (info: any) => <AgeColumn info={info} isDate={isDate} />, |
| 96 | + header: () => <AgeHeader isDate={isDate} setIsDate={setIsDate} />, |
| 97 | + }), |
| 98 | + columnHelper.accessor('from', { |
| 99 | + cell: (info: any) => ( |
| 100 | + <FromColumn |
| 101 | + info={info} |
| 102 | + hoverId={hoverId} |
| 103 | + setHoverId={setHoverId} |
| 104 | + address={address} |
| 105 | + chainId={chainId!} |
| 106 | + /> |
| 107 | + ), |
| 108 | + header: 'From', |
| 109 | + }), |
| 110 | + columnHelper.accessor('to', { |
| 111 | + cell: (info: any) => ( |
| 112 | + <ToColumn |
| 113 | + info={info} |
| 114 | + hoverId={hoverId} |
| 115 | + setHoverId={setHoverId} |
| 116 | + address={address} |
| 117 | + chainId={chainId!} |
| 118 | + /> |
| 119 | + ), |
| 120 | + header: 'To', |
| 121 | + }), |
| 122 | + columnHelper.accessor('amount', { |
| 123 | + cell: (info: any) => ( |
| 124 | + <AmountColumn info={info} symbol={chain?.nativeCurrency.symbol!} /> |
| 125 | + ), |
| 126 | + header: 'Amount', |
| 127 | + }), |
| 128 | + columnHelper.accessor('txnFee', { |
| 129 | + cell: (info: any) => <TxFeeColumn info={info} isGasPrice={isGasPrice} />, |
| 130 | + header: () => ( |
| 131 | + <TxFeeHeader isGasPrice={isGasPrice} setIsGasPrice={setIsGasPrice} /> |
| 132 | + ), |
| 133 | + }), |
| 134 | + columnHelper.accessor('gasPrice', { |
| 135 | + enableHiding: true, |
| 136 | + cell: () => null, |
| 137 | + header: () => null, |
| 138 | + }), |
| 139 | + columnHelper.accessor('contractAddress', { |
| 140 | + enableHiding: true, |
| 141 | + cell: () => null, |
| 142 | + header: () => null, |
| 143 | + }), |
| 144 | + ]; |
| 145 | + |
| 146 | + const table = useReactTable({ |
| 147 | + columns, |
| 148 | + data, |
| 149 | + getCoreRowModel: getCoreRowModel(), |
| 150 | + }); |
| 151 | + |
| 152 | + const isMaxPage = currentPageIndex >= MAX_PAGE_SIZE; |
| 153 | + |
| 154 | + return ( |
| 155 | + <> |
| 156 | + <Card className="rounded-sm w-full"> |
| 157 | + <CardHeader> |
| 158 | + {table.getRowModel().rows.length > 0 && ( |
| 159 | + <> |
| 160 | + <CardTitle> |
| 161 | + <div className="flex sm:flex-row flex-col justify-between w-full sm:space-y-0 space-y-2"> |
| 162 | + <div className="flex flex-wrap items-center space-x-1"> |
| 163 | + <span className="text-sm"> |
| 164 | + A total of {totalTxs.toLocaleString()} transactions found |
| 165 | + </span> |
| 166 | + </div> |
| 167 | + <div className="flex items-center gap-2"> |
| 168 | + <DownloadListButton |
| 169 | + txs={txs} |
| 170 | + receipts={receipts} |
| 171 | + chain={chain} |
| 172 | + fileName={`export-${address}.csv`} |
| 173 | + /> |
| 174 | + <TransactionsPagination |
| 175 | + address={address} |
| 176 | + chainId={chainId} |
| 177 | + currentPageIndex={currentPageIndex} |
| 178 | + isLastPage={isLastPage} |
| 179 | + isFirstPage={isFirstPage} |
| 180 | + totalPages={totalPages} |
| 181 | + /> |
| 182 | + </div> |
| 183 | + </div> |
| 184 | + {!isLastPage && isMaxPage && ( |
| 185 | + <Alert variant="info" className="mt-2"> |
| 186 | + <AlertDescription> |
| 187 | + This is the maximum number of pages currently supported |
| 188 | + by this website. |
| 189 | + </AlertDescription> |
| 190 | + </Alert> |
| 191 | + )} |
| 192 | + </CardTitle> |
| 193 | + </> |
| 194 | + )} |
| 195 | + </CardHeader> |
| 196 | + <CardContent> |
| 197 | + <div className="w-full rounded-md border border-border overflow-x-auto"> |
| 198 | + <AddressDataTable table={table} /> |
| 199 | + </div> |
| 200 | + </CardContent> |
| 201 | + </Card> |
| 202 | + </> |
| 203 | + ); |
| 204 | +}; |
| 205 | + |
| 206 | +export default TransactionsPaginatedList; |
0 commit comments