|
5 | 5 | const Immutable = require('immutable')
|
6 | 6 | const {makeImmutable} = require('./immutableUtil')
|
7 | 7 | const siteUtil = require('../../../js/state/siteUtil')
|
| 8 | +const aboutNewTabMaxEntries = 100 |
8 | 9 |
|
9 |
| -const excludeSiteDetail = (siteDetail) => { |
10 |
| - return !siteUtil.isBookmark(siteDetail) && !siteUtil.isHistoryEntry(siteDetail) |
| 10 | +const compareSites = (site1, site2) => { |
| 11 | + if (!site1 || !site2) return false |
| 12 | + return site1.get('location') === site2.get('location') && |
| 13 | + site1.get('partitionNumber') === site2.get('partitionNumber') |
11 | 14 | }
|
12 |
| - |
13 |
| -const removeDuplicateSites = (sites) => { |
14 |
| - // Filter out duplicate entries by location |
15 |
| - return sites.filter((element, index, list) => { |
16 |
| - if (!element) return false |
17 |
| - return index === list.findIndex((site) => site && site.get('location') === element.get('location')) |
18 |
| - }) |
| 15 | +const pinnedTopSites = (state) => { |
| 16 | + return (state.getIn(['about', 'newtab', 'pinnedTopSites']) || Immutable.List()).setSize(18) |
| 17 | +} |
| 18 | +const ignoredTopSites = (state) => { |
| 19 | + return state.getIn(['about', 'newtab', 'ignoredTopSites']) || Immutable.List() |
| 20 | +} |
| 21 | +const isPinned = (state, siteProps) => { |
| 22 | + return pinnedTopSites(state).filter((site) => compareSites(site, siteProps)).size > 0 |
| 23 | +} |
| 24 | +const isIgnored = (state, siteProps) => { |
| 25 | + return ignoredTopSites(state).filter((site) => compareSites(site, siteProps)).size > 0 |
| 26 | +} |
| 27 | +const sortCountDescending = (left, right) => { |
| 28 | + if (left.get('count') < right.get('count')) return 1 |
| 29 | + if (left.get('count') > right.get('count')) return -1 |
| 30 | + return 0 |
19 | 31 | }
|
20 | 32 |
|
21 |
| -const aboutNewTabState = { |
22 |
| - mergeDetails: (state, props) => { |
23 |
| - state = makeImmutable(state) |
24 |
| - if (!props) { |
25 |
| - return state |
| 33 | +/** |
| 34 | + * topSites are defined by users. Pinned sites are attached to their positions |
| 35 | + * in the grid, and the non pinned indexes are populated with newly accessed sites |
| 36 | + */ |
| 37 | +const getTopSites = (state) => { |
| 38 | + // remove folders; sort by visit count; enforce a max limit |
| 39 | + const sites = (state.get('sites') || new Immutable.List()) |
| 40 | + .filter((site) => !siteUtil.isFolder(site)) |
| 41 | + .sort(sortCountDescending) |
| 42 | + .slice(-aboutNewTabMaxEntries) |
| 43 | + |
| 44 | + // Filter out pinned and ignored sites |
| 45 | + let unpinnedSites = sites.filter((site) => !(isPinned(state, site) || isIgnored(state, site))) |
| 46 | + |
| 47 | + // TODO(bsclifton): de-dupe here |
| 48 | + // .. |
| 49 | + |
| 50 | + // Merge the pinned and unpinned lists together |
| 51 | + // Pinned items have priority because the position is important |
| 52 | + let gridSites = pinnedTopSites(state).map((pinnedSite) => { |
| 53 | + // Fetch latest siteDetail objects from appState.sites using location/partition |
| 54 | + if (pinnedSite) { |
| 55 | + const matches = sites.filter((site) => compareSites(site, pinnedSite)) |
| 56 | + if (matches.size > 0) return matches.first() |
26 | 57 | }
|
| 58 | + // Default to unpinned items |
| 59 | + const firstSite = unpinnedSites.first() |
| 60 | + unpinnedSites = unpinnedSites.shift() |
| 61 | + return firstSite |
| 62 | + }) |
27 | 63 |
|
28 |
| - state = state.mergeIn(['about', 'newtab'], props.newTabPageDetail) |
29 |
| - return state.setIn(['about', 'newtab', 'updatedStamp'], new Date().getTime()) |
30 |
| - }, |
31 |
| - |
32 |
| - addSite: (state, props) => { |
33 |
| - state = makeImmutable(state) |
34 |
| - if (!props) { |
35 |
| - return state |
36 |
| - } |
| 64 | + // Include up to [aboutNewTabMaxEntries] entries so that folks |
| 65 | + // can ignore sites and have new items fill those empty spaces |
| 66 | + if (unpinnedSites.size > 0) { |
| 67 | + gridSites = gridSites.concat(unpinnedSites) |
| 68 | + } |
37 | 69 |
|
38 |
| - // Add timestamp if missing (ex: this is a visit, not a bookmark) |
39 |
| - let siteDetail = makeImmutable(props.siteDetail) |
40 |
| - siteDetail = siteDetail.set('lastAccessedTime', siteDetail.get('lastAccessedTime') || new Date().getTime()) |
| 70 | + return gridSites.filter((site) => site != null) |
| 71 | +} |
41 | 72 |
|
42 |
| - // Only bookmarks and history items should be considered |
43 |
| - if (excludeSiteDetail(siteDetail)) { |
44 |
| - return state |
45 |
| - } |
| 73 | +const aboutNewTabState = { |
| 74 | + maxSites: aboutNewTabMaxEntries, |
46 | 75 |
|
47 |
| - // Keep track of the last 18 visited sites |
48 |
| - let sites = state.getIn(['about', 'newtab', 'sites']) || new Immutable.List() |
49 |
| - sites = sites.unshift(siteDetail) |
50 |
| - sites = removeDuplicateSites(sites) |
51 |
| - sites = sites.take(18) |
52 |
| - // TODO(cezaraugusto): Sort should respect unshift and don't prioritize bookmarks |
53 |
| - // | |
54 |
| - // V |
55 |
| - // .sort(suggestion.sortByAccessCountWithAgeDecay) |
56 |
| - sites = siteUtil.addSite(sites, siteDetail, props.tag, props.originalSiteDetail) |
57 |
| - state = state.setIn(['about', 'newtab', 'sites'], sites) |
58 |
| - return state.setIn(['about', 'newtab', 'updatedStamp'], new Date().getTime()) |
| 76 | + getSites: (state) => { |
| 77 | + return state.getIn(['about', 'newtab', 'sites']) |
59 | 78 | },
|
60 | 79 |
|
61 |
| - removeSite: (state, props) => { |
| 80 | + mergeDetails: (state, props) => { |
62 | 81 | state = makeImmutable(state)
|
63 | 82 | if (!props) {
|
64 | 83 | return state
|
65 | 84 | }
|
66 | 85 |
|
67 |
| - // Only bookmarks and history items should be considered |
68 |
| - let siteDetail = makeImmutable(props.siteDetail) |
69 |
| - if (excludeSiteDetail(siteDetail)) { |
70 |
| - return state |
71 |
| - } |
72 |
| - |
73 |
| - // Remove tags if this is a history item. |
74 |
| - // NOTE: siteUtil.removeSite won't delete the entry unless tags are missing |
75 |
| - if (siteDetail.get('tags') && siteDetail.get('tags').size === 0) { |
76 |
| - siteDetail = siteDetail.delete('tags') |
77 |
| - } |
78 |
| - |
79 |
| - const sites = state.getIn(['about', 'newtab', 'sites']) |
80 |
| - state = state.setIn(['about', 'newtab', 'sites'], siteUtil.removeSite(sites, siteDetail, undefined)) |
| 86 | + state = state.mergeIn(['about', 'newtab'], props.newTabPageDetail) |
81 | 87 | return state.setIn(['about', 'newtab', 'updatedStamp'], new Date().getTime())
|
82 | 88 | },
|
83 | 89 |
|
84 |
| - updateSiteFavicon: (state, props) => { |
| 90 | + setSites: (state) => { |
85 | 91 | state = makeImmutable(state)
|
86 |
| - props = makeImmutable(props) |
87 |
| - if (!props || !props.get('frameProps') || !props.getIn(['frameProps', 'location'])) { |
88 |
| - return state |
89 |
| - } |
90 | 92 |
|
91 |
| - const sites = state.getIn(['about', 'newtab', 'sites']) |
92 |
| - const sitesWithFavicon = siteUtil.updateSiteFavicon(sites, props.getIn(['frameProps', 'location']), props.get('favicon')) |
93 |
| - state = state.setIn(['about', 'newtab', 'sites'], sitesWithFavicon) |
| 93 | + // return a filtered version of the sites array |
| 94 | + state = state.setIn(['about', 'newtab', 'sites'], getTopSites(state)) |
94 | 95 | return state.setIn(['about', 'newtab', 'updatedStamp'], new Date().getTime())
|
95 | 96 | }
|
96 | 97 | }
|
|
0 commit comments