Mailspring/internal_packages/account-sidebar/lib/sidebar-store.coffee
Juan Tejada ce968c0d3e update(sidebar): Moves accounts in sidebar state to FocusedPerspectiveStore
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
2016-06-01 17:13:46 -07:00

123 lines
3.8 KiB
CoffeeScript

_ = require 'underscore'
NylasStore = require 'nylas-store'
{Actions,
AccountStore,
ThreadCountsStore,
WorkspaceStore,
OutboxStore,
FocusedPerspectiveStore,
CategoryStore} = require 'nylas-exports'
SidebarSection = require './sidebar-section'
SidebarActions = require './sidebar-actions'
AccountCommands = require './account-commands'
Sections = {
"Standard",
"User"
}
class SidebarStore extends NylasStore
constructor: ->
NylasEnv.savedState.sidebarKeysCollapsed ?= {}
@_sections = {}
@_sections[Sections.Standard] = {}
@_sections[Sections.User] = []
@_focusedAccounts = FocusedPerspectiveStore.sidebarAccounts()
@_registerCommands()
@_registerMenuItems()
@_registerListeners()
@_updateSections()
accounts: ->
AccountStore.accounts()
focusedAccounts: ->
@_focusedAccounts
standardSection: ->
@_sections[Sections.Standard]
userSections: ->
@_sections[Sections.User]
_registerListeners: ->
@listenTo Actions.setCollapsedSidebarItem, @_onSetCollapsedByName
@listenTo SidebarActions.setKeyCollapsed, @_onSetCollapsedByKey
@listenTo AccountStore, @_onAccountsChanged
@listenTo FocusedPerspectiveStore, @_onFocusedPerspectiveChanged
@listenTo WorkspaceStore, @_updateSections
@listenTo OutboxStore, @_updateSections
@listenTo ThreadCountsStore, @_updateSections
@listenTo CategoryStore, @_updateSections
@configSubscription = NylasEnv.config.onDidChange(
'core.workspace.showUnreadForAllCategories',
@_updateSections
)
return
_onSetCollapsedByKey: (itemKey, collapsed) =>
currentValue = NylasEnv.savedState.sidebarKeysCollapsed[itemKey]
if currentValue isnt collapsed
NylasEnv.savedState.sidebarKeysCollapsed[itemKey] = collapsed
@_updateSections()
_onSetCollapsedByName: (itemName, collapsed) =>
item = _.findWhere(@standardSection().items, {name: itemName})
if not item
for section in @userSections()
item = _.findWhere(section.items, {name: itemName})
break if item
return unless item
@_onSetCollapsedByKey(item.id, collapsed)
_registerCommands: (accounts = AccountStore.accounts()) =>
AccountCommands.registerCommands(accounts)
_registerMenuItems: (accounts = AccountStore.accounts()) =>
AccountCommands.registerMenuItems(accounts, @_focusedAccounts)
# TODO Refactor this
# Listen to changes on the account store only for when the account label
# changes. When accounts or added or removed, those changes will come in
# through the FocusedPerspectiveStore
_onAccountsChanged: =>
@_updateSections()
# TODO Refactor this
# The FocusedPerspectiveStore tells this store the accounts that should be
# displayed in the sidebar (i.e. unified inbox vs single account) and will
# trigger whenever an account is added or removed, as well as when a
# perspective is focused.
# However, when udpating the SidebarSections, we also depend on the actual
# accounts in the AccountStore. The problem is that the FocusedPerspectiveStore
# triggers before the AccountStore is actually updated, so we need to wait for
# the AccountStore to get updated (via `defer`) before updateing our sidebar
# sections
_onFocusedPerspectiveChanged: =>
_.defer =>
@_focusedAccounts = FocusedPerspectiveStore.sidebarAccounts()
@_registerCommands()
@_registerMenuItems()
@_updateSections()
_updateSections: =>
accounts = @_focusedAccounts
multiAccount = accounts.length > 1
@_sections[Sections.Standard] = SidebarSection.standardSectionForAccounts(accounts)
@_sections[Sections.User] = accounts.map (acc) ->
opts = {}
if multiAccount
opts.title = acc.label
opts.collapsible = true
SidebarSection.forUserCategories(acc, opts)
@trigger()
module.exports = new SidebarStore()