Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
export interface AccountRightButtonProps {
selectedAddress: string;
onPress: () => void;
dappOrigin: string;
}
74 changes: 63 additions & 11 deletions app/components/UI/AccountRightButton/index.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ const mockCreateEventBuilder = jest.fn(() => ({
jest.mock('@react-navigation/native', () => ({
...jest.requireActual('@react-navigation/native'),
useNavigation: () => ({ navigate: mockNavigate }),
useRoute: () => ({ params: {} }),
}));

jest.mock('../../../components/hooks/useAnalytics/useAnalytics', () => ({
Expand Down Expand Up @@ -62,6 +61,9 @@ const mockInitialState: DeepPartial<RootState> = {
},
};

/** Representative in-app browser origin for tests (non-empty). */
const MOCK_DAPP_ORIGIN = 'https://metamask.github.io';

describe('AccountRightButton', () => {
beforeEach(() => {
jest.clearAllMocks();
Expand All @@ -70,7 +72,11 @@ describe('AccountRightButton', () => {
it('should render correctly', () => {
const { toJSON } = renderScreen(
() => (
<AccountRightButton selectedAddress="0x123" onPress={() => undefined} />
<AccountRightButton
selectedAddress="0x123"
onPress={() => undefined}
dappOrigin={MOCK_DAPP_ORIGIN}
/>
),
{
name: 'AccountRightButton',
Expand All @@ -83,7 +89,11 @@ describe('AccountRightButton', () => {
it('should render correctly when a EVM network is selected', () => {
const { toJSON } = renderScreen(
() => (
<AccountRightButton selectedAddress="0x123" onPress={() => undefined} />
<AccountRightButton
selectedAddress="0x123"
onPress={() => undefined}
dappOrigin={MOCK_DAPP_ORIGIN}
/>
),
{
name: 'AccountRightButton',
Expand Down Expand Up @@ -111,7 +121,11 @@ describe('AccountRightButton', () => {

const { toJSON } = renderScreen(
() => (
<AccountRightButton selectedAddress="0x123" onPress={() => undefined} />
<AccountRightButton
selectedAddress="0x123"
onPress={() => undefined}
dappOrigin={MOCK_DAPP_ORIGIN}
/>
),
{
name: 'AccountRightButton',
Expand All @@ -126,7 +140,11 @@ describe('AccountRightButton', () => {
const onPressMock = jest.fn();
const { getByTestId } = renderScreen(
() => (
<AccountRightButton selectedAddress="0x123" onPress={onPressMock} />
<AccountRightButton
selectedAddress="0x123"
onPress={onPressMock}
dappOrigin={MOCK_DAPP_ORIGIN}
/>
),
{
name: 'AccountRightButton',
Expand All @@ -140,7 +158,13 @@ describe('AccountRightButton', () => {

it('should render network avatar when selectedAddress is not provided (EVM)', () => {
const { toJSON } = renderScreen(
() => <AccountRightButton selectedAddress="" onPress={() => undefined} />,
() => (
<AccountRightButton
selectedAddress=""
onPress={() => undefined}
dappOrigin={MOCK_DAPP_ORIGIN}
/>
),
{
name: 'AccountRightButton',
},
Expand All @@ -166,7 +190,13 @@ describe('AccountRightButton', () => {
};

const { toJSON } = renderScreen(
() => <AccountRightButton selectedAddress="" onPress={() => undefined} />,
() => (
<AccountRightButton
selectedAddress=""
onPress={() => undefined}
dappOrigin={MOCK_DAPP_ORIGIN}
/>
),
{
name: 'AccountRightButton',
},
Expand All @@ -178,7 +208,11 @@ describe('AccountRightButton', () => {
it('should render account avatar when selectedAddress is provided', () => {
const { toJSON } = renderScreen(
() => (
<AccountRightButton selectedAddress="0x123" onPress={() => undefined} />
<AccountRightButton
selectedAddress="0x123"
onPress={() => undefined}
dappOrigin={MOCK_DAPP_ORIGIN}
/>
),
{
name: 'AccountRightButton',
Expand All @@ -190,7 +224,13 @@ describe('AccountRightButton', () => {

it('should navigate with EVM chainId when selectedAddress is empty and EVM is selected', () => {
const { getByTestId } = renderScreen(
() => <AccountRightButton selectedAddress="" onPress={() => undefined} />,
() => (
<AccountRightButton
selectedAddress=""
onPress={() => undefined}
dappOrigin={MOCK_DAPP_ORIGIN}
/>
),
{
name: 'AccountRightButton',
},
Expand Down Expand Up @@ -228,7 +268,13 @@ describe('AccountRightButton', () => {
};

const { getByTestId } = renderScreen(
() => <AccountRightButton selectedAddress="" onPress={() => undefined} />,
() => (
<AccountRightButton
selectedAddress=""
onPress={() => undefined}
dappOrigin={MOCK_DAPP_ORIGIN}
/>
),
{
name: 'AccountRightButton',
},
Expand Down Expand Up @@ -272,7 +318,13 @@ describe('AccountRightButton', () => {
};

const { toJSON } = renderScreen(
() => <AccountRightButton selectedAddress="" onPress={() => undefined} />,
() => (
<AccountRightButton
selectedAddress=""
onPress={() => undefined}
dappOrigin={MOCK_DAPP_ORIGIN}
/>
),
{
name: 'AccountRightButton',
},
Expand Down
26 changes: 15 additions & 11 deletions app/components/UI/AccountRightButton/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import {
Platform,
EmitterSubscription,
} from 'react-native';
import { useNavigation, useRoute, RouteProp } from '@react-navigation/native';
import { useNavigation } from '@react-navigation/native';
import Device from '../../../util/device';
import AvatarAccount from '../../../component-library/components/Avatars/Avatar/variants/AvatarAccount';
import { AccountRightButtonProps } from './AccountRightButton.types';
Expand All @@ -31,7 +31,6 @@ import { MetaMetricsEvents } from '../../../core/Analytics';
import { AccountOverviewSelectorsIDs } from './AccountOverview.testIds';
import { useAnalytics } from '../../../components/hooks/useAnalytics/useAnalytics';
import { useNetworkInfo } from '../../../selectors/selectedNetworkController';
import UrlParser from 'url-parse';
import { selectEvmChainId } from '../../../selectors/networkController';
import {
selectIsEvmNetworkSelected,
Expand Down Expand Up @@ -63,6 +62,7 @@ const styles = StyleSheet.create({
const AccountRightButton = ({
selectedAddress,
onPress,
dappOrigin,
}: AccountRightButtonProps) => {
// Placeholder ref for dismissing keyboard. Works when the focused input is within a Webview.
const placeholderInputRef = useRef<TextInput>(null);
Expand Down Expand Up @@ -130,6 +130,15 @@ const AccountRightButton = ({
screen: Routes.SHEET.NETWORK_SELECTOR,
params: {
chainId: isEvmSelected ? chainId : selectedNonEvmNetworkChainId,
...(dappOrigin
? {
hostInfo: {
metadata: {
origin: dappOrigin,
},
},
}
: {}),
},
});
trackEvent(
Expand All @@ -152,17 +161,12 @@ const AccountRightButton = ({
onPress,
selectedNonEvmNetworkChainId,
isEvmSelected,
dappOrigin,
]);

const route = useRoute<RouteProp<Record<string, { url: string }>, string>>();
// url is defined if opened while in a dapp
const currentUrl = route.params?.url;
let hostname;
if (currentUrl) {
hostname = new UrlParser(currentUrl)?.origin;
}

const { networkName, networkImageSource } = useNetworkInfo(hostname);
const { networkName, networkImageSource } = useNetworkInfo(
dappOrigin || undefined,
);

const nonEvmNetworkImageSource = useMemo(() => {
if (!isEvmSelected && selectedNonEvmNetworkChainId) {
Expand Down
12 changes: 12 additions & 0 deletions app/components/UI/BrowserUrlBar/BrowserUrlBar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,16 @@ const BrowserUrlBar = forwardRef<BrowserUrlBarRef, BrowserUrlBarProps>(
const { trackEvent, createEventBuilder } = useAnalytics();
const navigation = useNavigation();
const selectedAddress = connectedAccounts?.[0];
const dappOrigin = useMemo(() => {
if (!activeUrl) {
return '';
}
try {
return new URLParse(activeUrl).origin;
} catch {
return '';
}
}, [activeUrl]);
const {
styles,
theme: { colors, themeAppearance },
Expand Down Expand Up @@ -138,6 +148,7 @@ const BrowserUrlBar = forwardRef<BrowserUrlBarRef, BrowserUrlBarProps>(
<AccountRightButton
selectedAddress={selectedAddress}
onPress={handleAccountRightButtonPress}
dappOrigin={dappOrigin}
/>
);
}
Expand All @@ -162,6 +173,7 @@ const BrowserUrlBar = forwardRef<BrowserUrlBarRef, BrowserUrlBarProps>(
onCancelInput,
styles.cancelButton,
styles.cancelButtonText,
dappOrigin,
]);

useImperativeHandle(ref, () => ({
Expand Down
Loading