From 16dd0dbd9e04ddb48ec98236023d48b94e7e57dc Mon Sep 17 00:00:00 2001 From: salimtb Date: Sun, 17 Nov 2024 08:52:59 +0100 Subject: [PATCH] fix: hide rpc url selector for networks with one rpc --- .../NetworkSelector/NetworkSelector.test.tsx | 20 +++ .../Views/NetworkSelector/NetworkSelector.tsx | 37 +++--- .../NetworkSelector.test.tsx.snap | 120 ------------------ 3 files changed, 39 insertions(+), 138 deletions(-) diff --git a/app/components/Views/NetworkSelector/NetworkSelector.test.tsx b/app/components/Views/NetworkSelector/NetworkSelector.test.tsx index 970634f0db7..9e4c7ffefde 100644 --- a/app/components/Views/NetworkSelector/NetworkSelector.test.tsx +++ b/app/components/Views/NetworkSelector/NetworkSelector.test.tsx @@ -138,6 +138,11 @@ const initialState = { type: 'custom', url: 'https://api.avax.network/ext/bc/C/rpc', }, + { + networkClientId: 'networkId1', + type: 'custom', + url: 'https://api.avax2.network/ext/bc/C/rpc', + }, ], }, '0x89': { @@ -152,6 +157,11 @@ const initialState = { type: 'infura', url: 'https://polygon-mainnet.infura.io/v3/12345', }, + { + networkClientId: 'networkId3', + type: 'infura', + url: 'https://polygon-mainnet2.infura.io/v3/12345', + }, ], }, '0xa': { @@ -520,6 +530,16 @@ describe('Network Selector', () => { expect(avalancheRpcUrl).toBeTruthy(); expect(avalancheCell).toBeTruthy(); }); + + it('renders the RPC URL correctly for optimism single RPC endpoint', () => { + (isNetworkUiRedesignEnabled as jest.Mock).mockImplementation(() => true); + const { getByText } = renderComponent(initialState); + const optimismCell = getByText('Optimism'); + expect(optimismCell).toBeTruthy(); + expect(() => { + getByText('https://optimism-mainnet.infura.io/v3/12345'); + }).toThrow(); + }); }); describe('renderMainnet', () => { diff --git a/app/components/Views/NetworkSelector/NetworkSelector.tsx b/app/components/Views/NetworkSelector/NetworkSelector.tsx index 21bf02d1b71..a142ebe8b39 100644 --- a/app/components/Views/NetworkSelector/NetworkSelector.tsx +++ b/app/components/Views/NetworkSelector/NetworkSelector.tsx @@ -35,10 +35,7 @@ import Networks, { getNetworkImageSource, isMainNet, } from '../../../util/networks'; -import { - LINEA_MAINNET, - MAINNET, -} from '../../../constants/network'; +import { LINEA_MAINNET, MAINNET } from '../../../constants/network'; import Button from '../../../component-library/components/Buttons/Button/Button'; import { ButtonSize, @@ -237,10 +234,7 @@ const NetworkSelector = () => { const deleteModalSheetRef = useRef(null); const onSetRpcTarget = async (networkConfiguration: NetworkConfiguration) => { - const { - NetworkController, - SelectedNetworkController, - } = Engine.context; + const { NetworkController, SelectedNetworkController } = Engine.context; trace({ name: TraceName.SwitchCustomNetwork, parentContext: parentSpan, @@ -370,7 +364,6 @@ const NetworkSelector = () => { if (domainIsConnectedDapp && process.env.MULTICHAIN_V1) { SelectedNetworkController.setNetworkClientIdForDomain(origin, type); } else { - const networkConfiguration = networkConfigurations[BUILT_IN_NETWORKS[type].chainId]; @@ -428,11 +421,11 @@ const NetworkSelector = () => { const renderMainnet = () => { const { name: mainnetName, chainId } = Networks.mainnet; + const rpcEndpoints = networkConfigurations?.[chainId]?.rpcEndpoints; const rpcUrl = - networkConfigurations?.[chainId]?.rpcEndpoints?.[ - networkConfigurations?.[chainId]?.defaultRpcEndpointIndex - ].url; + rpcEndpoints?.[networkConfigurations?.[chainId]?.defaultRpcEndpointIndex] + .url; const name = networkConfigurations?.[chainId]?.name ?? mainnetName; if (isNetworkUiRedesignEnabled() && isNoSearchResults(MAINNET)) return null; @@ -443,7 +436,9 @@ const NetworkSelector = () => { key={chainId} variant={CellVariant.SelectWithMenu} title={name} - secondaryText={hideKeyFromUrl(rpcUrl)} + secondaryText={ + rpcEndpoints?.length > 1 ? hideKeyFromUrl(rpcUrl) : undefined + } avatarProps={{ variant: AvatarVariant.Network, name: mainnetName, @@ -492,10 +487,10 @@ const NetworkSelector = () => { const renderLineaMainnet = () => { const { name: lineaMainnetName, chainId } = Networks['linea-mainnet']; const name = networkConfigurations?.[chainId]?.name ?? lineaMainnetName; + const rpcEndpoints = networkConfigurations?.[chainId]?.rpcEndpoints; const rpcUrl = - networkConfigurations?.[chainId]?.rpcEndpoints?.[ - networkConfigurations?.[chainId]?.defaultRpcEndpointIndex - ].url; + rpcEndpoints?.[networkConfigurations?.[chainId]?.defaultRpcEndpointIndex] + .url; if (isNetworkUiRedesignEnabled() && isNoSearchResults('linea-mainnet')) return null; @@ -516,7 +511,9 @@ const NetworkSelector = () => { onPress={() => onNetworkChange(LINEA_MAINNET)} style={styles.networkCell} buttonIcon={IconName.MoreVertical} - secondaryText={hideKeyFromUrl(rpcUrl)} + secondaryText={ + rpcEndpoints?.length > 1 ? hideKeyFromUrl(rpcUrl) : undefined + } buttonProps={{ onButtonClick: () => { openModal(chainId, false, LINEA_MAINNET, true); @@ -595,7 +592,11 @@ const NetworkSelector = () => { onPress={() => onSetRpcTarget(networkConfiguration)} style={styles.networkCell} buttonIcon={IconName.MoreVertical} - secondaryText={hideProtocolFromUrl(hideKeyFromUrl(rpcUrl))} + secondaryText={ + rpcEndpoints?.length > 1 + ? hideProtocolFromUrl(hideKeyFromUrl(rpcUrl)) + : undefined + } buttonProps={{ onButtonClick: () => { openModal(chainId, true, rpcUrl, false); diff --git a/app/components/Views/NetworkSelector/__snapshots__/NetworkSelector.test.tsx.snap b/app/components/Views/NetworkSelector/__snapshots__/NetworkSelector.test.tsx.snap index 3773996bbf7..d74a778a2c5 100644 --- a/app/components/Views/NetworkSelector/__snapshots__/NetworkSelector.test.tsx.snap +++ b/app/components/Views/NetworkSelector/__snapshots__/NetworkSelector.test.tsx.snap @@ -2814,66 +2814,6 @@ exports[`Network Selector renders correctly when network UI redesign is enabled > Optimism - - - optimism-mainnet.infura.io/v3 - - - @@ -3036,66 +2976,6 @@ exports[`Network Selector renders correctly when network UI redesign is enabled > Gnosis Chain - - - rpc.gnosischain.com - - -