Take advantage of SWR to not do prop drilling

This commit is contained in:
Willian Mitsuda 2022-08-22 08:04:05 -03:00
parent 57334f1f8f
commit 6c7fbd8f35
No known key found for this signature in database
3 changed files with 9 additions and 16 deletions

View File

@ -1,5 +1,4 @@
import React from "react"; import React from "react";
import { BigNumber } from "@ethersproject/bignumber";
import InternalTransfer from "./InternalTransfer"; import InternalTransfer from "./InternalTransfer";
import InternalSelfDestruct from "./InternalSelfDestruct"; import InternalSelfDestruct from "./InternalSelfDestruct";
import InternalCreate from "./InternalCreate"; import InternalCreate from "./InternalCreate";
@ -8,20 +7,14 @@ import { TransactionData, InternalOperation, OperationType } from "../types";
type InternalTransactionOperationProps = { type InternalTransactionOperationProps = {
txData: TransactionData; txData: TransactionData;
internalOp: InternalOperation; internalOp: InternalOperation;
// TODO: migrate all this logic to SWR
ethUSDPrice: BigNumber | undefined;
}; };
const InternalTransactionOperation: React.FC< const InternalTransactionOperation: React.FC<
InternalTransactionOperationProps InternalTransactionOperationProps
> = ({ txData, internalOp, ethUSDPrice }) => ( > = ({ txData, internalOp }) => (
<> <>
{internalOp.type === OperationType.TRANSFER && ( {internalOp.type === OperationType.TRANSFER && (
<InternalTransfer <InternalTransfer txData={txData} internalOp={internalOp} />
txData={txData}
internalOp={internalOp}
ethUSDPrice={ethUSDPrice}
/>
)} )}
{internalOp.type === OperationType.SELF_DESTRUCT && ( {internalOp.type === OperationType.SELF_DESTRUCT && (
<InternalSelfDestruct txData={txData} internalOp={internalOp} /> <InternalSelfDestruct txData={txData} internalOp={internalOp} />

View File

@ -1,5 +1,4 @@
import React, { useContext } from "react"; import React, { useContext } from "react";
import { BigNumber } from "@ethersproject/bignumber";
import { formatEther } from "@ethersproject/units"; import { formatEther } from "@ethersproject/units";
import { FontAwesomeIcon } from "@fortawesome/react-fontawesome"; import { FontAwesomeIcon } from "@fortawesome/react-fontawesome";
import { faAngleRight } from "@fortawesome/free-solid-svg-icons/faAngleRight"; import { faAngleRight } from "@fortawesome/free-solid-svg-icons/faAngleRight";
@ -11,19 +10,17 @@ import USDAmount from "./USDAmount";
import { RuntimeContext } from "../useRuntime"; import { RuntimeContext } from "../useRuntime";
import { useHasCode } from "../useErigonHooks"; import { useHasCode } from "../useErigonHooks";
import { useChainInfo } from "../useChainInfo"; import { useChainInfo } from "../useChainInfo";
import { useETHUSDOracle } from "../usePriceOracle";
import { TransactionData, InternalOperation } from "../types"; import { TransactionData, InternalOperation } from "../types";
type InternalTransferProps = { type InternalTransferProps = {
txData: TransactionData; txData: TransactionData;
internalOp: InternalOperation; internalOp: InternalOperation;
// TODO: migrate all this logic to SWR
ethUSDPrice: BigNumber | undefined;
}; };
const InternalTransfer: React.FC<InternalTransferProps> = ({ const InternalTransfer: React.FC<InternalTransferProps> = ({
txData, txData,
internalOp, internalOp,
ethUSDPrice,
}) => { }) => {
const { const {
nativeCurrency: { symbol, decimals }, nativeCurrency: { symbol, decimals },
@ -36,6 +33,10 @@ const InternalTransfer: React.FC<InternalTransferProps> = ({
internalOp.to === txData.confirmedData.miner; internalOp.to === txData.confirmedData.miner;
const { provider } = useContext(RuntimeContext); const { provider } = useContext(RuntimeContext);
const blockETHUSDPrice = useETHUSDOracle(
provider,
txData.confirmedData?.blockNumber
);
const fromHasCode = useHasCode( const fromHasCode = useHasCode(
provider, provider,
internalOp.from, internalOp.from,
@ -99,12 +100,12 @@ const InternalTransfer: React.FC<InternalTransferProps> = ({
<span> <span>
{formatEther(internalOp.value)} {symbol} {formatEther(internalOp.value)} {symbol}
</span> </span>
{ethUSDPrice && ( {blockETHUSDPrice && (
<span className="px-2 border-gray-200 border rounded-lg bg-gray-100 text-gray-600"> <span className="px-2 border-gray-200 border rounded-lg bg-gray-100 text-gray-600">
<USDAmount <USDAmount
amount={internalOp.value} amount={internalOp.value}
amountDecimals={decimals} amountDecimals={decimals}
quote={ethUSDPrice} quote={blockETHUSDPrice}
// TODO: migrate to SWR and standardize this magic number // TODO: migrate to SWR and standardize this magic number
quoteDecimals={8} quoteDecimals={8}
/> />

View File

@ -286,7 +286,6 @@ const Details: React.FC<DetailsProps> = ({ txData }) => {
key={i} key={i}
txData={txData} txData={txData}
internalOp={op} internalOp={op}
ethUSDPrice={blockETHUSDPrice}
/> />
))} ))}
</div> </div>