1
0
mirror of https://github.com/kremalicious/metamask-extension.git synced 2024-12-23 09:52:26 +01:00

Adding UI helpers for URL parsing (#11899)

This commit is contained in:
ryanml 2021-08-24 15:28:16 -07:00 committed by GitHub
parent b7009ac454
commit 472cf17bda
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
19 changed files with 62 additions and 65 deletions

View File

@ -15,7 +15,7 @@ import {
getSelectedAddress, getSelectedAddress,
getSelectedIdentity, getSelectedIdentity,
} from '../../../../selectors'; } from '../../../../selectors';
import { isExtensionUrl } from '../../../../helpers/utils/util'; import { isExtensionUrl, getURLHost } from '../../../../helpers/utils/util';
import Popover from '../../../ui/popover'; import Popover from '../../../ui/popover';
import Button from '../../../ui/button'; import Button from '../../../ui/button';
import Checkbox from '../../../ui/check-box'; import Checkbox from '../../../ui/check-box';
@ -88,7 +88,7 @@ const UnconnectedAccountAlert = () => {
return ( return (
<Popover <Popover
title={ title={
isExtensionUrl(origin) ? t('currentExtension') : new URL(origin).host isExtensionUrl(origin) ? t('currentExtension') : getURLHost(origin)
} }
subtitle={t('currentAccountNotConnected')} subtitle={t('currentAccountNotConnected')}
onClose={onClose} onClose={onClose}

View File

@ -6,6 +6,7 @@ import { getAccountLink } from '@metamask/etherscan-link';
import { showModal } from '../../../store/actions'; import { showModal } from '../../../store/actions';
import { CONNECTED_ROUTE } from '../../../helpers/constants/routes'; import { CONNECTED_ROUTE } from '../../../helpers/constants/routes';
import { getURLHostName } from '../../../helpers/utils/util';
import { Menu, MenuItem } from '../../ui/menu'; import { Menu, MenuItem } from '../../ui/menu';
import { import {
getCurrentChainId, getCurrentChainId,
@ -33,14 +34,7 @@ export default function AccountOptionsMenu({ anchorElement, onClose }) {
const { address } = selectedIdentity; const { address } = selectedIdentity;
const addressLink = getAccountLink(address, chainId, rpcPrefs); const addressLink = getAccountLink(address, chainId, rpcPrefs);
const { blockExplorerUrl } = rpcPrefs; const { blockExplorerUrl } = rpcPrefs;
const blockExplorerUrlSubTitle = getURLHostName(blockExplorerUrl);
const getBlockExplorerUrlHost = () => {
try {
return new URL(blockExplorerUrl)?.hostname;
} catch (err) {
return '';
}
};
const openFullscreenEvent = useMetricEvent({ const openFullscreenEvent = useMetricEvent({
eventOpts: { eventOpts: {
@ -71,12 +65,11 @@ export default function AccountOptionsMenu({ anchorElement, onClose }) {
properties: { properties: {
link_type: 'Account Tracker', link_type: 'Account Tracker',
action: 'Account Options', action: 'Account Options',
block_explorer_domain: addressLink ? new URL(addressLink)?.hostname : '', block_explorer_domain: getURLHostName(addressLink),
}, },
}); });
const isRemovable = keyring.type !== 'HD Key Tree'; const isRemovable = keyring.type !== 'HD Key Tree';
const blockExplorerUrlSubTitle = getBlockExplorerUrlHost();
return ( return (
<Menu <Menu

View File

@ -6,6 +6,7 @@ import AccountModalContainer from '../account-modal-container';
import QrView from '../../../ui/qr-code'; import QrView from '../../../ui/qr-code';
import EditableLabel from '../../../ui/editable-label'; import EditableLabel from '../../../ui/editable-label';
import Button from '../../../ui/button'; import Button from '../../../ui/button';
import { getURLHostName } from '../../../../helpers/utils/util';
export default class AccountDetailsModal extends Component { export default class AccountDetailsModal extends Component {
static propTypes = { static propTypes = {
@ -70,9 +71,7 @@ export default class AccountDetailsModal extends Component {
properties: { properties: {
link_type: 'Account Tracker', link_type: 'Account Tracker',
action: 'Account Details Modal', action: 'Account Details Modal',
block_explorer_domain: accountLink block_explorer_domain: getURLHostName(accountLink),
? new URL(accountLink)?.hostname
: '',
}, },
}); });
global.platform.openTab({ global.platform.openTab({

View File

@ -2,7 +2,7 @@ import React, { Component } from 'react';
import PropTypes from 'prop-types'; import PropTypes from 'prop-types';
import { getAccountLink } from '@metamask/etherscan-link'; import { getAccountLink } from '@metamask/etherscan-link';
import Modal from '../../modal'; import Modal from '../../modal';
import { addressSummary } from '../../../../helpers/utils/util'; import { addressSummary, getURLHostName } from '../../../../helpers/utils/util';
import Identicon from '../../../ui/identicon'; import Identicon from '../../../ui/identicon';
export default class ConfirmRemoveAccount extends Component { export default class ConfirmRemoveAccount extends Component {
@ -66,9 +66,7 @@ export default class ConfirmRemoveAccount extends Component {
properties: { properties: {
link_type: 'Account Tracker', link_type: 'Account Tracker',
action: 'Remove Account', action: 'Remove Account',
block_explorer_domain: accountLink block_explorer_domain: getURLHostName(accountLink),
? new URL(accountLink)?.hostname
: '',
}, },
}); });
global.platform.openTab({ global.platform.openTab({

View File

@ -7,6 +7,7 @@ import { ENVIRONMENT_TYPE_FULLSCREEN } from '../../../../../shared/constants/app
import { SECOND } from '../../../../../shared/constants/time'; import { SECOND } from '../../../../../shared/constants/time';
import Spinner from '../../../ui/spinner'; import Spinner from '../../../ui/spinner';
import WebcamUtils from '../../../../helpers/utils/webcam-utils'; import WebcamUtils from '../../../../helpers/utils/webcam-utils';
import { getURL } from '../../../../helpers/utils/util';
import PageContainerFooter from '../../../ui/page-container/page-container-footer/page-container-footer.component'; import PageContainerFooter from '../../../ui/page-container/page-container-footer/page-container-footer.component';
const READY_STATE = { const READY_STATE = {
@ -68,8 +69,8 @@ export default class QrScanner extends Component {
!environmentReady && !environmentReady &&
getEnvironmentType() !== ENVIRONMENT_TYPE_FULLSCREEN getEnvironmentType() !== ENVIRONMENT_TYPE_FULLSCREEN
) { ) {
const currentUrl = new URL(window.location.href); const currentUrl = getURL(window.location.href);
const currentHash = currentUrl.hash; const currentHash = currentUrl?.hash;
const currentRoute = currentHash ? currentHash.substring(1) : null; const currentRoute = currentHash ? currentHash.substring(1) : null;
global.platform.openExtensionInBrowser(currentRoute); global.platform.openExtensionInBrowser(currentRoute);
} }

View File

@ -7,7 +7,7 @@ import {
getEthConversionFromWeiHex, getEthConversionFromWeiHex,
getValueFromWeiHex, getValueFromWeiHex,
} from '../../../helpers/utils/conversions.util'; } from '../../../helpers/utils/conversions.util';
import { formatDate } from '../../../helpers/utils/util'; import { formatDate, getURLHostName } from '../../../helpers/utils/util';
import TransactionActivityLogIcon from './transaction-activity-log-icon'; import TransactionActivityLogIcon from './transaction-activity-log-icon';
import { CONFIRMED_STATUS } from './transaction-activity-log.constants'; import { CONFIRMED_STATUS } from './transaction-activity-log.constants';
@ -41,9 +41,7 @@ export default class TransactionActivityLog extends PureComponent {
properties: { properties: {
link_type: 'Transaction Block Explorer', link_type: 'Transaction Block Explorer',
action: 'Activity Details', action: 'Activity Details',
block_explorer_domain: etherscanUrl block_explorer_domain: getURLHostName(etherscanUrl),
? new URL(etherscanUrl)?.hostname
: '',
}, },
}); });

View File

@ -12,6 +12,7 @@ import Copy from '../../ui/icon/copy-icon.component';
import Popover from '../../ui/popover'; import Popover from '../../ui/popover';
import { SECOND } from '../../../../shared/constants/time'; import { SECOND } from '../../../../shared/constants/time';
import { TRANSACTION_TYPES } from '../../../../shared/constants/transaction'; import { TRANSACTION_TYPES } from '../../../../shared/constants/transaction';
import { getURLHostName } from '../../../helpers/utils/util';
export default class TransactionListItemDetails extends PureComponent { export default class TransactionListItemDetails extends PureComponent {
static contextTypes = { static contextTypes = {
@ -65,9 +66,7 @@ export default class TransactionListItemDetails extends PureComponent {
properties: { properties: {
link_type: 'Transaction Block Explorer', link_type: 'Transaction Block Explorer',
action: 'Transaction Details', action: 'Transaction Details',
block_explorer_domain: blockExplorerLink block_explorer_domain: getURLHostName(blockExplorerLink),
? new URL(blockExplorerLink)?.hostname
: '',
}, },
}); });

View File

@ -379,3 +379,19 @@ export function bnLessThanEqualTo(a, b) {
} }
return new BigNumber(a, 10).lte(b, 10); return new BigNumber(a, 10).lte(b, 10);
} }
export function getURL(url) {
try {
return new URL(url);
} catch (err) {
return '';
}
}
export function getURLHost(url) {
return getURL(url)?.host || '';
}
export function getURLHostName(url) {
return getURL(url)?.hostname || '';
}

View File

@ -1,7 +1,10 @@
import React, { Component } from 'react'; import React, { Component } from 'react';
import PropTypes from 'prop-types'; import PropTypes from 'prop-types';
import { getTokenTrackerLink } from '@metamask/etherscan-link'; import { getTokenTrackerLink } from '@metamask/etherscan-link';
import { checkExistingAddresses } from '../../helpers/utils/util'; import {
checkExistingAddresses,
getURLHostName,
} from '../../helpers/utils/util';
import { tokenInfoGetter } from '../../helpers/utils/token-util'; import { tokenInfoGetter } from '../../helpers/utils/token-util';
import { CONFIRM_ADD_TOKEN_ROUTE } from '../../helpers/constants/routes'; import { CONFIRM_ADD_TOKEN_ROUTE } from '../../helpers/constants/routes';
import TextField from '../../components/ui/text-field'; import TextField from '../../components/ui/text-field';
@ -262,7 +265,7 @@ class AddToken extends Component {
{ blockExplorerUrl: rpcPrefs?.blockExplorerUrl ?? null }, { blockExplorerUrl: rpcPrefs?.blockExplorerUrl ?? null },
); );
const blockExplorerLabel = rpcPrefs?.blockExplorerUrl const blockExplorerLabel = rpcPrefs?.blockExplorerUrl
? new URL(blockExplorerTokenLink).hostname ? getURLHostName(blockExplorerTokenLink)
: this.context.t('etherscan'); : this.context.t('etherscan');
return ( return (

View File

@ -13,6 +13,7 @@ import {
} from '../../../selectors/selectors'; } from '../../../selectors/selectors';
import { showModal } from '../../../store/actions'; import { showModal } from '../../../store/actions';
import { DEFAULT_ROUTE } from '../../../helpers/constants/routes'; import { DEFAULT_ROUTE } from '../../../helpers/constants/routes';
import { getURLHostName } from '../../../helpers/utils/util';
import { useNewMetricEvent } from '../../../hooks/useMetricEvent'; import { useNewMetricEvent } from '../../../hooks/useMetricEvent';
import AssetNavigation from './asset-navigation'; import AssetNavigation from './asset-navigation';
import AssetOptions from './asset-options'; import AssetOptions from './asset-options';
@ -35,7 +36,7 @@ export default function NativeAsset({ nativeCurrency }) {
properties: { properties: {
link_type: 'Account Tracker', link_type: 'Account Tracker',
action: 'Asset Options', action: 'Asset Options',
block_explorer_domain: accountLink ? new URL(accountLink)?.hostname : '', block_explorer_domain: getURLHostName(accountLink),
}, },
}); });

View File

@ -11,6 +11,7 @@ import {
getRpcPrefsForCurrentProvider, getRpcPrefsForCurrentProvider,
} from '../../../selectors/selectors'; } from '../../../selectors/selectors';
import { DEFAULT_ROUTE } from '../../../helpers/constants/routes'; import { DEFAULT_ROUTE } from '../../../helpers/constants/routes';
import { getURLHostName } from '../../../helpers/utils/util';
import { showModal } from '../../../store/actions'; import { showModal } from '../../../store/actions';
import { useNewMetricEvent } from '../../../hooks/useMetricEvent'; import { useNewMetricEvent } from '../../../hooks/useMetricEvent';
@ -39,9 +40,7 @@ export default function TokenAsset({ token }) {
properties: { properties: {
link_type: 'Token Tracker', link_type: 'Token Tracker',
action: 'Token Options', action: 'Token Options',
block_explorer_domain: tokenTrackerLink block_explorer_domain: getURLHostName(tokenTrackerLink),
? new URL(tokenTrackerLink)?.hostname
: '',
}, },
}); });

View File

@ -2,7 +2,7 @@ import React, { Component } from 'react';
import PropTypes from 'prop-types'; import PropTypes from 'prop-types';
import classnames from 'classnames'; import classnames from 'classnames';
import UrlIcon from '../../../components/ui/url-icon'; import UrlIcon from '../../../components/ui/url-icon';
import { addressSummary } from '../../../helpers/utils/util'; import { addressSummary, getURLHostName } from '../../../helpers/utils/util';
import { formatCurrency } from '../../../helpers/utils/confirm-tx.util'; import { formatCurrency } from '../../../helpers/utils/confirm-tx.util';
import { ConfirmPageContainerWarning } from '../../../components/app/confirm-page-container/confirm-page-container-content'; import { ConfirmPageContainerWarning } from '../../../components/app/confirm-page-container/confirm-page-container-content';
import Typography from '../../../components/ui/typography'; import Typography from '../../../components/ui/typography';
@ -256,7 +256,7 @@ export default class ConfirmApproveContent extends Component {
<UrlIcon <UrlIcon
className="confirm-approve-content__identicon" className="confirm-approve-content__identicon"
fallbackClassName="confirm-approve-content__identicon" fallbackClassName="confirm-approve-content__identicon"
name={origin ? new URL(origin).hostname : ''} name={getURLHostName(origin)}
url={siteImage} url={siteImage}
/> />
</div> </div>

View File

@ -9,6 +9,7 @@ import {
hexToDecimal, hexToDecimal,
hexWEIToDecGWEI, hexWEIToDecGWEI,
} from '../../helpers/utils/conversions.util'; } from '../../helpers/utils/conversions.util';
import { getURLHostName } from '../../helpers/utils/util';
import { import {
CONFIRM_TRANSACTION_ROUTE, CONFIRM_TRANSACTION_ROUTE,
DEFAULT_ROUTE, DEFAULT_ROUTE,
@ -300,14 +301,6 @@ export default class ConfirmTransactionBase extends Component {
} = this.props; } = this.props;
const { t } = this.context; const { t } = this.context;
const getRequestingOrigin = () => {
try {
return new URL(txData.origin)?.hostname;
} catch (err) {
return '';
}
};
const renderTotalMaxAmount = () => { const renderTotalMaxAmount = () => {
if ( if (
primaryTotalTextOverrideMaxAmount === undefined && primaryTotalTextOverrideMaxAmount === undefined &&
@ -409,7 +402,7 @@ export default class ConfirmTransactionBase extends Component {
txData.dappSuggestedGasFees ? ( txData.dappSuggestedGasFees ? (
<> <>
{t('transactionDetailDappGasHeading', [ {t('transactionDetailDappGasHeading', [
getRequestingOrigin(), getURLHostName(txData?.origin),
])} ])}
<InfoTooltip <InfoTooltip
contentText={t('transactionDetailDappGasTooltip')} contentText={t('transactionDetailDappGasTooltip')}

View File

@ -3,6 +3,7 @@ import React, { PureComponent } from 'react';
import Popover from '../../components/ui/popover'; import Popover from '../../components/ui/popover';
import ConnectedAccountsList from '../../components/app/connected-accounts-list'; import ConnectedAccountsList from '../../components/app/connected-accounts-list';
import ConnectedAccountsPermissions from '../../components/app/connected-accounts-permissions'; import ConnectedAccountsPermissions from '../../components/app/connected-accounts-permissions';
import { getURLHost } from '../../helpers/utils/util';
export default class ConnectedAccounts extends PureComponent { export default class ConnectedAccounts extends PureComponent {
static contextTypes = { static contextTypes = {
@ -54,7 +55,7 @@ export default class ConnectedAccounts extends PureComponent {
title={ title={
isActiveTabExtension isActiveTabExtension
? t('currentExtension') ? t('currentExtension')
: new URL(activeTabOrigin).host : getURLHost(activeTabOrigin)
} }
subtitle={ subtitle={
connectedAccounts.length connectedAccounts.length

View File

@ -7,6 +7,8 @@ import Checkbox from '../../../components/ui/check-box';
import Dropdown from '../../../components/ui/dropdown'; import Dropdown from '../../../components/ui/dropdown';
import Popover from '../../../components/ui/popover'; import Popover from '../../../components/ui/popover';
import { getURLHostName } from '../../../helpers/utils/util';
class AccountList extends Component { class AccountList extends Component {
state = { state = {
showPopover: false, showPopover: false,
@ -143,9 +145,7 @@ class AccountList extends Component {
properties: { properties: {
actions: 'Hardware Connect', actions: 'Hardware Connect',
link_type: 'Account Tracker', link_type: 'Account Tracker',
block_explorer_domain: accountLink block_explorer_domain: getURLHostName(accountLink),
? new URL(accountLink)?.hostname
: '',
}, },
}); });
global.platform.openTab({ global.platform.openTab({

View File

@ -3,6 +3,7 @@ import PropTypes from 'prop-types';
import classnames from 'classnames'; import classnames from 'classnames';
import { I18nContext } from '../../../../contexts/i18n'; import { I18nContext } from '../../../../contexts/i18n';
import { useNewMetricEvent } from '../../../../hooks/useMetricEvent'; import { useNewMetricEvent } from '../../../../hooks/useMetricEvent';
import { getURLHostName } from '../../../../helpers/utils/util';
export default function ViewOnEtherScanLink({ export default function ViewOnEtherScanLink({
txHash, txHash,
@ -17,9 +18,7 @@ export default function ViewOnEtherScanLink({
properties: { properties: {
link_type: 'Transaction Block Explorer', link_type: 'Transaction Block Explorer',
action: 'Swap Transaction', action: 'Swap Transaction',
block_explorer_domain: blockExplorerUrl block_explorer_domain: getURLHostName(blockExplorerUrl),
? new URL(blockExplorerUrl)?.hostname
: '',
}, },
}); });
@ -35,7 +34,7 @@ export default function ViewOnEtherScanLink({
}} }}
> >
{isCustomBlockExplorerUrl {isCustomBlockExplorerUrl
? t('viewOnCustomBlockExplorer', [new URL(blockExplorerUrl).hostname]) ? t('viewOnCustomBlockExplorer', [getURLHostName(blockExplorerUrl)])
: t('viewOnEtherscan')} : t('viewOnEtherscan')}
</div> </div>
); );

View File

@ -43,6 +43,7 @@ import {
hexToDecimal, hexToDecimal,
} from '../../../helpers/utils/conversions.util'; } from '../../../helpers/utils/conversions.util';
import { calcTokenAmount } from '../../../helpers/utils/token-util'; import { calcTokenAmount } from '../../../helpers/utils/token-util';
import { getURLHostName } from '../../../helpers/utils/util';
import { usePrevious } from '../../../hooks/usePrevious'; import { usePrevious } from '../../../hooks/usePrevious';
import { useTokenTracker } from '../../../hooks/useTokenTracker'; import { useTokenTracker } from '../../../hooks/useTokenTracker';
import { useTokenFiatAmount } from '../../../hooks/useTokenFiatAmount'; import { useTokenFiatAmount } from '../../../hooks/useTokenFiatAmount';
@ -241,7 +242,7 @@ export default function BuildQuote({
); );
const blockExplorerLabel = rpcPrefs.blockExplorerUrl const blockExplorerLabel = rpcPrefs.blockExplorerUrl
? new URL(blockExplorerTokenLink).hostname ? getURLHostName(blockExplorerTokenLink)
: t('etherscan'); : t('etherscan');
const blockExplorerLinkClickedEvent = useNewMetricEvent({ const blockExplorerLinkClickedEvent = useNewMetricEvent({
@ -250,9 +251,7 @@ export default function BuildQuote({
properties: { properties: {
link_type: 'Token Tracker', link_type: 'Token Tracker',
action: 'Swaps Confirmation', action: 'Swaps Confirmation',
block_explorer_domain: blockExplorerTokenLink block_explorer_domain: getURLHostName(blockExplorerTokenLink),
? new URL(blockExplorerTokenLink)?.hostname
: '',
}, },
}); });

View File

@ -23,6 +23,7 @@ import {
getRpcPrefsForCurrentProvider, getRpcPrefsForCurrentProvider,
} from '../../../selectors/selectors'; } from '../../../selectors/selectors';
import { SWAPS_CHAINID_DEFAULT_BLOCK_EXPLORER_URL_MAP } from '../../../../shared/constants/swaps'; import { SWAPS_CHAINID_DEFAULT_BLOCK_EXPLORER_URL_MAP } from '../../../../shared/constants/swaps';
import { getURLHostName } from '../../../helpers/utils/util';
export default function DropdownSearchList({ export default function DropdownSearchList({
searchListClassName, searchListClassName,
@ -138,7 +139,7 @@ export default function DropdownSearchList({
null; null;
const blockExplorerLabel = rpcPrefs.blockExplorerUrl const blockExplorerLabel = rpcPrefs.blockExplorerUrl
? new URL(blockExplorerLink).hostname ? getURLHostName(blockExplorerLink)
: t('etherscan'); : t('etherscan');
const blockExplorerLinkClickedEvent = useNewMetricEvent({ const blockExplorerLinkClickedEvent = useNewMetricEvent({
@ -147,9 +148,7 @@ export default function DropdownSearchList({
properties: { properties: {
link_type: 'Token Tracker', link_type: 'Token Tracker',
action: 'Verify Contract Address', action: 'Verify Contract Address',
block_explorer_domain: blockExplorerLink block_explorer_domain: getURLHostName(blockExplorerLink),
? new URL(blockExplorerLink)?.hostname
: '',
}, },
}); });

View File

@ -13,6 +13,7 @@ import {
} from '../../../../selectors'; } from '../../../../selectors';
import { SWAPS_CHAINID_DEFAULT_BLOCK_EXPLORER_URL_MAP } from '../../../../../shared/constants/swaps'; import { SWAPS_CHAINID_DEFAULT_BLOCK_EXPLORER_URL_MAP } from '../../../../../shared/constants/swaps';
import { useNewMetricEvent } from '../../../../hooks/useMetricEvent'; import { useNewMetricEvent } from '../../../../hooks/useMetricEvent';
import { getURLHostName } from '../../../../helpers/utils/util';
export default function ItemList({ export default function ItemList({
results = [], results = [],
@ -36,7 +37,7 @@ export default function ItemList({
null; null;
const blockExplorerLabel = rpcPrefs.blockExplorerUrl const blockExplorerLabel = rpcPrefs.blockExplorerUrl
? new URL(blockExplorerLink).hostname ? getURLHostName(blockExplorerLink)
: t('etherscan'); : t('etherscan');
const blockExplorerLinkClickedEvent = useNewMetricEvent({ const blockExplorerLinkClickedEvent = useNewMetricEvent({
@ -45,9 +46,7 @@ export default function ItemList({
properties: { properties: {
link_type: 'Token Tracker', link_type: 'Token Tracker',
action: 'Verify Contract Address', action: 'Verify Contract Address',
block_explorer_domain: blockExplorerLink block_explorer_domain: getURLHostName(blockExplorerLink),
? new URL(blockExplorerLink)?.hostname
: '',
}, },
}); });