Commit 64e8c3ce authored by Zach Pomerantz's avatar Zach Pomerantz Committed by GitHub

fix: token img jank (#3562)

parent 46e6c229
...@@ -3,7 +3,7 @@ import { useToken } from 'lib/hooks/useCurrency' ...@@ -3,7 +3,7 @@ import { useToken } from 'lib/hooks/useCurrency'
import useCurrencyLogoURIs from 'lib/hooks/useCurrencyLogoURIs' import useCurrencyLogoURIs from 'lib/hooks/useCurrencyLogoURIs'
import { MissingToken } from 'lib/icons' import { MissingToken } from 'lib/icons'
import styled from 'lib/theme' import styled from 'lib/theme'
import { useCallback, useEffect, useState } from 'react' import { useCallback, useMemo, useState } from 'react'
const badSrcs = new Set<string>() const badSrcs = new Set<string>()
...@@ -19,19 +19,23 @@ function TokenImg({ token, ...rest }: TokenImgProps) { ...@@ -19,19 +19,23 @@ function TokenImg({ token, ...rest }: TokenImgProps) {
// TODO(zzmp): TokenImg takes a frame to switch. // TODO(zzmp): TokenImg takes a frame to switch.
const srcs = useCurrencyLogoURIs(tokenInfo) const srcs = useCurrencyLogoURIs(tokenInfo)
const [src, setSrc] = useState<string | undefined>()
useEffect(() => { const [attempt, setAttempt] = useState(0)
setSrc(srcs.find((src) => !badSrcs.has(src))) const onError = useCallback((e) => {
}, [srcs]) if (e.target.src) badSrcs.add(e.target.src)
const onError = useCallback(() => { setAttempt((attempt) => ++attempt)
if (src) badSrcs.add(src) }, [])
setSrc(srcs.find((src) => !badSrcs.has(src)))
}, [src, srcs]) return useMemo(() => {
// Trigger a re-render when an error occurs.
if (src) { void attempt
return <img src={src} alt={tokenInfo.name || tokenInfo.symbol} onError={onError} {...rest} />
} const src = srcs.find((src) => !badSrcs.has(src))
return <MissingToken color="secondary" {...rest} /> if (!src) return <MissingToken color="secondary" {...rest} />
const alt = tokenInfo.name || tokenInfo.symbol
return <img src={src} alt={alt} key={alt} onError={onError} {...rest} />
}, [attempt, onError, rest, srcs, tokenInfo.name, tokenInfo.symbol])
} }
export default styled(TokenImg)<{ size?: number }>` export default styled(TokenImg)<{ size?: number }>`
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment