Mailspring/internal_packages/account-sidebar/lib/account-sidebar-store.coffee
Juan Tejada d265bf1248 fix(category-store): Fix issue with observables in CategoryStore
- Removes use of observables from category store and keeps a big cache
  of categories per account
- Upates Category Observables with new helper observables
- Updates CategoryPicker and AccountSidebarStore to use observables
- Misc fixes
2016-01-08 18:03:18 -08:00

184 lines
5.6 KiB
CoffeeScript

NylasStore = require 'nylas-store'
_ = require 'underscore'
{DatabaseStore,
AccountStore,
ThreadCountsStore,
WorkspaceStore,
Actions,
Label,
Folder,
Message,
MailboxPerspective,
FocusedPerspectiveStore,
SyncbackCategoryTask,
DestroyCategoryTask,
CategoryHelpers,
Thread} = require 'nylas-exports'
{Categories} = require 'nylas-observables'
class AccountSidebarStore extends NylasStore
constructor: ->
@_sections = []
@_account = AccountStore.accounts()[0] # TODO Temporarily, should be null
@_standardCategories = []
@_userCategories = []
@_registerListeners()
@_updateSections()
########### PUBLIC #####################################################
currentAccount: ->
@_account
sections: ->
@_sections
selected: ->
if WorkspaceStore.rootSheet() is WorkspaceStore.Sheet.Threads
FocusedPerspectiveStore.current()
else
WorkspaceStore.rootSheet()
########### PRIVATE ####################################################
_registerListeners: ->
@listenTo WorkspaceStore, @_updateSections
@listenTo ThreadCountsStore, @_updateSections
@listenTo FocusedPerspectiveStore, => @trigger()
@listenTo Actions.selectAccount, @_onSelectAccount
@configSubscription = NylasEnv.config.observe(
'core.workspace.showUnreadForAllCategories',
@_updateSections
)
_registerObservables: ->
@_disposables ?= []
@_disposables.forEach (disp) -> disp.dispose()
@_disposables = [
Categories.standard(@_currentAccount).subscribe(@_onStandardCategoriesChanged),
Categories.user(@_currentAccount).subscribe(@_onUserCategoriesChanged)
]
_onSelectAccount: (accountId)=>
@_account = AccountStore.accountForId(accountId)
@_registerObservables()
@trigger()
_onStandardCategoriesChanged: (categories) ->
@_standardCategories = categories
@_updateSections()
_onUserCategoriesChanged: (categories) ->
@_userCategories = categories
@_updateSections()
_updateSections: =>
# TODO As it is now, if the current account is null, we will display the
# categories for all accounts.
# Update this to reflect UI decision for sidebar
# Compute hierarchy for userCategoryItems 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
#
userCategoryItemsHierarchical = []
userCategoryItemsSeen = {}
for category in @_userCategories
# 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 = userCategoryItemsSeen[parentKey]
break if parent
if parent
itemDisplayName = category.displayName.substr(parentKey.length+1)
item = @_sidebarItemForCategory(category, itemDisplayName)
parent.children.push(item)
else
item = @_sidebarItemForCategory(category)
userCategoryItemsHierarchical.push(item)
userCategoryItemsSeen[itemKey] = item
# Our drafts are displayed via the `DraftListSidebarItem` which
# is loading into the `Drafts` Sheet.
standardCategories = _.reject @_standardCategories, (category) =>
category.name is "drafts"
standardCategoryItems = _.map standardCategories, (cat) => @_sidebarItemForCategory(cat)
starredItem = @_sidebarItemForMailView('starred', MailboxPerspective.forStarred(@_account))
# Find root views and add them to the bottom of the list (Drafts, etc.)
standardItems = standardCategoryItems
standardItems.splice(1, 0, starredItem)
customSections = {}
for item in WorkspaceStore.sidebarItems()
if item.section
customSections[item.section] ?= []
customSections[item.section].push(item)
else
standardItems.push(item)
@_sections = []
@_sections.push
label: 'Mailboxes'
items: standardItems
for section, items of customSections
@_sections.push
label: section
items: items
@_sections.push
label: CategoryHelpers.categoryLabel(@_account)
items: userCategoryItemsHierarchical
iconName: CategoryHelpers.categoryIconName(@_account)
createItem: @_createCategory
destroyItem: @_destroyCategory
@trigger()
_sidebarItemForMailView: (id, filter) =>
new WorkspaceStore.SidebarItem
id: id,
name: filter.name,
mailboxPerspective: filter
_sidebarItemForCategory: (category, shortenedName) =>
new WorkspaceStore.SidebarItem
id: category.id,
name: shortenedName || category.displayName
mailboxPerspective: MailboxPerspective.forCategory(@_account, category)
unreadCount: @_itemUnreadCount(category)
_createCategory: (displayName) ->
# TODO this needs an account param
return unless @_account?
CategoryClass = @_account.categoryClass()
category = new CategoryClass
displayName: displayName
accountId: @_account.id
Actions.queueTask(new SyncbackCategoryTask({category}))
_destroyCategory: (sidebarItem) ->
category = sidebarItem.mailboxPerspective.category
return if category.isDeleted is true
Actions.queueTask(new DestroyCategoryTask({category}))
_itemUnreadCount: (category) =>
unreadCountEnabled = NylasEnv.config.get('core.workspace.showUnreadForAllCategories')
if category and (category.name is 'inbox' or unreadCountEnabled)
return ThreadCountsStore.unreadCountForCategoryId(category.id)
return 0
module.exports = new AccountSidebarStore()