import React from 'react'; import _ from 'underscore'; import {Actions, AccountStore, MailRulesStore, MailRulesTemplates, TaskQueue, ReprocessMailRulesTask} from 'nylas-exports'; import {Flexbox, EditableList, RetinaImg, ScrollRegion, ScenarioEditor} from 'nylas-component-kit'; const { ActionTemplatesForAccount, ConditionTemplatesForAccount, } = MailRulesTemplates; class PreferencesMailRules extends React.Component { static displayName = 'PreferencesMailRules'; constructor() { super(); this.state = this._getStateFromStores(); } componentDidMount() { this._unsubscribers = []; this._unsubscribers.push(MailRulesStore.listen(this._onRulesChanged)); this._unsubscribers.push(TaskQueue.listen(this._onTasksChanged)); } componentWillUnmount() { this._unsubscribers.forEach(unsubscribe => unsubscribe()); } _getStateFromStores() { const accounts = AccountStore.accounts(); const state = this.state || {}; let {currentAccount} = state; if (!accounts.find(acct => acct === currentAccount)) { currentAccount = accounts[0]; } const rules = MailRulesStore.rulesForAccountId(currentAccount.id); const selectedRule = this.state && this.state.selectedRule ? rules.find(r => r.id === this.state.selectedRule.id) : rules[0]; return { accounts: accounts, currentAccount: currentAccount, rules: rules, selectedRule: selectedRule, tasks: TaskQueue.findTasks(ReprocessMailRulesTask, {}), actionTemplates: ActionTemplatesForAccount(currentAccount), conditionTemplates: ConditionTemplatesForAccount(currentAccount), } } _onSelectAccount = (event) => { const accountId = event.target.value; const currentAccount = this.state.accounts.find(acct => acct.id === accountId); this.setState({currentAccount: currentAccount}, () => { this.setState(this._getStateFromStores()) }); } _onReprocessRules = () => { const needsMessageBodies = () => { for (const rule of this.state.rules) { for (const condition of rule.conditions) { if (condition.templateKey === 'body') { return true; } } } return false; } if (needsMessageBodies()) { NylasEnv.showErrorDialog("One or more of your mail rules requires the bodies of messages being processed. These rules can't be run on your entire mailbox."); } const task = new ReprocessMailRulesTask(this.state.currentAccount.id) Actions.queueTask(task); } _onAddRule = () => { Actions.addMailRule({accountId: this.state.currentAccount.id}); } _onSelectRule = (rule) => { this.setState({selectedRule: rule}); } _onReorderRule = (rule, startIdx, endIdx) => { Actions.reorderMailRule(rule.id, endIdx); } _onDeleteRule = (rule) => { Actions.deleteMailRule(rule.id); } _onRuleNameEdited = (newName, rule) => { Actions.updateMailRule(rule.id, {name: newName}); } _onRuleConditionModeEdited = (event) => { Actions.updateMailRule(this.state.selectedRule.id, {conditionMode: event.target.value}); } _onRuleEnabled = () => { Actions.updateMailRule(this.state.selectedRule.id, {disabled: false, disabledReason: null}); } _onRulesChanged = () => { const next = this._getStateFromStores(); const nextRules = next.rules; const prevRules = this.state.rules ? this.state.rules : []; const added = _.difference(nextRules, prevRules); if (added.length === 1) { next.selectedRule = added[0]; } this.setState(next); } _onTasksChanged = () => { this.setState({tasks: TaskQueue.findTasks(ReprocessMailRulesTask, {})}) } _renderAccountPicker() { const options = this.state.accounts.map(account => ); return ( ); } _renderMailRules() { if (this.state.rules.length === 0) { return (

No rules

); } return ( {this._renderDetail()} ); } _renderListItemContent(rule) { if (rule.disabled) { return (
{rule.name}
); } return rule.name; } _renderDetail() { const rule = this.state.selectedRule; if (rule) { return ( {this._renderDetailDisabledNotice()}
If of the following conditions are met: Actions.updateMailRule(rule.id, {conditions})} className="well well-matchers" /> Perform the following actions: Actions.updateMailRule(rule.id, {actions})} className="well well-actions" />
); } return (
Create a rule or select one to get started
); } _renderDetailDisabledNotice() { if (!this.state.selectedRule.disabled) return false; return (
This rule has been disabled. Make sure the actions below are valid and re-enable the rule.
({this.state.selectedRule.disabledReason})
); } _renderTasks() { if (this.state.tasks.length === 0) return false; return (
{this.state.tasks.map((task) => { return (
{AccountStore.accountForId(task.accountId).emailAddress} {` — ${Number(task.numberOfImpactedItems()).toLocaleString()} processed...`}
); })}
); } render() { const processDisabled = this.state.tasks.some((task) => task.accountId === this.state.currentAccount.id ); return (
Account:
{this._renderAccountPicker()}

Rules only apply to the selected account.

{this._renderMailRules()}
{this._renderTasks()}

By default, mail rules are only applied to new mail as it arrives. Applying rules to your entire inbox may take a long time and degrade performance.

); } } export default PreferencesMailRules;