1
0
mirror of https://github.com/kremalicious/metamask-extension.git synced 2024-10-22 11:22:43 +02:00

Implemented functionality for displaying recent custom RPCs

This commit is contained in:
Kevin Serrano 2017-02-22 15:12:56 -08:00
parent 17a7436602
commit 7a0ce31bd3
No known key found for this signature in database
GPG Key ID: 7CC862A58D2889B4
3 changed files with 41 additions and 16 deletions

View File

@ -1,10 +1,11 @@
const ObservableStore = require('obs-store') const ObservableStore = require('obs-store')
const normalizeAddress = require('../sig-util').normalize const normalizeAddress = require('../sig-util').normalize
const extend = require('xtend')
class PreferencesController { class PreferencesController {
constructor (opts = {}) { constructor (opts = {}) {
const initState = opts.initState || { frequentRPCList: [] } const initState = extend({ frequentRpcList: [] }, opts.initState)
this.store = new ObservableStore(initState) this.store = new ObservableStore(initState)
} }
@ -25,23 +26,23 @@ class PreferencesController {
} }
addToFrequentRpcList (_url) { addToFrequentRpcList (_url) {
return new Promise((resolve, reject) => { let rpcList = this.getFrequentRpcList()
let rpcList = this.getFrequentRPCList() let index = rpcList.findIndex((element) => { return element === _url })
let index = rpcList.findIndex((element) => { element === _url }) if (index !== -1) {
if (index) { rpcList.splice(index, 1)
rpcList.splice(index, 1) }
} if (_url !== 'http://localhost:8545') {
if (rpcList.length >= 3) {
rpcList.shift()
}
rpcList.push(_url) rpcList.push(_url)
this.store.updateState({ frequentRPCList: rpcList }) }
resolve() if (rpcList.length > 2) {
}) rpcList.shift()
}
this.store.updateState({ frequentRpcList: rpcList })
return Promise.resolve()
} }
getFrequentRpcList () { getFrequentRpcList () {
return this.store.getState().frequentRPCList return this.store.getState().frequentRpcList
} }
// //

View File

@ -627,8 +627,9 @@ function markAccountsFound() {
function setRpcTarget (newRpc) { function setRpcTarget (newRpc) {
if (global.METAMASK_DEBUG) console.log(`background.setRpcTarget`) if (global.METAMASK_DEBUG) console.log(`background.setRpcTarget`)
background.setRpcTarget(newRpc) background.addToFrequentRpcList(newRpc, () => {
background.addToFrequentRpcList(newRpc) background.setRpcTarget(newRpc)
})
return { return {
type: actions.SET_RPC_TARGET, type: actions.SET_RPC_TARGET,
value: newRpc, value: newRpc,

View File

@ -58,6 +58,7 @@ function mapStateToProps (state) {
forgottenPassword: state.appState.forgottenPassword, forgottenPassword: state.appState.forgottenPassword,
lastUnreadNotice: state.metamask.lastUnreadNotice, lastUnreadNotice: state.metamask.lastUnreadNotice,
lostAccounts: state.metamask.lostAccounts, lostAccounts: state.metamask.lostAccounts,
frequentRpcList: state.metamask.frequentRpcList
} }
} }
@ -210,6 +211,7 @@ App.prototype.renderAppBar = function () {
App.prototype.renderNetworkDropdown = function () { App.prototype.renderNetworkDropdown = function () {
const props = this.props const props = this.props
const rpcList = props.frequentRpcList
const state = this.state || {} const state = this.state || {}
const isOpen = state.isNetworkMenuOpen const isOpen = state.isNetworkMenuOpen
@ -261,6 +263,7 @@ App.prototype.renderNetworkDropdown = function () {
}), }),
this.renderCustomOption(props.provider), this.renderCustomOption(props.provider),
this.renderCommonRpc(rpcList, props.provider),
props.isUnlocked && h(DropMenuItem, { props.isUnlocked && h(DropMenuItem, {
label: 'Custom RPC', label: 'Custom RPC',
@ -508,3 +511,23 @@ App.prototype.renderCustomOption = function (provider) {
}) })
} }
} }
App.prototype.renderCommonRpc = function (rpcList, provider) {
const { rpcTarget } = provider
const props = this.props
return rpcList.map((rpc) => {
if ((rpc === 'http://localhost:8545') || (rpc === rpcTarget)) {
return null
} else {
return h(DropMenuItem, {
label: rpc,
closeMenu: () => this.setState({ isNetworkMenuOpen: false }),
action: () => props.dispatch(actions.setRpcTarget(rpc)),
icon: h('i.fa.fa-question-circle.fa-lg'),
activeNetworkRender: rpc,
})
}
})
}