1
0
mirror of https://github.com/kremalicious/metamask-extension.git synced 2024-11-22 01:47:00 +01:00

Approvals selector refactor (#18664)

This commit is contained in:
Bernardo Garces Chapero 2023-04-25 11:47:49 +02:00 committed by GitHub
parent 64d4bfbbe9
commit c1614ec670
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 81 additions and 7 deletions

View File

@ -88,7 +88,7 @@ export default class Home extends PureComponent {
static propTypes = { static propTypes = {
history: PropTypes.object, history: PropTypes.object,
forgottenPassword: PropTypes.bool, forgottenPassword: PropTypes.bool,
suggestedAssets: PropTypes.array, hasWatchAssetPendingApprovals: PropTypes.bool,
unconfirmedTransactionsCount: PropTypes.number, unconfirmedTransactionsCount: PropTypes.number,
shouldShowSeedPhraseReminder: PropTypes.bool.isRequired, shouldShowSeedPhraseReminder: PropTypes.bool.isRequired,
isPopup: PropTypes.bool, isPopup: PropTypes.bool,
@ -169,7 +169,7 @@ export default class Home extends PureComponent {
haveSwapsQuotes, haveSwapsQuotes,
isNotification, isNotification,
showAwaitingSwapScreen, showAwaitingSwapScreen,
suggestedAssets = [], hasWatchAssetPendingApprovals,
swapsFetchParams, swapsFetchParams,
unconfirmedTransactionsCount, unconfirmedTransactionsCount,
} = this.props; } = this.props;
@ -180,7 +180,7 @@ export default class Home extends PureComponent {
} else if ( } else if (
firstPermissionsRequestId || firstPermissionsRequestId ||
unconfirmedTransactionsCount > 0 || unconfirmedTransactionsCount > 0 ||
suggestedAssets.length > 0 || hasWatchAssetPendingApprovals ||
(!isNotification && (!isNotification &&
(showAwaitingSwapScreen || haveSwapsQuotes || swapsFetchParams)) (showAwaitingSwapScreen || haveSwapsQuotes || swapsFetchParams))
) { ) {
@ -193,7 +193,7 @@ export default class Home extends PureComponent {
firstPermissionsRequestId, firstPermissionsRequestId,
history, history,
isNotification, isNotification,
suggestedAssets = [], hasWatchAssetPendingApprovals,
unconfirmedTransactionsCount, unconfirmedTransactionsCount,
haveSwapsQuotes, haveSwapsQuotes,
showAwaitingSwapScreen, showAwaitingSwapScreen,
@ -210,7 +210,7 @@ export default class Home extends PureComponent {
history.push(`${CONNECT_ROUTE}/${firstPermissionsRequestId}`); history.push(`${CONNECT_ROUTE}/${firstPermissionsRequestId}`);
} else if (unconfirmedTransactionsCount > 0) { } else if (unconfirmedTransactionsCount > 0) {
history.push(CONFIRM_TRANSACTION_ROUTE); history.push(CONFIRM_TRANSACTION_ROUTE);
} else if (suggestedAssets.length > 0) { } else if (hasWatchAssetPendingApprovals) {
history.push(CONFIRM_ADD_SUGGESTED_TOKEN_ROUTE); history.push(CONFIRM_ADD_SUGGESTED_TOKEN_ROUTE);
} else if (pendingConfirmations.length > 0) { } else if (pendingConfirmations.length > 0) {
history.push(CONFIRMATION_V_NEXT_ROUTE); history.push(CONFIRMATION_V_NEXT_ROUTE);

View File

@ -1,6 +1,7 @@
import { compose } from 'redux'; import { compose } from 'redux';
import { connect } from 'react-redux'; import { connect } from 'react-redux';
import { withRouter } from 'react-router-dom'; import { withRouter } from 'react-router-dom';
import { ApprovalType } from '@metamask/controller-utils';
import { import {
activeTabHasPermissions, activeTabHasPermissions,
getFirstPermissionRequest, getFirstPermissionRequest,
@ -26,6 +27,7 @@ import {
getNewTokensImported, getNewTokensImported,
getShouldShowSeedPhraseReminder, getShouldShowSeedPhraseReminder,
getRemoveNftMessage, getRemoveNftMessage,
hasPendingApprovalsSelector,
} from '../../selectors'; } from '../../selectors';
import { import {
@ -65,7 +67,6 @@ import Home from './home.component';
const mapStateToProps = (state) => { const mapStateToProps = (state) => {
const { metamask, appState } = state; const { metamask, appState } = state;
const { const {
suggestedAssets,
seedPhraseBackedUp, seedPhraseBackedUp,
selectedAddress, selectedAddress,
connectedStatusPopoverHasBeenShown, connectedStatusPopoverHasBeenShown,
@ -108,9 +109,14 @@ const mapStateToProps = (state) => {
hasUnsignedQRHardwareTransaction(state) || hasUnsignedQRHardwareTransaction(state) ||
hasUnsignedQRHardwareMessage(state); hasUnsignedQRHardwareMessage(state);
const hasWatchAssetPendingApprovals = hasPendingApprovalsSelector(
state,
ApprovalType.WatchAsset,
);
return { return {
forgottenPassword, forgottenPassword,
suggestedAssets, hasWatchAssetPendingApprovals,
swapsEnabled, swapsEnabled,
unconfirmedTransactionsCount: unconfirmedTransactionsCountSelector(state), unconfirmedTransactionsCount: unconfirmedTransactionsCountSelector(state),
shouldShowSeedPhraseReminder: getShouldShowSeedPhraseReminder(state), shouldShowSeedPhraseReminder: getShouldShowSeedPhraseReminder(state),

View File

@ -0,0 +1,48 @@
import { ApprovalType } from '@metamask/controller-utils';
import { hasPendingApprovalsSelector } from './approvals';
describe('approval selectors', () => {
const mockedState = {
metamask: {
pendingApprovalCount: 2,
pendingApprovals: {
'1': {
id: '1',
origin: 'origin',
time: Date.now(),
type: ApprovalType.WatchAsset,
requestData: {},
requestState: null,
},
'2': {
id: '2',
origin: 'origin',
time: Date.now(),
type: ApprovalType.EthSignTypedData,
requestData: {},
requestState: null,
},
},
},
};
describe('hasPendingApprovalsSelector', () => {
it('should return true if there is a pending approval request', () => {
const result = hasPendingApprovalsSelector(
mockedState,
ApprovalType.WatchAsset,
);
expect(result).toBe(true);
});
it('should return false if there is no pending approval request', () => {
const result = hasPendingApprovalsSelector(
mockedState,
ApprovalType.Transaction,
);
expect(result).toBe(false);
});
});
});

19
ui/selectors/approvals.ts Normal file
View File

@ -0,0 +1,19 @@
import { ApprovalControllerState } from '@metamask/approval-controller';
import { ApprovalType } from '@metamask/controller-utils';
type ApprovalsMetaMaskState = {
metamask: {
pendingApprovals: ApprovalControllerState['pendingApprovals'];
};
};
export function hasPendingApprovalsSelector(
state: ApprovalsMetaMaskState,
approvalType: ApprovalType,
) {
const pendingApprovalRequests = Object.values(
state.metamask.pendingApprovals,
).filter(({ type }) => type === approvalType);
return pendingApprovalRequests.length > 0;
}

View File

@ -5,3 +5,4 @@ export * from './metametrics';
export * from './permissions'; export * from './permissions';
export * from './selectors'; export * from './selectors';
export * from './transactions'; export * from './transactions';
export * from './approvals';