mirror of
https://github.com/oceanprotocol/market.git
synced 2024-12-02 05:57:29 +01:00
* refactor network output * fetch chain & network metadata from @ethereum-lists/chains * typed responses * switch warning icon for testnet badge * add supportedNetworks list, output warning based on it * markup & spacing tweaks * check networkId against ocean.js ConfigHelper * remove supportedNetworks app config * fetch EVM networks metadata on build time * fixes
72 lines
2.0 KiB
TypeScript
72 lines
2.0 KiB
TypeScript
import { useOcean } from '@oceanprotocol/react'
|
|
import { toDataUrl } from 'ethereum-blockies'
|
|
import React, { FormEvent } from 'react'
|
|
import { ReactComponent as Caret } from '../../../images/caret.svg'
|
|
import { accountTruncate } from '../../../utils/wallet'
|
|
import Loader from '../../atoms/Loader'
|
|
import Status from '../../atoms/Status'
|
|
import styles from './Account.module.css'
|
|
|
|
const Blockies = ({ account }: { account: string | undefined }) => {
|
|
if (!account) return null
|
|
const blockies = toDataUrl(account)
|
|
|
|
return (
|
|
<img
|
|
className={styles.blockies}
|
|
src={blockies}
|
|
alt="Blockies"
|
|
aria-hidden="true"
|
|
/>
|
|
)
|
|
}
|
|
|
|
// Forward ref for Tippy.js
|
|
// eslint-disable-next-line
|
|
const Account = React.forwardRef((props, ref: any) => {
|
|
const { accountId, status, connect, web3Modal } = useOcean()
|
|
const hasSuccess = status === 1
|
|
|
|
async function handleActivation(e: FormEvent<HTMLButtonElement>) {
|
|
// prevent accidentially submitting a form the button might be in
|
|
e.preventDefault()
|
|
|
|
await connect()
|
|
}
|
|
|
|
return !accountId && web3Modal?.cachedProvider ? (
|
|
// Improve user experience for cached provider when connecting takes some time
|
|
<button className={styles.button} onClick={(e) => e.preventDefault()}>
|
|
<Loader message="Reconnecting wallet..." />
|
|
</button>
|
|
) : accountId ? (
|
|
<button
|
|
className={styles.button}
|
|
aria-label="Account"
|
|
ref={ref}
|
|
onClick={(e) => e.preventDefault()}
|
|
>
|
|
<Blockies account={accountId} />
|
|
<span className={styles.address} title={accountId}>
|
|
{accountTruncate(accountId)}
|
|
</span>
|
|
{!hasSuccess && (
|
|
<Status className={styles.status} state="warning" aria-hidden />
|
|
)}
|
|
<Caret aria-hidden="true" />
|
|
</button>
|
|
) : (
|
|
<button
|
|
className={`${styles.button} ${styles.initial}`}
|
|
onClick={(e) => handleActivation(e)}
|
|
// Need the `ref` here although we do not want
|
|
// the Tippy to show in this state.
|
|
ref={ref}
|
|
>
|
|
Connect Wallet
|
|
</button>
|
|
)
|
|
})
|
|
|
|
export default Account
|