forked from BuidlGuidl/abi.ninja
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Copy AccountAvatar from oasis explorer
- Loading branch information
Showing
6 changed files
with
115 additions
and
2 deletions.
There are no files selected for viewing
20 changes: 20 additions & 0 deletions
20
packages/nextjs/components/AccountAvatar/addressToJazzIconSeed.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)} /> | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters