-
Notifications
You must be signed in to change notification settings - Fork 89
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
fix(widget): widget tokens flickering #4883
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
7838376
fix(widget): never notify widget consumer on first load
alfetopito e90df2c
refactor: move tradeStateFromUrl to atom, to update it in a single place
alfetopito 3518aa9
chore: remove debug logs
alfetopito bf80037
chore: fix linting
alfetopito File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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) |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I see this is a key change to break the loop.
But what the point of creating
tradeStateFromUrlAtom
? Probably you did it to cache thetradeStateFromUrl
value, correct?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Correct! I thought at first that could be related, since the hook is included in multiple places causing it to be constantly re-evaluated. Decided to add since it was already done 🤷