import React from 'react'; import PropTypes from 'prop-types'; import classNames from 'classnames'; import { RetinaImg } from 'mailspring-component-kit'; import { Actions, TaskFactory, FocusedContentStore, FocusedPerspectiveStore, } from 'mailspring-exports'; import ThreadListStore from './thread-list-store'; export class ArchiveButton extends React.Component { static displayName = 'ArchiveButton'; static containerRequired = false; static propTypes = { items: PropTypes.array.isRequired, }; _onArchive = event => { const tasks = TaskFactory.tasksForArchiving({ threads: this.props.items, source: 'Toolbar Button: Thread List', }); Actions.queueTasks(tasks); Actions.popSheet(); event.stopPropagation(); return; }; render() { const allowed = FocusedPerspectiveStore.current().canArchiveThreads(this.props.items); if (!allowed) { return ; } return ( ); } } export class TrashButton extends React.Component { static displayName = 'TrashButton'; static containerRequired = false; static propTypes = { items: PropTypes.array.isRequired, }; _onRemove = event => { const tasks = TaskFactory.tasksForMovingToTrash({ threads: this.props.items, source: 'Toolbar Button: Thread List', }); Actions.queueTasks(tasks); Actions.popSheet(); event.stopPropagation(); return; }; render() { const allowed = FocusedPerspectiveStore.current().canMoveThreadsTo(this.props.items, 'trash'); if (!allowed) { return ; } return ( ); } } export class MarkAsSpamButton extends React.Component { static displayName = 'MarkAsSpamButton'; static containerRequired = false; static propTypes = { items: PropTypes.array.isRequired, }; _allInSpam() { return this.props.items.every(item => item.folders.map(c => c.role).includes('spam')); } _onNotSpam = event => { // TODO BG REPLACE TASK FACTORY const tasks = TaskFactory.tasksForMarkingNotSpam({ source: 'Toolbar Button: Thread List', threads: this.props.items, }); Actions.queueTasks(tasks); Actions.popSheet(); event.stopPropagation(); return; }; _onMarkAsSpam = event => { const tasks = TaskFactory.tasksForMarkingAsSpam({ threads: this.props.items, source: 'Toolbar Button: Thread List', }); Actions.queueTasks(tasks); Actions.popSheet(); event.stopPropagation(); return; }; render() { if (this._allInSpam()) { return ( ); } const allowed = FocusedPerspectiveStore.current().canMoveThreadsTo(this.props.items, 'spam'); if (!allowed) { return ; } return ( ); } } export class ToggleStarredButton extends React.Component { static displayName = 'ToggleStarredButton'; static containerRequired = false; static propTypes = { items: PropTypes.array.isRequired, }; _onStar = event => { Actions.queueTask( TaskFactory.taskForInvertingStarred({ threads: this.props.items, source: 'Toolbar Button: Thread List', }) ); event.stopPropagation(); return; }; render() { const postClickStarredState = this.props.items.every(t => t.starred === false); const title = postClickStarredState ? 'Star' : 'Unstar'; const imageName = postClickStarredState ? 'toolbar-star.png' : 'toolbar-star-selected.png'; return ( ); } } export class ToggleUnreadButton extends React.Component { static displayName = 'ToggleUnreadButton'; static containerRequired = false; static propTypes = { items: PropTypes.array.isRequired, }; _onClick = event => { Actions.queueTask( TaskFactory.taskForInvertingUnread({ threads: this.props.items, source: 'Toolbar Button: Thread List', }) ); Actions.popSheet(); event.stopPropagation(); return; }; render() { const postClickUnreadState = this.props.items.every(t => t.unread === false); const fragment = postClickUnreadState ? 'unread' : 'read'; return ( ); } } class ThreadArrowButton extends React.Component { static propTypes = { getStateFromStores: PropTypes.func, direction: PropTypes.string, command: PropTypes.string, title: PropTypes.string, }; constructor(props) { super(props); this.state = this.props.getStateFromStores(); } componentDidMount() { this._unsubscribe = ThreadListStore.listen(this._onStoreChange); this._unsubscribe_focus = FocusedContentStore.listen(this._onStoreChange); } componentWillUnmount() { this._unsubscribe(); this._unsubscribe_focus(); } _onClick = () => { if (this.state.disabled) { return; } AppEnv.commands.dispatch(this.props.command); return; }; _onStoreChange = () => { this.setState(this.props.getStateFromStores()); }; render() { const { direction, title } = this.props; const classes = classNames({ 'btn-icon': true, 'message-toolbar-arrow': true, disabled: this.state.disabled, }); return (
); } } export const DownButton = () => { const getStateFromStores = () => { const selectedId = FocusedContentStore.focusedId('thread'); const lastIndex = ThreadListStore.dataSource().count() - 1; const lastItem = ThreadListStore.dataSource().get(lastIndex); return { disabled: lastItem && lastItem.id === selectedId, }; }; return ( ); }; DownButton.displayName = 'DownButton'; DownButton.containerRequired = false; export const UpButton = () => { const getStateFromStores = () => { const selectedId = FocusedContentStore.focusedId('thread'); const item = ThreadListStore.dataSource().get(0); return { disabled: item && item.id === selectedId, }; }; return ( ); }; UpButton.displayName = 'UpButton'; UpButton.containerRequired = false;