Skip to content

Commit

Permalink
Follow up for sites splits PR brave#10296
Browse files Browse the repository at this point in the history
Resolves brave#10296
Resolves brave#12378

Auditors:

Test Plan:
  • Loading branch information
NejcZdovc committed Dec 22, 2017
1 parent db4b3b9 commit 840342a
Show file tree
Hide file tree
Showing 8 changed files with 2,010 additions and 139 deletions.
44 changes: 34 additions & 10 deletions app/common/lib/historyUtil.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,20 +15,32 @@ const sortTimeDescending = (left, right) => {
}

const getHistory = (sites) => {
sites = makeImmutable(sites) ? makeImmutable(sites).toList() : new Immutable.List()
if (sites == null) {
return Immutable.List()
}

sites = makeImmutable(sites) ? makeImmutable(sites).toList() : Immutable.List()
return sites
.sort(sortTimeDescending)
.slice(0, aboutHistoryMaxEntries)
}

const getDayString = (entry, locale) => {
if (entry == null) {
return ''
}

const lastAccessedTime = entry.get('lastAccessedTime')
return lastAccessedTime
? new Date(lastAccessedTime).toLocaleDateString(locale, { weekday: 'long', month: 'long', day: 'numeric', year: 'numeric' })
: ''
}

const groupEntriesByDay = (history, locale) => {
if (history == null) {
return Immutable.List()
}

const reduced = history.reduce((previousValue, currentValue, currentIndex) => {
const result = currentIndex === 1 ? [] : previousValue
if (currentIndex === 1) {
Expand All @@ -44,31 +56,38 @@ const groupEntriesByDay = (history, locale) => {
}
return result
})

if (reduced) {
return Immutable.fromJS(
Array.isArray(reduced)
? reduced
: [{date: getDayString(reduced, locale), entries: [reduced]}]
)
}
return Immutable.fromJS([])
return Immutable.List()
}

/**
* Return an array with ALL entries.
* Format is expected to be array containing one array per day.
*/
const totalEntries = (entriesByDay) => {
entriesByDay = makeImmutable(entriesByDay) || new Immutable.List()
if (entriesByDay == null) {
return Immutable.List()
}

entriesByDay = makeImmutable(entriesByDay)

let result = new Immutable.List()
entriesByDay.forEach((entry) => {
result = result.push(entry.get('entries'))
return entriesByDay.map((entry) => {
return entry.get('entries')
})
return result
}

const prepareHistoryEntry = (siteDetail) => {
if (siteDetail == null) {
return Immutable.Map()
}

const time = siteDetail.has('lastAccessedTime')
? siteDetail.get('lastAccessedTime')
: new Date().getTime()
Expand All @@ -82,7 +101,7 @@ const prepareHistoryEntry = (siteDetail) => {
count: 1,
themeColor: siteDetail.get('themeColor'),
favicon: siteDetail.get('favicon', siteDetail.get('icon')),
key: getKey(siteDetail),
key: module.exports.getKey(siteDetail),
skipSync: siteDetail.get('skipSync', null)
})
}
Expand Down Expand Up @@ -115,16 +134,20 @@ const mergeSiteDetails = (oldDetail, newDetail) => {
site = site.set('favicon', favicon)
}

site = site.set('key', getKey(site))
site = site.set('key', module.exports.getKey(site))

return site
}

const getDetailFromFrame = (frame) => {
if (frame == null) {
return Immutable.Map()
}

return makeImmutable({
location: frame.get('location'),
title: frame.get('title'),
partitionNumber: frame.get('partitionNumber'),
partitionNumber: frame.get('partitionNumber') || 0,
favicon: frame.get('icon'),
themeColor: frame.get('themeColor') || frame.get('computedThemeColor')
})
Expand All @@ -142,6 +165,7 @@ const getKey = (siteDetail) => {
return location + '|' +
(siteDetail.get('partitionNumber') || 0)
}

return null
}

Expand Down
65 changes: 50 additions & 15 deletions app/common/state/bookmarkFoldersState.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,20 +36,39 @@ const bookmarkFoldersState = {

getFolder: (state, folderKey) => {
state = validateState(state)

if (folderKey == null) {
return Immutable.Map()
}

folderKey = folderKey.toString()
return state.getIn([STATE_SITES.BOOKMARK_FOLDERS, folderKey], Immutable.Map())
},

getFoldersByParentId: (state, parentFolderId) => {
state = validateState(state)

if (parentFolderId == null) {
return Immutable.List()
}

const folders = bookmarkOrderCache.getFoldersByParentId(state, parentFolderId)
return folders.map(folder => bookmarkFoldersState.getFolder(state, folder.get('key')))
},

addFolder: (state, folderDetails, destinationKey) => {
state = validateState(state)

if (folderDetails == null) {
return state
}

folderDetails = makeImmutable(folderDetails)

if (folderDetails.get('key') == null) {
return state
}

state = state.setIn([STATE_SITES.BOOKMARK_FOLDERS, folderDetails.get('key')], folderDetails)
state = bookmarkOrderCache.addFolderToCache(state, folderDetails.get('parentFolderId'), folderDetails.get('key'), destinationKey)
return state
Expand Down Expand Up @@ -78,24 +97,23 @@ const bookmarkFoldersState = {
},

removeFolder: (state, folderKey) => {
const bookmarksState = require('./bookmarksState')
const folders = bookmarkFoldersState.getFolders(state)
state = validateState(state)
const folder = bookmarkFoldersState.getFolder(state, folderKey)

if (folder.isEmpty()) {
return state
}

const bookmarksState = require('./bookmarksState')
const folders = bookmarkFoldersState.getFolders(state)

if (getSetting(settings.SYNC_ENABLED) === true) {
syncActions.removeSites([folder.toJS()])
}

folders.filter(folder => folder.get('parentFolderId') === Number(folderKey))
.map(folder => {
state = bookmarksState.removeBookmarksByParentId(state, folder.get('folderId'))
state = bookmarkFoldersState.removeFolder(state, folder.get('folderId'))
state = bookmarkOrderCache.removeCacheParent(state, folder.get('folderId'))
state = bookmarkOrderCache.removeCacheKey(state, folder.get('parentFolderId'), folderKey)
})

state = bookmarksState.removeBookmarksByParentId(state, folderKey)
Expand All @@ -105,14 +123,15 @@ const bookmarkFoldersState = {
},

/**
* Get all folders except provided folder
* Get a list of all folders except provided folder
* @param state
* @param folderKey
* @param {number} folderKey
* @param parentFolderId
* @param labelPrefix
* @returns {Array}
* @returns {Array} - each entry with folder id and label (title)
*/
getFoldersWithoutKey: (state, folderKey, parentFolderId = 0, labelPrefix = '') => {
state = validateState(state)
let folders = []
const results = bookmarkFoldersState.getFoldersByParentId(state, parentFolderId)

Expand All @@ -128,27 +147,36 @@ const bookmarkFoldersState = {
folderId: folder.get('folderId'),
label
})
const subSites = bookmarkFoldersState.getFoldersWithoutKey(state, folderKey, folder.get('folderId'), (label || '') + ' / ')
folders = folders.concat(subSites)
const subFolders = bookmarkFoldersState.getFoldersWithoutKey(state, folderKey, folder.get('folderId'), (label || '') + ' / ')
folders = folders.concat(subFolders)
}

return folders
},

moveFolder: (state, folderKey, destinationKey, append, moveIntoParent) => {
const bookmarksState = require('./bookmarksState')
state = validateState(state)
let folder = bookmarkFoldersState.getFolder(state, folderKey)
let destinationItem = bookmarksState.findBookmark(state, destinationKey)

if (folder.isEmpty()) {
return state
}

const bookmarksState = require('./bookmarksState')
let destinationItem = bookmarksState.findBookmark(state, destinationKey)
const numKey = Number(destinationKey)
if (destinationItem.isEmpty() && numKey !== -1 && numKey !== 0) {
return state
}

if (moveIntoParent || destinationItem.get('parentFolderId') !== folder.get('parentFolderId')) {
const parentFolderId = destinationItem.get('type') === siteTags.BOOKMARK
let parentFolderId = destinationItem.get('type') === siteTags.BOOKMARK
? destinationItem.get('parentFolderId')
: destinationItem.get('folderId')

if (parentFolderId == null) {
parentFolderId = destinationKey
}

state = bookmarkOrderCache.removeCacheKey(state, folder.get('parentFolderId'), folderKey)
folder = folder.set('parentFolderId', Number(parentFolderId))
const newKey = bookmarkFoldersUtil.getKey(folder)
Expand All @@ -169,7 +197,14 @@ const bookmarkFoldersState = {
},

setWidth: (state, key, width) => {
return state.setIn([STATE_SITES.BOOKMARK_FOLDERS, key, 'width'], parseFloat(width))
state = validateState(state)
width = parseFloat(width)

if (key == null || isNaN(width)) {
return state
}

return state.setIn([STATE_SITES.BOOKMARK_FOLDERS, key, 'width'], width)
}
}

Expand Down
Loading

0 comments on commit 840342a

Please sign in to comment.