You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
119 lines
3.4 KiB
119 lines
3.4 KiB
4 years ago
|
import { connect } from 'react-redux';
|
||
5 years ago
|
import {
|
||
|
getOpenMetamaskTabsIds,
|
||
4 years ago
|
requestAccountsPermissionWithId,
|
||
5 years ago
|
removePermissionsFor,
|
||
5 years ago
|
removePermittedAccount,
|
||
4 years ago
|
} from '../../store/actions';
|
||
5 years ago
|
import {
|
||
5 years ago
|
getConnectedDomainsForSelectedAddress,
|
||
5 years ago
|
getCurrentAccountWithSendEtherInfo,
|
||
5 years ago
|
getOriginOfCurrentTab,
|
||
5 years ago
|
getPermissionDomains,
|
||
5 years ago
|
getPermissionsMetadataHostCounts,
|
||
5 years ago
|
getPermittedAccountsByOrigin,
|
||
5 years ago
|
getSelectedAddress,
|
||
4 years ago
|
} from '../../selectors';
|
||
|
import { CONNECT_ROUTE } from '../../helpers/constants/routes';
|
||
|
import { getMostRecentOverviewPage } from '../../ducks/history/history';
|
||
|
import ConnectedSites from './connected-sites.component';
|
||
5 years ago
|
|
||
|
const mapStateToProps = (state) => {
|
||
4 years ago
|
const { openMetaMaskTabs } = state.appState;
|
||
|
const { id } = state.activeTab;
|
||
|
const connectedDomains = getConnectedDomainsForSelectedAddress(state);
|
||
|
const originOfCurrentTab = getOriginOfCurrentTab(state);
|
||
|
const permittedAccountsByOrigin = getPermittedAccountsByOrigin(state);
|
||
|
const selectedAddress = getSelectedAddress(state);
|
||
5 years ago
|
|
||
4 years ago
|
const currentTabHasNoAccounts = !permittedAccountsByOrigin[originOfCurrentTab]
|
||
4 years ago
|
?.length;
|
||
5 years ago
|
|
||
4 years ago
|
let tabToConnect;
|
||
5 years ago
|
if (originOfCurrentTab && currentTabHasNoAccounts && !openMetaMaskTabs[id]) {
|
||
5 years ago
|
tabToConnect = {
|
||
5 years ago
|
origin: originOfCurrentTab,
|
||
4 years ago
|
};
|
||
5 years ago
|
}
|
||
|
|
||
|
return {
|
||
|
accountLabel: getCurrentAccountWithSendEtherInfo(state).name,
|
||
5 years ago
|
connectedDomains,
|
||
5 years ago
|
domains: getPermissionDomains(state),
|
||
5 years ago
|
domainHostCount: getPermissionsMetadataHostCounts(state),
|
||
5 years ago
|
mostRecentOverviewPage: getMostRecentOverviewPage(state),
|
||
5 years ago
|
permittedAccountsByOrigin,
|
||
|
selectedAddress,
|
||
5 years ago
|
tabToConnect,
|
||
4 years ago
|
};
|
||
|
};
|
||
5 years ago
|
|
||
|
const mapDispatchToProps = (dispatch) => {
|
||
|
return {
|
||
|
getOpenMetamaskTabsIds: () => dispatch(getOpenMetamaskTabsIds()),
|
||
5 years ago
|
disconnectAccount: (domainKey, address) => {
|
||
4 years ago
|
dispatch(removePermittedAccount(domainKey, address));
|
||
5 years ago
|
},
|
||
|
disconnectAllAccounts: (domainKey, domain) => {
|
||
4 years ago
|
const permissionMethodNames = domain.permissions.map(
|
||
|
({ parentCapability }) => parentCapability,
|
||
4 years ago
|
);
|
||
4 years ago
|
dispatch(
|
||
|
removePermissionsFor({
|
||
|
[domainKey]: permissionMethodNames,
|
||
|
}),
|
||
4 years ago
|
);
|
||
5 years ago
|
},
|
||
4 years ago
|
requestAccountsPermissionWithId: (origin) =>
|
||
|
dispatch(requestAccountsPermissionWithId(origin)),
|
||
4 years ago
|
};
|
||
|
};
|
||
5 years ago
|
|
||
|
const mergeProps = (stateProps, dispatchProps, ownProps) => {
|
||
|
const {
|
||
5 years ago
|
connectedDomains,
|
||
5 years ago
|
domains,
|
||
5 years ago
|
mostRecentOverviewPage,
|
||
5 years ago
|
selectedAddress,
|
||
|
tabToConnect,
|
||
4 years ago
|
} = stateProps;
|
||
5 years ago
|
const {
|
||
5 years ago
|
disconnectAccount,
|
||
|
disconnectAllAccounts,
|
||
4 years ago
|
// eslint-disable-next-line no-shadow
|
||
4 years ago
|
requestAccountsPermissionWithId,
|
||
4 years ago
|
} = dispatchProps;
|
||
|
const { history } = ownProps;
|
||
5 years ago
|
|
||
4 years ago
|
const closePopover = () => history.push(mostRecentOverviewPage);
|
||
5 years ago
|
|
||
|
return {
|
||
|
...ownProps,
|
||
|
...stateProps,
|
||
|
...dispatchProps,
|
||
5 years ago
|
closePopover,
|
||
5 years ago
|
disconnectAccount: (domainKey) => {
|
||
4 years ago
|
disconnectAccount(domainKey, selectedAddress);
|
||
5 years ago
|
if (connectedDomains.length === 1) {
|
||
4 years ago
|
closePopover();
|
||
5 years ago
|
}
|
||
|
},
|
||
|
disconnectAllAccounts: (domainKey) => {
|
||
4 years ago
|
disconnectAllAccounts(domainKey, domains[domainKey]);
|
||
5 years ago
|
if (connectedDomains.length === 1) {
|
||
4 years ago
|
closePopover();
|
||
5 years ago
|
}
|
||
|
},
|
||
5 years ago
|
requestAccountsPermission: async () => {
|
||
4 years ago
|
const id = await requestAccountsPermissionWithId(tabToConnect.origin);
|
||
|
history.push(`${CONNECT_ROUTE}/${id}`);
|
||
5 years ago
|
},
|
||
4 years ago
|
};
|
||
|
};
|
||
5 years ago
|
|
||
4 years ago
|
export default connect(
|
||
|
mapStateToProps,
|
||
|
mapDispatchToProps,
|
||
|
mergeProps,
|
||
4 years ago
|
)(ConnectedSites);
|