Commit d37e963f authored by Connor McEwen's avatar Connor McEwen Committed by GitHub

fix: Show proper graph color for non-Ethereum L1 chains (#2049) (#2284)

* Switch to fetching token color from the logoURI included

* Fall back to github hosted images

* Move logger to changes to another PR
parent 2e40bef7
import { Token } from '@uniswap/sdk-core' import { Token } from '@uniswap/sdk-core'
import Vibrant from 'node-vibrant/lib/bundle' import { SupportedChainId } from 'constants/chains'
import Vibrant from 'node-vibrant'
import { shade } from 'polished' import { shade } from 'polished'
import { useLayoutEffect, useState } from 'react' import { useLayoutEffect, useState } from 'react'
import { WrappedTokenInfo } from 'state/lists/wrappedTokenInfo'
import uriToHttp from 'utils/uriToHttp' import uriToHttp from 'utils/uriToHttp'
import { hex } from 'wcag-contrast' import { hex } from 'wcag-contrast'
function URIForEthToken(address: string) {
return `https://raw.githubusercontent.com/uniswap/assets/master/blockchains/ethereum/assets/${address}/logo.png`
}
async function getColorFromToken(token: Token): Promise<string | null> { async function getColorFromToken(token: Token): Promise<string | null> {
if (token.chainId !== 1) { if (!(token instanceof WrappedTokenInfo)) {
return Promise.resolve('#FAAB14') return null
} }
const path = `https://raw.githubusercontent.com/uniswap/assets/master/blockchains/ethereum/assets/${token.address}/logo.png` const wrappedToken = token as WrappedTokenInfo
const { address } = wrappedToken
let { logoURI } = wrappedToken
if (!logoURI) {
if (token.chainId !== SupportedChainId.MAINNET) {
return null
} else {
logoURI = URIForEthToken(address)
}
}
return Vibrant.from(path) try {
.getPalette() return await getColorFromUriPath(logoURI)
.then((palette) => { } catch (e) {
if (palette?.Vibrant) { if (logoURI === URIForEthToken(address)) {
let detectedHex = palette.Vibrant.hex return null
let AAscore = hex(detectedHex, '#FFF')
while (AAscore < 3) {
detectedHex = shade(0.005, detectedHex)
AAscore = hex(detectedHex, '#FFF')
} }
return detectedHex
try {
logoURI = URIForEthToken(address)
return await getColorFromUriPath(logoURI)
} catch (e) {}
} }
return null return null
})
.catch(() => null)
} }
async function getColorFromUriPath(uri: string): Promise<string | null> { async function getColorFromUriPath(uri: string): Promise<string | null> {
const formattedPath = uriToHttp(uri)[0] const formattedPath = uriToHttp(uri)[0]
return Vibrant.from(formattedPath) const palette = await Vibrant.from(formattedPath).getPalette()
.getPalette() if (!palette?.Vibrant) {
.then((palette) => {
if (palette?.Vibrant) {
return palette.Vibrant.hex
}
return null return null
}) }
.catch(() => null)
let detectedHex = palette.Vibrant.hex
let AAscore = hex(detectedHex, '#FFF')
while (AAscore < 3) {
detectedHex = shade(0.005, detectedHex)
AAscore = hex(detectedHex, '#FFF')
}
return detectedHex
} }
export function useColor(token?: Token) { export function useColor(token?: Token) {
......
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