mirror of
https://github.com/Foundry376/Mailspring.git
synced 2024-11-11 18:32:20 +08:00
ce968c0d3e
Summary: - FocusedPerspectiveStore now holds the state of the accounts that are currently being displayed in the sidebar, i.e. unified inbox or not, which makes it globally accessible state. The SidebarStore just reads this state directly off of the FocusedPerspectiveStore to decide which sections and items to display in the sidebar. - Updates `Actions.focusDefaultMailboxPerspectiveForAccounts` to take an optional array of `sidebarAccountIds` to set in the sidebar. The default behavior is to show the accounts of the perspective that will be focused. E.g. when selecting an account via the account switcher, it will just show the sidebar items for the single account that was selected, but when adding a new account, we are setting the sidebar accounts to all account although we are still focusing the perspective for a single account. - Will now show unified inbox sidebar with correctly focused account when new account added. - Cleans up the code a little bit, but this package still needs major refactor Test Plan: Missing! Reviewers: jackie, bengotow Reviewed By: bengotow Differential Revision: https://phab.nylas.com/D3002
161 lines
4.9 KiB
CoffeeScript
161 lines
4.9 KiB
CoffeeScript
_ = require 'underscore'
|
|
{Actions,
|
|
SyncbackCategoryTask,
|
|
DestroyCategoryTask,
|
|
CategoryStore,
|
|
Category} = require 'nylas-exports'
|
|
SidebarItem = require './sidebar-item'
|
|
SidebarActions = require './sidebar-actions'
|
|
|
|
isSectionCollapsed = (title) ->
|
|
if NylasEnv.savedState.sidebarKeysCollapsed[title] isnt undefined
|
|
NylasEnv.savedState.sidebarKeysCollapsed[title]
|
|
else
|
|
false
|
|
|
|
toggleSectionCollapsed = (section) ->
|
|
return unless section
|
|
SidebarActions.setKeyCollapsed(section.title, not isSectionCollapsed(section.title))
|
|
|
|
class SidebarSection
|
|
|
|
@empty: (title) ->
|
|
return {
|
|
title,
|
|
items: []
|
|
}
|
|
|
|
@standardSectionForAccount: (account) ->
|
|
if not account
|
|
throw new Error("standardSectionForAccount: You must pass an account.")
|
|
|
|
cats = CategoryStore.standardCategories(account)
|
|
return @empty(account.label) if cats.length is 0
|
|
|
|
items = _
|
|
.reject(cats, (cat) -> cat.name is 'drafts')
|
|
.map (cat) => SidebarItem.forCategories([cat], editable: false, deletable: false)
|
|
|
|
unreadItem = SidebarItem.forUnread([account.id])
|
|
starredItem = SidebarItem.forStarred([account.id])
|
|
draftsItem = SidebarItem.forDrafts([account.id])
|
|
snoozedItem = SidebarItem.forSnoozed([account.id])
|
|
|
|
# Order correctly: Inbox, Unread, Starred, rest... , Drafts
|
|
items.splice(1, 0, unreadItem, starredItem, snoozedItem)
|
|
items.push(draftsItem)
|
|
|
|
return {
|
|
title: account.label
|
|
items: items
|
|
}
|
|
|
|
@standardSectionForAccounts: (accounts) ->
|
|
return @empty('All Accounts') if not accounts or accounts.length is 0
|
|
return @empty('All Accounts') if CategoryStore.categories().length is 0
|
|
return @standardSectionForAccount(accounts[0]) if accounts.length is 1
|
|
|
|
standardNames = [
|
|
'inbox',
|
|
'important'
|
|
'sent',
|
|
['archive', 'all'],
|
|
'spam'
|
|
'trash'
|
|
]
|
|
items = []
|
|
|
|
for names in standardNames
|
|
names = if Array.isArray(names) then names else [names]
|
|
categories = CategoryStore.getStandardCategories(accounts, names...)
|
|
continue if categories.length is 0
|
|
|
|
children = []
|
|
accounts.forEach (acc) ->
|
|
cat = _.first(_.compact(
|
|
names.map((name) -> CategoryStore.getStandardCategory(acc, name))
|
|
))
|
|
return unless cat
|
|
children.push(SidebarItem.forCategories([cat], name: acc.label, editable: false, deletable: false))
|
|
|
|
items.push SidebarItem.forCategories(categories, {children, editable: false, deletable: false})
|
|
|
|
accountIds = _.pluck(accounts, 'id')
|
|
|
|
starredItem = SidebarItem.forStarred(accountIds,
|
|
children: accounts.map (acc) -> SidebarItem.forStarred([acc.id], name: acc.label)
|
|
)
|
|
unreadItem = SidebarItem.forUnread(accountIds,
|
|
children: accounts.map (acc) -> SidebarItem.forUnread([acc.id], name: acc.label)
|
|
)
|
|
draftsItem = SidebarItem.forDrafts(accountIds,
|
|
children: accounts.map (acc) -> SidebarItem.forDrafts([acc.id], name: acc.label)
|
|
)
|
|
snoozedItem = SidebarItem.forSnoozed(accountIds,
|
|
children: accounts.map (acc) -> SidebarItem.forSnoozed([acc.id], name: acc.label)
|
|
)
|
|
|
|
# Order correctly: Inbox, Unread, Starred, rest... , Drafts
|
|
items.splice(1, 0, unreadItem, starredItem, snoozedItem)
|
|
items.push(draftsItem)
|
|
|
|
return {
|
|
title: 'All Accounts'
|
|
items: items
|
|
}
|
|
|
|
|
|
@forUserCategories: (account, {title, collapsible} = {}) ->
|
|
return unless account
|
|
# Compute hierarchy for user categories using known "path" separators
|
|
# NOTE: This code uses the fact that userCategoryItems is a sorted set, eg:
|
|
#
|
|
# Inbox
|
|
# Inbox.FolderA
|
|
# Inbox.FolderA.FolderB
|
|
# Inbox.FolderB
|
|
#
|
|
items = []
|
|
seenItems = {}
|
|
for category in CategoryStore.userCategories(account)
|
|
# https://regex101.com/r/jK8cC2/1
|
|
itemKey = category.displayName.replace(/[./\\]/g, '/')
|
|
|
|
parent = null
|
|
parentComponents = itemKey.split('/')
|
|
for i in [parentComponents.length..1] by -1
|
|
parentKey = parentComponents[0...i].join('/')
|
|
parent = seenItems[parentKey]
|
|
break if parent
|
|
|
|
if parent
|
|
itemDisplayName = category.displayName.substr(parentKey.length+1)
|
|
item = SidebarItem.forCategories([category], name: itemDisplayName)
|
|
parent.children.push(item)
|
|
else
|
|
item = SidebarItem.forCategories([category])
|
|
items.push(item)
|
|
seenItems[itemKey] = item
|
|
|
|
|
|
title ?= account.categoryLabel()
|
|
collapsed = isSectionCollapsed(title)
|
|
if collapsible
|
|
onCollapseToggled = toggleSectionCollapsed
|
|
|
|
return {
|
|
title: title
|
|
iconName: account.categoryIcon()
|
|
items: items
|
|
collapsed: collapsed
|
|
onCollapseToggled: onCollapseToggled
|
|
onItemCreated: (displayName) ->
|
|
return unless displayName
|
|
category = new Category
|
|
displayName: displayName
|
|
accountId: account.id
|
|
Actions.queueTask(new SyncbackCategoryTask({category}))
|
|
}
|
|
|
|
|
|
module.exports = SidebarSection
|