otterscan/src/Home.tsx

82 lines
2.5 KiB
TypeScript
Raw Normal View History

2021-07-08 19:02:42 +00:00
import React, { useState, useContext } from "react";
2021-07-01 18:21:40 +00:00
import { NavLink, useHistory } from "react-router-dom";
import { ethers } from "ethers";
import Logo from "./Logo";
import Timestamp from "./components/Timestamp";
2021-07-08 19:02:42 +00:00
import { ProviderContext } from "./useProvider";
import { useLatestBlock } from "./useLatestBlock";
2021-07-01 18:21:40 +00:00
const Home: React.FC = () => {
2021-07-08 19:02:42 +00:00
const provider = useContext(ProviderContext);
2021-07-01 18:21:40 +00:00
const [search, setSearch] = useState<string>();
const [canSubmit, setCanSubmit] = useState<boolean>(false);
const history = useHistory();
const handleChange: React.ChangeEventHandler<HTMLInputElement> = (e) => {
setCanSubmit(e.target.value.trim().length > 0);
setSearch(e.target.value.trim());
};
const handleSubmit: React.FormEventHandler<HTMLFormElement> = (e) => {
e.preventDefault();
if (!canSubmit) {
return;
}
history.push(`/search?q=${search}`);
};
2021-07-08 19:02:42 +00:00
const latestBlock = useLatestBlock(provider);
2021-07-01 18:21:40 +00:00
document.title = "Home | Otterscan";
return (
<div className="h-screen flex m-auto">
<div className="flex flex-col m-auto">
<Logo />
<form
className="flex flex-col m-auto"
onSubmit={handleSubmit}
autoComplete="off"
spellCheck={false}
>
<input
className="w-full border rounded focus:outline-none px-2 py-1 mb-10"
type="text"
size={50}
2021-07-02 19:24:37 +00:00
placeholder="Search by address / txn hash / block number / ENS name"
2021-07-01 18:21:40 +00:00
onChange={handleChange}
2021-07-02 19:22:43 +00:00
autoFocus
2021-07-01 18:21:40 +00:00
></input>
<button
className="mx-auto px-3 py-1 mb-10 rounded bg-gray-100 hover:bg-gray-200 focus:outline-none"
type="submit"
>
Search
</button>
{latestBlock && (
<NavLink
className="mx-auto flex flex-col items-center space-y-1 mt-5 text-sm text-gray-500 hover:text-link-blue"
to={`/block/${latestBlock.number}`}
>
<div>
Latest block: {ethers.utils.commify(latestBlock.number)}
</div>
<Timestamp value={latestBlock.timestamp} />
</NavLink>
)}
2021-07-04 02:02:04 +00:00
<span className="mx-auto mt-5 text-xs text-gray-500">
2021-07-08 19:02:42 +00:00
{provider ? (
<>Using Erigon node at {provider.connection.url}</>
) : (
<>Waiting for the provider...</>
)}
2021-07-04 02:02:04 +00:00
</span>
2021-07-01 18:21:40 +00:00
</form>
</div>
</div>
);
};
export default React.memo(Home);