import React from 'react'; import _ from 'underscore'; import { Actions, AccountStore, MailRulesStore, MailRulesTemplates, TaskQueue, ReprocessMailRulesTask, } from 'mailspring-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()) { AppEnv.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 (
Rules only apply to the selected account.
{this._renderMailRules()}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.