import { useState } from 'react'; import CopyToClipboard from 'react-copy-to-clipboard'; import { truncateByChars } from '@vegaprotocol/utils'; import { VegaIcon, VegaIconNames, TradingButton as Button, Intent, TradingDropdown, TradingDropdownTrigger, TradingDropdownContent, TradingDropdownRadioGroup, TradingDropdownSeparator, TradingDropdownItem, TradingDropdownRadioItem, TradingDropdownItemIndicator, Tooltip, } from '@vegaprotocol/ui-toolkit'; import { isBrowserWalletInstalled, type Key } from '@vegaprotocol/wallet'; import { useDialogStore, useVegaWallet } from '@vegaprotocol/wallet-react'; import { useCopyTimeout } from '@vegaprotocol/react-helpers'; import classNames from 'classnames'; import { ViewType, useSidebar } from '../sidebar'; import { useGetCurrentRouteId } from '../../lib/hooks/use-get-current-route-id'; import { useT } from '../../lib/use-t'; import { usePartyProfilesQuery } from './__generated__/PartyProfiles'; import { useProfileDialogStore } from '../../stores/profile-dialog-store'; export const VegaWalletConnectButton = ({ intent = Intent.None, onClick, }: { intent?: Intent; onClick?: () => void; }) => { const t = useT(); const [dropdownOpen, setDropdownOpen] = useState(false); const openVegaWalletDialog = useDialogStore((store) => store.open); const currentRouteId = useGetCurrentRouteId(); const setViews = useSidebar((store) => store.setViews); const { status, pubKeys, pubKey, selectPubKey, disconnect, refreshKeys, isReadOnly, } = useVegaWallet(); const walletInstalled = isBrowserWalletInstalled(); const activeKey = pubKeys?.find((pk) => pk.publicKey === pubKey); if (status === 'connected') { return ( { refreshKeys(); setDropdownOpen((x) => !x); }} > } > {activeKey ? ( <> {activeKey && ( {activeKey.name ? activeKey.name : t('Unnamed key')} )} > ) : ( <>{'Select key'}> )} } > setDropdownOpen(false)} sideOffset={12} side="bottom" align="end" onEscapeKeyDown={() => setDropdownOpen(false)} > {!isReadOnly && ( { setViews({ type: ViewType.Transfer }, currentRouteId); setDropdownOpen(false); }} > {t('Transfer')} )} {t('Disconnect')} ); } return ( { onClick?.(); openVegaWalletDialog(); }} size="small" intent={intent} icon={} > {walletInstalled ? t('Connect') : t('Get started')} ); }; const KeypairRadioGroup = ({ pubKey, pubKeys, activeKey, onSelect, }: { pubKey: string | undefined; pubKeys: Key[]; activeKey: string | undefined; onSelect: (pubKey: string) => void; }) => { const { data } = usePartyProfilesQuery({ variables: { partyIds: pubKeys.map((pk) => pk.publicKey) }, skip: pubKeys.length <= 0, fetchPolicy: 'cache-and-network', }); return ( {pubKeys.map((pk) => { const profile = data?.partiesProfilesConnection?.edges.find( (e) => e.node.partyId === pk.publicKey ); return ( ); })} ); }; const KeypairItem = ({ pk, isActive, alias, }: { pk: Key; alias: string | undefined; isActive: boolean; }) => { const t = useT(); const [copied, setCopied] = useCopyTimeout(); const setOpen = useProfileDialogStore((store) => store.setOpen); return ( {pk.name ? pk.name : t('Unnamed key')} {' | '} {truncateByChars(pk.publicKey, 3, 3)} setCopied(true)}> e.stopPropagation()} > {t('Copy')} {copied && {t('Copied')}} setOpen(pk.publicKey)} className="flex items-center gap-1" > {alias ? alias : t('No alias')} {isActive && } ); };