React = require 'react' {Actions, AccountStore} = require("nylas-exports") {ScrollRegion} = require("nylas-component-kit") crypto = require 'crypto' {RetinaImg} = require 'nylas-component-kit' classNames = require 'classnames' class AccountSwitcher extends React.Component @displayName: 'AccountSwitcher' 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
{@_renderAccount(@state.account, true)} {@_renderDropdown()}
_renderAccount: (account, isPrimaryItem) => classes = classNames "account": true "item": true "dropdown-item-padding": not isPrimaryItem "active": account is @state.account "bg-color-hover": not isPrimaryItem "primary-item": isPrimaryItem "account-option": not isPrimaryItem email = account.emailAddress.trim().toLowerCase() if isPrimaryItem dropdownClasses = classNames "account-switcher-dropdown": true, "account-switcher-dropdown-hidden": @state.showing dropdownArrow =
onClick = @_toggleDropdown else onClick = => @_onSwitchAccount account
{dropdownArrow}
{email}
_renderNewAccountOption: =>
Add account…
_renderDropdown: => display = if @state.showing then "block" else "none" # display = "block" accounts = @state.accounts.map (a) => @_renderAccount(a)
{accounts} {@_renderNewAccountOption()}
_toggleDropdown: => @setState showing: !@state.showing _gravatarUrl: (email) => hash = crypto.createHash('md5').update(email, 'utf8').digest('hex') "url(http://www.gravatar.com/avatar/#{hash}?d=blank&s=56)" _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.selectAccountId(account.id) @setState(showing: false) _onAddAccount: => ipc = require('ipc') ipc.send('command', 'application:add-account') @setState(showing: false) _getStateFromStores: => accounts: AccountStore.items() account: AccountStore.current() module.exports = AccountSwitcher