1
0
mirror of https://github.com/kremalicious/metamask-extension.git synced 2024-11-26 20:39:08 +01:00
metamask-extension/ui/pages/permissions-connect/permissions-connect.container.js
Hassan Malik a7179a6b88
[FLASK] Add update snap UI (#15143)
* added snap-update folder

* addded update route, snap update component, updated permissions connect components

* added actions and selectors

* updated permissions selectors and updated permissions connect container to have update snap logic

* updated translations, added selector, updated request object

* updated translations, added update snap permission list component

* more fixes

* added CSS, redid some HTML

* lint fixes

* Add missing grantPermissions action

* updated button padding

* fixes

* removed prop type

* fix Update & Install wrapping

* made changes for forthcoming snap controller PR

* removed ununsed imports

* updated css

* re-added padding rule and removed unused translation messages

* addressed comments

* add subtext for new permissions

* lint fix

* removed unused translations

* some more changes

* fix e2e tests

* lint fix

* added in delay for e2e tests

* Revert "added in delay for e2e tests"

This reverts commit 095962a2c0c9de0b0b343d3134bb0787044dd8ce.

* fixed routing logic

Co-authored-by: Frederik Bolding <frederik.bolding@gmail.com>
Co-authored-by: Guillaume Roux <guillaumeroux123@gmail.com>
2022-08-03 12:02:44 -04:00

171 lines
4.9 KiB
JavaScript

import { connect } from 'react-redux';
import PropTypes from 'prop-types';
import {
getAccountsWithLabels,
getLastConnectedInfo,
getPermissionsRequests,
getSelectedAddress,
///: BEGIN:ONLY_INCLUDE_IN(flask)
getSnapUpdateRequests,
///: END:ONLY_INCLUDE_IN
getTargetSubjectMetadata,
} from '../../selectors';
import { getNativeCurrency } from '../../ducks/metamask/metamask';
import { formatDate, getURLHostName } from '../../helpers/utils/util';
import {
approvePermissionsRequest,
rejectPermissionsRequest,
showModal,
getCurrentWindowTab,
getRequestAccountTabIds,
} from '../../store/actions';
import {
CONNECT_ROUTE,
CONNECT_CONFIRM_PERMISSIONS_ROUTE,
///: BEGIN:ONLY_INCLUDE_IN(flask)
CONNECT_SNAP_INSTALL_ROUTE,
CONNECT_SNAP_UPDATE_ROUTE,
///: END:ONLY_INCLUDE_IN
} from '../../helpers/constants/routes';
import { SUBJECT_TYPES } from '../../../shared/constants/app';
import PermissionApproval from './permissions-connect.component';
const mapStateToProps = (state, ownProps) => {
const {
match: {
params: { id: permissionsRequestId },
},
location: { pathname },
} = ownProps;
let permissionsRequests = getPermissionsRequests(state);
///: BEGIN:ONLY_INCLUDE_IN(flask)
permissionsRequests = [
...permissionsRequests,
...getSnapUpdateRequests(state),
];
///: END:ONLY_INCLUDE_IN
const currentAddress = getSelectedAddress(state);
const permissionsRequest = permissionsRequests.find(
(req) => req.metadata.id === permissionsRequestId,
);
const isRequestingAccounts = Boolean(
permissionsRequest?.permissions?.eth_accounts,
);
const { metadata = {} } = permissionsRequest || {};
const { origin } = metadata;
const nativeCurrency = getNativeCurrency(state);
const targetSubjectMetadata = getTargetSubjectMetadata(state, origin) ?? {
name: getURLHostName(origin) || origin,
origin,
iconUrl: null,
extensionId: null,
subjectType: SUBJECT_TYPES.UNKNOWN,
};
///: BEGIN:ONLY_INCLUDE_IN(flask)
const isSnap = targetSubjectMetadata.subjectType === SUBJECT_TYPES.SNAP;
///: END:ONLY_INCLUDE_IN
const accountsWithLabels = getAccountsWithLabels(state);
const lastConnectedInfo = getLastConnectedInfo(state) || {};
const addressLastConnectedMap = lastConnectedInfo[origin]?.accounts || {};
Object.keys(addressLastConnectedMap).forEach((key) => {
addressLastConnectedMap[key] = formatDate(
addressLastConnectedMap[key],
'yyyy-MM-dd',
);
});
const connectPath = `${CONNECT_ROUTE}/${permissionsRequestId}`;
const confirmPermissionPath = `${CONNECT_ROUTE}/${permissionsRequestId}${CONNECT_CONFIRM_PERMISSIONS_ROUTE}`;
///: BEGIN:ONLY_INCLUDE_IN(flask)
const snapInstallPath = `${CONNECT_ROUTE}/${permissionsRequestId}${CONNECT_SNAP_INSTALL_ROUTE}`;
const snapUpdatePath = `${CONNECT_ROUTE}/${permissionsRequestId}${CONNECT_SNAP_UPDATE_ROUTE}`;
///: END:ONLY_INCLUDE_IN
let totalPages = 1 + isRequestingAccounts;
///: BEGIN:ONLY_INCLUDE_IN(flask)
totalPages += isSnap;
///: END:ONLY_INCLUDE_IN
totalPages = totalPages.toString();
let page = '';
if (pathname === connectPath) {
page = '1';
} else if (pathname === confirmPermissionPath) {
page = isRequestingAccounts ? '2' : '1';
///: BEGIN:ONLY_INCLUDE_IN(flask)
} else if (pathname === snapInstallPath || pathname === snapUpdatePath) {
page = isRequestingAccounts ? '3' : '2';
///: END:ONLY_INCLUDE_IN
} else {
throw new Error('Incorrect path for permissions-connect component');
}
return {
isRequestingAccounts,
///: BEGIN:ONLY_INCLUDE_IN(flask)
isSnap,
snapInstallPath,
snapUpdatePath,
///: END:ONLY_INCLUDE_IN
permissionsRequest,
permissionsRequestId,
accounts: accountsWithLabels,
currentAddress,
origin,
newAccountNumber: accountsWithLabels.length + 1,
nativeCurrency,
addressLastConnectedMap,
lastConnectedInfo,
connectPath,
confirmPermissionPath,
totalPages,
page,
targetSubjectMetadata,
};
};
const mapDispatchToProps = (dispatch) => {
return {
approvePermissionsRequest: (request) =>
dispatch(approvePermissionsRequest(request)),
rejectPermissionsRequest: (requestId) =>
dispatch(rejectPermissionsRequest(requestId)),
showNewAccountModal: ({ onCreateNewAccount, newAccountNumber }) => {
return dispatch(
showModal({
name: 'NEW_ACCOUNT',
onCreateNewAccount,
newAccountNumber,
}),
);
},
getRequestAccountTabIds: () => dispatch(getRequestAccountTabIds()),
getCurrentWindowTab: () => dispatch(getCurrentWindowTab()),
};
};
const PermissionApprovalContainer = connect(
mapStateToProps,
mapDispatchToProps,
)(PermissionApproval);
PermissionApprovalContainer.propTypes = {
history: PropTypes.object.isRequired,
match: PropTypes.shape({
params: PropTypes.shape({
id: PropTypes.string,
}).isRequired,
}).isRequired,
};
export default PermissionApprovalContainer;