2021-07-01 18:21:40 +00:00
|
|
|
import { useLocation, useHistory } from "react-router-dom";
|
2021-08-08 22:49:45 +00:00
|
|
|
import { isAddress } from "@ethersproject/address";
|
|
|
|
import { isHexString } from "@ethersproject/bytes";
|
2021-07-01 18:21:40 +00:00
|
|
|
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();
|
2021-08-08 22:49:45 +00:00
|
|
|
if (isAddress(q)) {
|
2021-07-01 18:21:40 +00:00
|
|
|
history.replace(`/address/${q}`);
|
|
|
|
return <></>;
|
|
|
|
}
|
2021-08-08 22:49:45 +00:00
|
|
|
if (isHexString(q, 32)) {
|
2021-07-01 18:21:40 +00:00
|
|
|
history.replace(`/tx/${q}`);
|
|
|
|
return <></>;
|
|
|
|
}
|
|
|
|
|
|
|
|
const blockNumber = parseInt(q);
|
|
|
|
if (!isNaN(blockNumber)) {
|
|
|
|
history.replace(`/block/${blockNumber}`);
|
|
|
|
return <></>;
|
|
|
|
}
|
|
|
|
|
2021-07-02 19:05:18 +00:00
|
|
|
// Assume it is an ENS name
|
|
|
|
history.replace(`/address/${q}`);
|
2021-07-01 18:21:40 +00:00
|
|
|
return <></>;
|
|
|
|
};
|
|
|
|
|
|
|
|
export default Search;
|