React = require 'react' {Actions, AccountStore} = require("nylas-exports") crypto = require 'crypto' {RetinaImg} = require 'nylas-component-kit' classNames = require 'classnames' class AccountSwitcher extends React.Component @displayName: 'AccountSwitcher' @containerRequired: false @containerStyles: minWidth: 165 maxWidth: 210 constructor: (@props) -> @state = @_getStateFromStores() @state.showing = false componentDidMount: => @unsubscribers = [] @unsubscribers.push AccountStore.listen @_onStoreChange componentWillUnmount: => unsubscribe() for unsubscribe in @unsubscribers render: => return false unless @state.account classnames = "" classnames += "open" if @state.showing
{@_renderPrimaryItem()} {@_renderDropdown()}
_renderPrimaryItem: => label = @state.account.label.trim()
{@_renderGravatarForAccount(@state.account)}
{label}
_renderAccount: (account) => email = account.emailAddress.trim().toLowerCase() label = account.label.trim() classes = classNames "active": account is @state.account "item": true "secondary-item": true
@_onSwitchAccount(account)} key={email}> {@_renderGravatarForAccount(account)}
{label}
_renderNewAccountOption: =>
Manage accounts…
_renderDropdown: =>
{@state.accounts.map(@_renderAccount)} {@_renderNewAccountOption()}
_renderGravatarForAccount: (account) => email = account.emailAddress.trim().toLowerCase() hash = crypto.createHash('md5').update(email, 'utf8').digest('hex') url = "url(http://www.gravatar.com/avatar/#{hash}?d=blank&s=56)"
_toggleDropdown: => @setState showing: !@state.showing _onStoreChange: => @setState @_getStateFromStores() _onBlur: (e) => target = e.nativeEvent.relatedTarget if target? and React.findDOMNode(@refs.button).contains(target) return @setState(showing: false) _onSwitchAccount: (account) => Actions.selectAccount(account.id) @setState(showing: false) _onManageAccounts: => Actions.switchPreferencesTab('Accounts') Actions.openPreferences() @setState(showing: false) _getStateFromStores: => accounts: AccountStore.accounts() account: AccountSidebarStore.currentAccount() module.exports = AccountSwitcher