Mailspring/internal_packages/account-sidebar/lib/account-sidebar.cjsx
Ben Gotow b2f8b34a32 feat(counts): Unread counts for all folders and labels across all accounts
Summary:
This diff replaces the UnreadCountStore with a better approach that is able to track unread counts for all folders/labels without continuous (and cripplingly slow) SELECT COUNT(*) queries.

When models are written to the database, we currently don't send out notifications with the "previous" state of those objects in the database. This makes it hard to determine how to update counters. (In the future, we may need to do this for live queries). Unfortunately, getting the "previous" state is going to be very hard, because multiple windows write to the database and the "previous" state we have might be outdated. We'd almost have to run a "SELECT" right before every "REPLACE INTO".

I created an API that allows you to register observers around persistModel and unpersistModel. With this API, you can run queries before and after the database changes are made and pluck just the "before" state you're interested in.

The `ThreadCountsStore` uses this API to determine the impact of persisting a set of threads on the unread counts of different labels. Before the threads are saved, it says "how much do these thread IDs contribute to unread counts currently?". After the write is complete it looks at the models and computes the difference between the old count impact and the new count impact, and updates the counters.

I decided not to attach the unread count to the Label objects themselves because 1) they update frequently and 2) most things observing the DatabaseStore for categories do not care about counts, so they would be updating unnecessarily.

The AccountSidebar now listens to the ThreadCountsStore as well as the CategoryStore, and there's a new preference in the General tab for turning off the counts.

Test Plan: Tests are a work in progress, want to get feedback first!

Reviewers: juan, evan

Reviewed By: evan

Differential Revision: https://phab.nylas.com/D2232
2015-11-23 17:12:22 -08:00

134 lines
4.3 KiB
CoffeeScript

React = require 'react'
_ = require 'underscore'
{Actions, MailViewFilter, WorkspaceStore, ThreadCountsStore} = require("nylas-exports")
{ScrollRegion, Flexbox} = require("nylas-component-kit")
SidebarDividerItem = require("./account-sidebar-divider-item")
SidebarSheetItem = require("./account-sidebar-sheet-item")
AccountSidebarStore = require ("./account-sidebar-store")
AccountSidebarMailViewItem = require("./account-sidebar-mail-view-item")
{RetinaImg} = require 'nylas-component-kit'
class DisclosureTriangle extends React.Component
@displayName: 'DisclosureTriangle'
@propTypes:
collapsed: React.PropTypes.bool
visible: React.PropTypes.bool
onToggleCollapsed: React.PropTypes.func.isRequired
render: ->
classnames = "disclosure-triangle"
classnames += " visible" if @props.visible
classnames += " collapsed" if @props.collapsed
<div className={classnames} onClick={@props.onToggleCollapsed}><div></div></div>
class AccountSidebar extends React.Component
@displayName: 'AccountSidebar'
@containerRequired: false
@containerStyles:
minWidth: 165
maxWidth: 210
constructor: (@props) ->
@state = @_getStateFromStores()
@state.collapsed = NylasEnv.config.get('core.accountSidebarCollapsed') ? {}
componentDidMount: =>
@unsubscribers = []
@unsubscribers.push AccountSidebarStore.listen @_onStoreChange
@unsubscribers.push ThreadCountsStore.listen @_onStoreChange
@configSubscription = NylasEnv.config.observe('core.workspace.showUnreadForAllCategories', @_onStoreChange)
componentWillUnmount: =>
unsubscribe() for unsubscribe in @unsubscribers
@configSubscription?.dispose()
render: =>
<ScrollRegion style={flex:1} id="account-sidebar">
<div className="account-sidebar-sections">
{@_sections()}
</div>
</ScrollRegion>
_sections: =>
@state.sections.map (section) =>
<section key={section.label}>
<div className="heading">{section.label}</div>
{@_itemComponents(section.items)}
</section>
_itemComponents: (items) =>
components = []
items.forEach (item) =>
components.push(
<span key={item.id} className="item-container">
<DisclosureTriangle
collapsed={@state.collapsed[item.id]}
visible={item.children.length > 0}
onToggleCollapsed={ => @_onToggleCollapsed(item.id)}/>
{@_itemComponent(item)}
</span>
)
if item.children.length and not @state.collapsed[item.id]
components.push(
<section key={"#{item.id}-children"}>
{@_itemComponents(item.children)}
</section>
)
components
_itemUnreadCount: (item) =>
category = item.mailViewFilter.category
if category and (category.name is 'inbox' or @state.unreadCountsForAll)
return @state.unreadCounts[category.id]
return 0
_itemComponent: (item) =>
unless item instanceof WorkspaceStore.SidebarItem
throw new Error("AccountSidebar:_itemComponents: sections contained an \
item which was not a SidebarItem")
if item.component
Component = item.component
<Component
item={item}
select={item.id is @state.selected?.id } />
else if item.mailViewFilter
<AccountSidebarMailViewItem
item={item}
itemUnreadCount={@_itemUnreadCount(item)}
mailView={item.mailViewFilter}
select={item.mailViewFilter.isEqual(@state.selected)} />
else if item.sheet
<SidebarSheetItem
item={item}
select={item.sheet.id is @state.selected?.id} />
else
throw new Error("AccountSidebar:_itemComponents: each item must have a \
custom component, or a sheet or mailViewFilter")
_onStoreChange: =>
@setState @_getStateFromStores()
_onToggleCollapsed: (itemId) =>
collapsed = _.clone(@state.collapsed)
collapsed[itemId] = !collapsed[itemId]
NylasEnv.config.set('core.accountSidebarCollapsed', collapsed)
@setState({collapsed})
_getStateFromStores: =>
sections: AccountSidebarStore.sections()
selected: AccountSidebarStore.selected()
unreadCounts: ThreadCountsStore.unreadCounts()
unreadCountsForAll: NylasEnv.config.get('core.workspace.showUnreadForAllCategories')
module.exports = AccountSidebar