Skip to content
This repository has been archived by the owner on Jun 24, 2022. It is now read-only.

Commit

Permalink
chore: paint currency selector on initialization (#2245)
Browse files Browse the repository at this point in the history
Avoids a flash of "Select a token" as the App initializes.
  • Loading branch information
zzmp authored Sep 2, 2021
1 parent 4bdf3c1 commit f59c5f6
Show file tree
Hide file tree
Showing 9 changed files with 33 additions and 24 deletions.
4 changes: 3 additions & 1 deletion src/components/CurrencyInputPanel/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,8 @@ const Container = styled.div<{ hideInput: boolean }>`
}
`

const CurrencySelect = styled(ButtonGray)<{ selected: boolean; hideInput?: boolean }>`
const CurrencySelect = styled(ButtonGray)<{ visible: boolean; selected: boolean; hideInput?: boolean }>`
visibility: ${({ visible }) => (visible ? 'visible' : 'hidden')};
align-items: center;
font-size: 24px;
font-weight: 500;
Expand Down Expand Up @@ -211,6 +212,7 @@ export default function CurrencyInputPanel({
<Container hideInput={hideInput}>
<InputRow style={hideInput ? { padding: '0', borderRadius: '8px' } : {}} selected={!onCurrencySelect}>
<CurrencySelect
visible={currency !== null}
selected={!!currency}
hideInput={hideInput}
className="open-currency-select-button"
Expand Down
2 changes: 1 addition & 1 deletion src/components/CurrencyLogo/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ export default function CurrencyLogo({
style,
...rest
}: {
currency?: Currency
currency?: Currency | null
size?: string
style?: React.CSSProperties
}) {
Expand Down
2 changes: 1 addition & 1 deletion src/components/swap/UnsupportedCurrencyFooter.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ export default function UnsupportedCurrencyFooter({
currencies,
}: {
show: boolean
currencies: (Currency | undefined)[]
currencies: (Currency | undefined | null)[]
}) {
const { chainId } = useActiveWeb3React()
const [showDetails, setShowDetails] = useState(false)
Expand Down
8 changes: 5 additions & 3 deletions src/hooks/Tokens.ts
Original file line number Diff line number Diff line change
Expand Up @@ -123,9 +123,9 @@ function parseStringOrBytes32(str: string | undefined, bytes32: string | undefin
}

// undefined if invalid or does not exist
// null if loading
// null if loading or null was passed
// otherwise returns the token
export function useToken(tokenAddress?: string): Token | undefined | null {
export function useToken(tokenAddress?: string | null): Token | undefined | null {
const { chainId } = useActiveWeb3React()
const tokens = useAllTokens()

Expand All @@ -148,6 +148,7 @@ export function useToken(tokenAddress?: string): Token | undefined | null {

return useMemo(() => {
if (token) return token
if (tokenAddress === null) return null
if (!chainId || !address) return undefined
if (decimals.loading || symbol.loading || tokenName.loading) return null
if (decimals.result) {
Expand All @@ -169,13 +170,14 @@ export function useToken(tokenAddress?: string): Token | undefined | null {
symbol.result,
symbolBytes32.result,
token,
tokenAddress,
tokenName.loading,
tokenName.result,
tokenNameBytes32.result,
])
}

export function useCurrency(currencyId: string | undefined): Currency | null | undefined {
export function useCurrency(currencyId: string | null | undefined): Currency | null | undefined {
const { chainId } = useActiveWeb3React()
const isETH = currencyId?.toUpperCase() === 'ETH'
const token = useToken(isETH ? undefined : currencyId)
Expand Down
2 changes: 1 addition & 1 deletion src/hooks/useIsSwapUnsupported.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { useUnsupportedTokens } from './Tokens'
* @param currencyIn the input currency to check
* @param currencyOut the output currency to check
*/
export function useIsSwapUnsupported(currencyIn?: Currency, currencyOut?: Currency): boolean {
export function useIsSwapUnsupported(currencyIn?: Currency | null, currencyOut?: Currency | null): boolean {
const unsupportedTokens: { [address: string]: Token } = useUnsupportedTokens()

return useMemo(() => {
Expand Down
8 changes: 4 additions & 4 deletions src/hooks/useWrapCallback.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,15 @@ const NOT_APPLICABLE = { wrapType: WrapType.NOT_APPLICABLE }
* @param typedValue the user input value
*/
export default function useWrapCallback(
inputCurrency: Currency | undefined,
outputCurrency: Currency | undefined,
inputCurrency: Currency | undefined | null,
outputCurrency: Currency | undefined | null,
typedValue: string | undefined
): { wrapType: WrapType; execute?: undefined | (() => Promise<void>); inputError?: string } {
const { chainId, account } = useActiveWeb3React()
const wethContract = useWETHContract()
const balance = useCurrencyBalance(account ?? undefined, inputCurrency)
const balance = useCurrencyBalance(account ?? undefined, inputCurrency ?? undefined)
// we can always parse the amount typed as the input currency, since wrapping is 1:1
const inputAmount = useMemo(() => tryParseAmount(typedValue, inputCurrency), [inputCurrency, typedValue])
const inputAmount = useMemo(() => tryParseAmount(typedValue, inputCurrency ?? undefined), [inputCurrency, typedValue])
const addTransaction = useTransactionAdder()

return useMemo(() => {
Expand Down
7 changes: 5 additions & 2 deletions src/pages/Swap/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -345,7 +345,7 @@ export default function Swap({ history }: RouteComponentProps) {
[onCurrencySelection]
)

const swapIsUnsupported = useIsSwapUnsupported(currencies?.INPUT, currencies?.OUTPUT)
const swapIsUnsupported = useIsSwapUnsupported(currencies[Field.INPUT], currencies[Field.OUTPUT])

const priceImpactTooHigh = priceImpactSeverity > 3 && !isExpertMode

Expand Down Expand Up @@ -660,7 +660,10 @@ export default function Swap({ history }: RouteComponentProps) {
</AppBody>
<SwitchLocaleLink />
{!swapIsUnsupported ? null : (
<UnsupportedCurrencyFooter show={swapIsUnsupported} currencies={[currencies.INPUT, currencies.OUTPUT]} />
<UnsupportedCurrencyFooter
show={swapIsUnsupported}
currencies={[currencies[Field.INPUT], currencies[Field.OUTPUT]]}
/>
)}
</>
)
Expand Down
16 changes: 9 additions & 7 deletions src/state/swap/hooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ function involvesAddress(

// from the current swap inputs, compute the best trade and return it.
export function useDerivedSwapInfo(toggledVersion: Version): {
currencies: { [field in Field]?: Currency }
currencies: { [field in Field]?: Currency | null }
currencyBalances: { [field in Field]?: CurrencyAmount<Currency> }
parsedAmount: CurrencyAmount<Currency> | undefined
inputError?: string
Expand Down Expand Up @@ -167,9 +167,9 @@ export function useDerivedSwapInfo(toggledVersion: Version): {
[Field.OUTPUT]: relevantTokenBalances[1],
}

const currencies: { [field in Field]?: Currency } = {
[Field.INPUT]: inputCurrency ?? undefined,
[Field.OUTPUT]: outputCurrency ?? undefined,
const currencies: { [field in Field]?: Currency | null } = {
[Field.INPUT]: inputCurrency,
[Field.OUTPUT]: outputCurrency,
}

let inputError: string | undefined
Expand Down Expand Up @@ -287,18 +287,20 @@ export function useDefaultsFromURLSearch():
useEffect(() => {
if (!chainId) return
const parsed = queryParametersToSwapState(parsedQs)
const inputCurrencyId = parsed[Field.INPUT].currencyId ?? undefined
const outputCurrencyId = parsed[Field.OUTPUT].currencyId ?? undefined

dispatch(
replaceSwapState({
typedValue: parsed.typedValue,
field: parsed.independentField,
inputCurrencyId: parsed[Field.INPUT].currencyId,
outputCurrencyId: parsed[Field.OUTPUT].currencyId,
inputCurrencyId,
outputCurrencyId,
recipient: parsed.recipient,
})
)

setResult({ inputCurrencyId: parsed[Field.INPUT].currencyId, outputCurrencyId: parsed[Field.OUTPUT].currencyId })
setResult({ inputCurrencyId, outputCurrencyId })
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [dispatch, chainId])

Expand Down
8 changes: 4 additions & 4 deletions src/state/swap/reducer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@ export interface SwapState {
readonly independentField: Field
readonly typedValue: string
readonly [Field.INPUT]: {
readonly currencyId: string | undefined
readonly currencyId: string | undefined | null
}
readonly [Field.OUTPUT]: {
readonly currencyId: string | undefined
readonly currencyId: string | undefined | null
}
// the typed recipient address or ENS name, or null if swap should go to sender
readonly recipient: string | null
Expand All @@ -18,10 +18,10 @@ const initialState: SwapState = {
independentField: Field.INPUT,
typedValue: '',
[Field.INPUT]: {
currencyId: '',
currencyId: null,
},
[Field.OUTPUT]: {
currencyId: '',
currencyId: null,
},
recipient: null,
}
Expand Down

0 comments on commit f59c5f6

Please sign in to comment.