@broxus/tvm-connect
Version:
Nekoton-compatible wallets connector.
127 lines (126 loc) • 8.55 kB
JavaScript
"use strict";
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || (function () {
var ownKeys = function(o) {
ownKeys = Object.getOwnPropertyNames || function (o) {
var ar = [];
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
return ar;
};
return ownKeys(o);
};
return function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
__setModuleDefault(result, mod);
return result;
};
})();
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.TvmConnector = void 0;
const js_utils_1 = require("@broxus/js-utils");
const react_components_1 = require("@broxus/react-components");
const react_uikit_1 = require("@broxus/react-uikit");
const classnames_1 = __importDefault(require("classnames"));
const mobx_react_lite_1 = require("mobx-react-lite");
const React = __importStar(require("react"));
const react_intl_1 = require("react-intl");
const TvmConnectButton_1 = require("../../components/TvmConnectButton");
const context_1 = require("../../context");
const hooks_1 = require("../../hooks");
const intl_1 = __importDefault(require("../../intl"));
require("./index.css");
exports.TvmConnector = (0, mobx_react_lite_1.observer)((props) => {
const intl = (0, react_intl_1.useIntl)();
const walletService = (0, context_1.useTvmWalletService)();
const [recentMeta, setRecentMeta] = (0, hooks_1.useRecentConnectionMeta)();
const drop = React.useRef(null);
const { changeAccountButtonText = intl.formatMessage(intl_1.default.TVM_CONNECT_CHANGE_ACCOUNT_BTN_TEXT), changeWalletButtonText = intl.formatMessage(intl_1.default.TVM_CONNECT_CHANGE_WALLET_BTN_TEXT), className, connectButtonShape, connectButtonText = intl.formatMessage(intl_1.default.TVM_CONNECT_CONNECT_BTN_TEXT), connectButtonTrigger, connectButtonType, disconnectButtonText = intl.formatMessage(intl_1.default.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) {
(0, js_utils_1.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(intl_1.default.TVM_CONNECT_NOT_CONNECTED_HINT);
if (walletService.isUnsupportedNetwork && !isDisconnected && warnUnsupportedNetwork) {
subTitle = (React.createElement(react_uikit_1.Text, { color: "danger" }, intl.formatMessage(intl_1.default.TVM_CONNECT_UNSUPPORTED_NETWORK_WARNING)));
}
else if (walletService.isSyncing) {
subTitle = React.createElement(react_components_1.Skeleton, { width: 80 });
}
else if (walletService.isReady) {
subTitle = `${(0, js_utils_1.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(react_components_1.TokenIcon, { iconUrl: network.icon, size: 32 });
}
else if (walletService.network?.icon) {
networkIcon = React.createElement(react_components_1.TokenIcon, { iconUrl: walletService.network.icon, size: 32 });
}
if (walletService.providerInfo?.icon) {
providerIcon = React.createElement(react_components_1.TokenIcon, { iconUrl: walletService.providerInfo.icon, size: 16 });
}
if (walletService.isUnsupportedNetwork && !isDisconnected && warnUnsupportedNetwork) {
networkIcon = React.createElement(react_components_1.Icon, { icon: "danger", ratio: 1.6 });
}
const networkName = network?.shortName ?? walletService.network?.shortName;
return (React.createElement(react_uikit_1.Flex, { alignItems: "stretch", className: (0, classnames_1.default)('tvm-connect-connector', className, walletService.providerId), component: react_uikit_1.Grid },
React.createElement("div", null,
React.createElement(react_components_1.WalletAccount, { address: walletService.address?.toString(), icon: networkIcon, subIcon: !isDisconnected && showSubIcon ? providerIcon : undefined, subTitle: subTitle, title: isDisconnected
? networkName ?? intl.formatMessage(intl_1.default.TVM_CONNECT_CONNECTOR_BLOCKCHAIN_NAME)
: (React.createElement(react_components_1.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(intl_1.default.TVM_CONNECT_CONNECTOR_EXPLORER_HINT, {
explorerTitle: network?.explorer.title || walletService.network?.explorer.title || '',
}) })) })),
React.createElement(react_uikit_1.Flex, { alignItems: "center", className: "tvm-connect-connector-suffix" },
suffix,
!isDisconnected && showDropMenu && (React.createElement(react_uikit_1.Dropdown, { ref: drop, action: ['click'], hideAction: ['click'], overlayClassName: "tvm-connect-dropdown", overlay: (React.createElement(react_uikit_1.List, { className: "uk-margin-remove", size: "large" },
React.createElement(react_uikit_1.List.Item, null,
React.createElement(react_uikit_1.Button, { className: "tvm-connect-change-account", type: "link", onClick: changeAccount }, changeAccountButtonText)),
!standalone && (React.createElement(react_uikit_1.List.Item, { key: "connect-wallet" },
React.createElement(TvmConnectButton_1.TvmConnectButton, { className: "tvm-connect-change-wallet", network: network, popupType: popupType, type: "link", onOpen: changeWallet }, changeWalletButtonText))),
React.createElement(react_uikit_1.List.Item, null,
React.createElement(react_uikit_1.Button, { "aria-disabled": walletService.isDisconnecting, className: "tvm-connect-logout", disabled: walletService.isDisconnecting, type: "link", onClick: disconnect }, disconnectButtonText)))), placement: "bottom-right" },
React.createElement(react_components_1.Icon, { className: "tvm-connect-dropdown-trigger", icon: "ellipsisVertical", ratio: 0.8 }))),
isDisconnected && (React.createElement(TvmConnectButton_1.TvmConnectButton, { key: "connect", network: network, popupType: popupType, shape: connectButtonShape, standalone: standalone, trigger: connectButtonTrigger, type: connectButtonType }, connectButtonText)))));
});