1
0
mirror of https://github.com/kremalicious/metamask-extension.git synced 2024-12-18 07:23:21 +01:00
metamask-extension/ui/app/pages/connected-sites/connected-sites.container.js

119 lines
3.3 KiB
JavaScript
Raw Normal View History

import { connect } from 'react-redux'
import {
getOpenMetamaskTabsIds,
requestAccountsPermissionWithId,
removePermissionsFor,
removePermittedAccount,
} from '../../store/actions'
import {
getConnectedDomainsForSelectedAddress,
getCurrentAccountWithSendEtherInfo,
getOriginOfCurrentTab,
2020-05-02 21:41:17 +02:00
getPermissionDomains,
getPermissionsMetadataHostCounts,
2020-05-02 03:52:23 +02:00
getPermittedAccountsByOrigin,
2020-05-02 21:41:17 +02:00
getSelectedAddress,
} from '../../selectors'
import { CONNECT_ROUTE } from '../../helpers/constants/routes'
import { getMostRecentOverviewPage } from '../../ducks/history/history'
import ConnectedSites from './connected-sites.component'
const mapStateToProps = (state) => {
const { openMetaMaskTabs } = state.appState
const { id } = state.activeTab
const connectedDomains = getConnectedDomainsForSelectedAddress(state)
const originOfCurrentTab = getOriginOfCurrentTab(state)
const permittedAccountsByOrigin = getPermittedAccountsByOrigin(state)
const selectedAddress = getSelectedAddress(state)
2020-11-03 00:41:28 +01:00
const currentTabHasNoAccounts = !permittedAccountsByOrigin[originOfCurrentTab]
?.length
let tabToConnect
if (originOfCurrentTab && currentTabHasNoAccounts && !openMetaMaskTabs[id]) {
tabToConnect = {
origin: originOfCurrentTab,
}
}
return {
accountLabel: getCurrentAccountWithSendEtherInfo(state).name,
connectedDomains,
2020-05-02 21:41:17 +02:00
domains: getPermissionDomains(state),
domainHostCount: getPermissionsMetadataHostCounts(state),
mostRecentOverviewPage: getMostRecentOverviewPage(state),
permittedAccountsByOrigin,
selectedAddress,
tabToConnect,
}
}
const mapDispatchToProps = (dispatch) => {
return {
getOpenMetamaskTabsIds: () => dispatch(getOpenMetamaskTabsIds()),
disconnectAccount: (domainKey, address) => {
dispatch(removePermittedAccount(domainKey, address))
},
disconnectAllAccounts: (domainKey, domain) => {
2020-11-03 00:41:28 +01:00
const permissionMethodNames = domain.permissions.map(
({ parentCapability }) => parentCapability,
)
dispatch(
removePermissionsFor({
[domainKey]: permissionMethodNames,
}),
)
},
2020-11-03 00:41:28 +01:00
requestAccountsPermissionWithId: (origin) =>
dispatch(requestAccountsPermissionWithId(origin)),
}
}
const mergeProps = (stateProps, dispatchProps, ownProps) => {
const {
connectedDomains,
domains,
mostRecentOverviewPage,
selectedAddress,
tabToConnect,
} = stateProps
const {
disconnectAccount,
disconnectAllAccounts,
// eslint-disable-next-line no-shadow
requestAccountsPermissionWithId,
} = dispatchProps
const { history } = ownProps
const closePopover = () => history.push(mostRecentOverviewPage)
return {
...ownProps,
...stateProps,
...dispatchProps,
closePopover,
disconnectAccount: (domainKey) => {
disconnectAccount(domainKey, selectedAddress)
if (connectedDomains.length === 1) {
closePopover()
}
},
disconnectAllAccounts: (domainKey) => {
disconnectAllAccounts(domainKey, domains[domainKey])
if (connectedDomains.length === 1) {
closePopover()
}
},
requestAccountsPermission: async () => {
const id = await requestAccountsPermissionWithId(tabToConnect.origin)
history.push(`${CONNECT_ROUTE}/${id}`)
},
}
}
2020-11-03 00:41:28 +01:00
export default connect(
mapStateToProps,
mapDispatchToProps,
mergeProps,
)(ConnectedSites)