2020-04-01 00:10:02 +02:00
|
|
|
import { connect } from 'react-redux'
|
|
|
|
import ConnectedSites from './connected-sites.component'
|
2020-05-01 00:44:12 +02:00
|
|
|
import {
|
|
|
|
getOpenMetamaskTabsIds,
|
2020-05-28 00:09:59 +02:00
|
|
|
requestAccountsPermission,
|
2020-05-01 00:44:12 +02:00
|
|
|
removePermissionsFor,
|
2020-05-02 00:50:03 +02:00
|
|
|
removePermittedAccount,
|
2020-05-01 00:44:12 +02:00
|
|
|
} from '../../store/actions'
|
2020-04-01 00:10:02 +02:00
|
|
|
import {
|
2020-04-09 01:08:48 +02:00
|
|
|
getConnectedDomainsForSelectedAddress,
|
2020-04-01 00:10:02 +02:00
|
|
|
getCurrentAccountWithSendEtherInfo,
|
2020-05-02 00:50:03 +02:00
|
|
|
getOriginOfCurrentTab,
|
2020-05-02 21:41:17 +02:00
|
|
|
getPermissionDomains,
|
2020-06-09 22:56:24 +02:00
|
|
|
getPermissionsMetadataHostCounts,
|
2020-05-02 03:52:23 +02:00
|
|
|
getPermittedAccountsByOrigin,
|
2020-05-02 21:41:17 +02:00
|
|
|
getSelectedAddress,
|
|
|
|
} from '../../selectors'
|
2020-06-01 19:54:32 +02:00
|
|
|
import { CONNECT_ROUTE } from '../../helpers/constants/routes'
|
|
|
|
import { getMostRecentOverviewPage } from '../../ducks/history/history'
|
2020-04-01 00:10:02 +02:00
|
|
|
|
|
|
|
const mapStateToProps = (state) => {
|
|
|
|
const { openMetaMaskTabs } = state.appState
|
2020-05-19 19:17:55 +02:00
|
|
|
const { id } = state.activeTab
|
2020-04-09 01:08:48 +02:00
|
|
|
const connectedDomains = getConnectedDomainsForSelectedAddress(state)
|
2020-05-02 00:50:03 +02:00
|
|
|
const originOfCurrentTab = getOriginOfCurrentTab(state)
|
|
|
|
const permittedAccountsByOrigin = getPermittedAccountsByOrigin(state)
|
|
|
|
const selectedAddress = getSelectedAddress(state)
|
|
|
|
|
2020-05-02 03:52:23 +02:00
|
|
|
const currentTabHasNoAccounts = !permittedAccountsByOrigin[
|
2020-05-02 00:50:03 +02:00
|
|
|
originOfCurrentTab
|
|
|
|
]?.length
|
2020-04-01 00:10:02 +02:00
|
|
|
|
|
|
|
let tabToConnect
|
2020-05-19 00:34:51 +02:00
|
|
|
if (originOfCurrentTab && currentTabHasNoAccounts && !openMetaMaskTabs[id]) {
|
2020-04-01 00:10:02 +02:00
|
|
|
tabToConnect = {
|
2020-05-19 00:34:51 +02:00
|
|
|
origin: originOfCurrentTab,
|
2020-04-01 00:10:02 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return {
|
|
|
|
accountLabel: getCurrentAccountWithSendEtherInfo(state).name,
|
2020-04-08 14:44:54 +02:00
|
|
|
connectedDomains,
|
2020-05-02 21:41:17 +02:00
|
|
|
domains: getPermissionDomains(state),
|
2020-06-09 22:56:24 +02:00
|
|
|
domainHostCount: getPermissionsMetadataHostCounts(state),
|
2020-06-01 19:54:32 +02:00
|
|
|
mostRecentOverviewPage: getMostRecentOverviewPage(state),
|
2020-05-02 00:50:03 +02:00
|
|
|
permittedAccountsByOrigin,
|
|
|
|
selectedAddress,
|
2020-04-01 00:10:02 +02:00
|
|
|
tabToConnect,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const mapDispatchToProps = (dispatch) => {
|
|
|
|
return {
|
|
|
|
getOpenMetamaskTabsIds: () => dispatch(getOpenMetamaskTabsIds()),
|
2020-05-02 00:50:03 +02:00
|
|
|
disconnectAccount: (domainKey, address) => {
|
|
|
|
dispatch(removePermittedAccount(domainKey, address))
|
|
|
|
},
|
|
|
|
disconnectAllAccounts: (domainKey, domain) => {
|
2020-04-01 00:10:02 +02:00
|
|
|
const permissionMethodNames = domain.permissions.map(({ parentCapability }) => parentCapability)
|
|
|
|
dispatch(removePermissionsFor({
|
|
|
|
[domainKey]: permissionMethodNames,
|
|
|
|
}))
|
|
|
|
},
|
2020-05-28 00:09:59 +02:00
|
|
|
requestAccountsPermission: (origin) => dispatch(requestAccountsPermission(origin)),
|
2020-04-01 00:10:02 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const mergeProps = (stateProps, dispatchProps, ownProps) => {
|
|
|
|
const {
|
2020-05-02 00:50:03 +02:00
|
|
|
connectedDomains,
|
2020-04-30 23:19:55 +02:00
|
|
|
domains,
|
2020-06-01 19:54:32 +02:00
|
|
|
mostRecentOverviewPage,
|
2020-04-30 23:19:55 +02:00
|
|
|
selectedAddress,
|
|
|
|
tabToConnect,
|
|
|
|
} = stateProps
|
|
|
|
const {
|
2020-05-02 00:50:03 +02:00
|
|
|
disconnectAccount,
|
|
|
|
disconnectAllAccounts,
|
2020-05-28 00:09:59 +02:00
|
|
|
requestAccountsPermission: dispatchRequestAccountsPermission,
|
2020-04-01 00:10:02 +02:00
|
|
|
} = dispatchProps
|
2020-04-30 23:19:55 +02:00
|
|
|
const { history } = ownProps
|
|
|
|
|
2020-06-01 19:54:32 +02:00
|
|
|
const closePopover = () => history.push(mostRecentOverviewPage)
|
2020-04-01 00:10:02 +02:00
|
|
|
|
|
|
|
return {
|
|
|
|
...ownProps,
|
|
|
|
...stateProps,
|
|
|
|
...dispatchProps,
|
2020-04-30 23:19:55 +02:00
|
|
|
closePopover,
|
2020-05-02 00:50:03 +02:00
|
|
|
disconnectAccount: (domainKey) => {
|
|
|
|
disconnectAccount(domainKey, selectedAddress)
|
|
|
|
if (connectedDomains.length === 1) {
|
|
|
|
closePopover()
|
|
|
|
}
|
|
|
|
},
|
|
|
|
disconnectAllAccounts: (domainKey) => {
|
|
|
|
disconnectAllAccounts(domainKey, domains[domainKey])
|
2020-04-30 23:19:55 +02:00
|
|
|
if (connectedDomains.length === 1) {
|
|
|
|
closePopover()
|
|
|
|
}
|
|
|
|
},
|
2020-05-28 00:09:59 +02:00
|
|
|
requestAccountsPermission: async () => {
|
|
|
|
const id = await dispatchRequestAccountsPermission(tabToConnect.origin)
|
|
|
|
history.push(`${CONNECT_ROUTE}/${id}`)
|
|
|
|
},
|
2020-04-01 00:10:02 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export default connect(mapStateToProps, mapDispatchToProps, mergeProps)(ConnectedSites)
|