Mailspring/internal_packages/account-sidebar/lib/account-sidebar-store.coffee
Ben Gotow e69da22e5e feat(sidebar): Hierarchical folders/labels in the sidebar, rendering perf
Summary:
Fix label sorting... apparently we just synced them in creation date order

Allow labels / folders to be nested using separators `.`, `/`, and `\`

Allow collapsing of nested labels in sidebar

Add overflow hidden to some core flexboxes, which dramatically reduces repaints because it knows columns will not overflow into other columns

Prevent scroll region contents from re-rendering all the time, not sure why this works

Add test for account sidebar store

Test Plan: Run new test of AccountSidebarStore

Reviewers: evan

Reviewed By: evan

Differential Revision: https://phab.nylas.com/D2181
2015-10-22 10:53:57 -07:00

130 lines
3.7 KiB
CoffeeScript

NylasStore = require 'nylas-store'
_ = require 'underscore'
{CategoryStore,
DatabaseStore,
CategoryStore,
AccountStore,
WorkspaceStore,
DraftCountStore,
Actions,
Label,
Folder,
Message,
MailViewFilter,
FocusedMailViewStore,
NylasAPI,
Thread} = require 'nylas-exports'
class AccountSidebarStore extends NylasStore
constructor: ->
@_sections = []
@_registerListeners()
@_refreshSections()
########### PUBLIC #####################################################
sections: ->
@_sections
selected: ->
if WorkspaceStore.rootSheet() is WorkspaceStore.Sheet.Threads
FocusedMailViewStore.mailView()
else
WorkspaceStore.rootSheet()
########### PRIVATE ####################################################
_registerListeners: ->
@listenTo CategoryStore, @_refreshSections
@listenTo WorkspaceStore, @_refreshSections
@listenTo DraftCountStore, @_refreshSections
@listenTo FocusedMailViewStore, => @trigger()
_refreshSections: =>
account = AccountStore.current()
return unless account
userCategories = CategoryStore.getUserCategories()
userCategoryItems = _.map(userCategories, @_sidebarItemForCategory)
# 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 = CategoryStore.getStandardCategories()
standardCategories = _.reject standardCategories, (category) =>
category.name is "drafts"
standardCategoryItems = _.map standardCategories, (cat) => @_sidebarItemForCategory(cat)
starredItem = @_sidebarItemForMailView('starred', MailViewFilter.forStarred())
# 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: CategoryStore.categoryLabel()
items: userCategoryItemsHierarchical
@trigger()
_sidebarItemForMailView: (id, filter) =>
new WorkspaceStore.SidebarItem
id: id,
name: filter.name,
mailViewFilter: filter
_sidebarItemForCategory: (category, shortenedName) =>
new WorkspaceStore.SidebarItem
id: category.id,
name: shortenedName || category.displayName
mailViewFilter: MailViewFilter.forCategory(category)
module.exports = new AccountSidebarStore()