Hide zero balance tokens with preference (#10486)

feature/default_network_editable
David Walsh 4 years ago committed by GitHub
parent 3ba91df387
commit aeffe176b3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 3
      app/_locales/en/messages.json
  2. 1
      app/scripts/controllers/preferences.js
  3. 20
      ui/app/components/app/token-list/token-list.js
  4. 3
      ui/app/ducks/metamask/metamask.js
  5. 28
      ui/app/pages/settings/settings-tab/settings-tab.component.js
  6. 9
      ui/app/pages/settings/settings-tab/settings-tab.container.js
  7. 10
      ui/app/pages/settings/settings-tab/tests/settings-tab.test.js
  8. 5
      ui/app/selectors/selectors.js
  9. 4
      ui/app/store/actions.js

@ -843,6 +843,9 @@
"message": "Hide $1", "message": "Hide $1",
"description": "$1 is the symbol for a token (e.g. 'DAI')" "description": "$1 is the symbol for a token (e.g. 'DAI')"
}, },
"hideZeroBalanceTokens": {
"message": "Hide Tokens Without Balance"
},
"history": { "history": {
"message": "History" "message": "History"
}, },

@ -61,6 +61,7 @@ export default class PreferencesController {
autoLockTimeLimit: undefined, autoLockTimeLimit: undefined,
showFiatInTestnets: false, showFiatInTestnets: false,
useNativeCurrencyAsPrimaryCurrency: true, useNativeCurrencyAsPrimaryCurrency: true,
hideZeroBalanceTokens: false,
}, },
completedOnboarding: false, completedOnboarding: false,
// ENS decentralized website resolution // ENS decentralized website resolution

@ -6,17 +6,31 @@ import { useSelector } from 'react-redux';
import TokenCell from '../token-cell'; import TokenCell from '../token-cell';
import { useI18nContext } from '../../../hooks/useI18nContext'; import { useI18nContext } from '../../../hooks/useI18nContext';
import { useTokenTracker } from '../../../hooks/useTokenTracker'; import { useTokenTracker } from '../../../hooks/useTokenTracker';
import { getAssetImages } from '../../../selectors'; import {
import { getTokens } from '../../../ducks/metamask/metamask'; getAssetImages,
getShouldHideZeroBalanceTokens,
} from '../../../selectors';
import {
getTokens,
getTokensWithBalance,
} from '../../../ducks/metamask/metamask';
export default function TokenList({ onTokenClick }) { export default function TokenList({ onTokenClick }) {
const t = useI18nContext(); const t = useI18nContext();
const assetImages = useSelector(getAssetImages); const assetImages = useSelector(getAssetImages);
const shouldHideZeroBalanceTokens = useSelector(
getShouldHideZeroBalanceTokens,
);
// use `isEqual` comparison function because the token array is serialized // use `isEqual` comparison function because the token array is serialized
// from the background so it has a new reference with each background update, // from the background so it has a new reference with each background update,
// even if the tokens haven't changed // even if the tokens haven't changed
const tokens = useSelector(getTokens, isEqual); const tokens = useSelector(getTokens, isEqual);
const { loading, tokensWithBalances } = useTokenTracker(tokens, true); const tokensWithBalance = useSelector(getTokensWithBalance, isEqual);
const { loading, tokensWithBalances } = useTokenTracker(
shouldHideZeroBalanceTokens ? tokensWithBalance : tokens,
true,
);
if (loading) { if (loading) {
return ( return (

@ -386,3 +386,6 @@ export const getUnconnectedAccountAlertShown = (state) =>
state.metamask.unconnectedAccountAlertShownOrigins; state.metamask.unconnectedAccountAlertShownOrigins;
export const getTokens = (state) => state.metamask.tokens; export const getTokens = (state) => state.metamask.tokens;
export const getTokensWithBalance = (state) =>
state.metamask.tokens.filter((token) => Number(token.balance) > 0);

@ -41,6 +41,8 @@ export default class SettingsTab extends PureComponent {
nativeCurrency: PropTypes.string, nativeCurrency: PropTypes.string,
useNativeCurrencyAsPrimaryCurrency: PropTypes.bool, useNativeCurrencyAsPrimaryCurrency: PropTypes.bool,
setUseNativeCurrencyAsPrimaryCurrencyPreference: PropTypes.func, setUseNativeCurrencyAsPrimaryCurrencyPreference: PropTypes.func,
hideZeroBalanceTokens: PropTypes.bool,
setHideZeroBalanceTokens: PropTypes.func,
}; };
renderCurrentConversion() { renderCurrentConversion() {
@ -101,12 +103,35 @@ export default class SettingsTab extends PureComponent {
); );
} }
renderHideZeroBalanceTokensOptIn() {
const { t } = this.context;
const { hideZeroBalanceTokens, setHideZeroBalanceTokens } = this.props;
return (
<div className="settings-page__content-row" id="toggle-zero-balance">
<div className="settings-page__content-item">
<span>{t('hideZeroBalanceTokens')}</span>
</div>
<div className="settings-page__content-item">
<div className="settings-page__content-item-col">
<ToggleButton
value={hideZeroBalanceTokens}
onToggle={(value) => setHideZeroBalanceTokens(!value)}
offLabel={t('off')}
onLabel={t('on')}
/>
</div>
</div>
</div>
);
}
renderBlockieOptIn() { renderBlockieOptIn() {
const { t } = this.context; const { t } = this.context;
const { useBlockie, setUseBlockie } = this.props; const { useBlockie, setUseBlockie } = this.props;
return ( return (
<div className="settings-page__content-row"> <div className="settings-page__content-row" id="blockie-optin">
<div className="settings-page__content-item"> <div className="settings-page__content-item">
<span>{this.context.t('blockiesIdenticon')}</span> <span>{this.context.t('blockiesIdenticon')}</span>
</div> </div>
@ -192,6 +217,7 @@ export default class SettingsTab extends PureComponent {
{this.renderUsePrimaryCurrencyOptions()} {this.renderUsePrimaryCurrencyOptions()}
{this.renderCurrentLocale()} {this.renderCurrentLocale()}
{this.renderBlockieOptIn()} {this.renderBlockieOptIn()}
{this.renderHideZeroBalanceTokensOptIn()}
</div> </div>
); );
} }

@ -4,6 +4,7 @@ import {
setUseBlockie, setUseBlockie,
updateCurrentLocale, updateCurrentLocale,
setUseNativeCurrencyAsPrimaryCurrencyPreference, setUseNativeCurrencyAsPrimaryCurrencyPreference,
setHideZeroBalanceTokens,
setParticipateInMetaMetrics, setParticipateInMetaMetrics,
} from '../../../store/actions'; } from '../../../store/actions';
import { getPreferences } from '../../../selectors'; import { getPreferences } from '../../../selectors';
@ -21,7 +22,10 @@ const mapStateToProps = (state) => {
useBlockie, useBlockie,
currentLocale, currentLocale,
} = metamask; } = metamask;
const { useNativeCurrencyAsPrimaryCurrency } = getPreferences(state); const {
useNativeCurrencyAsPrimaryCurrency,
hideZeroBalanceTokens,
} = getPreferences(state);
return { return {
warning, warning,
@ -31,6 +35,7 @@ const mapStateToProps = (state) => {
nativeCurrency, nativeCurrency,
useBlockie, useBlockie,
useNativeCurrencyAsPrimaryCurrency, useNativeCurrencyAsPrimaryCurrency,
hideZeroBalanceTokens,
}; };
}; };
@ -44,6 +49,8 @@ const mapDispatchToProps = (dispatch) => {
}, },
setParticipateInMetaMetrics: (val) => setParticipateInMetaMetrics: (val) =>
dispatch(setParticipateInMetaMetrics(val)), dispatch(setParticipateInMetaMetrics(val)),
setHideZeroBalanceTokens: (value) =>
dispatch(setHideZeroBalanceTokens(value)),
}; };
}; };

@ -13,6 +13,7 @@ describe('Settings Tab', function () {
setUseBlockie: sinon.spy(), setUseBlockie: sinon.spy(),
updateCurrentLocale: sinon.spy(), updateCurrentLocale: sinon.spy(),
setUseNativeCurrencyAsPrimaryCurrencyPreference: sinon.spy(), setUseNativeCurrencyAsPrimaryCurrencyPreference: sinon.spy(),
setHideZeroBalanceTokens: sinon.spy(),
warning: '', warning: '',
currentLocale: 'en', currentLocale: 'en',
useBlockie: false, useBlockie: false,
@ -51,9 +52,16 @@ describe('Settings Tab', function () {
}); });
it('toggles blockies', function () { it('toggles blockies', function () {
const toggleBlockies = wrapper.find({ type: 'checkbox' }); const toggleBlockies = wrapper.find('#blockie-optin input');
toggleBlockies.simulate('click'); toggleBlockies.simulate('click');
assert(props.setUseBlockie.calledOnce); assert(props.setUseBlockie.calledOnce);
}); });
it('toggles hiding zero balance', function () {
const toggleBlockies = wrapper.find('#toggle-zero-balance input');
toggleBlockies.simulate('click');
assert(props.setHideZeroBalanceTokens.calledOnce);
});
}); });

@ -320,6 +320,11 @@ export function getShouldShowFiat(state) {
return Boolean(isMainNet || showFiatInTestnets); return Boolean(isMainNet || showFiatInTestnets);
} }
export function getShouldHideZeroBalanceTokens(state) {
const { hideZeroBalanceTokens } = getPreferences(state);
return hideZeroBalanceTokens;
}
export function getAdvancedInlineGasShown(state) { export function getAdvancedInlineGasShown(state) {
return Boolean(state.metamask.featureFlags.advancedInlineGas); return Boolean(state.metamask.featureFlags.advancedInlineGas);
} }

@ -2046,6 +2046,10 @@ export function setUseNativeCurrencyAsPrimaryCurrencyPreference(value) {
return setPreference('useNativeCurrencyAsPrimaryCurrency', value); return setPreference('useNativeCurrencyAsPrimaryCurrency', value);
} }
export function setHideZeroBalanceTokens(value) {
return setPreference('hideZeroBalanceTokens', value);
}
export function setShowFiatConversionOnTestnetsPreference(value) { export function setShowFiatConversionOnTestnetsPreference(value) {
return setPreference('showFiatInTestnets', value); return setPreference('showFiatInTestnets', value);
} }

Loading…
Cancel
Save