Skip to content

Commit f1a2b6c

Browse files
authored
chore: refactor Assets on settings V2 to use createToggleItem (#40750)
## **Description** Refactors Assets tab toggle items to use the `createToggleItem` factory, reducing boilerplate and improving consistency. **Changes:** - Extended `createToggleItem` factory to support optional `descriptionKey` - Converted 3 toggle items to use the factory inline in `assets-tab.tsx`: - `AutodetectTokensToggleItem` - `HideZeroBalanceTokensToggleItem` - `ShowNetworkTokenToggleItem` (with MetaMetrics tracking) - Removed standalone component files and their tests ## **Changelog** CHANGELOG entry: null ## **Related issues** Fixes: ## **Manual testing steps** 1. Uncomment object beneath `Uncomment to view Settings V2 in Hamburger Menu` 2. Navigate to Settings V2 > Assets 3. Verify all toggle items render correctly 4. Toggle each setting and verify state persists 5. Verify MetaMetrics events fire for "Show native token as main balance" 6. ## **Screenshots/Recordings** <!-- If applicable, add screenshots and/or recordings to visualize the before and after of your change. --> ### **Before** ### **After** ## **Pre-merge author checklist** - [ ] I've followed [MetaMask Contributor Docs](https://github.com/MetaMask/contributor-docs) and [MetaMask Extension Coding Standards](https://github.com/MetaMask/metamask-extension/blob/main/.github/guidelines/CODING_GUIDELINES.md). - [ ] I've completed the PR template to the best of my ability - [ ] I've included tests if applicable - [ ] I've documented my code using [JSDoc](https://jsdoc.app/) format if applicable - [ ] I've applied the right labels on the PR (see [labeling guidelines](https://github.com/MetaMask/metamask-extension/blob/main/.github/guidelines/LABELING_GUIDELINES.md)). <!-- CURSOR_SUMMARY --> --- > [!NOTE] > **Low Risk** > Primarily a UI refactor that reuses existing selectors/actions; main risk is subtle behavior/test-id changes affecting settings toggles and metrics firing. > > **Overview** > Refactors the Settings V2 *Assets* tab toggles to use the shared `createToggleItem` factory, inlining the configs in `assets-tab.tsx` and removing the standalone toggle components/tests for *Autodetect tokens*, *Hide zero balance tokens*, and *Show native token as main balance* (including its MetaMetrics tracking). > > Extends `createToggleItem` to make `descriptionKey` optional (and adds coverage for the no-description case), and updates the Assets tab snapshot/console baseline to reflect the refactor (including removal of a wrapper `data-testid` around the hide-zero-balance toggle). > > <sup>Written by [Cursor Bugbot](https://cursor.com/dashboard?tab=bugbot) for commit 49dd8e6. This will update automatically on new commits. Configure [here](https://cursor.com/dashboard?tab=bugbot).</sup> <!-- /CURSOR_SUMMARY -->
1 parent cfb5fce commit f1a2b6c

11 files changed

+73
-436
lines changed

test/jest/console-baseline-unit.json

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2802,28 +2802,16 @@
28022802
"React: componentWill* lifecycle deprecations": 1,
28032803
"warn: ⚠️ React Router Future Flag": 2
28042804
},
2805-
"ui/pages/settings-v2/assets-tab/autodetect-tokens-item.test.tsx": {
2806-
"React: componentWill* lifecycle deprecations": 1,
2807-
"warn: ⚠️ React Router Future Flag": 2
2808-
},
28092805
"ui/pages/settings-v2/assets-tab/currency-sub-page.test.tsx": {
28102806
"warn: ⚠️ React Router Future Flag": 2
28112807
},
28122808
"ui/pages/settings-v2/assets-tab/display-nft-media-item.test.tsx": {
28132809
"React: componentWill* lifecycle deprecations": 1,
28142810
"warn: ⚠️ React Router Future Flag": 2
28152811
},
2816-
"ui/pages/settings-v2/assets-tab/hide-zero-balance-tokens-item.test.tsx": {
2817-
"React: componentWill* lifecycle deprecations": 1,
2818-
"warn: ⚠️ React Router Future Flag": 2
2819-
},
28202812
"ui/pages/settings-v2/assets-tab/local-currency-item.test.tsx": {
28212813
"warn: ⚠️ React Router Future Flag": 2
28222814
},
2823-
"ui/pages/settings-v2/assets-tab/show-network-token-item.test.tsx": {
2824-
"React: componentWill* lifecycle deprecations": 1,
2825-
"warn: ⚠️ React Router Future Flag": 2
2826-
},
28272815
"ui/pages/settings-v2/preferences-and-display-tab/account-identicon-item.test.tsx": {
28282816
"React: Act warnings (component updates not wrapped)": 4,
28292817
"warn: ⚠️ React Router Future Flag": 2

ui/pages/settings-v2/assets-tab/__snapshots__/assets-tab.test.tsx.snap

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -99,9 +99,7 @@ exports[`Assets Tab snapshot matches snapshot 1`] = `
9999
>
100100
Hide tokens without balance
101101
</p>
102-
<div
103-
data-testid="hide-zero-balance-tokens"
104-
>
102+
<div>
105103
<label
106104
class="toggle-button toggle-button--off"
107105
tabindex="0"

ui/pages/settings-v2/assets-tab/assets-tab.tsx

Lines changed: 44 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,52 @@
11
import React from 'react';
22
import { SettingItemConfig } from '../types';
3-
import { SettingsTab } from '../shared';
3+
import { SettingsTab, createToggleItem } from '../shared';
4+
import {
5+
getUseTokenDetection,
6+
getShouldHideZeroBalanceTokens,
7+
getShowNativeTokenAsMainBalance,
8+
} from '../../../selectors';
9+
import {
10+
setUseTokenDetection,
11+
setHideZeroBalanceTokens,
12+
setShowNativeTokenAsMainBalancePreference,
13+
} from '../../../store/actions';
14+
import { MetaMetricsEventName } from '../../../../shared/constants/metametrics';
415
import { LocalCurrencyItem } from './local-currency-item';
5-
import { ShowNetworkTokenToggleItem } from './show-network-token-item';
6-
import { HideZeroBalanceTokensToggleItem } from './hide-zero-balance-tokens-item';
716
import { DisplayNftMediaToggleItem } from './display-nft-media-item';
817
import { AutodetectNftsToggleItem } from './autodetect-nfts-item';
9-
import { AutodetectTokensToggleItem } from './autodetect-tokens-item';
18+
19+
const ShowNetworkTokenToggleItem = createToggleItem({
20+
name: 'ShowNetworkTokenToggleItem',
21+
titleKey: 'showNativeTokenAsMainBalance',
22+
selector: getShowNativeTokenAsMainBalance,
23+
action: setShowNativeTokenAsMainBalancePreference,
24+
dataTestId: 'show-native-token-as-main-balance',
25+
trackEvent: {
26+
event: MetaMetricsEventName.SettingsUpdated,
27+
properties: (newValue) => ({
28+
// eslint-disable-next-line @typescript-eslint/naming-convention
29+
show_native_token_as_main_balance: newValue,
30+
}),
31+
},
32+
});
33+
34+
const HideZeroBalanceTokensToggleItem = createToggleItem({
35+
name: 'HideZeroBalanceTokensToggleItem',
36+
titleKey: 'hideZeroBalanceTokens',
37+
selector: getShouldHideZeroBalanceTokens,
38+
action: setHideZeroBalanceTokens,
39+
dataTestId: 'toggle-zero-balance-button',
40+
});
41+
42+
const AutodetectTokensToggleItem = createToggleItem({
43+
name: 'AutodetectTokensToggleItem',
44+
titleKey: 'autoDetectTokens',
45+
descriptionKey: 'autoDetectTokensDescriptionV2',
46+
selector: getUseTokenDetection,
47+
action: setUseTokenDetection,
48+
dataTestId: 'autodetect-tokens',
49+
});
1050

1151
/** Registry of setting items for the Assets page. Add new items here */
1252
const ASSET_SETTING_ITEMS: SettingItemConfig[] = [

ui/pages/settings-v2/assets-tab/autodetect-tokens-item.test.tsx

Lines changed: 0 additions & 99 deletions
This file was deleted.

ui/pages/settings-v2/assets-tab/autodetect-tokens-item.tsx

Lines changed: 0 additions & 22 deletions
This file was deleted.

ui/pages/settings-v2/assets-tab/hide-zero-balance-tokens-item.test.tsx

Lines changed: 0 additions & 115 deletions
This file was deleted.

ui/pages/settings-v2/assets-tab/hide-zero-balance-tokens-item.tsx

Lines changed: 0 additions & 22 deletions
This file was deleted.

0 commit comments

Comments
 (0)