Skip to content

Commit

Permalink
fix: prevent Duplicate Block Explorer Entries and Ensure Proper Input…
Browse files Browse the repository at this point in the history
… Validation (#11995)

<!--
Please submit this PR as a draft initially.
Do not mark it as "Ready for review" until the template has been
completely filled out, and PR status checks have passed at least once.
-->

## **Description**

When editing a network and attempting to add a new block explorer URL,
the 'Add Block Explorer URL +' button allows users to add a duplicate
entry even if no input is provided. This results in duplicate block
explorer URLs being added to the list. The expected behavior is that the
'Add Block Explorer URL' button should remain disabled until valid input
is provided, and existing URLs should not be re-added.

<!--
Write a short description of the changes included in this pull request,
also include relevant motivation and context. Have in mind the following
questions:
1. What is the reason for the change?
2. What is the improvement/solution?
-->

## **Related issues**

Fixes: #11990 

## **Manual testing steps**

1. Go to network add form
2. go to add block explorer url
3. The 'Add Block Explorer URL' button should be disabled until a valid
URL is inputted into the field.

## **Screenshots/Recordings**

<!-- If applicable, add screenshots and/or recordings to visualize the
before and after of your change. -->

### **Before**

<!-- [screenshots/recordings] -->

### **After**



https://github.com/user-attachments/assets/05f72bf8-8046-40a4-a941-51f39cfc9a09




<!-- [screenshots/recordings] -->

## **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.
  • Loading branch information
salimtb authored Oct 24, 2024
1 parent def6f50 commit 6a77e4b
Show file tree
Hide file tree
Showing 2 changed files with 494 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -447,6 +447,7 @@ export class NetworkSettings extends PureComponent {
blockExplorerUrls: [],
selectedRpcEndpointIndex: 0,
blockExplorerUrl: undefined,
blockExplorerUrlForm: undefined,
nickname: undefined,
chainId: undefined,
ticker: undefined,
Expand Down Expand Up @@ -714,7 +715,11 @@ export class NetworkSettings extends PureComponent {
// in an error message in the form.
if (!formChainId.startsWith('0x')) {
try {
endpointChainId = new BigNumber(endpointChainId, 16).toString(10);
const endpointChainIdNumber = new BigNumber(endpointChainId, 16);
if (endpointChainIdNumber.isNaN()) {
throw new Error('Invalid endpointChainId');
}
endpointChainId = endpointChainIdNumber.toString(10);
} catch (err) {
Logger.error(err, {
endpointChainId,
Expand Down Expand Up @@ -1288,10 +1293,21 @@ export class NetworkSettings extends PureComponent {
};

onBlockExplorerItemAdd = async (url) => {
// If URL is empty or undefined, return early
if (!url) {
return;
}

// Check if the URL already exists in blockExplorerUrls
const { blockExplorerUrls } = this.state;
const urlExists = blockExplorerUrls.includes(url);

if (urlExists) {
// If the URL already exists, return early
return;
}

// If the URL doesn't exist, proceed with adding it
await this.setState((prevState) => ({
blockExplorerUrls: [...prevState.blockExplorerUrls, url],
}));
Expand Down Expand Up @@ -1351,6 +1367,7 @@ export class NetworkSettings extends PureComponent {
onBlockExplorerUrlChange = async (url) => {
const { addMode } = this.state;
await this.setState({
blockExplorerUrlForm: url,
blockExplorerUrl: url,
});

Expand Down Expand Up @@ -1486,7 +1503,10 @@ export class NetworkSettings extends PureComponent {
};

closeAddBlockExplorerRpcForm = () => {
this.setState({ showAddBlockExplorerForm: { isVisible: false } });
this.setState({
showAddBlockExplorerForm: { isVisible: false },
blockExplorerUrlForm: undefined,
});
};

closeRpcModal = () => {
Expand Down Expand Up @@ -1603,6 +1623,7 @@ export class NetworkSettings extends PureComponent {
rpcUrlForm,
rpcNameForm,
rpcName,
blockExplorerUrlForm,
} = this.state;
const { route, networkConfigurations } = this.props;
const isCustomMainnet = route.params?.isCustomMainnet;
Expand Down Expand Up @@ -2204,6 +2225,7 @@ export class NetworkSettings extends PureComponent {
ref={this.inputBlockExplorerURL}
style={inputStyle}
autoCapitalize={'none'}
value={blockExplorerUrlForm}
autoCorrect={false}
onChangeText={this.onBlockExplorerUrlChange}
placeholder={strings(
Expand All @@ -2214,23 +2236,28 @@ export class NetworkSettings extends PureComponent {
onSubmitEditing={this.toggleNetworkDetailsModal}
keyboardAppearance={themeAppearance}
/>
{blockExplorerUrl && !isUrl(blockExplorerUrl) && (
<View>
{blockExplorerUrl &&
(!isUrl(blockExplorerUrl) ||
blockExplorerUrls.includes(blockExplorerUrlForm)) && (
<Text style={styles.warningText}>
{strings('app_settings.invalid_block_explorer_url')}
</Text>
</View>
)}
)}

<View style={styles.addRpcNameButton}>
<ButtonPrimary
label={strings('app_settings.add_block_explorer_url')}
size={ButtonSize.Lg}
onPress={() => {
this.onBlockExplorerItemAdd(blockExplorerUrl);
this.onBlockExplorerItemAdd(blockExplorerUrlForm);
}}
width={ButtonWidthTypes.Full}
labelTextVariant={TextVariant.DisplayMD}
isDisabled={!blockExplorerUrl || !isUrl(blockExplorerUrl)}
isDisabled={
!blockExplorerUrl ||
!blockExplorerUrlForm ||
!isUrl(blockExplorerUrl)
}
/>
</View>
</SafeAreaView>
Expand Down
Loading

0 comments on commit 6a77e4b

Please sign in to comment.