Mailspring/app/internal_packages/message-list/lib/message-controls.tsx
Ben Gotow 149b389508
Replace Babel with TypeScript compiler, switch entire app to TypeScript 🎉 (#1404)
* Switch to using Typescript instead of Babel

* Switch all es6 / jsx file extensions to ts / tsx

* Convert Utils to a TS module from module.exports style module

* Move everything from module.exports to typescript exports

* Define .d.ts files for mailspring-exports and component kit… Yes it seems this is the best option :(

* Load up on those @types

* Synthesize TS types from PropTypes for standard components

* Add types to Model classes and move constructor constants to instance vars

* 9800 => 7700 TS errors

* 7700 => 5600 TS errors

* 5600 => 5330 TS errors

* 5330 => 4866 TS errors

* 4866 => 4426 TS errors

* 4426 => 2411 TS errors

* 2411 > 1598 TS errors

* 1598 > 769 TS errors

* 769 > 129 TS errors

* 129 > 22 TS errors

* Fix runtime errors

* More runtime error fixes

* Remove support for custom .es6 file extension

* Remove a few odd remaining references to Nylas

* Don’t ship Typescript support in the compiled app for now

* Fix issues in compiled app - module resolution in TS is case sensitive?

* README updates

* Fix a few more TS errors

* Make “No Signature” option clickable + selectable

* Remove flicker when saving file and reloading keymaps

* Fix mail rule item height in preferences

* Fix missing spacing in thread sharing popover

* Fix scrollbar ticks being nested incorrectly

* Add Japanese as a manually reviewed language

* Prevent the thread list from “sticking”

* Re-use Sheet when switching root tabs, prevent sidebar from resetting

* Ensure specs run

* Update package configuration to avoid shpping types

* Turn eslint back on - we will opt-in to the TS rules one by one
2019-03-04 11:03:12 -08:00

176 lines
4.8 KiB
TypeScript

/* eslint global-require: 0 */
import { remote } from 'electron';
import React from 'react';
import {
localized,
PropTypes,
Actions,
TaskQueue,
GetMessageRFC2822Task,
Thread,
Message,
} from 'mailspring-exports';
import { RetinaImg, ButtonDropdown, Menu } from 'mailspring-component-kit';
interface MessageControlsProps {
thread: Thread;
message: Message;
}
export default class MessageControls extends React.Component<MessageControlsProps> {
static displayName = 'MessageControls';
static propTypes = {
thread: PropTypes.object.isRequired,
message: PropTypes.object.isRequired,
};
_items() {
const reply = {
name: localized('Reply'),
image: 'ic-dropdown-reply.png',
select: this._onReply,
};
const replyAll = {
name: localized('Reply All'),
image: 'ic-dropdown-replyall.png',
select: this._onReplyAll,
};
const forward = {
name: localized('Forward'),
image: 'ic-dropdown-forward.png',
select: this._onForward,
};
if (!this.props.message.canReplyAll()) {
return [reply, forward];
}
const defaultReplyType = AppEnv.config.get('core.sending.defaultReplyType');
return defaultReplyType === 'reply-all'
? [replyAll, reply, forward]
: [reply, replyAll, forward];
}
_dropdownMenu(items) {
const itemContent = item => (
<span>
<RetinaImg name={item.image} mode={RetinaImg.Mode.ContentIsMask} />
&nbsp;&nbsp;{item.name}
</span>
);
return (
<Menu
items={items}
itemKey={item => item.name}
itemContent={itemContent}
onSelect={item => item.select()}
/>
);
}
_onReply = () => {
const { thread, message } = this.props;
Actions.composeReply({
thread,
message,
type: 'reply',
behavior: 'prefer-existing-if-pristine',
});
};
_onReplyAll = () => {
const { thread, message } = this.props;
Actions.composeReply({
thread,
message,
type: 'reply-all',
behavior: 'prefer-existing-if-pristine',
});
};
_onForward = () => {
const { thread, message } = this.props;
Actions.composeForward({ thread, message });
};
_onShowActionsMenu = () => {
const SystemMenu = remote.Menu;
const SystemMenuItem = remote.MenuItem;
// Todo: refactor this so that message actions are provided
// dynamically. Waiting to see if this will be used often.
const menu = new SystemMenu();
menu.append(new SystemMenuItem({ label: localized('Log Data'), click: this._onLogData }));
menu.append(
new SystemMenuItem({ label: localized('Show Original'), click: this._onShowOriginal })
);
menu.append(
new SystemMenuItem({
label: localized('Copy Debug Info to Clipboard'),
click: this._onCopyToClipboard,
})
);
menu.popup({});
};
_onShowOriginal = async () => {
const { message } = this.props;
const filepath = require('path').join(remote.app.getPath('temp'), message.id);
const task = new GetMessageRFC2822Task({
messageId: message.id,
accountId: message.accountId,
filepath,
});
Actions.queueTask(task);
await TaskQueue.waitForPerformRemote(task);
const win = new remote.BrowserWindow({
width: 800,
height: 600,
title: `${message.subject} - RFC822`,
webPreferences: {
javascript: false,
nodeIntegration: false,
},
});
win.loadURL(`file://${filepath}`);
};
_onLogData = () => {
console.log(this.props.message);
(window as any).__message = this.props.message;
(window as any).__thread = this.props.thread;
console.log('Also now available in window.__message and window.__thread');
};
_onCopyToClipboard = () => {
const { message, thread } = this.props;
const clipboard = require('electron').clipboard;
const data = `
AccountID: ${message.accountId}
Message ID: ${message.id}
Message Metadata: ${JSON.stringify(message.pluginMetadata, null, ' ')}
Thread ID: ${thread.id}
Thread Metadata: ${JSON.stringify(thread.pluginMetadata, null, ' ')}
`;
clipboard.writeText(data);
};
render() {
const items = this._items();
return (
<div className="message-actions-wrap" onClick={e => e.stopPropagation()}>
<ButtonDropdown
primaryItem={<RetinaImg name={items[0].image} mode={RetinaImg.Mode.ContentIsMask} />}
primaryTitle={items[0].name}
primaryClick={items[0].select}
closeOnMenuClick
menu={this._dropdownMenu(items.slice(1))}
/>
<div className="message-actions-ellipsis" onClick={this._onShowActionsMenu}>
<RetinaImg name={'message-actions-ellipsis.png'} mode={RetinaImg.Mode.ContentIsMask} />
</div>
</div>
);
}
}