Skip to content

Commit

Permalink
fix: hide rpc url selector for networks with one rpc
Browse files Browse the repository at this point in the history
  • Loading branch information
salimtb committed Nov 17, 2024
1 parent 0b066a3 commit 457c0d6
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 18 deletions.
15 changes: 15 additions & 0 deletions app/components/Views/NetworkSelector/NetworkSelector.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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': {
Expand Down Expand Up @@ -520,6 +525,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', () => {
Expand Down
37 changes: 19 additions & 18 deletions app/components/Views/NetworkSelector/NetworkSelector.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -237,10 +234,7 @@ const NetworkSelector = () => {
const deleteModalSheetRef = useRef<BottomSheetRef>(null);

const onSetRpcTarget = async (networkConfiguration: NetworkConfiguration) => {
const {
NetworkController,
SelectedNetworkController,
} = Engine.context;
const { NetworkController, SelectedNetworkController } = Engine.context;
trace({
name: TraceName.SwitchCustomNetwork,
parentContext: parentSpan,
Expand Down Expand Up @@ -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];

Expand Down Expand Up @@ -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;
Expand All @@ -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,
Expand Down Expand Up @@ -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;
Expand All @@ -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);
Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit 457c0d6

Please sign in to comment.