Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/icon-load-fallback.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@relayprotocol/relay-kit-ui': patch
---

Gracefully handle broken token/chain logo images. ChainTokenIcon now falls back to the token symbol avatar and ChainIcon hides itself when the logo fails to load, instead of rendering a broken-image placeholder.
6 changes: 4 additions & 2 deletions packages/ui/src/components/primitives/ChainIcon.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { useContext, type FC } from 'react'
import { useContext, useState, type FC } from 'react'
import useRelayClient from '../../hooks/useRelayClient.js'
import { ProviderOptionsContext } from '../../providers/RelayKitProvider.js'
import { cn } from '../../utils/cn.js'
Expand All @@ -20,6 +20,7 @@ const ChainIcon: FC<Props> = ({
square = true,
borderRadius = 4
}) => {
const [erroredSrc, setErroredSrc] = useState<string | null>(null)
const providerOptions = useContext(ProviderOptionsContext)
const client = useRelayClient()
const chain = chainId
Expand All @@ -35,7 +36,7 @@ const ChainIcon: FC<Props> = ({
const iconUrl =
square && icon ? icon.replace('/icons/', '/icons/square/') : icon

return iconUrl ? (
return iconUrl && erroredSrc !== iconUrl ? (
<div
className={cn('relay:flex relay:shrink-0', className)}
style={{
Expand All @@ -48,6 +49,7 @@ const ChainIcon: FC<Props> = ({
<img
src={iconUrl}
alt={`Chain #${chainId}`}
onError={() => setErroredSrc(iconUrl ?? null)}
style={{
borderRadius: square ? borderRadius : 0,
width: '100%',
Expand Down
9 changes: 7 additions & 2 deletions packages/ui/src/components/primitives/ChainTokenIcon.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { type FC } from 'react'
import { type FC, useState } from 'react'
import ChainIcon from './ChainIcon.js'
import Text from './Text.js'
import { cn } from '../../utils/cn.js'
Expand Down Expand Up @@ -43,7 +43,11 @@ export const ChainTokenIcon: FC<ChainTokenProps> = ({
chainRadius = 4,
chainIconSize
}) => {
const isValidTokenLogo = tokenlogoURI && tokenlogoURI !== 'missing.png'
const [erroredSrc, setErroredSrc] = useState<string | null>(null)
const isValidTokenLogo =
tokenlogoURI &&
tokenlogoURI !== 'missing.png' &&
erroredSrc !== tokenlogoURI
const dimensions = SIZES[size]
const chainSize = chainIconSize ?? dimensions.chain

Expand All @@ -65,6 +69,7 @@ export const ChainTokenIcon: FC<ChainTokenProps> = ({
src={tokenlogoURI}
width={dimensions.token}
height={dimensions.token}
onError={() => setErroredSrc(tokenlogoURI ?? null)}
style={{
borderRadius: 9999,
overflow: 'hidden'
Expand Down
Loading