Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Modded and fixed issue when token image loading failed #443

Merged
merged 1 commit into from
Apr 22, 2022
Merged
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
1 change: 1 addition & 0 deletions src/custom/hooks/useColor/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './useColorMod'
115 changes: 115 additions & 0 deletions src/custom/hooks/useColor/useColorMod.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
/* import { Token } from '@uniswap/sdk-core'
import { SupportedChainId } from 'constants/chains'
import uriToHttp from 'lib/utils/uriToHttp'
import Vibrant from 'node-vibrant/lib/bundle.js'
import { shade } from 'polished' */
import { useLayoutEffect, useState } from 'react'
/* import { WrappedTokenInfo } from 'state/lists/wrappedTokenInfo'
import { hex } from 'wcag-contrast' */

// MOD imports
import { getColorFromUriPath } from '@src/hooks/useColor'

export * from '@src/hooks/useColor'

/* 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> {
if (!(token instanceof WrappedTokenInfo)) {
return null
}

const wrappedToken = token as WrappedTokenInfo
const { address } = wrappedToken
let { logoURI } = wrappedToken
if (!logoURI) {
if (token.chainId !== SupportedChainId.MAINNET) {
return null
} else {
logoURI = URIForEthToken(address)
}
}

try {
return await getColorFromUriPath(logoURI)
} catch (e) {
if (logoURI === URIForEthToken(address)) {
return null
}

try {
logoURI = URIForEthToken(address)
return await getColorFromUriPath(logoURI)
} catch (e) {}
}

return null
}

async function getColorFromUriPath(uri: string): Promise<string | null> {
const formattedPath = uriToHttp(uri)[0]

const palette = await Vibrant.from(formattedPath).getPalette()
if (!palette?.Vibrant) {
return 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) {
const [color, setColor] = useState('#2172E5')

useLayoutEffect(() => {
let stale = false

if (token) {
getColorFromToken(token).then((tokenColor) => {
if (!stale && tokenColor !== null) {
setColor(tokenColor)
}
})
}

return () => {
stale = true
setColor('#2172E5')
}
}, [token])

return color
} */

export function useListColor(listImageUri?: string) {
const [color, setColor] = useState('#2172E5')

useLayoutEffect(() => {
let stale = false

if (listImageUri) {
getColorFromUriPath(listImageUri)
.then((color) => {
if (!stale && color !== null) {
setColor(color)
}
})
.catch(console.warn) // mod: error handling
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The only effective change in this whole mod

}

return () => {
stale = true
setColor('#2172E5')
}
}, [listImageUri])

return color
}
2 changes: 1 addition & 1 deletion src/hooks/useColor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ async function getColorFromToken(token: Token): Promise<string | null> {
return null
}

async function getColorFromUriPath(uri: string): Promise<string | null> {
export async function getColorFromUriPath(uri: string): Promise<string | null> {
const formattedPath = uriToHttp(uri)[0]

const palette = await Vibrant.from(formattedPath).getPalette()
Expand Down