import type { Account } from '@lifi/wallet-management'
import {
getConnectorIcon,
useAccount,
useWalletMenu,
} from '@lifi/wallet-management'
import ExpandMore from '@mui/icons-material/ExpandMore'
import Wallet from '@mui/icons-material/Wallet'
import { Avatar, Badge } from '@mui/material'
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 { hiddenUI } = useWidgetConfig()
const { useExternalWalletProvidersOnly } = useExternalWalletProvider()
const isWalletMenuHidden = hiddenUI?.includes(HiddenUI.WalletMenu)
const shouldShowWalletMenu =
!useExternalWalletProvidersOnly && !isWalletMenuHidden
return {
shouldShowWalletMenu,
}
}
export const WalletHeader: React.FC = () => {
const { shouldShowWalletMenu } = useInternalWalletManagement()
return shouldShowWalletMenu ? (
) : null
}
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, variant } = useWidgetConfig()
const { openWalletMenu } = useWalletMenu()
const connect = async (event: React.MouseEvent) => {
if (
!walletConfig?.usePartialWalletManagement &&
!walletConfig?.forceInternalWalletManagement &&
walletConfig?.onConnect
) {
walletConfig.onConnect()
return
}
openWalletMenu()
event.currentTarget.blur() // Remove focus to prevent accessibility issues when opening drawer
}
const walletPosition = variant === 'drawer' ? 'start' : 'end'
return (
: undefined}
startIcon={
walletPosition === 'start' ? (
) : undefined
}
onClick={connect}
>
{t('button.connectWallet')}
)
}
const ConnectedButton = ({ account }: { account: Account }) => {
const { chain } = useChain(account.chainId)
const [anchorEl, setAnchorEl] = useState(null)
const walletAddress = shortenAddress(account.address)
const handleClick = (event: React.MouseEvent) => {
setAnchorEl(event.currentTarget)
event.currentTarget.blur() // Remove focus to prevent accessibility issues when opening drawer
}
const handleClose = () => {
setAnchorEl(null)
}
return (
<>
}
startIcon={
chain?.logoURI ? (
{chain?.name[0]}
}
>
{account.connector?.name[0]}
) : (
{account.connector?.name[0]}
)
}
onClick={handleClick}
>
{walletAddress}
>
)
}