React = require 'react' crypto = require 'crypto' classNames = require 'classnames' {Actions} = require 'nylas-exports' {RetinaImg} = require 'nylas-component-kit' ItemTypes = { "Unified" } class AccountSwitcher extends React.Component @displayName: 'AccountSwitcher' @containerRequired: false @containerStyles: minWidth: 165 maxWidth: 210 @propTypes: accounts: React.PropTypes.array.isRequired focusedAccounts: React.PropTypes.array.isRequired constructor: (@props) -> @state = showing: false # Helpers _makeAccountItem: (account) => {id, label, emailAddress, provider} = account email = emailAddress iconName = provider accounts = [account] return {id, label, email, iconName, accounts} _makeUnifiedItem: => id = ItemTypes.Unified label = "All Accounts" email = "" iconName = 'unified' accounts = @props.accounts return {id, label, email, iconName, accounts} _selectedItem: => if @props.focusedAccounts.length > 1 @_makeUnifiedItem() else @_makeAccountItem(@props.focusedAccounts[0]) _toggleDropdown: => @setState showing: !@state.showing # Handlers _onBlur: (e) => target = e.nativeEvent.relatedTarget if target? and React.findDOMNode(@refs.button).contains(target) return @setState(showing: false) _onSwitchAccount: (item) => Actions.focusDefaultMailboxPerspectiveForAccounts(item.accounts) Actions.focusSidebarAccounts(item.accounts) @setState(showing: false) _onManageAccounts: => Actions.switchPreferencesTab('Accounts') Actions.openPreferences() @setState(showing: false) _renderItem: (item) => classes = classNames "active": item.id is @_selectedItem().id "item": true "secondary-item": true
{@_renderGravatar(item)}
{item.label}
_renderManageAccountsItem: =>
Manage accounts…
_renderDropdown: (items) =>
{items.map(@_renderItem)} {@_renderManageAccountsItem()}
_renderGravatar: ({email, iconName}) => if email hash = crypto.createHash('md5').update(email, 'utf8').digest('hex') url = "url(http://www.gravatar.com/avatar/#{hash}?d=blank&s=56)" else url = ''
_renderPrimaryItem: (item) =>
{@_renderGravatar(item)}
{item.label}
render: => return unless @props.focusedAccounts classnames = "account-switcher" classnames += " open" if @state.showing selected = @_selectedItem() if @props.accounts.length is 1 items = @props.accounts.map(@_makeAccountItem) else items = [@_makeUnifiedItem()].concat @props.accounts.map(@_makeAccountItem)
{@_renderPrimaryItem(selected)} {@_renderDropdown(items)}
module.exports = AccountSwitcher