Remove hacky prefetcher

This commit is contained in:
Willian Mitsuda 2022-08-22 17:56:10 -03:00
parent 746a908e7a
commit 812539674e
No known key found for this signature in database
3 changed files with 37 additions and 52 deletions

View File

@ -1,5 +1,7 @@
import { PropsWithChildren } from "react"; import React, { PropsWithChildren, useContext, useState } from "react";
import { NavLink } from "react-router-dom"; import { NavLink } from "react-router-dom";
import { RuntimeContext } from "../useRuntime";
import { useTransactionBySenderAndNonce } from "../useErigonHooks";
import { ChecksummedAddress } from "../types"; import { ChecksummedAddress } from "../types";
import { addressByNonceURL } from "../url"; import { addressByNonceURL } from "../url";
@ -16,6 +18,8 @@ const NavButton: React.FC<PropsWithChildren<NavButtonProps>> = ({
disabled, disabled,
children, children,
}) => { }) => {
const [prefetch, setPrefetch] = useState<boolean>(false);
if (disabled) { if (disabled) {
return ( return (
<span className="bg-link-blue/10 text-gray-300 rounded px-2 py-1 text-xs"> <span className="bg-link-blue/10 text-gray-300 rounded px-2 py-1 text-xs">
@ -25,13 +29,36 @@ const NavButton: React.FC<PropsWithChildren<NavButtonProps>> = ({
} }
return ( return (
<NavLink <>
className="bg-link-blue/10 text-link-blue hover:bg-link-blue/100 hover:text-white rounded px-2 py-1 text-xs" <NavLink
to={addressByNonceURL(sender, nonce)} className="bg-link-blue/10 text-link-blue hover:bg-link-blue/100 hover:text-white rounded px-2 py-1 text-xs"
> to={addressByNonceURL(sender, nonce)}
{children} onMouseOver={() => setPrefetch(true)}
</NavLink> >
{children}
</NavLink>
{prefetch && <Prefetcher checksummedAddress={sender} nonce={nonce} />}
</>
); );
}; };
type PrefetcherProps = {
checksummedAddress: ChecksummedAddress;
nonce: number;
};
const Prefetcher: React.FC<PrefetcherProps> = ({
checksummedAddress,
nonce,
}) => {
const { provider } = useContext(RuntimeContext);
const _txHash = useTransactionBySenderAndNonce(
provider,
checksummedAddress,
nonce
);
return <></>;
};
export default NavButton; export default NavButton;

View File

@ -1,15 +1,11 @@
import React, { useContext, useEffect } from "react"; import React, { useContext } from "react";
import { FontAwesomeIcon } from "@fortawesome/react-fontawesome"; import { FontAwesomeIcon } from "@fortawesome/react-fontawesome";
import { faChevronLeft } from "@fortawesome/free-solid-svg-icons/faChevronLeft"; import { faChevronLeft } from "@fortawesome/free-solid-svg-icons/faChevronLeft";
import { faChevronRight } from "@fortawesome/free-solid-svg-icons/faChevronRight"; import { faChevronRight } from "@fortawesome/free-solid-svg-icons/faChevronRight";
import NavButton from "./NavButton"; import NavButton from "./NavButton";
import { ChecksummedAddress } from "../types"; import { ChecksummedAddress } from "../types";
import { RuntimeContext } from "../useRuntime"; import { RuntimeContext } from "../useRuntime";
import { import { useTransactionCount } from "../useErigonHooks";
prefetchTransactionBySenderAndNonce,
useTransactionCount,
} from "../useErigonHooks";
import { useSWRConfig } from "swr";
type NavNonceProps = { type NavNonceProps = {
sender: ChecksummedAddress; sender: ChecksummedAddress;
@ -20,25 +16,6 @@ const NavNonce: React.FC<NavNonceProps> = ({ sender, nonce }) => {
const { provider } = useContext(RuntimeContext); const { provider } = useContext(RuntimeContext);
const count = useTransactionCount(provider, sender); const count = useTransactionCount(provider, sender);
// Prefetch
const swrConfig = useSWRConfig();
useEffect(() => {
if (!provider || !sender || nonce === undefined || count === undefined) {
return;
}
prefetchTransactionBySenderAndNonce(swrConfig, provider, sender, nonce - 1);
prefetchTransactionBySenderAndNonce(swrConfig, provider, sender, nonce + 1);
if (count > 0) {
prefetchTransactionBySenderAndNonce(
swrConfig,
provider,
sender,
count - 1
);
}
}, [swrConfig, provider, sender, nonce, count]);
return ( return (
<div className="pl-2 self-center flex space-x-1"> <div className="pl-2 self-center flex space-x-1">
<NavButton sender={sender} nonce={nonce - 1} disabled={nonce === 0}> <NavButton sender={sender} nonce={nonce - 1} disabled={nonce === 0}>

View File

@ -10,7 +10,7 @@ import { Contract } from "@ethersproject/contracts";
import { defaultAbiCoder } from "@ethersproject/abi"; import { defaultAbiCoder } from "@ethersproject/abi";
import { BigNumber } from "@ethersproject/bignumber"; import { BigNumber } from "@ethersproject/bignumber";
import { arrayify, hexDataSlice, isHexString } from "@ethersproject/bytes"; import { arrayify, hexDataSlice, isHexString } from "@ethersproject/bytes";
import useSWR, { useSWRConfig } from "swr"; import useSWR from "swr";
import useSWRImmutable from "swr/immutable"; import useSWRImmutable from "swr/immutable";
import { getInternalOperations } from "./nodeFunctions"; import { getInternalOperations } from "./nodeFunctions";
import { import {
@ -527,25 +527,6 @@ const getTransactionBySenderAndNonceFetcher =
return result; return result;
}; };
export const prefetchTransactionBySenderAndNonce = (
{ mutate }: ReturnType<typeof useSWRConfig>,
provider: JsonRpcProvider,
sender: ChecksummedAddress,
nonce: number
) => {
const key: TransactionBySenderAndNonceKey = {
network: provider.network.chainId,
sender,
nonce,
};
mutate(key, (curr: any) => {
if (curr) {
return curr;
}
return getTransactionBySenderAndNonceFetcher(provider)(key);
});
};
export const useTransactionBySenderAndNonce = ( export const useTransactionBySenderAndNonce = (
provider: JsonRpcProvider | undefined, provider: JsonRpcProvider | undefined,
sender: ChecksummedAddress | undefined, sender: ChecksummedAddress | undefined,