Mailspring/internal_packages/composer/lib/send-action-button.jsx
Juan Tejada 5d837ffd02 feat(undo-send): Add undo send
Summary:
Add ability to undo send. We decided to make undo send completely client side for a couple of reasons. If we rely on send-later for undo-send, we would be giving /all/ send load to our send-later backend. If this increases the send-later load too much, it might cause delays in the regular send-later functionality and potentially other plugins like snooze that run under the same service. We would also need to rely on the network to be able to cancel a send, which would make it unusable offline or hard to debug if that specific request fails for any given reason.

This commit also refactors the way `ComposerExtension.sendActionConfig` works. The method has been renamed and now must return an array of send actions. Also, all of the business logic to handle different send actions registered by extensions has been pieced apart from the SendActionButton and into a new SendActionStore. This also enables undo send to undo custom send actions registered by extensions.
Along the way, this also fixes a pending TODO to show all registered custom send actions in the preferences for choosing the preferred send action for sending.

Undo send works via a task, so in case N1 closes before send goes through, it will still be persisted to the task queue and restored when opened again. Undoing a send means dequeuing this task.

Test Plan: Manual

Reviewers: jackie, bengotow, halla, evan

Reviewed By: bengotow, halla, evan

Differential Revision: https://phab.nylas.com/D3361
2016-10-26 20:40:10 -07:00

128 lines
3.5 KiB
JavaScript

import React from 'react'
import {Actions, SendActionsStore} from 'nylas-exports'
import {Menu, RetinaImg, ButtonDropdown, ListensToFluxStore} from 'nylas-component-kit'
class SendActionButton extends React.Component {
static displayName = "SendActionButton";
static containerRequired = false
static propTypes = {
draft: React.PropTypes.object,
isValidDraft: React.PropTypes.func,
sendActions: React.PropTypes.array,
orderedSendActions: React.PropTypes.object,
};
primarySend() {
this._onPrimaryClick();
}
_onPrimaryClick = () => {
const {orderedSendActions} = this.props
const {preferred} = orderedSendActions
this._onSendWithAction(preferred);
}
_onSendWithAction = (sendAction) => {
const {isValidDraft, draft} = this.props
if (isValidDraft()) {
Actions.sendDraft(draft.clientId, sendAction.configKey)
}
}
_renderSendActionItem = ({iconUrl}) => {
let plusHTML = "";
let additionalImg = false;
if (iconUrl) {
plusHTML = <span>&nbsp;+&nbsp;</span>;
additionalImg = <RetinaImg url={iconUrl} mode={RetinaImg.Mode.ContentIsMask} />;
}
return (
<span>
<RetinaImg name="icon-composer-send.png" mode={RetinaImg.Mode.ContentIsMask} />
<span className="text">Send{plusHTML}</span>{additionalImg}
</span>
);
}
_renderSingleButton() {
const {sendActions} = this.props
return (
<button
tabIndex={-1}
className={"btn btn-toolbar btn-normal btn-emphasis btn-text btn-send"}
style={{order: -100}}
onClick={this._onPrimaryClick}
>
{this._renderSendActionItem(sendActions[0])}
</button>
);
}
_renderButtonDropdown() {
const {orderedSendActions} = this.props
const {preferred, rest} = orderedSendActions
const menu = (
<Menu
items={rest}
itemKey={(actionConfig) => actionConfig.configKey}
itemContent={this._renderSendActionItem}
onSelect={this._onSendWithAction}
/>
);
return (
<ButtonDropdown
className={"btn-send btn-emphasis btn-text"}
style={{order: -100}}
primaryItem={this._renderSendActionItem(preferred)}
primaryTitle={preferred.title}
primaryClick={this._onPrimaryClick}
closeOnMenuClick
menu={menu}
/>
);
}
render() {
const {sendActions} = this.props
if (sendActions.length === 1) {
return this._renderSingleButton();
}
return this._renderButtonDropdown();
}
}
const EnhancedSendActionButton = ListensToFluxStore(SendActionButton, {
stores: [SendActionsStore],
getStateFromStores(props) {
const {draft} = props
return {
sendActions: SendActionsStore.availableSendActionsForDraft(draft),
orderedSendActions: SendActionsStore.orderedSendActionsForDraft(draft),
}
},
})
// TODO this is a hack so that the send button can still expose
// the `primarySend` method required by the ComposerView. Ideally, this
// decorator mechanism should expose whatever instance methods are exposed
// by the component its wrapping.
// However, I think the better fix will happen when mail merge lives in its
// own window and doesn't need to override the Composer's send button, which
// is already a bit of a hack.
Object.assign(EnhancedSendActionButton.prototype, {
primarySend() {
if (this.refs.composed) {
this.refs.composed.primarySend()
}
},
})
export const UndecoratedSendActionButton = SendActionButton
export default EnhancedSendActionButton