2024-03-01 14:25:56 +00:00
|
|
|
import { useState } from 'react';
|
2023-01-17 09:59:12 +00:00
|
|
|
import CopyToClipboard from 'react-copy-to-clipboard';
|
2023-02-28 18:56:29 +00:00
|
|
|
import { truncateByChars } from '@vegaprotocol/utils';
|
2022-08-31 04:35:46 +00:00
|
|
|
import {
|
2023-06-29 23:48:53 +00:00
|
|
|
VegaIcon,
|
|
|
|
VegaIconNames,
|
2023-07-31 16:08:55 +00:00
|
|
|
TradingButton as Button,
|
|
|
|
Intent,
|
|
|
|
TradingDropdown,
|
|
|
|
TradingDropdownTrigger,
|
|
|
|
TradingDropdownContent,
|
|
|
|
TradingDropdownRadioGroup,
|
|
|
|
TradingDropdownSeparator,
|
|
|
|
TradingDropdownItem,
|
|
|
|
TradingDropdownRadioItem,
|
|
|
|
TradingDropdownItemIndicator,
|
2024-03-06 10:47:16 +00:00
|
|
|
Tooltip,
|
2022-08-31 04:35:46 +00:00
|
|
|
} from '@vegaprotocol/ui-toolkit';
|
2024-03-01 14:25:56 +00:00
|
|
|
import { isBrowserWalletInstalled, type Key } from '@vegaprotocol/wallet';
|
|
|
|
import { useDialogStore, useVegaWallet } from '@vegaprotocol/wallet-react';
|
2023-05-18 04:05:53 +00:00
|
|
|
import { useCopyTimeout } from '@vegaprotocol/react-helpers';
|
2023-07-31 16:08:55 +00:00
|
|
|
import classNames from 'classnames';
|
2024-03-01 14:25:56 +00:00
|
|
|
import { ViewType, useSidebar } from '../sidebar';
|
2023-09-15 12:36:08 +00:00
|
|
|
import { useGetCurrentRouteId } from '../../lib/hooks/use-get-current-route-id';
|
2023-11-16 03:10:39 +00:00
|
|
|
import { useT } from '../../lib/use-t';
|
2024-03-06 10:47:16 +00:00
|
|
|
import { usePartyProfilesQuery } from './__generated__/PartyProfiles';
|
|
|
|
import { useProfileDialogStore } from '../../stores/profile-dialog-store';
|
2022-03-31 17:16:30 +00:00
|
|
|
|
2023-12-18 10:39:18 +00:00
|
|
|
export const VegaWalletConnectButton = ({
|
|
|
|
intent = Intent.None,
|
|
|
|
onClick,
|
|
|
|
}: {
|
|
|
|
intent?: Intent;
|
|
|
|
onClick?: () => void;
|
|
|
|
}) => {
|
2023-11-16 03:10:39 +00:00
|
|
|
const t = useT();
|
2022-08-31 04:35:46 +00:00
|
|
|
const [dropdownOpen, setDropdownOpen] = useState(false);
|
2024-03-01 14:25:56 +00:00
|
|
|
const openVegaWalletDialog = useDialogStore((store) => store.open);
|
2023-09-15 12:36:08 +00:00
|
|
|
const currentRouteId = useGetCurrentRouteId();
|
|
|
|
const setViews = useSidebar((store) => store.setViews);
|
2023-04-20 11:56:48 +00:00
|
|
|
const {
|
2024-03-01 14:25:56 +00:00
|
|
|
status,
|
2023-04-20 11:56:48 +00:00
|
|
|
pubKeys,
|
2024-03-01 14:25:56 +00:00
|
|
|
pubKey,
|
2023-04-20 11:56:48 +00:00
|
|
|
selectPubKey,
|
|
|
|
disconnect,
|
2024-03-01 14:25:56 +00:00
|
|
|
refreshKeys,
|
2023-04-20 11:56:48 +00:00
|
|
|
isReadOnly,
|
|
|
|
} = useVegaWallet();
|
2024-03-01 14:25:56 +00:00
|
|
|
|
2023-08-02 15:34:04 +00:00
|
|
|
const walletInstalled = isBrowserWalletInstalled();
|
2022-10-03 18:12:34 +00:00
|
|
|
|
2024-03-01 14:25:56 +00:00
|
|
|
const activeKey = pubKeys?.find((pk) => pk.publicKey === pubKey);
|
|
|
|
|
|
|
|
if (status === 'connected') {
|
2022-08-31 04:35:46 +00:00
|
|
|
return (
|
2023-07-31 16:08:55 +00:00
|
|
|
<TradingDropdown
|
|
|
|
open={dropdownOpen}
|
|
|
|
trigger={
|
|
|
|
<TradingDropdownTrigger
|
|
|
|
data-testid="manage-vega-wallet"
|
|
|
|
onClick={() => {
|
2024-03-01 14:25:56 +00:00
|
|
|
refreshKeys();
|
|
|
|
setDropdownOpen((x) => !x);
|
2023-07-31 16:08:55 +00:00
|
|
|
}}
|
|
|
|
>
|
|
|
|
<Button
|
|
|
|
size="small"
|
|
|
|
icon={<VegaIcon name={VegaIconNames.CHEVRON_DOWN} size={14} />}
|
|
|
|
>
|
2024-03-01 14:25:56 +00:00
|
|
|
{activeKey ? (
|
|
|
|
<>
|
|
|
|
{activeKey && (
|
2024-03-06 10:47:16 +00:00
|
|
|
<span className="uppercase">
|
|
|
|
{activeKey.name ? activeKey.name : t('Unnamed key')}
|
|
|
|
</span>
|
2024-03-01 14:25:56 +00:00
|
|
|
)}
|
|
|
|
</>
|
|
|
|
) : (
|
|
|
|
<>{'Select key'}</>
|
|
|
|
)}
|
2023-07-31 16:08:55 +00:00
|
|
|
</Button>
|
|
|
|
</TradingDropdownTrigger>
|
|
|
|
}
|
|
|
|
>
|
|
|
|
<TradingDropdownContent
|
|
|
|
onInteractOutside={() => setDropdownOpen(false)}
|
|
|
|
sideOffset={12}
|
|
|
|
side="bottom"
|
|
|
|
align="end"
|
|
|
|
onEscapeKeyDown={() => setDropdownOpen(false)}
|
|
|
|
>
|
|
|
|
<div className="min-w-[340px]" data-testid="keypair-list">
|
2024-03-06 10:47:16 +00:00
|
|
|
<KeypairRadioGroup
|
|
|
|
pubKey={pubKey}
|
|
|
|
pubKeys={pubKeys}
|
2024-03-08 17:29:41 +00:00
|
|
|
activeKey={activeKey?.publicKey}
|
2024-03-06 10:47:16 +00:00
|
|
|
onSelect={selectPubKey}
|
|
|
|
/>
|
2023-07-31 16:08:55 +00:00
|
|
|
<TradingDropdownSeparator />
|
|
|
|
{!isReadOnly && (
|
|
|
|
<TradingDropdownItem
|
|
|
|
data-testid="wallet-transfer"
|
2023-04-20 11:56:48 +00:00
|
|
|
onClick={() => {
|
2023-09-15 12:36:08 +00:00
|
|
|
setViews({ type: ViewType.Transfer }, currentRouteId);
|
2023-07-31 16:08:55 +00:00
|
|
|
setDropdownOpen(false);
|
2023-04-20 11:56:48 +00:00
|
|
|
}}
|
2023-01-26 08:52:49 +00:00
|
|
|
>
|
2023-07-31 16:08:55 +00:00
|
|
|
{t('Transfer')}
|
|
|
|
</TradingDropdownItem>
|
|
|
|
)}
|
|
|
|
<TradingDropdownItem data-testid="disconnect" onClick={disconnect}>
|
|
|
|
{t('Disconnect')}
|
|
|
|
</TradingDropdownItem>
|
|
|
|
</div>
|
|
|
|
</TradingDropdownContent>
|
|
|
|
</TradingDropdown>
|
2022-08-31 04:35:46 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
return (
|
2023-07-31 16:08:55 +00:00
|
|
|
<Button
|
|
|
|
data-testid="connect-vega-wallet"
|
2023-12-18 10:39:18 +00:00
|
|
|
onClick={() => {
|
|
|
|
onClick?.();
|
|
|
|
openVegaWalletDialog();
|
|
|
|
}}
|
2023-07-31 16:08:55 +00:00
|
|
|
size="small"
|
2023-12-18 10:39:18 +00:00
|
|
|
intent={intent}
|
2023-07-31 16:08:55 +00:00
|
|
|
icon={<VegaIcon name={VegaIconNames.ARROW_RIGHT} size={14} />}
|
|
|
|
>
|
2023-08-02 15:34:04 +00:00
|
|
|
<span className="whitespace-nowrap uppercase">
|
|
|
|
{walletInstalled ? t('Connect') : t('Get started')}
|
|
|
|
</span>
|
2023-07-31 16:08:55 +00:00
|
|
|
</Button>
|
2022-08-31 04:35:46 +00:00
|
|
|
);
|
|
|
|
};
|
|
|
|
|
2024-03-06 10:47:16 +00:00
|
|
|
const KeypairRadioGroup = ({
|
|
|
|
pubKey,
|
|
|
|
pubKeys,
|
2024-03-08 17:29:41 +00:00
|
|
|
activeKey,
|
2024-03-06 10:47:16 +00:00
|
|
|
onSelect,
|
|
|
|
}: {
|
|
|
|
pubKey: string | undefined;
|
|
|
|
pubKeys: Key[];
|
2024-03-08 17:29:41 +00:00
|
|
|
activeKey: string | undefined;
|
2024-03-06 10:47:16 +00:00
|
|
|
onSelect: (pubKey: string) => void;
|
|
|
|
}) => {
|
|
|
|
const { data } = usePartyProfilesQuery({
|
|
|
|
variables: { partyIds: pubKeys.map((pk) => pk.publicKey) },
|
|
|
|
skip: pubKeys.length <= 0,
|
2024-03-08 17:29:41 +00:00
|
|
|
fetchPolicy: 'cache-and-network',
|
2024-03-06 10:47:16 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
return (
|
|
|
|
<TradingDropdownRadioGroup value={pubKey} onValueChange={onSelect}>
|
|
|
|
{pubKeys.map((pk) => {
|
|
|
|
const profile = data?.partiesProfilesConnection?.edges.find(
|
|
|
|
(e) => e.node.partyId === pk.publicKey
|
|
|
|
);
|
|
|
|
return (
|
2024-03-08 17:29:41 +00:00
|
|
|
<KeypairItem
|
|
|
|
key={pk.publicKey}
|
|
|
|
pk={pk}
|
|
|
|
isActive={activeKey === pk.publicKey}
|
|
|
|
alias={profile?.node.alias}
|
|
|
|
/>
|
2024-03-06 10:47:16 +00:00
|
|
|
);
|
|
|
|
})}
|
|
|
|
</TradingDropdownRadioGroup>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
2024-03-08 17:29:41 +00:00
|
|
|
const KeypairItem = ({
|
|
|
|
pk,
|
|
|
|
isActive,
|
|
|
|
alias,
|
|
|
|
}: {
|
|
|
|
pk: Key;
|
|
|
|
alias: string | undefined;
|
|
|
|
isActive: boolean;
|
|
|
|
}) => {
|
2023-11-16 03:10:39 +00:00
|
|
|
const t = useT();
|
2023-05-18 04:05:53 +00:00
|
|
|
const [copied, setCopied] = useCopyTimeout();
|
2024-03-06 10:47:16 +00:00
|
|
|
const setOpen = useProfileDialogStore((store) => store.setOpen);
|
2022-03-31 17:16:30 +00:00
|
|
|
|
|
|
|
return (
|
2023-07-31 16:08:55 +00:00
|
|
|
<TradingDropdownRadioItem value={pk.publicKey}>
|
2024-03-06 10:47:16 +00:00
|
|
|
<div>
|
|
|
|
<div className="flex items-center gap-2">
|
|
|
|
<span>{pk.name ? pk.name : t('Unnamed key')}</span>
|
2023-07-31 16:08:55 +00:00
|
|
|
{' | '}
|
2024-03-06 10:47:16 +00:00
|
|
|
<span className="font-mono">
|
|
|
|
{truncateByChars(pk.publicKey, 3, 3)}
|
|
|
|
</span>
|
2022-10-21 08:34:36 +00:00
|
|
|
<CopyToClipboard text={pk.publicKey} onCopy={() => setCopied(true)}>
|
2022-08-31 04:35:46 +00:00
|
|
|
<button
|
|
|
|
data-testid="copy-vega-public-key"
|
2024-03-06 10:47:16 +00:00
|
|
|
className="relative -top-px"
|
2022-08-31 04:35:46 +00:00
|
|
|
onClick={(e) => e.stopPropagation()}
|
|
|
|
>
|
|
|
|
<span className="sr-only">{t('Copy')}</span>
|
2023-06-29 23:48:53 +00:00
|
|
|
<VegaIcon name={VegaIconNames.COPY} />
|
2022-08-31 04:35:46 +00:00
|
|
|
</button>
|
|
|
|
</CopyToClipboard>
|
2023-07-24 08:37:18 +00:00
|
|
|
{copied && <span className="text-xs">{t('Copied')}</span>}
|
2024-03-06 10:47:16 +00:00
|
|
|
</div>
|
|
|
|
<div
|
|
|
|
className={classNames('flex-1 mr-2 text-secondary text-sm')}
|
|
|
|
data-testid={`key-${pk.publicKey}`}
|
|
|
|
>
|
|
|
|
<Tooltip description={t('Public facing key alias. Click to edit')}>
|
2024-03-08 17:29:41 +00:00
|
|
|
<button
|
|
|
|
data-testid="alias"
|
|
|
|
onClick={() => setOpen(pk.publicKey)}
|
|
|
|
className="flex items-center gap-1"
|
|
|
|
>
|
2024-03-06 10:47:16 +00:00
|
|
|
{alias ? alias : t('No alias')}
|
2024-03-08 17:29:41 +00:00
|
|
|
{isActive && <VegaIcon name={VegaIconNames.EDIT} />}
|
2024-03-06 10:47:16 +00:00
|
|
|
</button>
|
|
|
|
</Tooltip>
|
|
|
|
</div>
|
2022-08-31 04:35:46 +00:00
|
|
|
</div>
|
2023-07-31 16:08:55 +00:00
|
|
|
<TradingDropdownItemIndicator />
|
|
|
|
</TradingDropdownRadioItem>
|
2023-01-17 09:59:12 +00:00
|
|
|
);
|
|
|
|
};
|