From a6d3b3dea858b62bead928ed51f2f449f1cc22dd Mon Sep 17 00:00:00 2001 From: NejcZdovc Date: Wed, 7 Jun 2017 16:13:51 -0700 Subject: [PATCH] Refactors PublisherToggle into redux component Resolves #9323 Auditors: @bridiver @bscfliton Test Plan: --- app/common/state/publisherState.js | 59 ++++++++ app/common/state/siteSettingsState.js | 17 +++ .../components/navigation/navigationBar.js | 69 ++++----- .../components/navigation/publisherToggle.js | 137 +++++++----------- .../navigation/publisherToggleTest.js | 2 +- 5 files changed, 155 insertions(+), 129 deletions(-) create mode 100644 app/common/state/publisherState.js diff --git a/app/common/state/publisherState.js b/app/common/state/publisherState.js new file mode 100644 index 00000000000..474626164a1 --- /dev/null +++ b/app/common/state/publisherState.js @@ -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 diff --git a/app/common/state/siteSettingsState.js b/app/common/state/siteSettingsState.js index 6effa414577..c6b81a3d859 100644 --- a/app/common/state/siteSettingsState.js +++ b/app/common/state/siteSettingsState.js @@ -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) { @@ -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 } diff --git a/app/renderer/components/navigation/navigationBar.js b/app/renderer/components/navigation/navigationBar.js index 32f45623646..a84e09ae9e6 100644 --- a/app/renderer/components/navigation/navigationBar.js +++ b/app/renderer/components/navigation/navigationBar.js @@ -24,6 +24,7 @@ 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 @@ -31,7 +32,7 @@ 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') @@ -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) @@ -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()) @@ -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 || @@ -148,33 +147,30 @@ 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) && + !locationInfo.isEmpty() + + // 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 } @@ -183,7 +179,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 @@ -193,7 +189,7 @@ class NavigationBar extends React.Component { { this.props.titleMode ? null - : this.props.loading + : this.props.isLoading ?