import React, { useState, useEffect } from "react"; import { NavLink, useHistory } from "react-router-dom"; import { ethers } from "ethers"; import Logo from "./Logo"; import Timestamp from "./components/Timestamp"; import { provider } from "./ethersconfig"; const Home: React.FC = () => { const [search, setSearch] = useState(); const [canSubmit, setCanSubmit] = useState(false); const history = useHistory(); const handleChange: React.ChangeEventHandler = (e) => { setCanSubmit(e.target.value.trim().length > 0); setSearch(e.target.value.trim()); }; const handleSubmit: React.FormEventHandler = (e) => { e.preventDefault(); if (!canSubmit) { return; } history.push(`/search?q=${search}`); }; const [latestBlock, setLatestBlock] = useState(); useEffect(() => { const readLatestBlock = async () => { const blockNum = await provider.getBlockNumber(); const _raw = await provider.send("erigon_getHeaderByNumber", [blockNum]); const _block = provider.formatter.block(_raw); setLatestBlock(_block); }; readLatestBlock(); const listener = async (blockNumber: number) => { const _raw = await provider.send("erigon_getHeaderByNumber", [ blockNumber, ]); const _block = provider.formatter.block(_raw); setLatestBlock(_block); }; provider.on("block", listener); return () => { provider.removeListener("block", listener); }; }, []); document.title = "Home | Otterscan"; return (
{latestBlock && (
Latest block: {ethers.utils.commify(latestBlock.number)}
)}
); }; export default React.memo(Home);