From f9515e3465bef914e3578726ee48daa534beed88 Mon Sep 17 00:00:00 2001 From: maro Date: Fri, 3 Jun 2022 09:54:56 +0900 Subject: [PATCH] hotfix: clear selected assets - Clear selected assets when the user changes the network. - Resolved #112 --- src/components/SwapPage.tsx | 38 ++++++++++++++++++++++++++++++++++++- src/forms/SwapTokens.tsx | 7 ++++--- src/index.tsx | 12 ++++++------ src/layouts/ConnectList.tsx | 8 ++++++-- src/layouts/Connected.tsx | 12 +----------- src/rest/usePairs.ts | 1 + 6 files changed, 55 insertions(+), 23 deletions(-) diff --git a/src/components/SwapPage.tsx b/src/components/SwapPage.tsx index 7fa0b3cd..53ad584e 100644 --- a/src/components/SwapPage.tsx +++ b/src/components/SwapPage.tsx @@ -1,4 +1,12 @@ -import { FC, PropsWithChildren, ReactNode } from "react" +import { NetworkInfo, useWallet } from "@terra-money/wallet-provider" +import { + FC, + PropsWithChildren, + ReactNode, + useLayoutEffect, + useRef, +} from "react" +import { useSearchParams } from "react-router-dom" import Container from "./Container" import styles from "./SwapPage.module.scss" @@ -16,6 +24,34 @@ const Page: FC> = ({ }) => { const { sm } = props + const lastNetworkRef = useRef() + const { network } = useWallet() + const [searchParams, setSearchParams] = useSearchParams() + + useLayoutEffect(() => { + const timerId = setTimeout(() => { + if ( + network && + lastNetworkRef.current && + network?.name !== lastNetworkRef.current?.name && + window.location.pathname.includes("/swap") && + searchParams && + setSearchParams + ) { + searchParams.set("from", "") + searchParams.set("to", "") + setSearchParams(searchParams, { replace: true }) + } + lastNetworkRef.current = network + }, 10) + + return () => { + clearTimeout(timerId) + } + // #112: Do not add searchParams, setSearchParams to deps for performance reasons. + // eslint-disable-next-line react-hooks/exhaustive-deps + }, [network]) + return (
{sm ? {children} : children} diff --git a/src/forms/SwapTokens.tsx b/src/forms/SwapTokens.tsx index 01c49b96..ed18acbb 100644 --- a/src/forms/SwapTokens.tsx +++ b/src/forms/SwapTokens.tsx @@ -2,7 +2,7 @@ import { FC, useEffect, useMemo, useRef, useState } from "react" import classNames from "classnames/bind" import SwapToken from "./SwapToken" import styles from "./SwapTokens.module.scss" -import { lpTokenInfos } from "../rest/usePairs" +import usePairs, { lpTokenInfos } from "../rest/usePairs" import { Type } from "../pages/Swap" import { tokenInfos } from "../rest/usePairs" import Loading from "components/Loading" @@ -58,6 +58,7 @@ const SwapTokens = ({ formatTokenName, }: Props) => { const listRef = useRef(null) + const { isLoading: isPairsLoading } = usePairs() /* search */ const [searchKeyword, setSearchKeyword] = useState("") @@ -146,7 +147,7 @@ const SwapTokens = ({
    - {assetElements ? ( + {assetElements && !isPairsLoading ? ( <> {assetElements?.length ? ( - + )}
diff --git a/src/index.tsx b/src/index.tsx index c22f966d..fe488a1a 100644 --- a/src/index.tsx +++ b/src/index.tsx @@ -11,13 +11,13 @@ const container = document.getElementById("terraswap") const root = createRoot(container!) root.render( - - - + + + - - - + + + ) diff --git a/src/layouts/ConnectList.tsx b/src/layouts/ConnectList.tsx index 918ac87f..b9e45ca6 100644 --- a/src/layouts/ConnectList.tsx +++ b/src/layouts/ConnectList.tsx @@ -28,6 +28,11 @@ const ConnectList = () => { const connectModal = useConnectModal() const supportModal = useModal() + const handleConnectClick = (type: ConnectType, identifier?: string) => { + connect(type, identifier) + connectModal.close() + } + const buttons: Button[] = [ ...availableConnections .filter(({ type }) => type !== ConnectType.READONLY) @@ -36,8 +41,7 @@ const ConnectList = () => { image: {name}, isInstalled: true, onClick: () => { - connect(type, identifier) - connectModal.close() + handleConnectClick(type, identifier) }, })), ...availableInstallations diff --git a/src/layouts/Connected.tsx b/src/layouts/Connected.tsx index ee867eec..61e1cfa0 100644 --- a/src/layouts/Connected.tsx +++ b/src/layouts/Connected.tsx @@ -13,27 +13,17 @@ interface Props { const Connected = ({ className, icon }: Props) => { const ref = useRef(null) - // const [isOpen, setIsOpen] = useState(false); - // const close = () => setIsOpen(false); - // const toggle = () => setIsOpen((current) => !current); - const { name } = useNetwork() - /* close wallet on click outside */ const { disconnect } = useWallet() const address = useAddress() - // useOnClickOutside(ref, close); - // useEffect(() => { - // close(); - // }, [address]); - const convertName = name[0].toUpperCase() + name.slice(1, name.length) return (