Skip to content

Commit

Permalink
Refactors PublisherToggle into redux component
Browse files Browse the repository at this point in the history
Resolves brave#9323

Auditors: @bridiver @bscfliton

Test Plan:
  • Loading branch information
NejcZdovc committed Jun 7, 2017
1 parent d2dd9a4 commit 8fc3221
Show file tree
Hide file tree
Showing 4 changed files with 153 additions and 124 deletions.
59 changes: 59 additions & 0 deletions app/common/state/publisherState.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this file,
* You can obtain one at http://mozilla.org/MPL/2.0/. */

const Immutable = require('immutable')

// Constants
const settings = require('../../../js/constants/settings')
const siteSettingsState = require('./siteSettingsState')

// Utils
const {getSetting} = require('../../../js/settings')
const {isHttpOrHttps} = require('../../../js/lib/urlutil')

const visiblePublisher = (publisherId) => {
// ledgerPaymentsShown is undefined by default until
// user decide to permanently hide the publisher,
// so for icon to be shown it can be everything but false
const hostSettings = siteSettingsState.getSettingsByHost(publisherId)
const ledgerPaymentsShown = hostSettings && hostSettings.get('ledgerPaymentsShown')

return typeof ledgerPaymentsShown === 'boolean'
? ledgerPaymentsShown
: true
}

const publisherState = {
enabledForPaymentsPublisher: (state, locationId) => {
const locationInfo = state.get('locationInfo', Immutable.Map())
const publisherId = locationInfo.getIn([locationId, 'publisher'])
const synopsis = state.getIn(['publisherInfo', 'synopsis'], Immutable.Map())
const hostSettings = siteSettingsState.getSettingsByHost(publisherId)

// All publishers will be enabled by default if AUTO_SUGGEST is ON,
// excluding publishers defined on ledger's exclusion list
const excluded = locationInfo.getIn([locationId, 'exclude'])
const autoSuggestSites = getSetting(settings.AUTO_SUGGEST_SITES)

// If session is clear then siteSettings is undefined and icon
// will never be shown, but synopsis may not be empty.
// In such cases let's check if synopsis matches current publisherId
const isvalidPublisherSynopsis = !!synopsis.map(entry => entry.get('site'))
.includes(publisherId)

// hostSettings is undefined until user hit addFunds button.
// For such cases check autoSuggestSites for eligibility.
return hostSettings
? hostSettings.get('ledgerPayments') !== false
: isvalidPublisherSynopsis || (autoSuggestSites && !excluded)
},

shouldShowAddPublisherButton: (location, publisherId) => {
return getSetting(settings.PAYMENTS_ENABLED) &&
isHttpOrHttps(location) &&
visiblePublisher(publisherId)
}
}

module.exports = publisherState
17 changes: 17 additions & 0 deletions app/common/state/siteSettingsState.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,16 @@
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this file,
* You can obtain one at http://mozilla.org/MPL/2.0/. */

// Constants
const appConfig = require('../../../js/constants/appConfig')

// State
const siteSettings = require('../../../js/state/siteSettings')

// Utils
const {getHostPattern} = require('../../../js/lib/urlutil')

const api = {
getAllSiteSettings: (state, isPrivate) => {
if (isPrivate) {
Expand All @@ -9,6 +19,13 @@ const api = {
return state.get('siteSettings')
},

getSettingsByHost: (state, url) => {
const siteSettings = state.get('siteSettings')
const hostPattern = getHostPattern(url)

return siteSettings.get(hostPattern)
},

isNoScriptEnabled (state, settings) {
return siteSettings.activeSettings(settings, state, appConfig).noScript === true
}
Expand Down
68 changes: 28 additions & 40 deletions app/renderer/components/navigation/navigationBar.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,15 @@ const settings = require('../../../../js/constants/settings')

// State
const tabState = require('../../../common/state/tabState')
const publisherState = require('../../../common/state/publisherState')
const frameStateUtil = require('../../../../js/state/frameStateUtil')

// Store
const windowStore = require('../../../../js/stores/windowStore')

// Utils
const cx = require('../../../../js/lib/classSet')
const {isSourceAboutUrl} = require('../../../../js/lib/appUrlUtil')
const {isSourceAboutUrl, getBaseUrl} = require('../../../../js/lib/appUrlUtil')
const siteUtil = require('../../../../js/state/siteUtil')
const eventUtil = require('../../../../js/lib/eventUtil')
const UrlUtil = require('../../../../js/lib/urlutil')
Expand All @@ -55,7 +56,7 @@ class NavigationBar extends React.Component {
}

onToggleBookmark () {
const editing = this.bookmarked
const editing = this.props.isBookmarked
// show the AddEditBookmarkHanger control; saving/deleting takes place there
let siteDetail = siteUtil.getDetailFromFrame(this.activeFrame, siteTags.BOOKMARK)
const key = siteUtil.getSiteKey(siteDetail)
Expand Down Expand Up @@ -109,11 +110,6 @@ class NavigationBar extends React.Component {
}
}

get bookmarked () {
return this.props.activeFrameKey !== undefined &&
this.props.bookmarked
}

componentDidMount () {
ipc.on(messages.SHORTCUT_ACTIVE_FRAME_BOOKMARK, () => this.onToggleBookmark())
ipc.on(messages.SHORTCUT_ACTIVE_FRAME_REMOVE_BOOKMARK, () => this.onToggleBookmark())
Expand All @@ -132,7 +128,10 @@ class NavigationBar extends React.Component {
const title = activeFrame.get('title') || ''
const loading = activeFrame.get('loading')
const location = activeFrame.get('location') || ''
const navbar = activeFrame.get('navbar') || Immutable.Map()
const locationId = getBaseUrl(location)
const locationInfo = state.get('locationInfo', Immutable.Map())
const publisherId = locationInfo.getIn([locationId, 'publisher'])
const navbar = activeFrame.get('navbar', Immutable.Map())

const hasTitle = title && location && title !== location.replace(/^https?:\/\//, '')
const titleMode = activeTabShowingMessageBox ||
Expand All @@ -148,33 +147,29 @@ class NavigationBar extends React.Component {
)

const props = {}

props.navbar = navbar
props.sites = state.get('sites') // TODO(nejc) remove, primitives only
// used in renderer
props.activeFrameKey = activeFrameKey
props.location = location
props.title = title
props.bookmarked = activeTab && activeTab.get('bookmarked')
props.partitionNumber = activeFrame.get('partitionNumber') || 0
props.isSecure = activeFrame.getIn(['security', 'isSecure'])
props.loading = loading
props.showBookmarkHanger = bookmarkDetail && bookmarkDetail.get('isBookmarkHanger')
props.mouseInTitlebar = mouseInTitlebar
props.settings = state.get('settings')
props.siteSettings = state.get('siteSettings')
props.synopsis = state.getIn(['publisherInfo', 'synopsis']) || new Immutable.Map()
props.activeTabShowingMessageBox = activeTabShowingMessageBox
props.locationInfo = state.get('locationInfo')
props.titleMode = titleMode
props.isWideURLbarEnabled = getSetting(settings.WIDE_URL_BAR)
props.dontRender = activeFrameKey === undefined ||
state.get('siteSettings') === undefined
props.isBookmarked = props.activeFrameKey !== undefined &&
activeTab && activeTab.get('bookmarked')
props.isWideUrlBarEnabled = getSetting(settings.WIDE_URL_BAR)
props.showBookmarkHanger = bookmarkDetail && bookmarkDetail.get('isBookmarkHanger')
props.isLoading = loading
props.showPublisherToggle = !isSourceAboutUrl(location) &&
publisherState.shouldShowAddPublisherButton(location, publisherId)

// used in other functions
props.navbar = navbar // TODO(nejc) remove, primitives only
props.sites = state.get('sites') // TODO(nejc) remove, primitives only
props.activeTabId = activeTabId

return props
}

render () {
if (this.props.activeFrameKey === undefined ||
this.props.siteSettings === undefined) {
if (this.props.dontRender) {
return null
}

Expand All @@ -183,7 +178,7 @@ class NavigationBar extends React.Component {
data-frame-key={this.props.activeFrameKey}
className={cx({
titleMode: this.props.titleMode,
[css(styles.navigator_wide)]: this.props.isWideURLbarEnabled
[css(styles.navigator_wide)]: this.props.isWideUrlBarEnabled
})}>
{
this.props.showBookmarkHanger
Expand All @@ -193,7 +188,7 @@ class NavigationBar extends React.Component {
{
this.props.titleMode
? null
: this.props.loading
: this.props.isLoading
? <span className='navigationButtonContainer'>
<button data-l10n-id='stopButton'
className='normalizeButton navigationButton stopButton'
Expand Down Expand Up @@ -222,11 +217,11 @@ class NavigationBar extends React.Component {
{
!this.props.titleMode
? <span className='bookmarkButtonContainer'>
<button data-l10n-id={this.bookmarked ? 'removeBookmarkButton' : 'addBookmarkButton'}
<button data-l10n-id={this.props.isBookmarked ? 'removeBookmarkButton' : 'addBookmarkButton'}
className={cx({
navigationButton: true,
bookmarkButton: true,
removeBookmarkButton: this.bookmarked,
removeBookmarkButton: this.props.isBookmarked,
withHomeButton: getSetting(settings.SHOW_HOME_BUTTON),
normalizeButton: true
})}
Expand All @@ -240,17 +235,10 @@ class NavigationBar extends React.Component {
onStop={this.onStop}
/>
{
isSourceAboutUrl(this.props.location)
this.props.showPublisherToggle
? null
: <div className='endButtons'>
{
<PublisherToggle
location={this.props.location}
locationInfo={this.props.locationInfo}
siteSettings={this.props.siteSettings}
synopsis={this.props.synopsis}
/>
}
<PublisherToggle />
</div>
}
</div>
Expand Down
Loading

0 comments on commit 8fc3221

Please sign in to comment.