Mailspring/internal_packages/search-bar/lib/search-suggestion-store.coffee
Ben Gotow 607ca3bf10 feat(accounts): Kill namespaces, long live accounts
Summary:
This diff replaces the Namespace object with the Account object, and changes all references to namespace_id => account_id, etc. The endpoints are now `/threads` instead of `/n/<id>/threads`.

This diff also adds preliminary support for multiple accounts. When you log in, we now log you in to all the attached accounts on edgehill server. From the preferences panel, you can auth with / unlink additional accounts. Shockingly, this all seems to pretty much work.

When replying to a thread, you cannot switch from addresses. However, when creating a new message in a popout composer, you can change the from address and the SaveDraftTask will delete/re-root the draft on the new account.

Search bar doesn't need to do full refresh on clear if it never committed

Allow drafts to be switched to a different account when not in reply to an existing thread

Fix edge case where ChangeMailTask throws exception if no models are modified during performLocal

Show many dots for many accounts in long polling status bar

add/remove accounts from prefs

Spec fixes!

Test Plan: Run tests, none broken!

Reviewers: evan, dillon

Reviewed By: evan, dillon

Differential Revision: https://phab.nylas.com/D1928
2015-08-21 15:29:58 -07:00

115 lines
2.9 KiB
CoffeeScript

Reflux = require 'reflux'
{Actions,
Contact,
Thread,
DatabaseStore,
ContactStore} = require 'nylas-exports'
_ = require 'underscore'
# Stores should closely match the needs of a particular part of the front end.
# For example, we might create a "MessageStore" that observes this store
# for changes in selectedThread, "DatabaseStore" for changes to the underlying database,
# and vends up the array used for that view.
SearchSuggestionStore = Reflux.createStore
init: ->
@_query = ""
@_committedQuery = ""
@_clearResults()
@listenTo Actions.searchQueryChanged, @onSearchQueryChanged
@listenTo Actions.searchQueryCommitted, @onSearchQueryCommitted
@listenTo Actions.searchBlurred, @onSearchBlurred
onSearchQueryChanged: (query) ->
@_query = query
@_rebuildResults()
onSearchQueryCommitted: (query) ->
@_query = query
@_committedQuery = query
@_clearResults()
@trigger()
onSearchBlurred: ->
@_clearResults()
@trigger()
_clearResults: ->
@_threadResults = null
@_contactResults = null
@_suggestions = []
_rebuildResults: ->
{key, val} = @queryKeyAndVal()
unless key and val
@_clearResults()
@trigger(@)
return
@_contactResults = ContactStore.searchContacts(val, limit:10)
@_rebuildThreadResults()
@_compileSuggestions()
_rebuildThreadResults: ->
{key, val} = @queryKeyAndVal()
return @_threadResults = [] unless val
# Don't update thread results if a previous query is still running, it'll
# just make performance even worse. When the old result comes in, re-run
return if @_threadQueryInFlight
@_threadQueryInFlight = true
DatabaseStore.findAll(Thread, [Thread.attributes.subject.like(val)])
.order(Thread.attributes.lastMessageReceivedTimestamp.descending())
.limit(4)
.then (results) =>
@_threadQueryInFlight = false
if val is @queryKeyAndVal().val
@_threadResults = results
@_compileSuggestions()
else
@_rebuildThreadResults()
_compileSuggestions: ->
{key, val} = @queryKeyAndVal()
return unless key and val
@_suggestions = []
@_suggestions.push
label: "Message Contains: #{val}"
value: [{"all": val}]
if @_threadResults?.length
@_suggestions.push
divider: 'Threads'
_.each @_threadResults, (thread) =>
@_suggestions.push({thread: thread})
if @_contactResults?.length
@_suggestions.push
divider: 'People'
_.each @_contactResults, (contact) =>
@_suggestions.push
contact: contact
value: [{"all": contact.email}]
@trigger(@)
# Exposed Data
query: -> @_query
queryKeyAndVal: ->
return {} unless @_query and @_query.length > 0
term = @_query[0]
key = Object.keys(term)[0]
val = term[key]
{key, val}
committedQuery: -> @_committedQuery
suggestions: ->
@_suggestions
module.exports = SearchSuggestionStore