import { ExpandMore, Wallet } from '@mui/icons-material'
import { Avatar, Badge } from '@mui/material'
import type { Account } from '@openocean.finance/wallet-management'
import {
getConnectorIcon,
useAccount,
useWalletMenu,
} from '@openocean.finance/wallet-management'
import { useState } from 'react'
import { useTranslation } from 'react-i18next'
import { useChain } from '../../hooks/useChain.js'
import { useExternalWalletProvider } from '../../providers/WalletProvider/useExternalWalletProvider.js'
import { useWidgetConfig } from '../../providers/WidgetProvider/WidgetProvider.js'
import { useFieldValues } from '../../stores/form/useFieldValues.js'
import { HiddenUI } from '../../types/widget.js'
import { shortenAddress } from '../../utils/wallet.js'
import { SmallAvatar } from '../Avatar/SmallAvatar.js'
import { CloseDrawerButton } from './CloseDrawerButton.js'
import {
DrawerWalletContainer,
HeaderAppBar,
WalletAvatar,
WalletButton,
} from './Header.style.js'
import { WalletMenu } from './WalletMenu.js'
import { WalletMenuContainer } from './WalletMenu.style.js'
const useInternalWalletManagement = () => {
const { subvariant, hiddenUI } = useWidgetConfig()
const { useExternalWalletProvidersOnly } = useExternalWalletProvider()
const isSplitVariant = subvariant === 'split'
const isWalletMenuHidden = hiddenUI?.includes(HiddenUI.WalletMenu)
const shouldShowWalletMenu =
!useExternalWalletProvidersOnly && !isWalletMenuHidden
return {
shouldShowWalletMenu,
isSplitVariant,
}
}
export const WalletHeader: React.FC = () => {
const { shouldShowWalletMenu } = useInternalWalletManagement()
return shouldShowWalletMenu ? (
) : null
}
export const SplitWalletMenuButton: React.FC = () => {
const { shouldShowWalletMenu } = useInternalWalletManagement()
return shouldShowWalletMenu ? : null
}
export const WalletMenuButton: React.FC = () => {
const { variant, hiddenUI } = useWidgetConfig()
const { account, accounts } = useAccount()
const [fromChainId] = useFieldValues('fromChain')
const { chain: fromChain } = useChain(fromChainId)
const activeAccount =
(fromChain
? accounts.find((account) => account.chainType === fromChain.chainType)
: undefined) || account
if (variant === 'drawer') {
return (
{activeAccount.isConnected ? (
) : (
)}
{!hiddenUI?.includes(HiddenUI.DrawerCloseButton) ? (
) : null}
)
}
return activeAccount.isConnected ? (
) : (
)
}
const ConnectButton = () => {
const { t } = useTranslation()
const { walletConfig, subvariant, variant } = useWidgetConfig()
const { openWalletMenu } = useWalletMenu()
const connect = async () => {
if (!walletConfig?.usePartialWalletManagement && walletConfig?.onConnect) {
walletConfig.onConnect()
return
}
openWalletMenu()
}
return (
: undefined
}
startIcon={
variant === 'drawer' || subvariant === 'split' ? (
) : undefined
}
onClick={connect}
>
{t('button.connectWallet')}
)
}
const ConnectedButton = ({ account }: { account: Account }) => {
const { subvariant } = useWidgetConfig()
const { chain } = useChain(account.chainId)
const [anchorEl, setAnchorEl] = useState(null)
const walletAddress = shortenAddress(account.address)
const handleClick = (event: React.MouseEvent) => {
setAnchorEl(event.currentTarget)
}
const handleClose = () => {
setAnchorEl(null)
}
return (
<>
}
startIcon={
chain?.logoURI ? (
{chain?.name[0]}
}
>
{account.connector?.name[0]}
) : (
{account.connector?.name[0]}
)
}
onClick={handleClick}
>
{walletAddress}
>
)
}