otterscan/src/BlockTransactions.tsx

63 lines
1.7 KiB
TypeScript
Raw Normal View History

2021-08-02 02:53:36 +00:00
import React, { useMemo, useContext } from "react";
2021-07-01 18:21:40 +00:00
import { useParams, useLocation } from "react-router";
import { ethers } from "ethers";
import queryString from "query-string";
import StandardFrame from "./StandardFrame";
2021-07-23 22:48:34 +00:00
import BlockTransactionHeader from "./block/BlockTransactionHeader";
import BlockTransactionResults from "./block/BlockTransactionResults";
2021-07-01 18:21:40 +00:00
import { PAGE_SIZE } from "./params";
2021-07-09 05:07:20 +00:00
import { RuntimeContext } from "./useRuntime";
2021-08-02 02:53:36 +00:00
import { useBlockTransactions } from "./useErigonHooks";
2021-07-01 18:21:40 +00:00
type BlockParams = {
blockNumber: string;
};
type PageParams = {
p?: number;
};
const BlockTransactions: React.FC = () => {
2021-07-09 05:07:20 +00:00
const { provider } = useContext(RuntimeContext);
2021-07-01 18:21:40 +00:00
const params = useParams<BlockParams>();
const location = useLocation<PageParams>();
const qs = queryString.parse(location.search);
let pageNumber = 1;
if (qs.p) {
try {
pageNumber = parseInt(qs.p as string);
} catch (err) {}
}
const blockNumber = useMemo(
() => ethers.BigNumber.from(params.blockNumber),
[params.blockNumber]
);
2021-08-02 02:53:36 +00:00
const txs = useBlockTransactions(provider, blockNumber.toNumber());
2021-07-01 18:21:40 +00:00
const page = useMemo(() => {
if (!txs) {
return undefined;
}
const pageStart = (pageNumber - 1) * PAGE_SIZE;
return txs.slice(pageStart, pageStart + PAGE_SIZE);
}, [txs, pageNumber]);
const total = useMemo(() => txs?.length ?? 0, [txs]);
document.title = `Block #${blockNumber} Txns | Otterscan`;
return (
<StandardFrame>
2021-07-14 19:57:08 +00:00
<BlockTransactionHeader blockTag={blockNumber.toNumber()} />
<BlockTransactionResults
page={page}
total={total}
pageNumber={pageNumber}
/>
2021-07-01 18:21:40 +00:00
</StandardFrame>
);
};
export default React.memo(BlockTransactions);