From 7d940fe237677ab6bfbadaf271a699bb71a967d2 Mon Sep 17 00:00:00 2001 From: Griko Nibras Date: Wed, 24 Jan 2024 06:25:35 +0700 Subject: [PATCH] chore: housekeeping RouteDisplay --- src/components/RouteDisplay.tsx | 120 ++++++++++++++++++-------------- 1 file changed, 66 insertions(+), 54 deletions(-) diff --git a/src/components/RouteDisplay.tsx b/src/components/RouteDisplay.tsx index 480bdd4d..29a501b9 100644 --- a/src/components/RouteDisplay.tsx +++ b/src/components/RouteDisplay.tsx @@ -2,13 +2,14 @@ import { CheckCircleIcon, XCircleIcon } from "@heroicons/react/20/solid"; import { RouteResponse } from "@skip-router/core"; import { ethers } from "ethers"; -import { Dispatch, FC, Fragment, SetStateAction, useMemo } from "react"; +import { Dispatch, Fragment, SetStateAction, useMemo } from "react"; import { useAssets } from "@/context/assets"; import { useChainByID } from "@/hooks/useChains"; import { useBroadcastedTxsStatus } from "@/solve"; import { AdaptiveLink } from "./AdaptiveLink"; +import { SimpleTooltip } from "./SimpleTooltip"; import { BroadcastedTx } from "./TransactionDialog/TransactionDialogContent"; export interface SwapVenueConfig { @@ -54,12 +55,14 @@ interface SwapAction { type Action = TransferAction | SwapAction; -const RouteEnd: FC<{ +interface RouteEndProps { amount: string; symbol: string; chain: string; logo: string; -}> = ({ amount, symbol, logo, chain }) => { +} + +function RouteEnd({ amount, symbol, logo, chain }: RouteEndProps) { return (
@@ -71,19 +74,28 @@ const RouteEnd: FC<{

- {amount} {symbol} + + + {parseFloat(amount).toLocaleString("en-US", { + maximumFractionDigits: 8, + })} + + {" "} + {symbol}

On {chain}

); -}; +} -const TransferStep: FC<{ +interface TransferStepProps { action: TransferAction; id: string; statusData?: ReturnType["data"]; -}> = ({ action, id, statusData }) => { +} + +function TransferStep({ action, id, statusData }: TransferStepProps) { const { data: sourceChain } = useChainByID(action.sourceChain); const { data: destinationChain } = useChainByID(action.destinationChain); @@ -92,7 +104,7 @@ const TransferStep: FC<{ const transfer = statusData?.transferSequence[operationCount]; // We can assume that the transfer is successful when the state is TRANSFER_SUCCESS or TRANSFER_RECEIVED - const transferState = useMemo(() => { + const renderTransferState = useMemo(() => { switch (transfer?.state) { case "TRANSFER_SUCCESS": return ( @@ -124,7 +136,7 @@ const TransferStep: FC<{ } }, [transfer?.state]); - const renderExplorerLink = () => { + const renderExplorerLink = useMemo(() => { if (!transfer?.explorerLink) return null; return ( ); - }; + }, [transfer?.explorerLink]); const { getAsset } = useAssets(); @@ -150,7 +162,7 @@ const TransferStep: FC<{ if (!asset) { return (
-
{transferState}
+
{renderTransferState}

Transfer to{" "} @@ -161,7 +173,7 @@ const TransferStep: FC<{ />{" "} {destinationChain.prettyName}

- {renderExplorerLink()} + {renderExplorerLink}
); @@ -169,7 +181,7 @@ const TransferStep: FC<{ return (
-
{transferState}
+
{renderTransferState}

Transfer{" "} @@ -194,18 +206,20 @@ const TransferStep: FC<{ />{" "} {destinationChain.prettyName}

- {renderExplorerLink()} + {renderExplorerLink}
); -}; +} -const SwapStep: FC<{ +interface SwapStepProps { action: SwapAction; actions: Action[]; id: string; statusData?: ReturnType["data"]; -}> = ({ action, actions, id, statusData }) => { +} + +function SwapStep({ action, actions, id, statusData }: SwapStepProps) { const { getAsset } = useAssets(); const assetIn = getAsset(action.sourceAsset, action.chain); @@ -225,7 +239,7 @@ const SwapStep: FC<{ const swap = statusData?.transferSequence[operationCount]; // as for swap operations, we can assume that the swap is successful if the previous transfer state is TRANSFER_SUCCESS - const swapState = useMemo(() => { + const renderSwapState = useMemo(() => { switch (swap?.state) { case "TRANSFER_RECEIVED": return ( @@ -251,7 +265,7 @@ const SwapStep: FC<{ } }, [swap?.state]); - const renderExplorerLink = () => { + const renderExplorerLink = useMemo(() => { if (!swap?.explorerLink) return null; if (swap?.state !== "TRANSFER_SUCCESS") return null; return ( @@ -264,12 +278,12 @@ const SwapStep: FC<{ ); - }; + }, [swap?.explorerLink, swap?.state]); if (!assetIn && assetOut) { return (
-
{swapState}
+
{renderSwapState}

Swap to{" "} @@ -288,7 +302,7 @@ const SwapStep: FC<{ />{" "} {venue.name}

- {renderExplorerLink()} + {renderExplorerLink}
); @@ -297,7 +311,7 @@ const SwapStep: FC<{ if (assetIn && !assetOut) { return (
-
{swapState}
+
{renderSwapState}

Swap{" "} @@ -314,7 +328,7 @@ const SwapStep: FC<{ />{" "} {venue.name}

- {renderExplorerLink()} + {renderExplorerLink}
); @@ -326,7 +340,7 @@ const SwapStep: FC<{ return (
-
{swapState}
+
{renderSwapState}

Swap{" "} @@ -349,20 +363,20 @@ const SwapStep: FC<{ />{" "} {venue.name}

- {renderExplorerLink()} + {renderExplorerLink}
); -}; +} -interface Props { +interface RouteDisplayProps { route: RouteResponse; isRouteExpanded: boolean; setIsRouteExpanded: Dispatch>; broadcastedTxs?: BroadcastedTx[]; } -const RouteDisplay: FC = ({ route, isRouteExpanded, setIsRouteExpanded, broadcastedTxs }) => { +function RouteDisplay({ route, isRouteExpanded, setIsRouteExpanded, broadcastedTxs }: RouteDisplayProps) { const { getAsset } = useAssets(); const sourceAsset = getAsset(route.sourceAssetDenom, route.sourceAssetChainID); @@ -507,7 +521,7 @@ const RouteDisplay: FC = ({ route, isRouteExpanded, setIsRouteExpanded, b /> {isRouteExpanded && (