2023-01-17 09:59:12 +00:00
|
|
|
import { useCallback, useEffect, useMemo, useState } from 'react';
|
|
|
|
import CopyToClipboard from 'react-copy-to-clipboard';
|
|
|
|
import classNames from 'classnames';
|
2023-02-28 18:56:29 +00:00
|
|
|
import { truncateByChars } from '@vegaprotocol/utils';
|
|
|
|
import { t } from '@vegaprotocol/i18n';
|
2022-08-31 04:35:46 +00:00
|
|
|
import {
|
|
|
|
Button,
|
|
|
|
DropdownMenu,
|
|
|
|
DropdownMenuContent,
|
|
|
|
DropdownMenuItem,
|
|
|
|
DropdownMenuItemIndicator,
|
|
|
|
DropdownMenuRadioGroup,
|
|
|
|
DropdownMenuRadioItem,
|
|
|
|
DropdownMenuTrigger,
|
|
|
|
Icon,
|
2023-01-17 09:59:12 +00:00
|
|
|
Drawer,
|
2023-02-06 19:35:40 +00:00
|
|
|
DropdownMenuSeparator,
|
2022-08-31 04:35:46 +00:00
|
|
|
} from '@vegaprotocol/ui-toolkit';
|
2022-10-21 08:34:36 +00:00
|
|
|
import type { PubKey } from '@vegaprotocol/wallet';
|
2022-10-14 15:42:53 +00:00
|
|
|
import { useVegaWallet, useVegaWalletDialogStore } from '@vegaprotocol/wallet';
|
2023-01-17 09:59:12 +00:00
|
|
|
import { Networks, useEnvironment } from '@vegaprotocol/environment';
|
|
|
|
import { WalletIcon } from '../icons/wallet';
|
2023-02-06 19:35:40 +00:00
|
|
|
import { useTransferDialog } from '@vegaprotocol/accounts';
|
2023-01-17 09:59:12 +00:00
|
|
|
|
|
|
|
const MobileWalletButton = ({
|
|
|
|
isConnected,
|
|
|
|
activeKey,
|
|
|
|
}: {
|
|
|
|
isConnected?: boolean;
|
|
|
|
activeKey?: PubKey;
|
|
|
|
}) => {
|
2023-04-20 11:56:48 +00:00
|
|
|
const { pubKeys, selectPubKey, disconnect, fetchPubKeys } = useVegaWallet();
|
2023-01-17 09:59:12 +00:00
|
|
|
const openVegaWalletDialog = useVegaWalletDialogStore(
|
|
|
|
(store) => store.openVegaWalletDialog
|
|
|
|
);
|
2023-02-06 19:35:40 +00:00
|
|
|
const openTransferDialog = useTransferDialog((store) => store.open);
|
2023-01-17 09:59:12 +00:00
|
|
|
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 {
|
2023-04-20 11:56:48 +00:00
|
|
|
if (fetchPubKeys) {
|
|
|
|
fetchPubKeys();
|
|
|
|
}
|
2023-01-17 09:59:12 +00:00
|
|
|
setDrawerOpen(!drawerOpen);
|
|
|
|
}
|
2023-04-20 11:56:48 +00:00
|
|
|
}, [drawerOpen, fetchPubKeys, isConnected, openVegaWalletDialog]);
|
2023-01-17 09:59:12 +00:00
|
|
|
|
|
|
|
const iconClass = drawerOpen
|
|
|
|
? 'hidden'
|
|
|
|
: isYellow
|
|
|
|
? 'fill-black'
|
2023-03-10 15:46:51 +00:00
|
|
|
: 'fill-black dark:fill-white';
|
2023-01-17 09:59:12 +00:00
|
|
|
const [container, setContainer] = useState<HTMLElement | null>(null);
|
|
|
|
|
|
|
|
const walletButton = (
|
|
|
|
<button
|
|
|
|
className="my-2 transition-all flex flex-col justify-around gap-3 p-2 relative h-[34px]"
|
|
|
|
onClick={openDrawer}
|
|
|
|
data-testid="connect-vega-wallet-mobile"
|
|
|
|
>
|
|
|
|
<WalletIcon className={iconClass} />
|
|
|
|
</button>
|
|
|
|
);
|
|
|
|
const onSelectItem = useCallback(
|
|
|
|
(pubkey: string) => {
|
|
|
|
setDrawerOpen(false);
|
|
|
|
selectPubKey(pubkey);
|
|
|
|
},
|
|
|
|
[selectPubKey]
|
|
|
|
);
|
|
|
|
return (
|
2023-01-18 17:39:55 +00:00
|
|
|
<div className="lg:hidden overflow-hidden flex" ref={setContainer}>
|
2023-01-17 09:59:12 +00:00
|
|
|
<Drawer
|
|
|
|
dataTestId="wallets-drawer"
|
|
|
|
open={drawerOpen}
|
|
|
|
onChange={setDrawerOpen}
|
|
|
|
container={container}
|
|
|
|
trigger={walletButton}
|
|
|
|
>
|
|
|
|
<div className="border-l border-default p-2 gap-4 flex flex-col w-full h-full bg-white dark:bg-black dark:text-white justify-between">
|
|
|
|
<div className="flex h-5 justify-end">
|
|
|
|
<button
|
|
|
|
className="transition-all flex flex-col justify-around gap-3 p-2 relative h-[34px]"
|
|
|
|
onClick={() => setDrawerOpen(false)}
|
|
|
|
data-testid="connect-vega-wallet-mobile-close"
|
|
|
|
>
|
|
|
|
<>
|
|
|
|
<div
|
|
|
|
className={classNames(
|
|
|
|
'w-[26px] h-[2px] bg-black dark:bg-white transition-all translate-y-[7.5px] rotate-45',
|
|
|
|
{
|
|
|
|
hidden: !drawerOpen,
|
|
|
|
}
|
|
|
|
)}
|
|
|
|
/>
|
|
|
|
<div
|
|
|
|
className={classNames(
|
|
|
|
'w-[26px] h-[2px] bg-black dark:bg-white transition-all -translate-y-[7.5px] -rotate-45',
|
|
|
|
{
|
|
|
|
hidden: !drawerOpen,
|
|
|
|
}
|
|
|
|
)}
|
|
|
|
/>
|
|
|
|
</>
|
|
|
|
</button>
|
|
|
|
</div>
|
|
|
|
<div className="grow my-4" role="list">
|
|
|
|
{(pubKeys || []).map((pk) => (
|
|
|
|
<KeypairListItem
|
|
|
|
key={pk.publicKey}
|
|
|
|
pk={pk}
|
|
|
|
isActive={activeKey?.publicKey === pk.publicKey}
|
|
|
|
onSelectItem={onSelectItem}
|
|
|
|
/>
|
|
|
|
))}
|
|
|
|
</div>
|
2023-02-06 19:35:40 +00:00
|
|
|
<div className="flex flex-col gap-2 m-4">
|
|
|
|
<Button
|
|
|
|
onClick={() => {
|
|
|
|
setDrawerOpen(false);
|
|
|
|
openTransferDialog(true);
|
|
|
|
}}
|
|
|
|
fill
|
|
|
|
>
|
|
|
|
{t('Transfer')}
|
|
|
|
</Button>
|
2023-01-17 09:59:12 +00:00
|
|
|
<Button onClick={mobileDisconnect} fill>
|
|
|
|
{t('Disconnect')}
|
|
|
|
</Button>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
</Drawer>
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
};
|
2022-03-31 17:16:30 +00:00
|
|
|
|
2022-10-14 15:42:53 +00:00
|
|
|
export const VegaWalletConnectButton = () => {
|
2022-08-31 04:35:46 +00:00
|
|
|
const [dropdownOpen, setDropdownOpen] = useState(false);
|
2023-01-17 09:59:12 +00:00
|
|
|
const openVegaWalletDialog = useVegaWalletDialogStore(
|
|
|
|
(store) => store.openVegaWalletDialog
|
|
|
|
);
|
2023-02-06 19:35:40 +00:00
|
|
|
const openTransferDialog = useTransferDialog((store) => store.open);
|
2023-04-20 11:56:48 +00:00
|
|
|
const {
|
|
|
|
pubKey,
|
|
|
|
pubKeys,
|
|
|
|
selectPubKey,
|
|
|
|
disconnect,
|
|
|
|
isReadOnly,
|
|
|
|
fetchPubKeys,
|
|
|
|
} = useVegaWallet();
|
2022-10-03 18:12:34 +00:00
|
|
|
const isConnected = pubKey !== null;
|
2022-03-31 17:16:30 +00:00
|
|
|
|
2022-10-03 18:12:34 +00:00
|
|
|
const activeKey = useMemo(() => {
|
|
|
|
return pubKeys?.find((pk) => pk.publicKey === pubKey);
|
|
|
|
}, [pubKey, pubKeys]);
|
|
|
|
|
|
|
|
if (isConnected && pubKeys) {
|
2022-08-31 04:35:46 +00:00
|
|
|
return (
|
2023-01-17 09:59:12 +00:00
|
|
|
<>
|
2023-01-18 17:39:55 +00:00
|
|
|
<div className="hidden lg:block">
|
2023-01-26 08:52:49 +00:00
|
|
|
<DropdownMenu
|
|
|
|
open={dropdownOpen}
|
|
|
|
trigger={
|
|
|
|
<DropdownMenuTrigger
|
|
|
|
data-testid="manage-vega-wallet"
|
2023-04-20 11:56:48 +00:00
|
|
|
onClick={() => {
|
|
|
|
if (fetchPubKeys) {
|
|
|
|
fetchPubKeys();
|
|
|
|
}
|
|
|
|
setDropdownOpen(!dropdownOpen);
|
|
|
|
}}
|
2023-01-26 08:52:49 +00:00
|
|
|
>
|
|
|
|
{activeKey && (
|
|
|
|
<span className="uppercase">{activeKey.name}</span>
|
|
|
|
)}
|
|
|
|
{': '}
|
|
|
|
{truncateByChars(pubKey)}
|
|
|
|
</DropdownMenuTrigger>
|
|
|
|
}
|
|
|
|
>
|
2023-01-17 09:59:12 +00:00
|
|
|
<DropdownMenuContent
|
|
|
|
onInteractOutside={() => setDropdownOpen(false)}
|
|
|
|
>
|
|
|
|
<div className="min-w-[340px]" data-testid="keypair-list">
|
|
|
|
<DropdownMenuRadioGroup
|
|
|
|
value={pubKey}
|
|
|
|
onValueChange={(value) => {
|
|
|
|
selectPubKey(value);
|
|
|
|
}}
|
|
|
|
>
|
|
|
|
{pubKeys.map((pk) => (
|
|
|
|
<KeypairItem key={pk.publicKey} pk={pk} />
|
|
|
|
))}
|
|
|
|
</DropdownMenuRadioGroup>
|
2023-02-06 19:35:40 +00:00
|
|
|
<DropdownMenuSeparator />
|
2023-03-14 14:39:47 +00:00
|
|
|
{!isReadOnly && (
|
|
|
|
<DropdownMenuItem
|
|
|
|
data-testid="wallet-transfer"
|
|
|
|
onClick={() => openTransferDialog(true)}
|
|
|
|
>
|
|
|
|
{t('Transfer')}
|
|
|
|
</DropdownMenuItem>
|
|
|
|
)}
|
2023-01-17 09:59:12 +00:00
|
|
|
<DropdownMenuItem data-testid="disconnect" onClick={disconnect}>
|
|
|
|
{t('Disconnect')}
|
|
|
|
</DropdownMenuItem>
|
|
|
|
</div>
|
|
|
|
</DropdownMenuContent>
|
|
|
|
</DropdownMenu>
|
|
|
|
</div>
|
|
|
|
<MobileWalletButton isConnected activeKey={activeKey} />
|
|
|
|
</>
|
2022-08-31 04:35:46 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
return (
|
2023-01-17 09:59:12 +00:00
|
|
|
<>
|
|
|
|
<Button
|
|
|
|
data-testid="connect-vega-wallet"
|
|
|
|
onClick={openVegaWalletDialog}
|
|
|
|
size="sm"
|
2023-01-18 17:39:55 +00:00
|
|
|
className="hidden lg:block"
|
2023-01-17 09:59:12 +00:00
|
|
|
>
|
|
|
|
<span className="whitespace-nowrap">{t('Connect Vega wallet')}</span>
|
|
|
|
</Button>
|
|
|
|
<MobileWalletButton />
|
|
|
|
</>
|
2022-08-31 04:35:46 +00:00
|
|
|
);
|
|
|
|
};
|
|
|
|
|
2022-10-21 08:34:36 +00:00
|
|
|
const KeypairItem = ({ pk }: { pk: PubKey }) => {
|
2022-08-31 04:35:46 +00:00
|
|
|
const [copied, setCopied] = useState(false);
|
|
|
|
useEffect(() => {
|
|
|
|
// eslint-disable-next-line
|
|
|
|
let timeout: any;
|
|
|
|
|
|
|
|
if (copied) {
|
|
|
|
timeout = setTimeout(() => {
|
|
|
|
setCopied(false);
|
|
|
|
}, 800);
|
2022-03-31 17:16:30 +00:00
|
|
|
}
|
2022-08-31 04:35:46 +00:00
|
|
|
|
|
|
|
return () => {
|
|
|
|
clearTimeout(timeout);
|
|
|
|
};
|
|
|
|
}, [copied]);
|
2022-03-31 17:16:30 +00:00
|
|
|
|
|
|
|
return (
|
2022-10-21 08:34:36 +00:00
|
|
|
<DropdownMenuRadioItem value={pk.publicKey}>
|
|
|
|
<div className="flex-1 mr-2" data-testid={`key-${pk.publicKey}`}>
|
2022-08-31 04:35:46 +00:00
|
|
|
<span className="mr-2">
|
2022-10-21 08:34:36 +00:00
|
|
|
<span>
|
|
|
|
<span className="uppercase">{pk.name}</span>:{' '}
|
|
|
|
{truncateByChars(pk.publicKey)}
|
|
|
|
</span>
|
2022-08-31 04:35:46 +00:00
|
|
|
</span>
|
|
|
|
<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"
|
|
|
|
onClick={(e) => e.stopPropagation()}
|
|
|
|
>
|
|
|
|
<span className="sr-only">{t('Copy')}</span>
|
|
|
|
<Icon name="duplicate" className="mr-2" />
|
|
|
|
</button>
|
|
|
|
</CopyToClipboard>
|
|
|
|
{copied && (
|
|
|
|
<span className="text-xs text-neutral-500">{t('Copied')}</span>
|
|
|
|
)}
|
2022-07-14 16:03:17 +00:00
|
|
|
</span>
|
2022-08-31 04:35:46 +00:00
|
|
|
</div>
|
|
|
|
<DropdownMenuItemIndicator />
|
|
|
|
</DropdownMenuRadioItem>
|
2022-03-31 17:16:30 +00:00
|
|
|
);
|
|
|
|
};
|
2023-01-17 09:59:12 +00:00
|
|
|
|
|
|
|
const KeypairListItem = ({
|
|
|
|
pk,
|
|
|
|
isActive,
|
|
|
|
onSelectItem,
|
|
|
|
}: {
|
|
|
|
pk: PubKey;
|
|
|
|
isActive: boolean;
|
|
|
|
onSelectItem: (pk: string) => void;
|
|
|
|
}) => {
|
|
|
|
const [copied, setCopied] = useState(false);
|
|
|
|
useEffect(() => {
|
|
|
|
// eslint-disable-next-line
|
|
|
|
let timeout: any;
|
|
|
|
|
|
|
|
if (copied) {
|
|
|
|
timeout = setTimeout(() => {
|
|
|
|
setCopied(false);
|
|
|
|
}, 800);
|
|
|
|
}
|
|
|
|
|
|
|
|
return () => {
|
|
|
|
clearTimeout(timeout);
|
|
|
|
};
|
|
|
|
}, [copied]);
|
|
|
|
|
|
|
|
return (
|
|
|
|
<div
|
|
|
|
className="flex flex-col w-full ml-4 mr-2 mb-4"
|
|
|
|
data-testid={`key-${pk.publicKey}-mobile`}
|
|
|
|
>
|
|
|
|
<span className="mr-2">
|
|
|
|
<button onClick={() => onSelectItem(pk.publicKey)}>
|
|
|
|
<span className="uppercase">{pk.name}</span>
|
|
|
|
</button>
|
|
|
|
{isActive && <Icon name="tick" className="ml-2" />}
|
|
|
|
</span>
|
|
|
|
<span className="text-neutral-500 dark:text-neutral-400">
|
|
|
|
{truncateByChars(pk.publicKey)}{' '}
|
|
|
|
<CopyToClipboard text={pk.publicKey} onCopy={() => setCopied(true)}>
|
|
|
|
<button
|
|
|
|
data-testid="copy-vega-public-key"
|
|
|
|
onClick={(e) => e.stopPropagation()}
|
|
|
|
>
|
|
|
|
<span className="sr-only">{t('Copy')}</span>
|
|
|
|
<Icon name="duplicate" className="mr-2" />
|
|
|
|
</button>
|
|
|
|
</CopyToClipboard>
|
|
|
|
{copied && (
|
|
|
|
<span className="text-xs text-neutral-500">{t('Copied')}</span>
|
|
|
|
)}
|
|
|
|
</span>
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
};
|