vega-frontend-monorepo/apps/trading/components/vega-wallet-connect-button/vega-wallet-connect-button.tsx
macqbat 37a6217169
market page: break down components to smaller chunks for better performance (#1726)
* chore: break down components to smaller chunks for better performance

* chore: break down components to smaller chunks for better performance

* chore: break down components to smaller chunks for better performance - fix failing tests

* chore: break down components to smaller chunks for better performance - adjust token app cases

* chore: break down components to smaller chunks for better performance - small fixes

* chore: break down components to smaller chunks for better performance - small fixes

* chore: break down components to smaller chunks for better performance - small fixes

* chore: break down components to smaller chunks for better performance - small fixes

* chore: break down components to smaller chunks for better performance - add nwe store for pageTitle

* chore: break down components to smaller chunks for better performance - sm fix

* chore: break down components to smaller chunks for better performance - sm fix

* chore: break down components to smaller chunks for better performance - sm imprv

* chore: break down components to smaller chunks for better performance - change prop names

* chore: break down components to smaller chunks for better performance - fix some test

* chore: break down components to smaller chunks for better performance - change cypress url

* chore: break down components to smaller chunks for better perf - set back redundant changes

* chore: resolve conflicts

Co-authored-by: maciek <maciek@vegaprotocol.io>
2022-10-14 17:42:53 +02:00

114 lines
3.3 KiB
TypeScript

import { t, truncateByChars } from '@vegaprotocol/react-helpers';
import {
Button,
DropdownMenu,
DropdownMenuContent,
DropdownMenuItem,
DropdownMenuItemIndicator,
DropdownMenuRadioGroup,
DropdownMenuRadioItem,
DropdownMenuTrigger,
Icon,
} from '@vegaprotocol/ui-toolkit';
import { useVegaWallet, useVegaWalletDialogStore } from '@vegaprotocol/wallet';
import { useEffect, useMemo, useState } from 'react';
import CopyToClipboard from 'react-copy-to-clipboard';
export const VegaWalletConnectButton = () => {
const [dropdownOpen, setDropdownOpen] = useState(false);
const { openVegaWalletDialog } = useVegaWalletDialogStore((store) => ({
openVegaWalletDialog: store.openVegaWalletDialog,
}));
const { pubKey, pubKeys, selectPubKey, disconnect } = useVegaWallet();
const isConnected = pubKey !== null;
const activeKey = useMemo(() => {
return pubKeys?.find((pk) => pk.publicKey === pubKey);
}, [pubKey, pubKeys]);
if (isConnected && pubKeys) {
return (
<DropdownMenu open={dropdownOpen}>
<DropdownMenuTrigger
data-testid="manage-vega-wallet"
onClick={() => setDropdownOpen((curr) => !curr)}
>
{activeKey && <span className="uppercase">{activeKey.name}</span>}
{': '}
{truncateByChars(pubKey)}
</DropdownMenuTrigger>
<DropdownMenuContent onInteractOutside={() => setDropdownOpen(false)}>
<div className="min-w-[340px]" data-testid="keypair-list">
<DropdownMenuRadioGroup
value={pubKey}
onValueChange={(value) => {
selectPubKey(value);
}}
>
{pubKeys.map((k) => (
<KeypairItem key={k.publicKey} kp={k.publicKey} />
))}
</DropdownMenuRadioGroup>
<DropdownMenuItem data-testid="disconnect" onClick={disconnect}>
{t('Disconnect')}
</DropdownMenuItem>
</div>
</DropdownMenuContent>
</DropdownMenu>
);
}
return (
<Button
data-testid="connect-vega-wallet"
onClick={openVegaWalletDialog}
size="sm"
>
<span className="whitespace-nowrap">{t('Connect Vega wallet')}</span>
</Button>
);
};
const KeypairItem = ({ kp }: { kp: string }) => {
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 (
<DropdownMenuRadioItem key={kp} value={kp}>
<div className="flex-1 mr-2" data-testid={`key-${kp}`}>
<span className="mr-2">
<span>{truncateByChars(kp)}</span>
</span>
<span>
<CopyToClipboard text={kp} 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>
<DropdownMenuItemIndicator />
</DropdownMenuRadioItem>
);
};