import React from 'react'; import { ipcRenderer } from 'electron'; import { AccountStore, Actions } from 'mailspring-exports'; import PreferencesAccountList from './preferences-account-list'; import PreferencesAccountDetails from './preferences-account-details'; class PreferencesAccounts extends React.Component { static displayName = 'PreferencesAccounts'; constructor() { super(); this.state = this.getStateFromStores(); } componentDidMount() { this.unsubscribe = AccountStore.listen(this._onAccountsChanged); } componentWillUnmount() { if (this.unsubscribe) { this.unsubscribe(); } } getStateFromStores({ selected } = {}) { const accounts = AccountStore.accounts(); let selectedAccount; if (selected) { selectedAccount = accounts.find(a => a.id === selected.id); } // If selected was null or no longer exists in the AccountStore, // just use the first account. if (!selectedAccount) { selectedAccount = accounts[0]; } return { accounts, selected: selectedAccount, }; } _onAccountsChanged = () => { this.setState(this.getStateFromStores(this.state)); }; // Update account list actions _onAddAccount() { ipcRenderer.send('command', 'application:add-account', { source: 'Preferences' }); } _onReorderAccount(account, oldIdx, newIdx) { Actions.reorderAccount(account.id, newIdx); } _onSelectAccount = account => { this.setState({ selected: account }); }; _onRemoveAccount(account) { Actions.removeAccount(account.id); } // Update account actions _onAccountUpdated(account, updates) { Actions.updateAccount(account.id, updates); } render() { return (
); } } export default PreferencesAccounts;