@broxus/tvm-connect
Version:
Nekoton-compatible wallets connector.
88 lines (87 loc) • 6.47 kB
JavaScript
import { error, formattedTokenAmount } from '@broxus/js-utils';
import { ExplorerAccountLink, Icon, Skeleton, TokenIcon, WalletAccount } from '@broxus/react-components';
import { Button, Dropdown, Flex, Grid, List, Text } from '@broxus/react-uikit';
import classNames from 'classnames';
import { observer } from 'mobx-react-lite';
import * as React from 'react';
import { useIntl } from 'react-intl';
import { TvmConnectButton } from '../../components/TvmConnectButton';
import { useTvmWalletService } from '../../context';
import { useRecentConnectionMeta } from '../../hooks';
import messages from '../../intl';
import './index.css';
export const TvmConnector = observer((props) => {
const intl = useIntl();
const walletService = useTvmWalletService();
const [recentMeta, setRecentMeta] = useRecentConnectionMeta();
const drop = React.useRef(null);
const { changeAccountButtonText = intl.formatMessage(messages.TVM_CONNECT_CHANGE_ACCOUNT_BTN_TEXT), changeWalletButtonText = intl.formatMessage(messages.TVM_CONNECT_CHANGE_WALLET_BTN_TEXT), className, connectButtonShape, connectButtonText = intl.formatMessage(messages.TVM_CONNECT_CONNECT_BTN_TEXT), connectButtonTrigger, connectButtonType, disconnectButtonText = intl.formatMessage(messages.TVM_CONNECT_DISCONNECT_BTN_TEXT), network = walletService.network, popupType, suffix, showDropMenu = true, showSubIcon = true, standalone = walletService.providers?.length === 1, warnUnsupportedNetwork = true, } = props;
const changeAccount = async () => {
if (walletService.hasProvider) {
drop.current?.close();
try {
await walletService.provider?.changeAccount();
}
catch (e) {
error(e);
}
}
};
const changeWallet = () => {
drop.current?.close();
};
const disconnect = async () => {
if (walletService.hasProvider) {
drop.current?.close();
await walletService.disconnect();
if (recentMeta) {
setRecentMeta({ ...recentMeta, disconnected: true });
}
}
};
const isDisconnected = !walletService.isConnected || walletService.address === undefined;
let subTitle = intl.formatMessage(messages.TVM_CONNECT_NOT_CONNECTED_HINT);
if (walletService.isUnsupportedNetwork && !isDisconnected && warnUnsupportedNetwork) {
subTitle = (React.createElement(Text, { color: "danger" }, intl.formatMessage(messages.TVM_CONNECT_UNSUPPORTED_NETWORK_WARNING)));
}
else if (walletService.isSyncing) {
subTitle = React.createElement(Skeleton, { width: 80 });
}
else if (walletService.isReady) {
subTitle = `${formattedTokenAmount(walletService.balance, network?.currency.decimals ?? walletService.currency.decimals)} ${network?.currency.symbol || walletService.currency.symbol}`;
}
let networkIcon = React.createElement("div", { className: "tvm-connect-connector-icon" }), providerIcon;
if (network?.icon) {
networkIcon = React.createElement(TokenIcon, { iconUrl: network.icon, size: 32 });
}
else if (walletService.network?.icon) {
networkIcon = React.createElement(TokenIcon, { iconUrl: walletService.network.icon, size: 32 });
}
if (walletService.providerInfo?.icon) {
providerIcon = React.createElement(TokenIcon, { iconUrl: walletService.providerInfo.icon, size: 16 });
}
if (walletService.isUnsupportedNetwork && !isDisconnected && warnUnsupportedNetwork) {
networkIcon = React.createElement(Icon, { icon: "danger", ratio: 1.6 });
}
const networkName = network?.shortName ?? walletService.network?.shortName;
return (React.createElement(Flex, { alignItems: "stretch", className: classNames('tvm-connect-connector', className, walletService.providerId), component: Grid },
React.createElement("div", null,
React.createElement(WalletAccount, { address: walletService.address?.toString(), icon: networkIcon, subIcon: !isDisconnected && showSubIcon ? providerIcon : undefined, subTitle: subTitle, title: isDisconnected
? networkName ?? intl.formatMessage(messages.TVM_CONNECT_CONNECTOR_BLOCKCHAIN_NAME)
: (React.createElement(ExplorerAccountLink, { address: walletService.address?.toString(), baseUrl: network?.explorer.baseUrl || walletService.network?.explorer.baseUrl, copyable: true,
/* eslint-disable-next-line max-len */
subPath: network?.explorer.accountsSubPath || walletService.network?.explorer.accountsSubPath, tooltip: intl.formatMessage(messages.TVM_CONNECT_CONNECTOR_EXPLORER_HINT, {
explorerTitle: network?.explorer.title || walletService.network?.explorer.title || '',
}) })) })),
React.createElement(Flex, { alignItems: "center", className: "tvm-connect-connector-suffix" },
suffix,
!isDisconnected && showDropMenu && (React.createElement(Dropdown, { ref: drop, action: ['click'], hideAction: ['click'], overlayClassName: "tvm-connect-dropdown", overlay: (React.createElement(List, { className: "uk-margin-remove", size: "large" },
React.createElement(List.Item, null,
React.createElement(Button, { className: "tvm-connect-change-account", type: "link", onClick: changeAccount }, changeAccountButtonText)),
!standalone && (React.createElement(List.Item, { key: "connect-wallet" },
React.createElement(TvmConnectButton, { className: "tvm-connect-change-wallet", network: network, popupType: popupType, type: "link", onOpen: changeWallet }, changeWalletButtonText))),
React.createElement(List.Item, null,
React.createElement(Button, { "aria-disabled": walletService.isDisconnecting, className: "tvm-connect-logout", disabled: walletService.isDisconnecting, type: "link", onClick: disconnect }, disconnectButtonText)))), placement: "bottom-right" },
React.createElement(Icon, { className: "tvm-connect-dropdown-trigger", icon: "ellipsisVertical", ratio: 0.8 }))),
isDisconnected && (React.createElement(TvmConnectButton, { key: "connect", network: network, popupType: popupType, shape: connectButtonShape, standalone: standalone, trigger: connectButtonTrigger, type: connectButtonType }, connectButtonText)))));
});