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

Commit c002e3f

Browse files
authored
Merge pull request #8692 from brave/fix/url-bar-autocomplete
Optimize URL bar history and bookmark suggestions (generateNewSuggestionsList())
2 parents 96b2f07 + 90c5b2d commit c002e3f

File tree

1 file changed

+84
-74
lines changed

1 file changed

+84
-74
lines changed

app/renderer/reducers/urlBarReducer.js

+84-74
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,13 @@ const {aboutUrls, isNavigatableAboutPage, isSourceAboutUrl, isUrl, getSourceAbou
99
const {isURL, isPotentialPhishingUrl, getUrlFromInput} = require('../../../js/lib/urlutil')
1010
const {getFrameByKey, getFrameKeyByTabId, activeFrameStatePath, frameStatePath, frameStatePathForFrame, getActiveFrame, tabStatePath, getFrameByTabId} = require('../../../js/state/frameStateUtil')
1111
const getSetting = require('../../../js/settings').getSetting
12-
const {isDefaultEntry} = require('../../../js/state/siteUtil')
12+
const {isBookmark, isDefaultEntry, isHistoryEntry} = require('../../../js/state/siteUtil')
1313
const fetchSearchSuggestions = require('../fetchSearchSuggestions')
1414
const searchProviders = require('../../../js/data/searchProviders')
1515
const settings = require('../../../js/constants/settings')
1616
const Immutable = require('immutable')
1717
const config = require('../../../js/constants/config')
1818
const top500 = require('../../../js/data/top500')
19-
const siteTags = require('../../../js/constants/siteTags')
2019
const suggestion = require('../lib/suggestion')
2120
const suggestionTypes = require('../../../js/constants/suggestionTypes')
2221
const {navigateSiteClickHandler, frameClickHandler} = require('../suggestionClickHandlers')
@@ -129,11 +128,6 @@ const updateUrlSuffix = (state, suggestionList) => {
129128
const generateNewSuggestionsList = (state) => {
130129
const activeFrameKey = state.get('activeFrameKey')
131130
const urlLocation = state.getIn(activeFrameStatePath(state).concat(['navbar', 'urlbar', 'location']))
132-
let sites = appStoreRenderer.state.get('sites')
133-
if (sites) {
134-
// Filter out Brave default newtab sites and sites with falsey location
135-
sites = sites.filter((site) => !isDefaultEntry(site) && site.get('location'))
136-
}
137131
const searchResults = state.getIn(activeFrameStatePath(state).concat(['navbar', 'urlbar', 'suggestions', 'searchResults']))
138132
const frameSearchDetail = state.getIn(activeFrameStatePath(state).concat(['navbar', 'urlbar', 'searchDetail']))
139133
const searchDetail = state.get('searchDetail')
@@ -148,27 +142,32 @@ const generateNewSuggestionsList = (state) => {
148142
const mapListToElements = ({data, maxResults, type, clickHandler = navigateSiteClickHandler.bind(this),
149143
sortHandler = defaultme, formatTitle = defaultme, formatUrl = defaultme,
150144
filterValue = (site) => {
151-
return site.toLowerCase().includes(urlLocationLower)
145+
return site.toLowerCase().indexOf(urlLocationLower) !== -1
152146
}
153-
}) => // Filter out things which are already in our own list at a smaller index
154-
data
155-
// Per suggestion provider filter
156-
.filter(filterValue)
147+
}) => {
148+
// Filter out things which are already in our own list at a smaller index
157149
// Filter out things which are already in the suggestions list
158-
.filter((site) =>
150+
let filteredData = data.filter((site) =>
159151
suggestionsList.findIndex((x) => (x.location || '').toLowerCase() === (formatUrl(site) || '').toLowerCase()) === -1 ||
160152
// Tab autosuggestions should always be included since they will almost always be in history
161153
type === suggestionTypes.TAB)
162-
.sort(sortHandler)
163-
.take(maxResults)
164-
.map((site) => {
165-
return {
166-
onClick: clickHandler.bind(null, site),
167-
title: formatTitle(site),
168-
location: formatUrl(site),
169-
type
170-
}
171-
})
154+
// Per suggestion provider filter
155+
if (filterValue) {
156+
filteredData = filteredData.filter(filterValue)
157+
}
158+
159+
return filteredData
160+
.sort(sortHandler)
161+
.take(maxResults)
162+
.map((site) => {
163+
return {
164+
onClick: clickHandler.bind(null, site),
165+
title: formatTitle(site),
166+
location: formatUrl(site),
167+
type
168+
}
169+
})
170+
}
172171

173172
const shouldNormalize = suggestion.shouldNormalizeLocation(urlLocationLower)
174173
const urlLocationLowerNormalized = suggestion.normalizeLocation(urlLocationLower)
@@ -201,62 +200,73 @@ const generateNewSuggestionsList = (state) => {
201200
}
202201
}
203202

204-
const historyFilter = (site) => {
205-
if (!site) {
206-
return false
207-
}
208-
const title = site.get('title') || ''
209-
const location = site.get('location') || ''
203+
// NOTE: Iterating sites can take a long time! Please be mindful when
204+
// working with the history and bookmark suggestion code.
205+
const historySuggestionsOn = getSetting(settings.HISTORY_SUGGESTIONS)
206+
const bookmarkSuggestionsOn = getSetting(settings.BOOKMARK_SUGGESTIONS)
207+
const shouldIterateSites = historySuggestionsOn || bookmarkSuggestionsOn
208+
if (shouldIterateSites) {
210209
// Note: Bookmark sites are now included in history. This will allow
211210
// sites to appear in the auto-complete regardless of their bookmark
212211
// status. If history is turned off, bookmarked sites will appear
213212
// in the bookmark section.
214-
return (title.toLowerCase().includes(urlLocationLower) ||
215-
location.toLowerCase().includes(urlLocationLower)) &&
216-
site.get('lastAccessedTime')
217-
}
218-
var historySites = sites.filter(historyFilter)
213+
const sitesFilter = (site) => {
214+
const location = site.get('location')
215+
if (!location) {
216+
return false
217+
}
218+
const title = site.get('title')
219+
return location.toLowerCase().indexOf(urlLocationLower) !== -1 ||
220+
(title && title.toLowerCase().indexOf(urlLocationLower) !== -1)
221+
}
219222

220-
// potentially append virtual history items (such as www.google.com when
221-
// searches have been made but the root site has not been visited)
222-
historySites = historySites.concat(suggestion.createVirtualHistoryItems(historySites))
223+
let historySites = new Immutable.List()
224+
let bookmarkSites = new Immutable.List()
225+
const sites = appStoreRenderer.state.get('sites')
226+
sites.forEach(site => {
227+
if (!sitesFilter(site)) {
228+
return
229+
}
230+
if (historySuggestionsOn && isHistoryEntry(site) && !isDefaultEntry(site)) {
231+
historySites = historySites.push(site)
232+
return
233+
}
234+
if (bookmarkSuggestionsOn && isBookmark(site) && !isDefaultEntry(site)) {
235+
bookmarkSites = bookmarkSites.push(site)
236+
}
237+
})
223238

224-
// history
225-
if (getSetting(settings.HISTORY_SUGGESTIONS)) {
226-
suggestionsList = suggestionsList.concat(mapListToElements({
227-
data: historySites,
228-
maxResults: config.urlBarSuggestions.maxHistorySites,
229-
type: suggestionTypes.HISTORY,
230-
clickHandler: navigateSiteClickHandler((site) => {
231-
return site.get('location')
232-
}),
233-
sortHandler: sortBasedOnLocationPos,
234-
formatTitle: (site) => site.get('title'),
235-
formatUrl: (site) => site.get('location'),
236-
filterValue: historyFilter
237-
}))
238-
}
239+
if (historySites.size > 0) {
240+
historySites = historySites.concat(suggestion.createVirtualHistoryItems(historySites))
241+
242+
suggestionsList = suggestionsList.concat(mapListToElements({
243+
data: historySites,
244+
maxResults: config.urlBarSuggestions.maxHistorySites,
245+
type: suggestionTypes.HISTORY,
246+
clickHandler: navigateSiteClickHandler((site) => {
247+
return site.get('location')
248+
}),
249+
sortHandler: sortBasedOnLocationPos,
250+
formatTitle: (site) => site.get('title'),
251+
formatUrl: (site) => site.get('location'),
252+
filterValue: null
253+
}))
254+
}
239255

240-
// bookmarks
241-
if (getSetting(settings.BOOKMARK_SUGGESTIONS)) {
242-
suggestionsList = suggestionsList.concat(mapListToElements({
243-
data: sites,
244-
maxResults: config.urlBarSuggestions.maxBookmarkSites,
245-
type: suggestionTypes.BOOKMARK,
246-
clickHandler: navigateSiteClickHandler((site) => {
247-
return site.get('location')
248-
}),
249-
sortHandler: sortBasedOnLocationPos,
250-
formatTitle: (site) => site.get('title'),
251-
formatUrl: (site) => site.get('location'),
252-
filterValue: (site) => {
253-
const title = site.get('title') || ''
254-
const location = site.get('location') || ''
255-
return (title.toLowerCase().includes(urlLocationLower) ||
256-
location.toLowerCase().includes(urlLocationLower)) &&
257-
site.get('tags') && site.get('tags').includes(siteTags.BOOKMARK)
258-
}
259-
}))
256+
if (bookmarkSites.size > 0) {
257+
suggestionsList = suggestionsList.concat(mapListToElements({
258+
data: bookmarkSites,
259+
maxResults: config.urlBarSuggestions.maxBookmarkSites,
260+
type: suggestionTypes.BOOKMARK,
261+
clickHandler: navigateSiteClickHandler((site) => {
262+
return site.get('location')
263+
}),
264+
sortHandler: sortBasedOnLocationPos,
265+
formatTitle: (site) => site.get('title'),
266+
formatUrl: (site) => site.get('location'),
267+
filterValue: null
268+
}))
269+
}
260270
}
261271

262272
// about pages
@@ -279,8 +289,8 @@ const generateNewSuggestionsList = (state) => {
279289
filterValue: (frame) => !isSourceAboutUrl(frame.get('location')) &&
280290
frame.get('key') !== activeFrameKey &&
281291
(
282-
(frame.get('title') && frame.get('title').toLowerCase().includes(urlLocationLower)) ||
283-
(frame.get('location') && frame.get('location').toLowerCase().includes(urlLocationLower))
292+
(frame.get('title') && frame.get('title').toLowerCase().indexOf(urlLocationLower) !== -1) ||
293+
(frame.get('location') && frame.get('location').toLowerCase().indexOf(urlLocationLower) !== -1)
284294
)
285295
}))
286296
}

0 commit comments

Comments
 (0)