Mailspring/internal_packages/search-bar/lib/search-bar.cjsx
Ben Gotow 606909e256 feat(mail-rules): Per-account mail rules filter incoming, existing mail
Summary:
Originally, this was going to be a totally independent package, but
I wasn't able to isolate the functionality and get it tied in to
the delta-stream consumption. Here's how it currently works:

- The preferences package has a new tab which allows you to edit
  mail filters. Filters are saved in a new core store, and a new
  stock component (ScenarioEditor) renders the editor. The editor
  takes a set of templates that define a value space, and outputs
  a valid set of values.

- A new MailFilterProcessor takes messages and creates tasks to
  apply the actions from the MailFiltersStore.

- The worker-sync package now uses the MailFilterProcessor to
  apply filters /before/ it calls didPassivelyReceiveNewModels,
  so filtrs are applied before any notifications are created.

- A new task, ReprocessMailFiltersTask allows you to run filters
  on all of your existing mail. It leverages the existing TaskQueue
  architecture to: a) resume where it left off if you quit midway,
  b) be queryable (for status) from all windows and c) cancelable.
  The TaskQueue is a bit strange because it runs performLocal and
  performRemote very differently, and I had to use `performRemote`.
  (todo refactor soon.)

This diff also changes the EditableList a bit to behave like a
controlled component and render focused / unfocused states.

Test Plan: Run tests, only for actual filter processing atm.

Reviewers: juan, evan

Reviewed By: evan

Differential Revision: https://phab.nylas.com/D2379
2015-12-23 02:19:32 -05:00

155 lines
4.4 KiB
CoffeeScript

React = require 'react/addons'
classNames = require 'classnames'
{Actions, WorkspaceStore} = require 'nylas-exports'
{Menu, RetinaImg, KeyCommandsRegion} = require 'nylas-component-kit'
SearchSuggestionStore = require './search-suggestion-store'
_ = require 'underscore'
class SearchBar extends React.Component
@displayName = 'SearchBar'
constructor: (@props) ->
@state =
query: ""
focused: false
suggestions: []
committedQuery: null
componentDidMount: =>
@usub = []
@usub.push SearchSuggestionStore.listen @_onChange
@usub.push WorkspaceStore.listen =>
@setState(focused: false) if @state.focused
# It's important that every React class explicitly stops listening to
# N1 events before it unmounts. Thank you event-kit
# This can be fixed via a Reflux mixin
componentWillUnmount: =>
usub() for usub in @usub
_keymapHandlers: ->
'application:focus-search': @_onFocusSearch
'search-bar:escape-search': @_clearAndBlur
render: =>
inputValue = @_queryToString(@state.query)
inputClass = classNames
'empty': inputValue.length is 0
headerComponents = [
<input type="text"
ref="searchInput"
key="input"
className={inputClass}
placeholder="Search all email"
value={inputValue}
onChange={@_onValueChange}
onFocus={@_onFocus}
onBlur={@_onBlur} />
<RetinaImg className="search-accessory search"
name="searchloupe.png"
key="accessory"
mode={RetinaImg.Mode.ContentDark}
onClick={@_doSearch} />
<RetinaImg className="search-accessory clear"
name="searchclear.png"
key="clear"
mode={RetinaImg.Mode.ContentDark}
onClick={@_onClearSearch} />
]
itemContentFunc = (item) =>
if item.divider
<Menu.Item divider={item.divider} />
else if item.contact
<Menu.NameEmailItem name={item.contact.name} email={item.contact.email} />
else if item.thread
item.thread.subject
else
item.label
<KeyCommandsRegion className="search-bar" globalHandlers={@_keymapHandlers()}>
<div>
<Menu ref="menu"
className={@_containerClasses()}
headerComponents={headerComponents}
items={@state.suggestions}
itemContent={itemContentFunc}
itemKey={ (item) -> item.id ? item.label }
onSelect={@_onSelectSuggestion}
/>
</div>
</KeyCommandsRegion>
_onFocusSearch: =>
React.findDOMNode(@refs.searchInput).focus()
_containerClasses: =>
classNames
'focused': @state.focused
'showing-query': @state.query?.length > 0
'committed-query': @state.committedQuery?.length > 0
'search-container': true
'showing-suggestions': @state.suggestions?.length > 0
_queryToString: (query) =>
return "" unless query instanceof Array
str = ""
for term in query
for key,val of term
if key == "all"
str += val
else
str += val
str
_stringToQuery: (str) =>
return [] unless str
return [{all: str}]
_onValueChange: (event) =>
Actions.searchQueryChanged(@_stringToQuery(event.target.value))
if (event.target.value is '')
@_onClearSearch()
_onSelectSuggestion: (item) =>
if item.thread?
Actions.searchQueryCommitted([{all: "\"#{item.thread.subject}\""}])
else
Actions.searchQueryCommitted(item.value)
_onClearSearch: (event) =>
if @state.committedQuery
Actions.searchQueryCommitted(null)
else
Actions.searchQueryChanged(null)
_clearAndBlur: =>
@_onClearSearch()
React.findDOMNode(@refs.searchInput)?.blur()
_onFocus: =>
@setState focused: true
_onBlur: =>
# Don't immediately hide the menu when the text input is blurred,
# because the user might have clicked an item in the menu. Wait to
# handle the touch event, then dismiss the menu.
setTimeout =>
Actions.searchBlurred()
@setState(focused: false)
, 150
_doSearch: =>
Actions.searchQueryCommitted(@state.query)
_onChange: => @setState @_getStateFromStores()
_getStateFromStores: =>
query: SearchSuggestionStore.query()
suggestions: SearchSuggestionStore.suggestions()
committedQuery: SearchSuggestionStore.committedQuery()
module.exports = SearchBar