Skip to content

Commit

Permalink
IdenticonMod update not quite working yet for some reason
Browse files Browse the repository at this point in the history
  • Loading branch information
Leandro committed Mar 10, 2022
1 parent 60c992c commit 301ba12
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 17 deletions.
64 changes: 49 additions & 15 deletions src/custom/components/Identicon/IdenticonMod.tsx
Original file line number Diff line number Diff line change
@@ -1,34 +1,68 @@
import Davatar, { Image } from '@davatar/react'
import { useMemo } from 'react'
import jazzicon from '@metamask/jazzicon'
import useActiveWeb3React from 'hooks/useActiveWeb3React'
import useENSAvatar from 'hooks/useENSAvatar'
import { useLayoutEffect, useMemo, useRef, useState } from 'react'
import styled from 'styled-components/macro'
import { IdenticonProps } from 'components/Identicon'

import { useActiveWeb3React } from 'hooks/web3'
// MOD imports
// import Davatar, { Image } from '@davatar/react'
import { IdenticonProps } from 'components/Identicon'

export const StyledIdenticonContainer = styled.div`
export const StyledIdenticon = styled.div`
height: 1rem;
width: 1rem;
border-radius: 1.125rem;
background-color: ${({ theme }) => theme.bg4};
font-size: initial;
`

const StyledAvatar = styled.img`
height: inherit;
width: inherit;
border-radius: inherit;
`

export default function Identicon({ account: customAccount, size = 16 }: IdenticonProps) {
const { account: chainAccount, library } = useActiveWeb3React()
const { account: chainAccount /*, library*/ } = useActiveWeb3React()
const account = customAccount || chainAccount

// restrict usage of Davatar until it stops sending 3p requests
// see https://github.com/metaphor-xyz/davatar-helpers/issues/18
const supportsENS = useMemo(() => {
return ([1, 3, 4, 5] as Array<number | undefined>).includes(library?.network?.chainId)
}, [library])
const { avatar } = useENSAvatar(account ?? undefined, false)
const [fetchable, setFetchable] = useState(true)

const icon = useMemo(() => account && jazzicon(16, parseInt(account.slice(2, 10), 16)), [account])
const iconRef = useRef<HTMLDivElement>(null)
useLayoutEffect(() => {
const current = iconRef.current
if (icon) {
current?.appendChild(icon)
return () => {
try {
current?.removeChild(icon)
} catch (e) {
console.error('Avatar icon not found')
}
}
}
return
}, [icon, iconRef])

console.log(`identicon`, chainAccount, customAccount, account, avatar, fetchable)
return (
<StyledIdenticonContainer>
{account && supportsENS ? (
<Davatar address={account} size={size} provider={library} />
<StyledIdenticon>
{avatar && fetchable ? (
<StyledAvatar alt="avatar" src={avatar} onError={() => setFetchable(false)} />
) : (
<span ref={iconRef} />
)}
</StyledIdenticon>
// TODO: revisit if previous changes should be re-applied
/*<StyledIdenticon>
{/!*{account && supportsENS ? (*!/}
{avatar && fetchable ? (
<Davatar address={avatar} size={size} provider={library} />
) : (
<Image address={account} size={size} />
)}
</StyledIdenticonContainer>
</StyledIdenticon>*/
)
}
4 changes: 2 additions & 2 deletions src/custom/components/Identicon/index.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import React from 'react'
import IdenticonMod, { StyledIdenticonContainer } from './IdenticonMod'
import IdenticonMod, { StyledIdenticon } from './IdenticonMod'
import styled from 'styled-components/macro'

const Wrapper = styled.div<{ size?: number }>`
${StyledIdenticonContainer} {
${StyledIdenticon} {
height: ${({ size }) => (size ? `${size}px` : '1rem')};
width: ${({ size }) => (size ? `${size}px` : '1rem')};
border-radius: ${({ size }) => (size ? `${size}px` : '1rem')};
Expand Down

0 comments on commit 301ba12

Please sign in to comment.