otterscan/src/Search.tsx

36 lines
807 B
TypeScript
Raw Normal View History

2021-07-01 18:21:40 +00:00
import { useLocation, useHistory } from "react-router-dom";
import { ethers } from "ethers";
import queryString from "query-string";
type SearchParams = {
q: string;
};
const Search: React.FC = () => {
const location = useLocation<SearchParams>();
const history = useHistory();
const qs = queryString.parse(location.search);
const q = (qs.q ?? "").toString();
if (ethers.utils.isAddress(q)) {
history.replace(`/address/${q}`);
return <></>;
}
if (ethers.utils.isHexString(q, 32)) {
history.replace(`/tx/${q}`);
return <></>;
}
const blockNumber = parseInt(q);
if (!isNaN(blockNumber)) {
history.replace(`/block/${blockNumber}`);
return <></>;
}
// Assume it is an ENS name
history.replace(`/address/${q}`);
2021-07-01 18:21:40 +00:00
return <></>;
};
export default Search;