<commit_before>_ = require 'underscore'
React = require 'react'
{Actions,
 Utils,
 Thread,
 ChangeLabelsTask,
 CategoryStore,
 AccountStore} = require 'nylas-exports'

class MailImportantIcon extends React.Component
  @displayName: 'MailImportantIcon'
  @propTypes:
    thread: React.PropTypes.object

  constructor: (@props) ->
    @state = @getStateFromStores()

  getStateFromStores: =>
    showing: AccountStore.current().usesImportantFlag() and atom.config.get('core.showImportant')

  componentDidMount: =>
    @subscription = atom.config.observe 'core.showImportant', =>
      @setState(@getStateFromStores())

  componentWillUnmount: =>
    @subscription?.dispose()

  shouldComponentUpdate: (nextProps, nextState) =>
    return false if nextProps.thread is @props.thread and @state.showing is nextState.showing
    true

  render: =>
    return false unless @state.showing

    importantId = CategoryStore.getStandardCategory('important').id
    isImportant = _.findWhere(@props.thread.labels, {id: importantId})?

    activeClassname = if isImportant then "active" else ""
    <div className="mail-important-icon #{activeClassname}" onClick={@_onToggleImportant}></div>

  _onToggleImportant: (event) =>
    importantLabel = CategoryStore.getStandardCategory('important')
    isImportant = _.findWhere(@props.thread.labels, {id: importantLabel.id})?

    if isImportant
      task = new ChangeLabelsTask(thread: @props.thread, labelsToRemove: [importantLabel], labelsToAdd: [])
    else
      task = new ChangeLabelsTask(thread: @props.thread, labelsToAdd: [importantLabel], labelsToRemove: [])

    Actions.queueTask(task)

    # Don't trigger the thread row click
    event.stopPropagation()

module.exports = MailImportantIcon<commit_msg>fix(mail-important): Fix broken specs when getStandardCategory('important') returns null<commit_after>_ = require 'underscore'
React = require 'react'
{Actions,
 Utils,
 Thread,
 ChangeLabelsTask,
 CategoryStore,
 AccountStore} = require 'nylas-exports'

class MailImportantIcon extends React.Component
  @displayName: 'MailImportantIcon'
  @propTypes:
    thread: React.PropTypes.object

  constructor: (@props) ->
    @state = @getStateFromStores()

  getStateFromStores: =>
    showing: AccountStore.current().usesImportantFlag() and atom.config.get('core.showImportant')

  componentDidMount: =>
    @subscription = atom.config.observe 'core.showImportant', =>
      @setState(@getStateFromStores())

  componentWillUnmount: =>
    @subscription?.dispose()

  shouldComponentUpdate: (nextProps, nextState) =>
    return false if nextProps.thread is @props.thread and @state.showing is nextState.showing
    true

  render: =>
    return false unless @state.showing

    importantId = CategoryStore.getStandardCategory('important')?.id
    return false unless importantId

    isImportant = _.findWhere(@props.thread.labels, {id: importantId})?

    activeClassname = if isImportant then "active" else ""
    <div className="mail-important-icon #{activeClassname}" onClick={@_onToggleImportant}></div>

  _onToggleImportant: (event) =>
    importantLabel = CategoryStore.getStandardCategory('important')
    isImportant = _.findWhere(@props.thread.labels, {id: importantLabel.id})?

    if isImportant
      task = new ChangeLabelsTask(thread: @props.thread, labelsToRemove: [importantLabel], labelsToAdd: [])
    else
      task = new ChangeLabelsTask(thread: @props.thread, labelsToAdd: [importantLabel], labelsToRemove: [])

    Actions.queueTask(task)

    # Don't trigger the thread row click
    event.stopPropagation()

module.exports = MailImportantIcon
{'fraction_non_alphanumeric': 0.11590594744121716, 'fraction_numerical': 0.0, 'mean_word_length': 4.832258064516129, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 23, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4318878', 'n_tokens_mistral': 1062, 'n_tokens_neox': 1037, 'n_words': 228}