@@ -9,14 +9,13 @@ const {aboutUrls, isNavigatableAboutPage, isSourceAboutUrl, isUrl, getSourceAbou
9
9
const { isURL, isPotentialPhishingUrl, getUrlFromInput} = require ( '../../../js/lib/urlutil' )
10
10
const { getFrameByKey, getFrameKeyByTabId, activeFrameStatePath, frameStatePath, frameStatePathForFrame, getActiveFrame, tabStatePath, getFrameByTabId} = require ( '../../../js/state/frameStateUtil' )
11
11
const getSetting = require ( '../../../js/settings' ) . getSetting
12
- const { isDefaultEntry} = require ( '../../../js/state/siteUtil' )
12
+ const { isBookmark , isDefaultEntry, isHistoryEntry } = require ( '../../../js/state/siteUtil' )
13
13
const fetchSearchSuggestions = require ( '../fetchSearchSuggestions' )
14
14
const searchProviders = require ( '../../../js/data/searchProviders' )
15
15
const settings = require ( '../../../js/constants/settings' )
16
16
const Immutable = require ( 'immutable' )
17
17
const config = require ( '../../../js/constants/config' )
18
18
const top500 = require ( '../../../js/data/top500' )
19
- const siteTags = require ( '../../../js/constants/siteTags' )
20
19
const suggestion = require ( '../lib/suggestion' )
21
20
const suggestionTypes = require ( '../../../js/constants/suggestionTypes' )
22
21
const { navigateSiteClickHandler, frameClickHandler} = require ( '../suggestionClickHandlers' )
@@ -129,11 +128,6 @@ const updateUrlSuffix = (state, suggestionList) => {
129
128
const generateNewSuggestionsList = ( state ) => {
130
129
const activeFrameKey = state . get ( 'activeFrameKey' )
131
130
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
- }
137
131
const searchResults = state . getIn ( activeFrameStatePath ( state ) . concat ( [ 'navbar' , 'urlbar' , 'suggestions' , 'searchResults' ] ) )
138
132
const frameSearchDetail = state . getIn ( activeFrameStatePath ( state ) . concat ( [ 'navbar' , 'urlbar' , 'searchDetail' ] ) )
139
133
const searchDetail = state . get ( 'searchDetail' )
@@ -148,27 +142,32 @@ const generateNewSuggestionsList = (state) => {
148
142
const mapListToElements = ( { data, maxResults, type, clickHandler = navigateSiteClickHandler . bind ( this ) ,
149
143
sortHandler = defaultme , formatTitle = defaultme , formatUrl = defaultme ,
150
144
filterValue = ( site ) => {
151
- return site . toLowerCase ( ) . includes ( urlLocationLower )
145
+ return site . toLowerCase ( ) . indexOf ( urlLocationLower ) !== - 1
152
146
}
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
157
149
// Filter out things which are already in the suggestions list
158
- . filter ( ( site ) =>
150
+ let filteredData = data . filter ( ( site ) =>
159
151
suggestionsList . findIndex ( ( x ) => ( x . location || '' ) . toLowerCase ( ) === ( formatUrl ( site ) || '' ) . toLowerCase ( ) ) === - 1 ||
160
152
// Tab autosuggestions should always be included since they will almost always be in history
161
153
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
+ }
172
171
173
172
const shouldNormalize = suggestion . shouldNormalizeLocation ( urlLocationLower )
174
173
const urlLocationLowerNormalized = suggestion . normalizeLocation ( urlLocationLower )
@@ -201,62 +200,73 @@ const generateNewSuggestionsList = (state) => {
201
200
}
202
201
}
203
202
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 ) {
210
209
// Note: Bookmark sites are now included in history. This will allow
211
210
// sites to appear in the auto-complete regardless of their bookmark
212
211
// status. If history is turned off, bookmarked sites will appear
213
212
// 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
+ }
219
222
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
+ } )
223
238
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
+ }
239
255
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
+ }
260
270
}
261
271
262
272
// about pages
@@ -279,8 +289,8 @@ const generateNewSuggestionsList = (state) => {
279
289
filterValue : ( frame ) => ! isSourceAboutUrl ( frame . get ( 'location' ) ) &&
280
290
frame . get ( 'key' ) !== activeFrameKey &&
281
291
(
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 )
284
294
)
285
295
} ) )
286
296
}
0 commit comments