Skip to content
This repository has been archived by the owner on Jan 26, 2022. It is now read-only.

Commit

Permalink
Fixes brave/brave-browser#4773 - adds BAT token by default to accounts
Browse files Browse the repository at this point in the history
  • Loading branch information
ryanml committed Jul 9, 2019
1 parent 57a3595 commit 069ce49
Show file tree
Hide file tree
Showing 16 changed files with 635 additions and 12 deletions.
9 changes: 8 additions & 1 deletion .babelrc
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,13 @@
"transform-runtime",
"transform-async-to-generator",
"transform-class-properties",
"transform-object-rest-spread"
"transform-object-rest-spread",
[
"babel-plugin-root-import",
{
"braveSuffix": "./",
"bravePrefix": "~"
}
]
]
}
16 changes: 16 additions & 0 deletions brave/app/scripts/controllers/preferences.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
const PreferencesController = require('../../../../app/scripts/controllers/preferences')

module.exports = class BravePreferencesController extends PreferencesController {
constructor (opts = {}) {
if (opts.initState === undefined) {
opts['initState'] = {}
}
opts.initState.batTokenAdded = false
super(opts)
}

setBatTokenAdded () {
this.store.updateState({ batTokenAdded: true })
return Promise.resolve(true)
}
}
14 changes: 14 additions & 0 deletions brave/app/scripts/metamask-controller.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
const MetamaskController = require('../../../app/scripts/metamask-controller')
const nodeify = require('../../../app/scripts/lib/nodeify')

module.exports = class BraveController extends MetamaskController {
constructor (opts) {
super(opts)
}

getApi () {
const api = super.getApi()
api.setBatTokenAdded = nodeify(this.preferencesController.setBatTokenAdded, this.preferencesController)
return api
}
}
83 changes: 83 additions & 0 deletions brave/gulp.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
const gulp = require('gulp')
const replace = require('gulp-replace')

/*
* Brave
*/

const overrideDirs = [
'ui/app/**/*',
'app/scripts/**/*'
]

const bravePrefix = '~/brave/'

/*
* ToDo(ryanml) - Write a method to convert simple paths to a Regex obj
* then a simple mapping can be created for these replacements.
* Ex (psuedo-ish):
* const replacements = [
* {
* target: 'actions',
* replace: 'ui/app/store/actions'
* }
* ]
*
* replacements.map((item) => {
* .pipe(
* replace(convertToRegex(item.target), fmtPath(item.replace))
* )
* })
*/
module.exports = function () {
return gulp.src(overrideDirs)
.pipe(
replace(
/\'(.*)\/home\'/gm,
`'${bravePrefix}ui/app/pages/home'`
)
)
.pipe(
replace(
/\'\.\/routes\'/gm,
`'${bravePrefix}ui/app/pages/routes'`
)
)
.pipe(
replace(
/\'(.*)\/actions\'/gm,
`'${bravePrefix}ui/app/store/actions'`
)
)
.pipe(
replace(
/\'(.*)\/preferences\'/gm,
`'${bravePrefix}app/scripts/controllers/preferences'`
)
)
.pipe(
replace(
/\'(.*)\/metamask-controller\'/gm,
`'${bravePrefix}app/scripts/metamask-controller'`
)
)
.pipe(
replace(
/\'(.*)\/metamask\/metamask\'/gm,
`'${bravePrefix}ui/app/ducks/metamask/metamask'`
)
)
.pipe(
replace(
/\'(.*)\/components\/menu\'/gm,
`'${bravePrefix}ui/app/components/app/dropdowns/components/menu'`
)
)
.pipe(
replace(
/\'(.*)\/token-menu-dropdown\.js\'/gm,
`'${bravePrefix}ui/app/components/app/dropdowns/token-menu-dropdown'`
)
)
.pipe(gulp.dest(file => file.base))
}
30 changes: 30 additions & 0 deletions brave/ui/app/components/app/dropdowns/components/menu.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
const h = require('react-hyperscript')

import { Menu, Item, Divider, CloseArea } from '../../../../../../../ui/app/components/app/dropdowns/components/menu'

Item.prototype.render = function () {
const {
icon,
children,
text,
className = '',
onClick,
isShowing
} = this.props

if (isShowing === false) {
return h('noscript')
}

const itemClassName = `menu__item ${className} ${onClick ? 'menu__item--clickable' : ''}`
const iconComponent = icon ? h('div.menu__item__icon', [icon]) : null
const textComponent = text ? h('div.menu__item__text', text) : null

return children
? h('div', { className: itemClassName, onClick }, children)
: h('div.menu__item', { className: itemClassName, onClick }, [ iconComponent, textComponent ]
.filter(d => Boolean(d))
)
}

module.exports = { Menu, Item, Divider, CloseArea }
74 changes: 74 additions & 0 deletions brave/ui/app/components/app/dropdowns/token-menu-dropdown.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
const Component = require('react').Component
const PropTypes = require('prop-types')
const h = require('react-hyperscript')
const inherits = require('util').inherits
const connect = require('react-redux').connect
const genAccountLink = require('etherscan-link').createAccountLink
const { Menu, Item, CloseArea } = require('./components/menu')

import actions from '../../../store/actions'

function mapStateToProps(state) {
return {
network: state.metamask.network,
}
}

function mapDispatchToProps(dispatch) {
return {
showHideTokenConfirmationModal: (token) => {
dispatch(actions.showModal({
name: 'HIDE_TOKEN_CONFIRMATION',
token
}))
},
}
}

BraveTokenMenuDropdown.contextTypes = {
t: PropTypes.func,
}

inherits(BraveTokenMenuDropdown, Component)
function BraveTokenMenuDropdown () {
Component.call(this)

this.onClose = this.onClose.bind(this)
}

BraveTokenMenuDropdown.prototype.onClose = function (e) {
e.stopPropagation()
this.props.onClose()
}

BraveTokenMenuDropdown.prototype.render = function() {
const { showHideTokenConfirmationModal } = this.props

return h(Menu, { className: 'token-menu-dropdown', isShowing: true }, [
h(CloseArea, {
onClick: this.onClose,
}),
h(Item, {
onClick: (e) => {
e.stopPropagation()
showHideTokenConfirmationModal(this.props.token)
this.props.onClose()
},
isShowing: (this.props.token.symbol !== 'BAT'),
text: 'Hide Tokens',
}),
h(Item, {
onClick: (e) => {
e.stopPropagation()
const url = genAccountLink(this.props.token.address, this.props.network)
global.platform.openWindow({
url
})
this.props.onClose()
},
text: 'View on Etherscan',
}),
])
}

module.exports = connect(mapStateToProps, mapDispatchToProps)(BraveTokenMenuDropdown)
12 changes: 12 additions & 0 deletions brave/ui/app/ducks/metamask/metamask.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
const reduceMetamask = require('../../../../../ui/app/ducks/metamask/metamask')

module.exports = function (state, action) {
const newState = reduceMetamask(state, action)
newState.batTokenAdded = newState.batTokenAdded || false

if (action.type === 'SET_BAT_TOKEN_ADDED') {
newState.batTokenAdded = action.value
}

return newState
}
24 changes: 24 additions & 0 deletions brave/ui/app/pages/home/home.component.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import Home from '../../../../../ui/app/pages/home/home.component'
import PropTypes from 'prop-types'
import actions from '../../store/actions'
import batToken from '../../store/bat-token'

const BraveHome = class BraveHome extends Home {
constructor (props) {
super(props)
}

componentDidMount () {
super.componentDidMount()

const { batTokenAdded } = this.props

if (!batTokenAdded) {
this.props.dispatch(actions.addTokens(batToken))
}
}
}

BraveHome.propTypes.batTokenAdded = PropTypes.bool

module.exports = BraveHome
32 changes: 32 additions & 0 deletions brave/ui/app/pages/home/home.container.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import Home from './home.component'
import { compose } from 'recompose'
import { connect } from 'react-redux'
import { withRouter } from 'react-router-dom'
import { unconfirmedTransactionsCountSelector } from '../../../../../ui/app/selectors/confirm-transaction'

const mapStateToProps = state => {
const { metamask, appState } = state
const {
lostAccounts,
seedWords,
suggestedTokens,
providerRequests,
batTokenAdded
} = metamask
const { forgottenPassword } = appState

return {
lostAccounts,
forgottenPassword,
seedWords,
suggestedTokens,
unconfirmedTransactionsCount: unconfirmedTransactionsCountSelector(state),
providerRequests,
batTokenAdded
}
}

export default compose(
withRouter,
connect(mapStateToProps)
)(Home)
1 change: 1 addition & 0 deletions brave/ui/app/pages/home/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { default } from './home.container'
Loading

0 comments on commit 069ce49

Please sign in to comment.