FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
interface/src/hooks/useAssetLogoSource.ts at main · lineCode/interface · GitHub
lineCode
interface
Repository navigation
Code
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
interface
/
src
/
hooks
/
useAssetLogoSource.ts
Copy path
More file actions
More file actions
Latest commit
History
History
History
99 lines (84 loc) · 3.33 KB
Breadcrumbs
interface
/
src
/
hooks
/
useAssetLogoSource.ts
Copy path
File metadata and controls
99 lines (84 loc) · 3.33 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
import
tokenLogoLookup
from
'constants/tokenLogoLookup'
import
{
isCelo
,
nativeOnChain
}
from
'constants/tokens'
import
{
checkWarning
,
WARNING_LEVEL
}
from
'constants/tokenSafety'
import
{
chainIdToNetworkName
,
getNativeLogoURI
}
from
'lib/hooks/useCurrencyLogoURIs'
import
uriToHttp
from
'lib/utils/uriToHttp'
import
{
useCallback
,
useEffect
,
useState
}
from
'react'
import
{
isAddress
}
from
'utils'
import
celoLogo
from
'../assets/svg/celo_logo.svg'
const
BAD_SRCS
:
{
[
tokenAddress
:
string
]
:
true
}
=
{
}
// Converts uri's into fetchable urls
function
parseLogoSources
(
uris
:
string
[
]
)
{
const
urls
:
string
[
]
=
[
]
uris
.
forEach
(
(
uri
)
=>
urls
.
push
(
...
uriToHttp
(
uri
)
)
)
return
urls
}
// Parses uri's, favors non-coingecko images, and improves coingecko logo quality
function
prioritizeLogoSources
(
uris
:
string
[
]
)
{
const
parsedUris
=
uris
.
map
(
(
uri
)
=>
uriToHttp
(
uri
)
)
.
flat
(
1
)
const
preferredUris
:
string
[
]
=
[
]
// Consolidate duplicate coingecko urls into one fallback source
let
coingeckoUrl
:
string
|
undefined
=
undefined
parsedUris
.
forEach
(
(
uri
)
=>
{
if
(
uri
.
startsWith
(
'https://assets.coingecko'
)
)
{
if
(
!
coingeckoUrl
)
{
coingeckoUrl
=
uri
.
replace
(
/
s
m
a
l
l
|
t
h
u
m
b
/
g
,
'large'
)
}
}
else
{
preferredUris
.
push
(
uri
)
}
}
)
// Places coingecko urls in the back of the source array
return
coingeckoUrl
?
[
...
preferredUris
,
coingeckoUrl
]
:
preferredUris
}
function
getInitialUrl
(
address
?:
string
|
null
,
chainId
?:
number
|
null
,
isNative
?:
boolean
,
backupImg
?:
string
|
null
)
{
if
(
chainId
&&
isNative
)
return
getNativeLogoURI
(
chainId
)
const
networkName
=
chainId
?
chainIdToNetworkName
(
chainId
)
:
'ethereum'
const
checksummedAddress
=
isAddress
(
address
)
if
(
chainId
&&
isCelo
(
chainId
)
&&
address
===
nativeOnChain
(
chainId
)
.
wrapped
.
address
)
{
return
celoLogo
}
if
(
checksummedAddress
)
{
return
`https://raw.githubusercontent.com/Uniswap/assets/master/blockchains/
${
networkName
}
/assets/
${
checksummedAddress
}
/logo.png`
}
else
{
return
backupImg
??
undefined
}
}
export
default
function
useAssetLogoSource
(
address
?:
string
|
null
,
chainId
?:
number
|
null
,
isNative
?:
boolean
,
backupImg
?:
string
|
null
)
:
[
string
|
undefined
,
(
)
=>
void
]
{
const
hideLogo
=
Boolean
(
address
&&
checkWarning
(
address
,
chainId
)
?.
level
===
WARNING_LEVEL
.
BLOCKED
)
const
[
current
,
setCurrent
]
=
useState
<
string
|
undefined
>
(
hideLogo
?
undefined
:
getInitialUrl
(
address
,
chainId
,
isNative
,
backupImg
)
)
const
[
fallbackSrcs
,
setFallbackSrcs
]
=
useState
<
string
[
]
|
undefined
>
(
undefined
)
useEffect
(
(
)
=>
{
if
(
hideLogo
)
return
setCurrent
(
getInitialUrl
(
address
,
chainId
,
isNative
)
)
setFallbackSrcs
(
undefined
)
}
,
[
address
,
chainId
,
hideLogo
,
isNative
]
)
const
nextSrc
=
useCallback
(
(
)
=>
{
if
(
current
)
{
BAD_SRCS
[
current
]
=
true
}
// Parses and stores logo sources from tokenlists if assets repo url fails
if
(
!
fallbackSrcs
)
{
const
uris
=
tokenLogoLookup
.
getIcons
(
address
,
chainId
)
??
[
]
if
(
backupImg
)
uris
.
push
(
backupImg
)
const
tokenListIcons
=
prioritizeLogoSources
(
parseLogoSources
(
uris
)
)
setCurrent
(
tokenListIcons
.
find
(
(
src
)
=>
!
BAD_SRCS
[
src
]
)
)
setFallbackSrcs
(
tokenListIcons
)
}
else
{
setCurrent
(
fallbackSrcs
.
find
(
(
src
)
=>
!
BAD_SRCS
[
src
]
)
)
}
}
,
[
current
,
fallbackSrcs
,
address
,
chainId
,
backupImg
]
)
return
[
current
,
nextSrc
]
}
Back
|
FazBrowse Home
|
New Git URL