Skip to content

Commit

Permalink
Copy AccountAvatar from oasis explorer
Browse files Browse the repository at this point in the history
  • Loading branch information
lukaw3d committed Aug 28, 2024
1 parent 645c461 commit a3995f5
Show file tree
Hide file tree
Showing 6 changed files with 115 additions and 2 deletions.
20 changes: 20 additions & 0 deletions packages/nextjs/components/AccountAvatar/addressToJazzIconSeed.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { staking } from '@oasisprotocol/client'

const ethAddressToSeed = (address_eth: string) => {
// https://github.com/oasisprotocol/oasis-wallet-ext/blob/da7ad67/src/popup/component/AccountIcon/index.js#L20-L25
// https://github.com/MetaMask/metamask-extension/blob/v10.7.0/ui/helpers/utils/icon-factory.js#L84-L88
const addr = address_eth.slice(2, 10)
const seed = parseInt(addr, 16)
return seed
}

const oasisAddressToSeed = (address: string) => {
// https://github.com/oasisprotocol/oasis-wallet-ext/blob/da7ad67/src/popup/component/AccountIcon/index.js#L26
const addressU8 = staking.addressFromBech32(address)
const seed = addressU8[20] | (addressU8[19] << 8) | (addressU8[18] << 16) | (addressU8[17] << 24)
return seed
}

export function addressToJazzIconSeed(account: { address: string; address_eth?: string }) {
return account.address_eth ? ethAddressToSeed(account.address_eth) : oasisAddressToSeed(account.address)
}
21 changes: 21 additions & 0 deletions packages/nextjs/components/AccountAvatar/index.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { FC } from 'react'
import { useScreenSize } from '../../hooks/useScreensize'
import { JazzIcon } from '../JazzIcon'
import { addressToJazzIconSeed } from './addressToJazzIconSeed'

type AccountAvatarProps = {
account: {
address: string
address_eth?: string
}
}

export const AccountAvatar: FC<AccountAvatarProps> = ({ account }) => {
const { isMobile } = useScreenSize()

if (!account.address) {
return null
}

return <JazzIcon diameter={isMobile ? 30 : 40} seed={addressToJazzIconSeed(account)} />
}
22 changes: 22 additions & 0 deletions packages/nextjs/components/JazzIcon/index.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import React, { memo, useEffect, useRef } from 'react'
import jazzicon from '@metamask/jazzicon'
import { Box } from 'grommet/es6/components/Box'

interface JazzIconProps {
diameter: number
seed: number
}

export const JazzIcon = memo(({ diameter, seed }: JazzIconProps) => {
const ref = useRef<HTMLDivElement>(null)

useEffect(() => {
if (ref?.current) {
const icon = jazzicon(diameter, seed)

ref.current.replaceChildren(icon)
}
}, [diameter, ref, seed])

return <Box ref={ref}></Box>
})
4 changes: 4 additions & 0 deletions packages/nextjs/components/JazzIcon/jazzicon.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
declare module '@metamask/jazzicon' {
const jazzicon: (diameter: number, seed: number) => HTMLDivElement
export default jazzicon
}
1 change: 1 addition & 0 deletions packages/nextjs/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
"dependencies": {
"@ethersproject/providers": "^5.7.2",
"@heroicons/react": "^2.0.11",
"@metamask/jazzicon": "^2.0.0",
"@rainbow-me/rainbowkit": "1.3.0",
"@uniswap/sdk-core": "^4.0.1",
"@uniswap/v2-sdk": "^3.0.1",
Expand Down
49 changes: 47 additions & 2 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -929,6 +929,16 @@ __metadata:
languageName: node
linkType: hard

"@metamask/jazzicon@npm:^2.0.0":
version: 2.0.0
resolution: "@metamask/jazzicon@npm:2.0.0"
dependencies:
color: ^0.11.3
mersenne-twister: ^1.1.0
checksum: 0b5add49e8d28270e4f8ca6159aa4ea0848f70cbb1cb217de2b0c13799a45fb27b8a6f0b3f72b6a76cbb55446764f79daf71e7d2a830a14b8e5e54d5e57bf7d1
languageName: node
linkType: hard

"@metamask/safe-event-emitter@npm:2.0.0, @metamask/safe-event-emitter@npm:^2.0.0":
version: 2.0.0
resolution: "@metamask/safe-event-emitter@npm:2.0.0"
Expand Down Expand Up @@ -1348,6 +1358,7 @@ __metadata:
dependencies:
"@ethersproject/providers": ^5.7.2
"@heroicons/react": ^2.0.11
"@metamask/jazzicon": ^2.0.0
"@rainbow-me/rainbowkit": 1.3.0
"@trivago/prettier-plugin-sort-imports": ^4.1.1
"@types/node": ^17.0.35
Expand Down Expand Up @@ -3600,6 +3611,13 @@ __metadata:
languageName: node
linkType: hard

"clone@npm:^1.0.2":
version: 1.0.4
resolution: "clone@npm:1.0.4"
checksum: d06418b7335897209e77bdd430d04f882189582e67bd1f75a04565f3f07f5b3f119a9d670c943b6697d0afb100f03b866b3b8a1f91d4d02d72c4ecf2bb64b5dd
languageName: node
linkType: hard

"clsx@npm:1.1.1":
version: 1.1.1
resolution: "clsx@npm:1.1.1"
Expand All @@ -3621,7 +3639,7 @@ __metadata:
languageName: node
linkType: hard

"color-convert@npm:^1.9.0":
"color-convert@npm:^1.3.0, color-convert@npm:^1.9.0":
version: 1.9.3
resolution: "color-convert@npm:1.9.3"
dependencies:
Expand All @@ -3646,13 +3664,22 @@ __metadata:
languageName: node
linkType: hard

"color-name@npm:~1.1.4":
"color-name@npm:^1.0.0, color-name@npm:~1.1.4":
version: 1.1.4
resolution: "color-name@npm:1.1.4"
checksum: b0445859521eb4021cd0fb0cc1a75cecf67fceecae89b63f62b201cca8d345baf8b952c966862a9d9a2632987d4f6581f0ec8d957dfacece86f0a7919316f610
languageName: node
linkType: hard

"color-string@npm:^0.3.0":
version: 0.3.0
resolution: "color-string@npm:0.3.0"
dependencies:
color-name: ^1.0.0
checksum: cc65013a94d399ddcb81b3b4b6e0d2166cbaa77311260ff6271f215a286d3db58ab3a217bdb175f8b9dd95f9ea7ac94f71a595af6bcd3b6a02fea6e75ddaf5fb
languageName: node
linkType: hard

"color-support@npm:^1.1.2, color-support@npm:^1.1.3":
version: 1.1.3
resolution: "color-support@npm:1.1.3"
Expand All @@ -3662,6 +3689,17 @@ __metadata:
languageName: node
linkType: hard

"color@npm:^0.11.3":
version: 0.11.4
resolution: "color@npm:0.11.4"
dependencies:
clone: ^1.0.2
color-convert: ^1.3.0
color-string: ^0.3.0
checksum: a6f624fd159e9311a6c1c86ac05b5b550c98e2d1e5646e032b8481e5e113127c06a84678b91c89c96b11b58c1c723dd9766b1b02cb2d441c6212a0f7cb3dad73
languageName: node
linkType: hard

"colorette@npm:^2.0.16, colorette@npm:^2.0.17":
version: 2.0.19
resolution: "colorette@npm:2.0.19"
Expand Down Expand Up @@ -6443,6 +6481,13 @@ __metadata:
languageName: node
linkType: hard

"mersenne-twister@npm:^1.1.0":
version: 1.1.0
resolution: "mersenne-twister@npm:1.1.0"
checksum: 7de1940ded117f2aad9320ae4d21d647b0ecf0667abbadcfe6a2835c669feb674ef46cb7a72da7af69a56d8b19e50e95e2fb7ef6d780efab7a6acd4d87f4cb2d
languageName: node
linkType: hard

"micromatch@npm:^4.0.2, micromatch@npm:^4.0.4, micromatch@npm:^4.0.5":
version: 4.0.5
resolution: "micromatch@npm:4.0.5"
Expand Down

0 comments on commit a3995f5

Please sign in to comment.