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 16dd0db
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 138 deletions.
20 changes: 20 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 All @@ -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': {
Expand Down Expand Up @@ -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', () => {
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
Original file line number Diff line number Diff line change
Expand Up @@ -2814,66 +2814,6 @@ exports[`Network Selector renders correctly when network UI redesign is enabled
>
Optimism
</Text>
<TouchableOpacity
accessibilityState={
{
"busy": undefined,
"checked": undefined,
"disabled": undefined,
"expanded": undefined,
"selected": undefined,
}
}
accessible={true}
focusable={false}
onClick={[Function]}
onPress={[Function]}
onResponderGrant={[Function]}
onResponderMove={[Function]}
onResponderRelease={[Function]}
onResponderTerminate={[Function]}
onResponderTerminationRequest={[Function]}
onStartShouldSetResponder={[Function]}
style={
{
"alignItems": "flex-start",
"flexDirection": "row",
"marginBottom": 0,
"zIndex": 1,
}
}
>
<Text
accessibilityRole="text"
numberOfLines={1}
style={
{
"color": "#6a737d",
"fontFamily": "EuclidCircularB-Regular",
"fontSize": 14,
"fontWeight": "400",
"letterSpacing": 0,
"lineHeight": 22,
}
}
>
optimism-mainnet.infura.io/v3
</Text>
<SvgMock
color="#141618"
height={10}
name="ArrowDown"
style={
{
"height": 10,
"paddingLeft": 8,
"paddingTop": 24,
"width": 10,
}
}
width={10}
/>
</TouchableOpacity>
</View>
</View>
</View>
Expand Down Expand Up @@ -3036,66 +2976,6 @@ exports[`Network Selector renders correctly when network UI redesign is enabled
>
Gnosis Chain
</Text>
<TouchableOpacity
accessibilityState={
{
"busy": undefined,
"checked": undefined,
"disabled": undefined,
"expanded": undefined,
"selected": undefined,
}
}
accessible={true}
focusable={false}
onClick={[Function]}
onPress={[Function]}
onResponderGrant={[Function]}
onResponderMove={[Function]}
onResponderRelease={[Function]}
onResponderTerminate={[Function]}
onResponderTerminationRequest={[Function]}
onStartShouldSetResponder={[Function]}
style={
{
"alignItems": "flex-start",
"flexDirection": "row",
"marginBottom": 0,
"zIndex": 1,
}
}
>
<Text
accessibilityRole="text"
numberOfLines={1}
style={
{
"color": "#6a737d",
"fontFamily": "EuclidCircularB-Regular",
"fontSize": 14,
"fontWeight": "400",
"letterSpacing": 0,
"lineHeight": 22,
}
}
>
rpc.gnosischain.com
</Text>
<SvgMock
color="#141618"
height={10}
name="ArrowDown"
style={
{
"height": 10,
"paddingLeft": 8,
"paddingTop": 24,
"width": 10,
}
}
width={10}
/>
</TouchableOpacity>
</View>
</View>
</View>
Expand Down

0 comments on commit 16dd0db

Please sign in to comment.