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

Change sites from List to Map #5531

Merged
merged 5 commits into from
Nov 11, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 1 addition & 11 deletions app/common/state/aboutNewTabState.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,6 @@ const excludeSiteDetail = (siteDetail) => {
return !siteUtil.isBookmark(siteDetail) && !siteUtil.isHistoryEntry(siteDetail)
}

const removeDuplicateSites = (sites) => {
// Filter out duplicate entries by location
return sites.filter((element, index, list) => {
if (!element) return false
return index === list.findIndex((site) => site && site.get('location') === element.get('location'))
})
}

const aboutNewTabState = {
mergeDetails: (state, props) => {
state = makeImmutable(state)
Expand Down Expand Up @@ -45,9 +37,7 @@ const aboutNewTabState = {
}

// Keep track of the last 18 visited sites
let sites = state.getIn(['about', 'newtab', 'sites']) || new Immutable.List()
sites = sites.unshift(siteDetail)
sites = removeDuplicateSites(sites)
let sites = state.getIn(['about', 'newtab', 'sites']) || new Immutable.Map()
sites = sites.take(18)
// TODO(cezaraugusto): Sort should respect unshift and don't prioritize bookmarks
// |
Expand Down
4 changes: 3 additions & 1 deletion app/importer.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ const locale = require('./locale')
var isMergeFavorites = false
var isImportingBookmarks = false
var hasBookmarks
var importedSites

exports.init = () => {
importer.initialize()
Expand Down Expand Up @@ -171,6 +172,7 @@ importer.on('add-bookmarks', (e, bookmarks, topLevelFolder) => {
sites.push(site)
}
}
importedSites = Immutable.fromJS(sites)
appActions.addSite(Immutable.fromJS(sites))
})

Expand All @@ -187,7 +189,7 @@ importer.on('add-favicons', (e, detail) => {
}
}
})
let sites = AppStore.getState().get('sites')
let sites = importedSites
sites = sites.map((site) => {
if ((site.get('favicon') === undefined && site.get('location') !== undefined &&
faviconMap[site.get('location')] !== undefined) ||
Expand Down
22 changes: 16 additions & 6 deletions app/renderer/components/bookmarksToolbar.js
Original file line number Diff line number Diff line change
Expand Up @@ -319,27 +319,37 @@ class BookmarksToolbar extends ImmutableComponent {

// Loop through until we fill up the entire bookmark toolbar width
let i
for (i = 0; i < noParentItems.size; i++) {
let noParentItemsList = noParentItems.toList()
for (i = 0; i < noParentItemsList.size; i++) {
let iconWidth = props.showFavicon ? iconSize : 0
// font-awesome file icons are 3px smaller
if (props.showFavicon && !noParentItems.getIn([i, 'folderId']) && !noParentItems.getIn([i, 'favicon'])) {
if (props.showFavicon && !noParentItemsList.getIn([i, 'folderId']) && !noParentItemsList.getIn([i, 'favicon'])) {
iconWidth -= 3
}
const chevronWidth = props.showFavicon && noParentItems.getIn([i, 'folderId']) ? this.chevronWidth : 0
const chevronWidth = props.showFavicon && noParentItemsList.getIn([i, 'folderId']) ? this.chevronWidth : 0
if (props.showFavicon && props.showOnlyFavicon) {
widthAccountedFor += this.padding + iconWidth + chevronWidth
} else {
const text = noParentItems.getIn([i, 'customTitle']) || noParentItems.getIn([i, 'title']) || noParentItems.getIn([i, 'location'])
const text = noParentItemsList.getIn([i, 'customTitle']) || noParentItemsList.getIn([i, 'title']) || noParentItemsList.getIn([i, 'location'])
widthAccountedFor += Math.min(calculateTextWidth(text, `${this.fontSize} ${this.fontFamily}`) + this.padding + iconWidth + chevronWidth, this.maxWidth)
}
widthAccountedFor += margin
if (widthAccountedFor >= props.windowWidth - overflowButtonWidth) {
break
}
}
this.bookmarksForToolbar = noParentItems.take(i)
const bookmarkSort = (x, y) => {
if (x.get('order') < y.get('order')) {
return -1
} else if (x.get('order') > y.get('order')) {
return 1
} else {
return 0
}
}
this.bookmarksForToolbar = noParentItems.take(i).sort(bookmarkSort)
// Show at most 100 items in the overflow menu
this.overflowBookmarkItems = noParentItems.skip(i).take(100)
this.overflowBookmarkItems = noParentItems.skip(i).take(100).sort(bookmarkSort)
}
componentWillMount () {
this.updateBookmarkData(this.props)
Expand Down
24 changes: 23 additions & 1 deletion app/sessionStore.js
Original file line number Diff line number Diff line change
Expand Up @@ -432,6 +432,28 @@ module.exports.loadAppState = () => {
}
}

// sites refactoring migration
if (Array.isArray(data.sites) && data.sites.length) {
let sites = {}
data.sites.forEach((site) => {
let key = siteUtil.getSiteKey(Immutable.fromJS(site))
sites[key] = site
})
data.sites = sites
}
if (data.about && data.about.newtab && data.about.newtab.sites) {
if (Array.isArray(data.about.newtab.sites) && data.about.newtab.sites.length) {
let sites = {}
data.about.newtab.sites.forEach((site) => {
if (site) {
let key = siteUtil.getSiteKey(Immutable.fromJS(site))
sites[key] = site
}
})
data.about.newtab.sites = sites
}
}

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great job with the above 😄 It's super important to have migrations for the session data

// version information (shown on about:brave)
const os = require('os')
const versionInformation = [
Expand Down Expand Up @@ -466,7 +488,7 @@ module.exports.loadAppState = () => {
module.exports.defaultAppState = () => {
return {
firstRunTimestamp: new Date().getTime(),
sites: [],
sites: {},
tabs: [],
extensions: {},
visits: [],
Expand Down
5 changes: 3 additions & 2 deletions docs/state.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@ AppStore
}
}
},
sites: [{
sites: {
[siteKey]: { // folder: folderId; bookmark/history: location + partitionNumber + parentFolderId
location: string,
title: string,
customTitle: string, // User provided title for bookmark; overrides title
Expand All @@ -46,7 +47,7 @@ AppStore
partitionNumber: number, // Optionally specifies a specific session
folderId: number, // Set for bookmark folders only
parentFolderId: number // Set for bookmarks and bookmark folders only
}],
}},
downloads: [{
[downloadId]: {
startTime: number, // datetime.getTime()
Expand Down
2 changes: 1 addition & 1 deletion js/about/newtab.js
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ class NewTabPage extends React.Component {

const getUnpinned = () => {
const firstSite = unpinnedTopSites.first()
unpinnedTopSites = unpinnedTopSites.shift()
unpinnedTopSites = unpinnedTopSites.slice(1)
return firstSite
}

Expand Down
Loading