Skip to content

Commit

Permalink
hotfix: clear selected assets
Browse files Browse the repository at this point in the history
- Clear selected assets when the user changes the network.
- Resolved #112
  • Loading branch information
honeymaro authored and JoowonYun committed Jun 3, 2022
1 parent 8cda285 commit 32b409c
Show file tree
Hide file tree
Showing 6 changed files with 55 additions and 23 deletions.
38 changes: 37 additions & 1 deletion src/components/SwapPage.tsx
Original file line number Diff line number Diff line change
@@ -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"

Expand All @@ -16,6 +24,34 @@ const Page: FC<PropsWithChildren<Props>> = ({
}) => {
const { sm } = props

const lastNetworkRef = useRef<NetworkInfo>()
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 (
<article className={styles.article}>
{sm ? <Container sm>{children}</Container> : children}
Expand Down
7 changes: 4 additions & 3 deletions src/forms/SwapTokens.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -58,6 +58,7 @@ const SwapTokens = ({
formatTokenName,
}: Props) => {
const listRef = useRef<HTMLUListElement>(null)
const { isLoading: isPairsLoading } = usePairs()

/* search */
const [searchKeyword, setSearchKeyword] = useState("")
Expand Down Expand Up @@ -146,7 +147,7 @@ const SwapTokens = ({
</section>

<ul ref={listRef} className={classNames(styles.list)}>
{assetElements ? (
{assetElements && !isPairsLoading ? (
<>
{assetElements?.length ? (
<VariableSizeList
Expand Down Expand Up @@ -181,7 +182,7 @@ const SwapTokens = ({
left: 0,
}}
>
<Loading />
<Loading color="#0222ba" />
</div>
)}
</ul>
Expand Down
12 changes: 6 additions & 6 deletions src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,13 @@ const container = document.getElementById("terraswap")
const root = createRoot(container!)
root.render(
<StrictMode>
<WalletConnectProvider>
<Network>
<Router>
<Router>
<WalletConnectProvider>
<Network>
<ScrollToTop />
<App />
</Router>
</Network>
</WalletConnectProvider>
</Network>
</WalletConnectProvider>
</Router>
</StrictMode>
)
8 changes: 6 additions & 2 deletions src/layouts/ConnectList.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -36,8 +41,7 @@ const ConnectList = () => {
image: <img src={icon} {...size} alt={name} />,
isInstalled: true,
onClick: () => {
connect(type, identifier)
connectModal.close()
handleConnectClick(type, identifier)
},
})),
...availableInstallations
Expand Down
12 changes: 1 addition & 11 deletions src/layouts/Connected.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
<div className={styles.wrapper} ref={ref}>
<button
className={classNames(styles.connected, className)}
onClick={disconnect}
onClick={() => disconnect()}
>
{icon}
<span className={styles.address}>
Expand Down
1 change: 1 addition & 0 deletions src/rest/usePairs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,7 @@ const usePairs = () => {
}
setIsLoading(true)
setCurrentNetworkName(networkName)
setResult({ pairs: [] })

const fetchTokensInfo = async () => {
try {
Expand Down

0 comments on commit 32b409c

Please sign in to comment.