Skip to content
This repository was archived by the owner on Dec 11, 2019. It is now read-only.

Commit 03a2b59

Browse files
authored
Merge pull request #8266 from brave/redux-navbar
convert navigationBar to redux component
2 parents 7d5560f + 0c842ef commit 03a2b59

File tree

10 files changed

+129
-103
lines changed

10 files changed

+129
-103
lines changed

app/common/state/siteSettingsState.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
const appConfig = require('../../../js/constants/appConfig')
22
const siteSettings = require('../../../js/state/siteSettings')
33

4-
module.exports.isNoScriptEnabled = (settings, state) => {
4+
module.exports.isNoScriptEnabled = (state, settings) => {
55
return siteSettings.activeSettings(settings, state, appConfig).noScript === true
66
}

app/common/state/tabState.js

+10-2
Original file line numberDiff line numberDiff line change
@@ -288,6 +288,9 @@ const tabState = {
288288
},
289289

290290
isShowingMessageBox: (state, tabId) => {
291+
if (tabId === tabState.TAB_ID_NONE) {
292+
return false
293+
}
291294
return tabState.getTabPropertyByTabId(state, tabId, 'messageBoxDetail') || false
292295
},
293296

@@ -309,9 +312,14 @@ const tabState = {
309312
}
310313
},
311314

312-
getActiveTabId: (state) => {
315+
getActiveTab: (state, windowId = windowState.getActiveWindowId(state)) => {
313316
state = validateState(state)
314-
const tab = state.get('tabs').find((tab) => tab.get('active') === true)
317+
windowId = validateId('windowId', windowId)
318+
return state.get('tabs').find((tab) => tab.get('active') === true && tab.get('windowId') === windowId)
319+
},
320+
321+
getActiveTabId: (state, windowId = windowState.getActiveWindowId(state)) => {
322+
const tab = tabState.getActiveTab(state, windowId)
315323
return tab ? tab.get('tabId') : tabState.TAB_ID_NONE
316324
},
317325

app/common/state/windowState.js

+13
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,9 @@ const validateAction = function (action) {
3434
}
3535

3636
const api = {
37+
WINDOW_ID_NONE: -1,
38+
WINDOW_ID_CURRENT: -2,
39+
3740
getWindowIndex: (state, windowValue) => {
3841
state = validateState(state)
3942

@@ -68,6 +71,16 @@ const api = {
6871
}
6972
},
7073

74+
getActiveWindow: (state) => {
75+
state = validateState(state)
76+
return state.get('windows').find((win) => win.get('focused') === true)
77+
},
78+
79+
getActiveWindowId: (state) => {
80+
const win = api.getActiveWindow(state)
81+
return (win && win.get('windowId')) || api.WINDOW_ID_NONE
82+
},
83+
7184
getByWindowId: (state, windowId) => {
7285
state = validateState(state)
7386
windowId = validateId('windowId', windowId)

app/importer.js

+2-3
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ const getSetting = require('../js/settings').getSetting
1919
const locale = require('./locale')
2020
const tabMessageBox = require('./browser/tabMessageBox')
2121
const {makeImmutable} = require('./common/state/immutableUtil')
22+
const tabState = require('./common/state/tabState')
2223

2324
var isMergeFavorites = false
2425
var isImportingBookmarks = false
@@ -229,9 +230,7 @@ importer.on('add-cookies', (e, cookies) => {
229230
})
230231

231232
const getActiveTabId = () => {
232-
const tabs = makeImmutable(AppStore.getState()).get('tabs')
233-
const activeTab = tabs.find((tab) => tab.get('active'))
234-
return activeTab && activeTab.get('id')
233+
return tabState.getActiveTabId(AppStore.getState())
235234
}
236235

237236
const showImportWarning = function () {

app/renderer/components/navigation/navigationBar.js

+39-45
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
const React = require('react')
66
const Immutable = require('immutable')
7-
const ImmutableComponent = require('./../../../../js/components/immutableComponent')
7+
const ReduxComponent = require('../reduxComponent')
88

99
const cx = require('../../../../js/lib/classSet')
1010
const UrlBar = require('./urlBar')
@@ -14,7 +14,7 @@ const siteTags = require('../../../../js/constants/siteTags')
1414
const messages = require('../../../../js/constants/messages')
1515
const settings = require('../../../../js/constants/settings')
1616
const ipc = require('electron').ipcRenderer
17-
const {isSourceAboutUrl, getBaseUrl} = require('../../../../js/lib/appUrlUtil')
17+
const {isSourceAboutUrl} = require('../../../../js/lib/appUrlUtil')
1818
const AddEditBookmarkHanger = require('../addEditBookmarkHanger')
1919
const siteUtil = require('../../../../js/state/siteUtil')
2020
const eventUtil = require('../../../../js/lib/eventUtil')
@@ -24,10 +24,15 @@ const windowStore = require('../../../../js/stores/windowStore')
2424
const contextMenus = require('../../../../js/contextMenus')
2525
const LongPressButton = require('./../../../../js/components/longPressButton')
2626
const PublisherToggle = require('../publisherToggle')
27+
const {getCurrentWindowId} = require('../../currentWindow')
2728

28-
class NavigationBar extends ImmutableComponent {
29-
constructor () {
30-
super()
29+
// State
30+
const tabState = require('../../../common/state/tabState')
31+
const frameStateUtil = require('../../../../js/state/frameStateUtil')
32+
33+
class NavigationBar extends React.Component {
34+
constructor (props) {
35+
super(props)
3136
this.onToggleBookmark = this.onToggleBookmark.bind(this)
3237
this.onStop = this.onStop.bind(this)
3338
this.onReload = this.onReload.bind(this)
@@ -121,34 +126,38 @@ class NavigationBar extends ImmutableComponent {
121126
)
122127
}
123128

124-
get locationId () {
125-
return getBaseUrl(this.props.location)
126-
}
127-
128-
get publisherId () {
129-
return this.props.locationInfo && this.props.locationInfo.getIn([this.locationId, 'publisher'])
130-
}
131-
132-
get visiblePublisher () {
133-
const hostPattern = UrlUtil.getHostPattern(this.publisherId)
134-
const hostSettings = this.props.siteSettings.get(hostPattern)
135-
const ledgerPaymentsShown = hostSettings && hostSettings.get('ledgerPaymentsShown')
136-
return typeof ledgerPaymentsShown === 'boolean'
137-
? ledgerPaymentsShown
138-
: true
139-
}
140-
141-
get isPublisherButtonEnabled () {
142-
return getSetting(settings.PAYMENTS_ENABLED) &&
143-
UrlUtil.isHttpOrHttps(this.props.location) &&
144-
this.visiblePublisher
145-
}
146-
147129
componentDidMount () {
148130
ipc.on(messages.SHORTCUT_ACTIVE_FRAME_BOOKMARK, () => this.onToggleBookmark())
149131
ipc.on(messages.SHORTCUT_ACTIVE_FRAME_REMOVE_BOOKMARK, () => this.onToggleBookmark())
150132
}
151133

134+
mergeProps (state, dispatchProps, ownProps) {
135+
const windowState = state.get('currentWindow')
136+
const activeFrame = frameStateUtil.getActiveFrame(windowState) || Immutable.Map()
137+
const activeTabId = tabState.getActiveTabId(state, getCurrentWindowId())
138+
const props = {}
139+
140+
props.navbar = activeFrame.get('navbar')
141+
props.sites = state.get('sites')
142+
props.activeFrameKey = activeFrame.get('key')
143+
props.location = activeFrame.get('location') || ''
144+
props.title = activeFrame.get('title') || ''
145+
props.partitionNumber = activeFrame.get('partitionNumber') || 0
146+
props.isSecure = activeFrame.getIn(['security', 'isSecure'])
147+
props.loading = activeFrame.get('loading')
148+
props.bookmarkDetail = windowState.get('bookmarkDetail')
149+
props.mouseInTitlebar = windowState.getIn(['ui', 'mouseInTitlebar'])
150+
props.enableNoScript = ownProps.enableNoScript
151+
props.settings = state.get('settings')
152+
props.menubarVisible = ownProps.menubarVisible
153+
props.siteSettings = state.get('siteSettings')
154+
props.synopsis = state.getIn(['publisherInfo', 'synopsis']) || new Immutable.Map()
155+
props.activeTabShowingMessageBox = tabState.isShowingMessageBox(state, activeTabId)
156+
props.locationInfo = state.get('locationInfo')
157+
158+
return props
159+
}
160+
152161
render () {
153162
if (this.props.activeFrameKey === undefined ||
154163
this.props.siteSettings === undefined) {
@@ -214,26 +223,11 @@ class NavigationBar extends ImmutableComponent {
214223
: null
215224
}
216225
</div>
217-
<UrlBar ref='urlBar'
218-
activeFrameKey={this.props.activeFrameKey}
219-
canGoForward={this.props.canGoForward}
220-
searchDetail={this.props.searchDetail}
221-
loading={this.loading}
222-
location={this.props.location}
223-
title={this.props.title}
224-
history={this.props.history}
225-
isSecure={this.props.isSecure}
226-
hasLocationValueSuffix={this.props.hasLocationValueSuffix}
227-
startLoadTime={this.props.startLoadTime}
228-
endLoadTime={this.props.endLoadTime}
226+
<UrlBar
229227
titleMode={this.titleMode}
230-
urlbar={this.props.navbar.get('urlbar')}
231228
onStop={this.onStop}
232229
menubarVisible={this.props.menubarVisible}
233-
noBorderRadius={this.isPublisherButtonEnabled}
234-
activeTabShowingMessageBox={this.props.activeTabShowingMessageBox}
235230
enableNoScript={this.props.enableNoScript}
236-
scriptsBlocked={this.props.scriptsBlocked}
237231
/>
238232
{
239233
isSourceAboutUrl(this.props.location)
@@ -253,4 +247,4 @@ class NavigationBar extends ImmutableComponent {
253247
}
254248
}
255249

256-
module.exports = NavigationBar
250+
module.exports = ReduxComponent.connect(NavigationBar)

app/renderer/components/navigation/navigator.js

+4-31
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
* You can obtain one at http://mozilla.org/MPL/2.0/. */
44

55
const React = require('react')
6-
const Immutable = require('immutable')
76
const {StyleSheet, css} = require('aphrodite')
87

98
// Actions
@@ -31,7 +30,6 @@ const {getCurrentWindowId, isMaximized} = require('../../currentWindow')
3130
const {makeImmutable} = require('../../../common/state/immutableUtil')
3231
const platformUtil = require('../../../common/lib/platformUtil')
3332
const {braveShieldsEnabled} = require('../../../common/state/shieldState')
34-
const tabUtil = require('../../lib/tabUtil')
3533
const eventUtil = require('../../../../js/lib/eventUtil')
3634
const {isNavigatableAboutPage, getBaseUrl} = require('./../../../../js/lib/appUrlUtil')
3735
const frameStateUtil = require('../../../../js/state/frameStateUtil')
@@ -57,8 +55,8 @@ class Navigator extends ImmutableComponent {
5755

5856
onNav (e, navCheckProp, navType, navAction) {
5957
const activeFrame = frameStateUtil.getActiveFrame(this.props.windowState)
60-
const activeTabId = tabUtil.activeTabId(this.props.windowState)
61-
const activeTab = activeFrame ? this.props.appState.get('tabs').find((tab) => tab.get('tabId') === activeTabId) : null
58+
const activeTab = tabState.getActiveTab(this.props.appState)
59+
const activeTabId = tabState.getActiveTabId(this.props.appState)
6260
const isNavigable = isNavigatableAboutPage(getBaseUrl(activeFrame.get('location')))
6361
if (e && eventUtil.isForSecondaryAction(e) && isNavigable) {
6462
if (activeTab && activeTab.get(navCheckProp)) {
@@ -96,7 +94,7 @@ class Navigator extends ImmutableComponent {
9694
}
9795

9896
get extensionButtons () {
99-
const activeTabId = tabUtil.activeTabId(this.props.windowState)
97+
const activeTabId = tabState.getActiveTabId(this.props.appState)
10098
const enabledExtensions = extensionState.getEnabledExtensions(this.props.appState)
10199
const extensionBrowserActions = enabledExtensions
102100
.map((extension) => extensionState.getBrowserActionByTabId(this.props.appState, extension.get('id'), activeTabId))
@@ -178,7 +176,6 @@ class Navigator extends ImmutableComponent {
178176
const activeFrame = frameStateUtil.getActiveFrame(this.props.windowState)
179177
const totalBlocks = activeFrame ? this.getTotalBlocks(activeFrame) : false
180178
const contextMenuDetail = this.props.windowState.get('contextMenuDetail')
181-
const noScriptIsVisible = this.props.windowState.getIn(['ui', 'noScriptInfo', 'isVisible'])
182179
const braverySettings = siteSettings.activeSettings(this.props.activeSiteSettings, this.props.appState, appConfig)
183180
const shieldEnabled = braveShieldsEnabled(activeFrame)
184181

@@ -235,32 +232,8 @@ class Navigator extends ImmutableComponent {
235232
</div>
236233
</div>
237234
<NavigationBar
238-
navbar={activeFrame && activeFrame.get('navbar')}
239-
sites={this.props.appState.get('sites')}
240-
canGoForward={activeTab && activeTab.get('canGoForward')}
241-
activeFrameKey={(activeFrame && activeFrame.get('key')) || undefined}
242-
location={(activeFrame && activeFrame.get('location')) || ''}
243-
title={(activeFrame && activeFrame.get('title')) || ''}
244-
scriptsBlocked={activeFrame && activeFrame.getIn(['noScript', 'blocked'])}
245-
partitionNumber={(activeFrame && activeFrame.get('partitionNumber')) || 0}
246-
history={(activeFrame && activeFrame.get('history')) || new Immutable.List()}
247-
suggestionIndex={(activeFrame && activeFrame.getIn(['navbar', 'urlbar', 'suggestions', 'selectedIndex'])) || 0}
248-
isSecure={activeFrame ? activeFrame.getIn(['security', 'isSecure']) : null}
249-
hasLocationValueSuffix={activeFrame && activeFrame.getIn(['navbar', 'urlbar', 'suggestions', 'urlSuffix'])}
250-
startLoadTime={(activeFrame && activeFrame.get('startLoadTime')) || undefined}
251-
endLoadTime={(activeFrame && activeFrame.get('endLoadTime')) || undefined}
252-
loading={activeFrame && activeFrame.get('loading')}
253-
bookmarkDetail={this.props.windowState.get('bookmarkDetail')}
254-
mouseInTitlebar={this.props.windowState.getIn(['ui', 'mouseInTitlebar'])}
255-
searchDetail={this.props.windowState.get('searchDetail')}
256-
enableNoScript={siteSettingsState.isNoScriptEnabled(this.props.activeSiteSettings, this.props.appState)}
257-
settings={this.props.appState.get('settings')}
258-
noScriptIsVisible={noScriptIsVisible}
235+
enableNoScript={siteSettingsState.isNoScriptEnabled(this.props.appState, this.props.activeSiteSettings)}
259236
menubarVisible={this.props.customTitlebar.menubarVisible}
260-
siteSettings={this.props.appState.get('siteSettings')}
261-
synopsis={this.props.appState.getIn(['publisherInfo', 'synopsis']) || new Immutable.Map()}
262-
activeTabShowingMessageBox={activeTabShowingMessageBox}
263-
locationInfo={this.props.appState.get('locationInfo')}
264237
/>
265238
<div className='topLevelEndButtons'>
266239
<div className={cx({

app/renderer/components/navigation/urlBar.js

+54-6
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,9 @@
44

55
const React = require('react')
66

7-
const ImmutableComponent = require('../../../../js/components/immutableComponent')
7+
const ReduxComponent = require('../reduxComponent')
88
const {StyleSheet, css} = require('aphrodite')
9+
const Immutable = require('immutable')
910

1011
const windowActions = require('../../../../js/actions/windowActions')
1112
const appActions = require('../../../../js/actions/appActions')
@@ -18,20 +19,26 @@ const ipc = require('electron').ipcRenderer
1819
const UrlBarSuggestions = require('./urlBarSuggestions')
1920
const UrlBarIcon = require('./urlBarIcon')
2021
const messages = require('../../../../js/constants/messages')
22+
const siteSettings = require('../../../../js/state/siteSettings')
2123
const {getSetting} = require('../../../../js/settings')
2224
const settings = require('../../../../js/constants/settings')
2325
const contextMenus = require('../../../../js/contextMenus')
2426
const windowStore = require('../../../../js/stores/windowStore')
2527
const UrlUtil = require('../../../../js/lib/urlutil')
2628
const {eventElHasAncestorWithClasses, isForSecondaryAction} = require('../../../../js/lib/eventUtil')
27-
const {isUrl, isIntermediateAboutPage} = require('../../../../js/lib/appUrlUtil')
29+
const {getBaseUrl, isUrl, isIntermediateAboutPage} = require('../../../../js/lib/appUrlUtil')
30+
const {getCurrentWindowId} = require('../../currentWindow')
2831

2932
// Icons
3033
const iconNoScript = require('../../../../img/url-bar-no-script.svg')
3134

32-
class UrlBar extends ImmutableComponent {
33-
constructor () {
34-
super()
35+
// State
36+
const frameStateUtil = require('../../../../js/state/frameStateUtil')
37+
const tabState = require('../../../common/state/tabState')
38+
39+
class UrlBar extends React.Component {
40+
constructor (props) {
41+
super(props)
3542
this.onFocus = this.onFocus.bind(this)
3643
this.onBlur = this.onBlur.bind(this)
3744
this.onKeyDown = this.onKeyDown.bind(this)
@@ -505,6 +512,47 @@ class UrlBar extends ImmutableComponent {
505512
contextMenus.onUrlBarContextMenu(this.props.searchDetail, this.activeFrame, e)
506513
}
507514

515+
mergeProps (state, dispatchProps, ownProps) {
516+
const windowState = state.get('currentWindow')
517+
const activeFrame = frameStateUtil.getActiveFrame(windowState) || Immutable.Map()
518+
const activeTabId = tabState.getActiveTabId(state, getCurrentWindowId())
519+
520+
const location = activeFrame.get('location') || ''
521+
const baseUrl = getBaseUrl(location)
522+
523+
// TODO(bridiver) - this definitely needs a helper
524+
const publisherId = state.getIn(['locationInfo', baseUrl, 'publisher'])
525+
const hostPattern = UrlUtil.getHostPattern(publisherId)
526+
const hostSettings = siteSettings.getSiteSettingsForHostPattern(state.get('settings'), hostPattern)
527+
const ledgerPaymentsShown = hostSettings && hostSettings.get('ledgerPaymentsShown')
528+
const visiblePublisher = typeof ledgerPaymentsShown === 'boolean' ? ledgerPaymentsShown : true
529+
const isPublisherButtonEnabled = getSetting(settings.PAYMENTS_ENABLED) &&
530+
UrlUtil.isHttpOrHttps(location) && visiblePublisher
531+
532+
const props = {}
533+
534+
props.activeFrameKey = activeFrame.get('key')
535+
props.location = location
536+
props.title = activeFrame.get('title') || ''
537+
props.scriptsBlocked = activeFrame.getIn(['noScript', 'blocked'])
538+
props.history = activeFrame.get('history') || new Immutable.List()
539+
props.isSecure = activeFrame.getIn(['security', 'isSecure'])
540+
props.hasLocationValueSuffix = activeFrame.getIn(['navbar', 'urlbar', 'suggestions', 'urlSuffix'])
541+
props.startLoadTime = activeFrame.get('startLoadTime')
542+
props.endLoadTime = activeFrame.get('endLoadTime')
543+
props.loading = activeFrame.get('loading')
544+
props.searchDetail = windowState.get('searchDetail')
545+
props.enableNoScript = ownProps.enableNoScript
546+
props.noScriptIsVisible = windowState.getIn(['ui', 'noScriptInfo', 'isVisible']) || false
547+
props.menubarVisible = ownProps.menubarVisible
548+
props.activeTabShowingMessageBox = tabState.isShowingMessageBox(state, activeTabId)
549+
props.noBorderRadius = isPublisherButtonEnabled
550+
props.urlbar = activeFrame.getIn(['navbar', 'urlbar'])
551+
props.onStop = ownProps.onStop
552+
props.titleMode = ownProps.titleMode
553+
return props
554+
}
555+
508556
render () {
509557
return <form
510558
className={cx({
@@ -605,4 +653,4 @@ const styles = StyleSheet.create({
605653
}
606654
})
607655

608-
module.exports = UrlBar
656+
module.exports = ReduxComponent.connect(UrlBar)

0 commit comments

Comments
 (0)