Skip to content

Commit b9004a8

Browse files
committed
feat: reset swap states on swap view dismount
1 parent f3a8124 commit b9004a8

File tree

4 files changed

+19
-5
lines changed

4 files changed

+19
-5
lines changed

packages/wallet-widget/src/components/SequenceWalletProvider/ProviderComponents/SwapProvider.tsx

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ export const SwapProvider = ({ children }: { children: ReactNode }) => {
3737
const { data: walletClient } = useWalletClient({ chainId: connectedChainId })
3838
const indexerClient = useIndexerClient(connectedChainId)
3939

40-
useEffect(() => {
40+
const resetSwapStates = () => {
4141
setFromCoin(undefined)
4242
setFromAmount(0)
4343
setToCoin(undefined)
@@ -49,6 +49,10 @@ export const SwapProvider = ({ children }: { children: ReactNode }) => {
4949
setIsErrorSwapQuote(false)
5050
setIsTxnPending(false)
5151
setIsErrorTxn(false)
52+
}
53+
54+
useEffect(() => {
55+
resetSwapStates()
5256
}, [userAddress, connectedChainId])
5357

5458
useEffect(() => {
@@ -241,7 +245,8 @@ export const SwapProvider = ({ children }: { children: ReactNode }) => {
241245
setRecentInput,
242246
setNonRecentAmount,
243247
switchCoinOrder,
244-
onSubmitSwap
248+
onSubmitSwap,
249+
resetSwapStates
245250
}}
246251
>
247252
{children}

packages/wallet-widget/src/contexts/Swap.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ export interface SwapContext {
2222
setNonRecentAmount: (amount: number) => void
2323
switchCoinOrder: () => void
2424
onSubmitSwap: () => void
25+
resetSwapStates: () => void
2526
}
2627

2728
const [useSwapContext, SwapContextProvider] = createGenericContext<SwapContext>()

packages/wallet-widget/src/hooks/useSwap.tsx

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,8 @@ export const useSwap = () => {
2020
setRecentInput,
2121
setNonRecentAmount,
2222
switchCoinOrder,
23-
onSubmitSwap
23+
onSubmitSwap,
24+
resetSwapStates
2425
} = useSwapContext()
2526

2627
return {
@@ -42,6 +43,7 @@ export const useSwap = () => {
4243
setRecentInput,
4344
setNonRecentAmount,
4445
switchCoinOrder,
45-
onSubmitSwap
46+
onSubmitSwap,
47+
resetSwapStates
4648
}
4749
}

packages/wallet-widget/src/views/Swap/Swap.tsx

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { ArrowRightIcon, Card, cardVariants, cn, IconButton, Spinner, Text } from '@0xsequence/design-system'
22
import { useGetCoinPrices, useGetExchangeRate, useGetTokenBalancesSummary } from '@0xsequence/hooks'
3+
import { useEffect } from 'react'
34
import { useAccount, useChainId } from 'wagmi'
45

56
import { NetworkSelect } from '../../components/Select/NetworkSelect'
@@ -20,12 +21,17 @@ export const Swap = () => {
2021
isErrorSwapQuote,
2122
isTxnPending,
2223
switchCoinOrder,
23-
onSubmitSwap
24+
onSubmitSwap,
25+
resetSwapStates
2426
} = useSwap()
2527
const { fiatCurrency } = useSettings()
2628
const { address: userAddress } = useAccount()
2729
const connectedChainId = useChainId()
2830

31+
useEffect(() => {
32+
return resetSwapStates()
33+
}, [])
34+
2935
const { data: tokenBalances } = useGetTokenBalancesSummary({
3036
chainIds: [connectedChainId],
3137
filter: {

0 commit comments

Comments
 (0)