1
0
mirror of https://github.com/kremalicious/metamask-extension.git synced 2024-11-23 02:10:12 +01:00
metamask-extension/ui/pages/permissions-connect/permissions-connect.container.js
Hassan Malik f946c030b5
Choose accounts refactor (#13039)
* added wrapper around account list to prevent storybook from collapsing the list

* updated translation files

* added snap-connect page

* refactored account list out of the choose account component

* fixed width

* removed unnecessary scss from choose-account component, fixed props in choose account story

* removed snaps-connect page, added comments to ChooseAccount

* updated choose account subtitle text, updated styling for title & subtitle, removed redundant account list story

* updated component name, updated paths

* fixed linter errors

* added comments

* removed unused message

* removed selectAccounts key from all locales

* updated class name for account list header, updated allAreSelected function to use length checks

* Revert "removed unused message"

This reverts commit 32771bc83c08f120825ef75f0741f3034e7dbecb.

* Revert "removed selectAccounts key from all locales"

This reverts commit ccfa4a860f9a75693d893d7c404384e719de297e.

* updated locale messages to use selectAccounts key

* removed stray import

* updated scss

* updated translation key

* removed chooseAccounts key from en locale

* removed optional chaining

* changes

* updated subjectMetadata

* updated subject types

* update useOriginMetadata function to include unknown subject type

* updated permission connect header props, removed host and added subjectType to targetSubjectMetadata

* added subjectType to targetSubjectMetadata

* removed console.log

* changed prop name to iconUrl
2021-12-14 18:54:46 -05:00

139 lines
3.7 KiB
JavaScript

import { connect } from 'react-redux';
import PropTypes from 'prop-types';
import {
getPermissionsRequests,
getAccountsWithLabels,
getLastConnectedInfo,
getSubjectMetadata,
getSelectedAddress,
} from '../../selectors';
import { getNativeCurrency } from '../../ducks/metamask/metamask';
import { formatDate } from '../../helpers/utils/util';
import {
approvePermissionsRequest,
rejectPermissionsRequest,
showModal,
getCurrentWindowTab,
getRequestAccountTabIds,
} from '../../store/actions';
import {
CONNECT_ROUTE,
CONNECT_CONFIRM_PERMISSIONS_ROUTE,
} 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;
const permissionsRequests = getPermissionsRequests(state);
const currentAddress = getSelectedAddress(state);
const permissionsRequest = permissionsRequests.find(
(req) => req.metadata.id === permissionsRequestId,
);
const { metadata = {} } = permissionsRequest || {};
const { origin } = metadata;
const nativeCurrency = getNativeCurrency(state);
const subjectMetadata = getSubjectMetadata(state);
let targetSubjectMetadata = null;
if (origin) {
if (subjectMetadata[origin]) {
targetSubjectMetadata = subjectMetadata[origin];
} else {
const targetUrl = new URL(origin);
targetSubjectMetadata = {
name: targetUrl.hostname,
origin,
iconUrl: null,
extensionId: null,
subjectType: SUBJECT_TYPES.UNKNOWN,
};
}
}
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}`;
let page = '';
if (pathname === connectPath) {
page = '1';
} else if (pathname === confirmPermissionPath) {
page = '2';
} else {
throw new Error('Incorrect path for permissions-connect component');
}
return {
permissionsRequest,
permissionsRequestId,
accounts: accountsWithLabels,
currentAddress,
origin,
newAccountNumber: accountsWithLabels.length + 1,
nativeCurrency,
addressLastConnectedMap,
lastConnectedInfo,
connectPath,
confirmPermissionPath,
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;