React = require 'react'
_ = require 'underscore'
classNames = require 'classnames'
NotificationStore = require './notifications-store'
{Actions,
TaskQueue,
NamespaceStore,
NylasAPI} = require 'nylas-exports'
{TimeoutTransitionGroup} = require 'nylas-component-kit'
class ActivitySidebar extends React.Component
@displayName: 'ActivitySidebar'
@containerRequired: false
constructor: (@props) ->
@state = @_getStateFromStores()
componentDidMount: =>
@_unlisteners = []
@_unlisteners.push NamespaceStore.listen @_onNamespacesChanged
@_unlisteners.push TaskQueue.listen @_onDataChanged
@_unlisteners.push NotificationStore.listen @_onDataChanged
@_onNamespacesChanged()
componentWillUnmount: =>
unlisten() for unlisten in @_unlisteners
@_workerUnlisten() if @_workerUnlisten
render: =>
items = [].concat(@_renderSyncActivityItem(), @_renderNotificationActivityItems(), @_renderTaskActivityItems())
names = classNames
"sidebar-activity": true
"sidebar-activity-empty": items.length is 0
"sidebar-activity-error": error?
{items}
_renderSyncActivityItem: =>
count = 0
fetched = 0
progress = 0
incomplete = 0
error = null
for model, modelState of @state.sync
incomplete += 1 unless modelState.complete
error ?= modelState.error
if modelState.count
count += modelState.count / 1
fetched += modelState.fetched / 1
progress = (fetched / count) * 100 if count > 0
if incomplete is 0
return []
else if error
Initial sync encountered an error. Waiting to retry...
Try Again
else
_renderTaskActivityItems: =>
summary = {}
@state.tasks.map (task) ->
label = task.label?()
return unless label
summary[label] ?= 0
summary[label] += 1
_.pairs(summary).map ([label, count]) ->
_renderNotificationActivityItems: =>
@state.notifications.map (notification) ->
_onNamespacesChanged: =>
namespace = NamespaceStore.current()
return unless namespace
@_worker = NylasAPI.workerForNamespace(namespace)
@_workerUnlisten() if @_workerUnlisten
@_workerUnlisten = @_worker.listen(@_onDataChanged, @)
@_onDataChanged()
_onTryAgain: =>
@_worker.resumeFetches()
_onDataChanged: =>
@setState(@_getStateFromStores())
_getStateFromStores: =>
tasks: TaskQueue.queue()
notifications: NotificationStore.notifications()
sync: @_worker?.state()
module.exports = ActivitySidebar