import { useCallback, useMemo, useState } from 'react'; import CopyToClipboard from 'react-copy-to-clipboard'; import classNames from 'classnames'; import { truncateByChars } from '@vegaprotocol/utils'; import { t } from '@vegaprotocol/i18n'; import { Button, DropdownMenu, DropdownMenuContent, DropdownMenuItem, DropdownMenuItemIndicator, DropdownMenuRadioGroup, DropdownMenuRadioItem, DropdownMenuTrigger, Drawer, DropdownMenuSeparator, VegaIcon, VegaIconNames, } from '@vegaprotocol/ui-toolkit'; import type { PubKey } from '@vegaprotocol/wallet'; import { useVegaWallet, useVegaWalletDialogStore } from '@vegaprotocol/wallet'; import { Networks, useEnvironment } from '@vegaprotocol/environment'; import { WalletIcon } from '../icons/wallet'; import { useCopyTimeout } from '@vegaprotocol/react-helpers'; import { ViewType, useSidebar } from '../sidebar'; const MobileWalletButton = ({ isConnected, activeKey, }: { isConnected?: boolean; activeKey?: PubKey; }) => { const { pubKeys, selectPubKey, disconnect, fetchPubKeys } = useVegaWallet(); const openVegaWalletDialog = useVegaWalletDialogStore( (store) => store.openVegaWalletDialog ); const setView = useSidebar((store) => store.setView); const { VEGA_ENV } = useEnvironment(); const isYellow = VEGA_ENV === Networks.TESTNET; const [drawerOpen, setDrawerOpen] = useState(false); const mobileDisconnect = useCallback(() => { setDrawerOpen(false); disconnect(); }, [disconnect]); const openDrawer = useCallback(() => { if (!isConnected) { openVegaWalletDialog(); setDrawerOpen(false); } else { if (fetchPubKeys) { fetchPubKeys(); } setDrawerOpen(!drawerOpen); } }, [drawerOpen, fetchPubKeys, isConnected, openVegaWalletDialog]); const iconClass = drawerOpen ? 'hidden' : isYellow ? 'fill-black' : 'fill-white'; const [container, setContainer] = useState(null); const walletButton = ( ); const onSelectItem = useCallback( (pubkey: string) => { setDrawerOpen(false); selectPubKey(pubkey); }, [selectPubKey] ); return (
{(pubKeys || []).map((pk) => ( ))}
); }; export const VegaWalletConnectButton = () => { const [dropdownOpen, setDropdownOpen] = useState(false); const openVegaWalletDialog = useVegaWalletDialogStore( (store) => store.openVegaWalletDialog ); const setView = useSidebar((store) => store.setView); const { pubKey, pubKeys, selectPubKey, disconnect, isReadOnly, fetchPubKeys, } = useVegaWallet(); const isConnected = pubKey !== null; const activeKey = useMemo(() => { return pubKeys?.find((pk) => pk.publicKey === pubKey); }, [pubKey, pubKeys]); if (isConnected && pubKeys) { return ( <>
{ if (fetchPubKeys) { fetchPubKeys(); } setDropdownOpen(!dropdownOpen); }} > {activeKey && ( {activeKey.name} )} {': '} {truncateByChars(pubKey)} } > setDropdownOpen(false)} sideOffset={17} side="bottom" align="end" onEscapeKeyDown={() => setDropdownOpen(false)} >
{ selectPubKey(value); }} > {pubKeys.map((pk) => ( ))} {!isReadOnly && ( { setView({ type: ViewType.Transfer }); setDropdownOpen(false); }} > {t('Transfer')} )} {t('Disconnect')}
); } return ( <> ); }; const KeypairItem = ({ pk }: { pk: PubKey }) => { const [copied, setCopied] = useCopyTimeout(); return (
{pk.name}:{' '} {truncateByChars(pk.publicKey)} setCopied(true)}> {copied && {t('Copied')}}
); }; const KeypairListItem = ({ pk, isActive, onSelectItem, }: { pk: PubKey; isActive: boolean; onSelectItem: (pk: string) => void; }) => { const [copied, setCopied] = useCopyTimeout(); return (
{isActive && } {truncateByChars(pk.publicKey)}{' '} setCopied(true)}> {copied && {t('Copied')}}
); };