-
Notifications
You must be signed in to change notification settings - Fork 89
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(widget): widget tokens flickering (#4883)
* fix(widget): never notify widget consumer on first load * refactor: move tradeStateFromUrl to atom, to update it in a single place * chore: remove debug logs * chore: fix linting
- Loading branch information
1 parent
e28364f
commit 0f8f256
Showing
7 changed files
with
72 additions
and
33 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
41 changes: 41 additions & 0 deletions
41
apps/cowswap-frontend/src/modules/trade/hooks/setupTradeState/useSetupTradeStateFromUrl.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
import { useSetAtom } from 'jotai' | ||
import { useEffect } from 'react' | ||
|
||
import { useLocation, useParams } from 'react-router-dom' | ||
|
||
import { tradeStateFromUrlAtom } from 'modules/trade/state/tradeStateFromUrlAtom' | ||
|
||
import { TradeRawState } from '../../types/TradeRawState' | ||
|
||
/** | ||
* Updater to fetch trade state from URL params and query, and store it on jotai state | ||
* /1/swap/WETH/DAI?recipient=0x -> { chainId: 1, inputCurrencyId: 'WETH', outputCurrencyId: 'DAI', recipient: '0x' } | ||
* | ||
* Load this hook only once to avoid unnecessary re-renders | ||
*/ | ||
export function useSetupTradeStateFromUrl(): null { | ||
const params = useParams() | ||
const location = useLocation() | ||
const stringifiedParams = JSON.stringify(params) | ||
const setState = useSetAtom(tradeStateFromUrlAtom) | ||
|
||
useEffect(() => { | ||
const searchParams = new URLSearchParams(location.search) | ||
const recipient = searchParams.get('recipient') | ||
const recipientAddress = searchParams.get('recipientAddress') | ||
const { chainId, inputCurrencyId, outputCurrencyId } = JSON.parse(stringifiedParams) | ||
const chainIdAsNumber = chainId && /^\d+$/.test(chainId) ? parseInt(chainId) : null | ||
|
||
const state: TradeRawState = { | ||
chainId: chainIdAsNumber, | ||
inputCurrencyId: inputCurrencyId || searchParams.get('inputCurrency') || null, | ||
outputCurrencyId: outputCurrencyId || searchParams.get('outputCurrency') || null, | ||
...(recipient ? { recipient } : undefined), | ||
...(recipientAddress ? { recipientAddress } : undefined), | ||
} | ||
|
||
setState(state) | ||
}, [location.search, stringifiedParams, setState]) | ||
|
||
return null | ||
} |
28 changes: 4 additions & 24 deletions
28
apps/cowswap-frontend/src/modules/trade/hooks/setupTradeState/useTradeStateFromUrl.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,33 +1,13 @@ | ||
import { useMemo } from 'react' | ||
import { useAtomValue } from 'jotai' | ||
|
||
import { useLocation, useParams } from 'react-router-dom' | ||
import { tradeStateFromUrlAtom } from 'modules/trade/state/tradeStateFromUrlAtom' | ||
|
||
import { TradeRawState } from '../../types/TradeRawState' | ||
|
||
/** | ||
* Get trade state from URL params and query | ||
* /1/swap/WETH/DAI?recipient=0x -> { chainId: 1, inputCurrencyId: 'WETH', outputCurrencyId: 'DAI', recipient: '0x' } | ||
*/ | ||
export function useTradeStateFromUrl(): TradeRawState { | ||
const params = useParams() | ||
const location = useLocation() | ||
|
||
return useMemo(() => { | ||
const searchParams = new URLSearchParams(location.search) | ||
const recipient = searchParams.get('recipient') | ||
const recipientAddress = searchParams.get('recipientAddress') | ||
const { chainId, inputCurrencyId, outputCurrencyId } = params | ||
const chainIdAsNumber = chainId && /^\d+$/.test(chainId) ? parseInt(chainId) : null | ||
|
||
const state: TradeRawState = { | ||
chainId: chainIdAsNumber, | ||
inputCurrencyId: inputCurrencyId || searchParams.get('inputCurrency') || null, | ||
outputCurrencyId: outputCurrencyId || searchParams.get('outputCurrency') || null, | ||
...(recipient ? { recipient } : undefined), | ||
...(recipientAddress ? { recipientAddress } : undefined), | ||
} | ||
|
||
return state | ||
// eslint-disable-next-line react-hooks/exhaustive-deps | ||
}, [location.search, JSON.stringify(params)]) | ||
export function useTradeStateFromUrl(): TradeRawState | null { | ||
return useAtomValue(tradeStateFromUrlAtom) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
5 changes: 5 additions & 0 deletions
5
apps/cowswap-frontend/src/modules/trade/state/tradeStateFromUrlAtom.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
import { atom } from 'jotai' | ||
|
||
import { TradeRawState } from '../types/TradeRawState' | ||
|
||
export const tradeStateFromUrlAtom = atom<TradeRawState | null>(null) |