2022-08-31 04:35:46 +00:00
|
|
|
import { t, truncateByChars } from '@vegaprotocol/react-helpers';
|
|
|
|
import {
|
|
|
|
Button,
|
|
|
|
DropdownMenu,
|
|
|
|
DropdownMenuContent,
|
|
|
|
DropdownMenuItem,
|
|
|
|
DropdownMenuItemIndicator,
|
|
|
|
DropdownMenuRadioGroup,
|
|
|
|
DropdownMenuRadioItem,
|
|
|
|
DropdownMenuTrigger,
|
|
|
|
Icon,
|
|
|
|
} from '@vegaprotocol/ui-toolkit';
|
2022-03-31 17:16:30 +00:00
|
|
|
import { useVegaWallet } from '@vegaprotocol/wallet';
|
2022-10-03 18:12:34 +00:00
|
|
|
import { useEffect, useMemo, useState } from 'react';
|
2022-08-31 04:35:46 +00:00
|
|
|
import CopyToClipboard from 'react-copy-to-clipboard';
|
2022-03-31 17:16:30 +00:00
|
|
|
|
|
|
|
export interface VegaWalletConnectButtonProps {
|
|
|
|
setConnectDialog: (isOpen: boolean) => void;
|
|
|
|
}
|
|
|
|
|
|
|
|
export const VegaWalletConnectButton = ({
|
|
|
|
setConnectDialog,
|
|
|
|
}: VegaWalletConnectButtonProps) => {
|
2022-08-31 04:35:46 +00:00
|
|
|
const [dropdownOpen, setDropdownOpen] = useState(false);
|
2022-10-03 18:12:34 +00:00
|
|
|
const { pubKey, pubKeys, selectPubKey, disconnect } = useVegaWallet();
|
|
|
|
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 (
|
|
|
|
<DropdownMenu open={dropdownOpen}>
|
|
|
|
<DropdownMenuTrigger
|
|
|
|
data-testid="manage-vega-wallet"
|
|
|
|
onClick={() => setDropdownOpen((curr) => !curr)}
|
|
|
|
>
|
2022-10-03 18:12:34 +00:00
|
|
|
{activeKey && <span className="uppercase">{activeKey.name}</span>}
|
|
|
|
{': '}
|
|
|
|
{truncateByChars(pubKey)}
|
2022-08-31 04:35:46 +00:00
|
|
|
</DropdownMenuTrigger>
|
|
|
|
<DropdownMenuContent onInteractOutside={() => setDropdownOpen(false)}>
|
|
|
|
<div className="min-w-[340px]" data-testid="keypair-list">
|
|
|
|
<DropdownMenuRadioGroup
|
2022-10-03 18:12:34 +00:00
|
|
|
value={pubKey}
|
2022-08-31 04:35:46 +00:00
|
|
|
onValueChange={(value) => {
|
2022-10-03 18:12:34 +00:00
|
|
|
selectPubKey(value);
|
2022-08-31 04:35:46 +00:00
|
|
|
}}
|
|
|
|
>
|
2022-10-03 18:12:34 +00:00
|
|
|
{pubKeys.map((k) => (
|
|
|
|
<KeypairItem key={k.publicKey} kp={k.publicKey} />
|
2022-08-31 04:35:46 +00:00
|
|
|
))}
|
|
|
|
</DropdownMenuRadioGroup>
|
2022-08-31 10:47:08 +00:00
|
|
|
<DropdownMenuItem data-testid="disconnect" onClick={disconnect}>
|
2022-08-31 04:35:46 +00:00
|
|
|
{t('Disconnect')}
|
|
|
|
</DropdownMenuItem>
|
|
|
|
</div>
|
|
|
|
</DropdownMenuContent>
|
|
|
|
</DropdownMenu>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
return (
|
|
|
|
<Button
|
|
|
|
data-testid="connect-vega-wallet"
|
|
|
|
onClick={() => setConnectDialog(true)}
|
|
|
|
size="sm"
|
|
|
|
>
|
|
|
|
<span className="whitespace-nowrap">{t('Connect Vega wallet')}</span>
|
|
|
|
</Button>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
2022-10-03 18:12:34 +00:00
|
|
|
const KeypairItem = ({ kp }: { kp: string }) => {
|
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-03 18:12:34 +00:00
|
|
|
<DropdownMenuRadioItem key={kp} value={kp}>
|
|
|
|
<div className="flex-1 mr-2" data-testid={`key-${kp}`}>
|
2022-08-31 04:35:46 +00:00
|
|
|
<span className="mr-2">
|
2022-10-03 18:12:34 +00:00
|
|
|
<span>{truncateByChars(kp)}</span>
|
2022-08-31 04:35:46 +00:00
|
|
|
</span>
|
|
|
|
<span>
|
2022-10-03 18:12:34 +00:00
|
|
|
<CopyToClipboard text={kp} 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
|
|
|
);
|
|
|
|
};
|