import { FileCopy as CopyIcon, LinkOff as DisconnectIcon, SwapHoriz as SwitchIcon } from '@mui/icons-material'; import type { ButtonProps, Theme } from '@mui/material'; import { Button, Collapse, Fade, ListItemIcon, Menu, MenuItem, styled } from '@mui/material'; import { useWallet } from '@solana/wallet-adapter-react'; import type { FC } from 'react'; import React, { useMemo, useState } from 'react'; import { useWalletDialog } from './useWalletDialog.js'; import { WalletConnectButton } from './WalletConnectButton.js'; import { WalletDialogButton } from './WalletDialogButton.js'; import { WalletIcon } from './WalletIcon.js'; const StyledMenu = styled(Menu)(({ theme }: { theme: Theme }) => ({ '& .MuiList-root': { padding: 0, }, '& .MuiListItemIcon-root': { marginRight: theme.spacing(), minWidth: 'unset', '& .MuiSvgIcon-root': { width: 20, height: 20, }, }, })); const WalletActionMenuItem = styled(MenuItem)(({ theme }: { theme: Theme }) => ({ padding: theme.spacing(1, 2), boxShadow: 'inset 0 1px 0 0 ' + 'rgba(255, 255, 255, 0.1)', '&:hover': { boxShadow: 'inset 0 1px 0 0 ' + 'rgba(255, 255, 255, 0.1)' + ', 0 1px 0 0 ' + 'rgba(255, 255, 255, 0.05)', }, })); const WalletMenuItem = styled(WalletActionMenuItem)(({ theme }: { theme: Theme }) => ({ padding: 0, '& .MuiButton-root': { borderRadius: 0, }, })); export const WalletMultiButton: FC = ({ color = 'primary', variant = 'contained', type = 'button', children, ...props }) => { const { publicKey, wallet, disconnect } = useWallet(); const { setOpen } = useWalletDialog(); const [anchor, setAnchor] = useState(); const base58 = useMemo(() => publicKey?.toBase58(), [publicKey]); const content = useMemo(() => { if (children) return children; if (!wallet || !base58) return null; return base58.slice(0, 4) + '..' + base58.slice(-4); }, [children, wallet, base58]); if (!wallet) { return ( {children} ); } if (!base58) { return ( {children} ); } return ( <> setAnchor(undefined)} marginThreshold={0} TransitionComponent={Fade} transitionDuration={250} keepMounted anchorOrigin={{ vertical: 'top', horizontal: 'left', }} > setAnchor(undefined)}> { setAnchor(undefined); await navigator.clipboard.writeText(base58); }} > Copy address { setAnchor(undefined); setOpen(true); }} > Change wallet { setAnchor(undefined); // eslint-disable-next-line @typescript-eslint/no-empty-function disconnect().catch(() => { // Silently catch because any errors are caught by the context `onError` handler }); }} > Disconnect ); };