Retrofit search urls into search

This commit is contained in:
Willian Mitsuda 2022-02-01 15:31:07 -03:00
parent 3695db552b
commit 6a6e2cdb14
1 changed files with 19 additions and 67 deletions

View File

@ -2,7 +2,6 @@ import {
ChangeEventHandler, ChangeEventHandler,
FormEventHandler, FormEventHandler,
RefObject, RefObject,
useContext,
useRef, useRef,
useState, useState,
} from "react"; } from "react";
@ -13,8 +12,6 @@ import { isHexString } from "@ethersproject/bytes";
import useKeyboardShortcut from "use-keyboard-shortcut"; import useKeyboardShortcut from "use-keyboard-shortcut";
import { PAGE_SIZE } from "../params"; import { PAGE_SIZE } from "../params";
import { ProcessedTransaction, TransactionChunk } from "../types"; import { ProcessedTransaction, TransactionChunk } from "../types";
import { RuntimeContext } from "../useRuntime";
import { getTransactionBySenderAndNonceFetcher } from "../useErigonHooks";
export class SearchController { export class SearchController {
private txs: ProcessedTransaction[]; private txs: ProcessedTransaction[];
@ -209,38 +206,36 @@ export class SearchController {
} }
} }
const doSearch = async ( const doSearch = async (q: string, navigate: NavigateFunction) => {
provider: JsonRpcProvider,
q: string,
navigate: NavigateFunction
) => {
// Cleanup // Cleanup
q = q.trim(); q = q.trim();
let maybeAddress = q; let maybeAddress = q;
let maybeIndex: string | undefined; let maybeIndex = "";
const sepIndex = q.lastIndexOf(":"); const sepIndex = q.lastIndexOf(":");
if (sepIndex !== -1) { if (sepIndex !== -1) {
maybeAddress = q.substring(0, sepIndex); maybeAddress = q.substring(0, sepIndex);
maybeIndex = q.substring(sepIndex + 1); maybeIndex = q.substring(sepIndex + 1);
} }
// Plain address?
if (isAddress(maybeAddress)) { if (isAddress(maybeAddress)) {
// Plain address + nonce? navigate(
if (await navigateToTx(provider, maybeAddress, maybeIndex, navigate)) { `/address/${maybeAddress}${
return; maybeIndex !== "" ? `?nonce=${maybeIndex}` : ""
} }`,
{ replace: true }
// Plain address );
navigate(`/address/${maybeAddress}`, { replace: true });
return; return;
} }
// Tx hash?
if (isHexString(q, 32)) { if (isHexString(q, 32)) {
navigate(`/tx/${q}`, { replace: true }); navigate(`/tx/${q}`, { replace: true });
return; return;
} }
// Block number?
const blockNumber = parseInt(q); const blockNumber = parseInt(q);
if (!isNaN(blockNumber)) { if (!isNaN(blockNumber)) {
navigate(`/block/${blockNumber}`, { replace: true }); navigate(`/block/${blockNumber}`, { replace: true });
@ -248,54 +243,12 @@ const doSearch = async (
} }
// Assume it is an ENS name // Assume it is an ENS name
const resolvedName = await provider.resolveName(maybeAddress); navigate(
if (resolvedName !== null) { `/address/${maybeAddress}${
// ENS name + nonce? maybeIndex !== "" ? `?nonce=${maybeIndex}` : ""
if (await navigateToTx(provider, resolvedName, maybeIndex, navigate)) { }`,
return; { replace: true }
} );
// ENS name
navigate(`/address/${maybeAddress}`, { replace: true });
return;
}
// TODO: handle default
};
const navigateToTx = async (
provider: JsonRpcProvider,
maybeAddress: string,
maybeIndex: string | undefined,
navigate: NavigateFunction
): Promise<boolean> => {
if (maybeIndex !== undefined) {
try {
let nonce = 0;
if (maybeIndex === "latest") {
const count = await provider.getTransactionCount(maybeAddress);
if (count > 0) {
nonce = count - 1;
}
} else {
nonce = parseInt(maybeIndex);
}
const txHash = await getTransactionBySenderAndNonceFetcher({
provider,
sender: maybeAddress,
nonce,
});
if (txHash) {
navigate(`/tx/${txHash}`, { replace: true });
}
return true;
} catch (err) {
// ignore
}
}
return false;
}; };
export const useGenericSearch = (): [ export const useGenericSearch = (): [
@ -303,7 +256,6 @@ export const useGenericSearch = (): [
ChangeEventHandler<HTMLInputElement>, ChangeEventHandler<HTMLInputElement>,
FormEventHandler<HTMLFormElement> FormEventHandler<HTMLFormElement>
] => { ] => {
const { provider } = useContext(RuntimeContext);
const [searchString, setSearchString] = useState<string>(""); const [searchString, setSearchString] = useState<string>("");
const [canSubmit, setCanSubmit] = useState<boolean>(false); const [canSubmit, setCanSubmit] = useState<boolean>(false);
const navigate = useNavigate(); const navigate = useNavigate();
@ -316,14 +268,14 @@ export const useGenericSearch = (): [
const handleSubmit: React.FormEventHandler<HTMLFormElement> = (e) => { const handleSubmit: React.FormEventHandler<HTMLFormElement> = (e) => {
e.preventDefault(); e.preventDefault();
if (!canSubmit || !provider) { if (!canSubmit) {
return; return;
} }
if (searchRef.current) { if (searchRef.current) {
searchRef.current.value = ""; searchRef.current.value = "";
} }
doSearch(provider, searchString, navigate); doSearch(searchString, navigate);
}; };
const searchRef = useRef<HTMLInputElement>(null); const searchRef = useRef<HTMLInputElement>(null);