Mailspring/app/internal_packages/thread-search/lib/search-bar-util.ts
Ben Gotow 149b389508
Replace Babel with TypeScript compiler, switch entire app to TypeScript 🎉 (#1404)
* Switch to using Typescript instead of Babel

* Switch all es6 / jsx file extensions to ts / tsx

* Convert Utils to a TS module from module.exports style module

* Move everything from module.exports to typescript exports

* Define .d.ts files for mailspring-exports and component kit… Yes it seems this is the best option :(

* Load up on those @types

* Synthesize TS types from PropTypes for standard components

* Add types to Model classes and move constructor constants to instance vars

* 9800 => 7700 TS errors

* 7700 => 5600 TS errors

* 5600 => 5330 TS errors

* 5330 => 4866 TS errors

* 4866 => 4426 TS errors

* 4426 => 2411 TS errors

* 2411 > 1598 TS errors

* 1598 > 769 TS errors

* 769 > 129 TS errors

* 129 > 22 TS errors

* Fix runtime errors

* More runtime error fixes

* Remove support for custom .es6 file extension

* Remove a few odd remaining references to Nylas

* Don’t ship Typescript support in the compiled app for now

* Fix issues in compiled app - module resolution in TS is case sensitive?

* README updates

* Fix a few more TS errors

* Make “No Signature” option clickable + selectable

* Remove flicker when saving file and reloading keymaps

* Fix mail rule item height in preferences

* Fix missing spacing in thread sharing popover

* Fix scrollbar ticks being nested incorrectly

* Add Japanese as a manually reviewed language

* Prevent the thread list from “sticking”

* Re-use Sheet when switching root tabs, prevent sidebar from resetting

* Ensure specs run

* Update package configuration to avoid shpping types

* Turn eslint back on - we will opt-in to the TS rules one by one
2019-03-04 11:03:12 -08:00

150 lines
4.1 KiB
TypeScript

import {
localized,
CategoryStore,
DatabaseStore,
SearchQueryParser,
Thread,
ContactStore,
} from 'mailspring-exports';
import _ from 'underscore';
// start of string or preceding whitespace
// a known token
// one of:
// - whitespace
// - end of string
// - a colon, optional space, and optional word
export const TokenAndTermRegexp = () =>
/(^|\s)(i[ns]?|s[iu]?[nb]?[cj]?e?c?t?|fr?o?m?|to?|ha?s?|be?f?o?r?e?|af?t?e?r?)(?::? ?$|: ?("[^"]*"?|[^\s]+))/gi;
export const LearnMoreURL =
'https://foundry376.zendesk.com/hc/en-us/articles/115002212931-Search-with-advanced-Gmail-style-queries';
export const rankOfRole = role => {
const rank = ['inbox', 'important', 'snoozed', 'sent', 'all', 'spam', 'trash'].indexOf(role);
return rank !== -1 ? 20 - rank : -1000;
};
export const wrapInQuotes = s => `"${s.replace(/"/g, '')}"`;
export const getThreadSuggestions = async (term, accountIds) => {
let dbQuery = DatabaseStore.findAll<Thread>(Thread)
.structuredSearch(SearchQueryParser.parse(`subject:${wrapInQuotes(term)}`))
.order(Thread.attributes.lastMessageReceivedTimestamp.descending())
.limit(10);
if (Array.isArray(accountIds) && accountIds.length === 1) {
dbQuery = dbQuery.where({ accountId: accountIds[0] });
}
return dbQuery.background().then(results => results);
};
export const getContactSuggestions = async (term, accountIds) => {
const results = [];
const contacts = term
? await ContactStore.searchContacts(term, { limit: 5 })
: await ContactStore.topContacts({ limit: 5 });
contacts.forEach(c => results.push(c.email, c.name));
return _.uniq(results).filter(r => r.toLowerCase().startsWith(term));
};
export const getCategorySuggestions = async (term, accountIds) =>
_.uniq(
CategoryStore.categories()
.filter(c => accountIds.includes(c.accountId))
.sort((a, b) => rankOfRole(b.role) - rankOfRole(a.role))
.map(c => c.displayName)
)
.filter(s => s.toLowerCase().startsWith(term))
.slice(0, 8);
export const TokenSuggestions = [
{
token: 'from',
term: '',
description: localized('an email address'),
termSuggestions: getContactSuggestions,
},
{
token: 'to',
term: '',
description: localized('an email address'),
termSuggestions: getContactSuggestions,
},
{
token: 'in',
term: '',
description: localized('folder or label'),
termSuggestions: getCategorySuggestions,
},
{
token: 'before',
term: '',
description: localized('date received or range'),
termSuggestions: ['yesterday', '2 days ago', 'last week', 'last month', '>=2018/05/31'],
},
{
token: 'since',
term: '',
description: localized('date received or range'),
termSuggestions: ['yesterday', '2 days ago', 'last week', 'last month', '>=2018/05/31'],
},
{
token: 'after',
hidden: true,
term: '',
description: localized('date received or range'),
termSuggestions: ['yesterday', '2 days ago', 'last week', 'last month', '>=2018/05/31'],
},
{
token: 'is',
term: '',
description: 'unread, starred',
termSuggestions: ['unread', 'starred'],
},
{
token: 'has',
term: '',
description: 'attachment',
termSuggestions: ['attachment'],
},
{
token: 'subject',
term: '',
description: localized('an email subject'),
termSuggestions: [],
},
// {
// token: 'has',
// term: '',
// description: 'attachment',
// termSuggestions: ['attachment'],
// },
];
export const TokenSuggestionsForEmpty = TokenSuggestions.filter(t => !t.hidden);
export function getCurrentTokenAndTerm(query, insertionIndex) {
const regexp = TokenAndTermRegexp();
const queryWithSpaces = query.replace(/\s/g, ' ');
let next = null;
while ((next = regexp.exec(queryWithSpaces))) {
if (next.index <= insertionIndex && next.index + next[0].length >= insertionIndex) {
return {
token: next[2],
term: (next[3] || '')
.replace(/"$/, '')
.replace(/^"/, '')
.toLowerCase(),
index: next.index,
length: next[0].length,
};
}
}
return { token: null, term: null, index: -1, length: 0 };
}