Mailspring/app/internal_packages/undo-redo/lib/undo-redo-toast.jsx

98 lines
2.3 KiB
React
Raw Normal View History

import { React, UndoRedoStore } from 'mailspring-exports';
import { RetinaImg } from 'mailspring-component-kit';
import ReactCSSTransitionGroup from 'react-addons-css-transition-group';
const VISIBLE_DURATION = 3000;
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-22 02:48:04 +08:00
2017-11-07 01:12:14 +08:00
export default class UndoRedoToast extends React.Component {
static displayName = 'UndoRedoToast';
static containerRequired = false;
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-22 02:48:04 +08:00
constructor(props) {
super(props);
this._timeout = null;
this._unlisten = null;
this._mounted = false;
// Note: we explicitly do /not/ set initial state to the state of
// the UndoRedoStore here because "getMostRecent" might be more
// than 3000ms old.
this.state = {
block: null,
};
}
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-22 02:48:04 +08:00
componentDidMount() {
this._mounted = true;
this._unlisten = UndoRedoStore.listen(() => {
this.setState({
2017-11-07 01:12:14 +08:00
block: UndoRedoStore.getMostRecent(),
});
});
}
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-22 02:48:04 +08:00
componentDidUpdate() {
this._ensureTimeout();
}
componentWillUnmount() {
this._mounted = false;
this._clearTimeout();
if (this._unlisten) {
this._unlisten();
}
}
_clearTimeout() {
clearTimeout(this._timeout);
this._timeout = null;
}
_ensureTimeout() {
this._clearTimeout();
if (this.state.block) {
this._timeout = setTimeout(() => {
this._mounted = false;
this.setState({ block: null });
}, VISIBLE_DURATION);
}
}
_onMouseEnter = () => {
this._clearTimeout();
};
_onMouseLeave = () => {
this._ensureTimeout();
};
render() {
const { block } = this.state;
return (
<ReactCSSTransitionGroup
className="undo-redo-toast"
transitionLeaveTimeout={150}
transitionEnterTimeout={150}
transitionName="undo-redo-toast-fade"
>
{block ? (
<div
className="content"
onMouseEnter={this._onMouseEnter}
onMouseLeave={this._onMouseLeave}
>
<div className="message">{block.description}</div>
<div className="action" onClick={() => AppEnv.commands.dispatch('core:undo')}>
<RetinaImg name="undo-icon@2x.png" mode={RetinaImg.Mode.ContentIsMask} />
<span className="undo-action-text">Undo</span>
</div>
</div>
) : null}
</ReactCSSTransitionGroup>
);
}
}