From 37747293f215384984b25694a8dff8ba839d0bd1 Mon Sep 17 00:00:00 2001 From: Vinicius Stevam <45455812+vinistevam@users.noreply.github.com> Date: Fri, 11 Oct 2024 12:46:56 +0100 Subject: [PATCH] fix: Use domain for origin pill component (#11730) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit ## **Description** This PR fixes an issue where the full URL is displayed in the origin pill for transaction requests triggered from a dapp within the in-app browser, rather than just the domain. The expected behavior is to display only the domain, similar to how signature requests are handled. This update ensures consistency across both transaction and signature requests. ## **Related issues** Fixes: https://github.com/MetaMask/metamask-mobile/issues/11690 ## **Manual testing steps** 1. Open a dapp within the in app browser 2. Trigger a transaction request 3. See the origin pill ## **Screenshots/Recordings** [origin.webm](https://github.com/user-attachments/assets/31736e36-9054-496e-992e-5ab55688361c) ### **Before** ### **After** ## **Pre-merge author checklist** - [x] I’ve followed [MetaMask Contributor Docs](https://github.com/MetaMask/contributor-docs) and [MetaMask Mobile Coding Standards](https://github.com/MetaMask/metamask-mobile/blob/main/.github/guidelines/CODING_GUIDELINES.md). - [x] I've completed the PR template to the best of my ability - [x] I’ve included tests if applicable - [x] I’ve documented my code using [JSDoc](https://jsdoc.app/) format if applicable - [x] I’ve applied the right labels on the PR (see [labeling guidelines](https://github.com/MetaMask/metamask-mobile/blob/main/.github/guidelines/LABELING_GUIDELINES.md)). Not required for external contributors. ## **Pre-merge reviewer checklist** - [ ] I've manually tested the PR (e.g. pull and build branch, run the app, test code being changed). - [ ] I confirm that this PR addresses all acceptance criteria described in the ticket it closes and includes the necessary testing evidence such as recordings and or screenshots. --- .../UI/ApprovalTagUrl/ApprovalTagUrl.test.tsx | 34 +++++++++ .../UI/ApprovalTagUrl/ApprovalTagUrl.tsx | 6 +- .../ApprovalTagUrl.test.tsx.snap | 69 +++++++++++++++++++ .../Views/AccountConnect/AccountConnect.tsx | 4 +- .../ApproveTransactionHeader.tsx | 9 +-- 5 files changed, 111 insertions(+), 11 deletions(-) create mode 100644 app/components/UI/ApprovalTagUrl/ApprovalTagUrl.test.tsx create mode 100644 app/components/UI/ApprovalTagUrl/__snapshots__/ApprovalTagUrl.test.tsx.snap diff --git a/app/components/UI/ApprovalTagUrl/ApprovalTagUrl.test.tsx b/app/components/UI/ApprovalTagUrl/ApprovalTagUrl.test.tsx new file mode 100644 index 00000000000..415cff34f01 --- /dev/null +++ b/app/components/UI/ApprovalTagUrl/ApprovalTagUrl.test.tsx @@ -0,0 +1,34 @@ +import React from 'react'; +import renderWithProvider from '../../../util/test/renderWithProvider'; +import ApprovalTagUrl from './ApprovalTagUrl'; +import { backgroundState } from '../../../util/test/initial-root-state'; + +const ADDRESS_MOCK = '0x1234567890abcdef1234567890abcdef12345678'; +const DOMAIN_MOCK = 'metamask.github.io'; +const mockInitialState = { + settings: {}, + engine: { + backgroundState: { + ...backgroundState, + PreferencesController: { + selectedAddress: ADDRESS_MOCK, + }, + }, + }, +}; + +describe('ApprovalTagUrl', () => { + it('renders correctly', () => { + const { toJSON } = renderWithProvider( + , + { state: mockInitialState }, + ); + + expect(toJSON()).toMatchSnapshot(); + }); +}); diff --git a/app/components/UI/ApprovalTagUrl/ApprovalTagUrl.tsx b/app/components/UI/ApprovalTagUrl/ApprovalTagUrl.tsx index e7980f2848d..126f3132b17 100644 --- a/app/components/UI/ApprovalTagUrl/ApprovalTagUrl.tsx +++ b/app/components/UI/ApprovalTagUrl/ApprovalTagUrl.tsx @@ -8,7 +8,7 @@ import { useStyles } from '../../../component-library/hooks'; import AppConstants from '../../../core/AppConstants'; import { selectInternalAccounts } from '../../../selectors/accountsController'; import { selectAccountsByChainId } from '../../../selectors/accountTrackerController'; -import { prefixUrlWithProtocol } from '../../../util/browser'; +import { getHost, prefixUrlWithProtocol } from '../../../util/browser'; import useFavicon from '../../hooks/useFavicon/useFavicon'; import stylesheet from './ApprovalTagUrl.styles'; @@ -51,8 +51,8 @@ const ApprovalTagUrl = ({ const domainTitle = useMemo(() => { let title = ''; - if (url || currentEnsName || origin) { - title = prefixUrlWithProtocol(currentEnsName || origin || url); + if (currentEnsName || origin || url) { + title = prefixUrlWithProtocol(currentEnsName || origin || getHost(url)); } else { title = ''; } diff --git a/app/components/UI/ApprovalTagUrl/__snapshots__/ApprovalTagUrl.test.tsx.snap b/app/components/UI/ApprovalTagUrl/__snapshots__/ApprovalTagUrl.test.tsx.snap new file mode 100644 index 00000000000..b68dc550e4d --- /dev/null +++ b/app/components/UI/ApprovalTagUrl/__snapshots__/ApprovalTagUrl.test.tsx.snap @@ -0,0 +1,69 @@ +// Jest Snapshot v1, https://goo.gl/fbAQLP + +exports[`ApprovalTagUrl renders correctly 1`] = ` + + + + + + https://metamask.github.io + + +`; diff --git a/app/components/Views/AccountConnect/AccountConnect.tsx b/app/components/Views/AccountConnect/AccountConnect.tsx index 5ed77fe0df5..6bb65b3e2d0 100644 --- a/app/components/Views/AccountConnect/AccountConnect.tsx +++ b/app/components/Views/AccountConnect/AccountConnect.tsx @@ -38,7 +38,7 @@ import { getAddressAccountType, safeToChecksumAddress, } from '../../../util/address'; -import { getUrlObj, prefixUrlWithProtocol } from '../../../util/browser'; +import { getHost, getUrlObj, prefixUrlWithProtocol } from '../../../util/browser'; import { getActiveTabUrl } from '../../../util/transactions'; import { Account, useAccounts } from '../../hooks/useAccounts'; @@ -177,7 +177,7 @@ const AccountConnect = (props: AccountConnectProps) => { const urlWithProtocol = hostname && !isUUID(hostname) - ? prefixUrlWithProtocol(hostname) + ? prefixUrlWithProtocol(getHost(hostname)) : domainTitle; const isAllowedOrigin = useCallback((origin: string) => { diff --git a/app/components/Views/confirmations/components/ApproveTransactionHeader/ApproveTransactionHeader.tsx b/app/components/Views/confirmations/components/ApproveTransactionHeader/ApproveTransactionHeader.tsx index 5870aae52da..6db2535f4e9 100644 --- a/app/components/Views/confirmations/components/ApproveTransactionHeader/ApproveTransactionHeader.tsx +++ b/app/components/Views/confirmations/components/ApproveTransactionHeader/ApproveTransactionHeader.tsx @@ -1,5 +1,3 @@ -// eslint-disable-next-line @typescript-eslint/ban-ts-comment -// @ts-nocheck - Confirmations team or Transactions team import { toChecksumAddress } from 'ethereumjs-util'; import React, { useEffect, useState } from 'react'; import { View } from 'react-native'; @@ -27,6 +25,7 @@ import stylesheet from './ApproveTransactionHeader.styles'; import { ApproveTransactionHeaderI } from './ApproveTransactionHeader.types'; import { selectInternalAccounts } from '../../../../../selectors/accountsController'; import ApprovalTagUrl from '../../../../UI/ApprovalTagUrl'; +import { RootState } from '../../../../../reducers'; const ApproveTransactionHeader = ({ from, @@ -51,9 +50,7 @@ const ApproveTransactionHeader = ({ const networkName = useSelector(selectNetworkName); const useBlockieIcon = useSelector( - // TODO: Replace "any" with type - // eslint-disable-next-line @typescript-eslint/no-explicit-any - (state: any) => state.settings.useBlockieIcon, + (state: RootState) => state.settings.useBlockieIcon, ); useEffect(() => { @@ -70,7 +67,7 @@ const ApproveTransactionHeader = ({ const networkImage = useSelector(selectNetworkImageSource); - const accountTypeLabel = getLabelTextByAddress(activeAddress); + const accountTypeLabel = getLabelTextByAddress(activeAddress) ?? undefined; return (