import React from 'react'; import { FocusContainer, ListensToFluxStore, ListDataSource, RetinaImg, BindGlobalCommands, } from 'mailspring-component-kit'; import { Store, ContactsPerspective } from './Store'; import { localized, DestroyContactTask, Actions, Contact, ChangeContactGroupMembershipTask, } from 'mailspring-exports'; interface ContactDetailToolbarProps { editing: string | 'new' | false; listSource: ListDataSource; perspective: ContactsPerspective; focusedId?: string; } class ContactDetailToolbarWithData extends React.Component { constructor(props: ContactDetailToolbarProps) { super(props); } _onRemoveFromSource = () => { if (this.props.perspective.type !== 'group') { throw new Error('Remove from source but perspective is not a group'); return; } const groupId = this.props.perspective.groupId; const group = Store.groups().find(g => g.id === groupId); Actions.queueTask( ChangeContactGroupMembershipTask.forMoving({ contacts: this.actionSet(), group: group, direction: 'remove', }) ); }; _onDelete = () => { Actions.queueTask( DestroyContactTask.forRemoving({ contacts: this.actionSet(), }) ); }; actionSet() { const { listSource, focusedId } = this.props; const focused = focusedId && listSource.getById(focusedId); const models = focused ? [focused] : listSource.selection.items(); return (models as any) as Contact[]; } render() { const { perspective, editing } = this.props; const actionSet = this.actionSet(); const editable = actionSet.length === 1 && actionSet[0].source !== 'mail'; if (editing) { return ; } const commands = {}; if (perspective && perspective.type === 'group' && actionSet.length > 0) { commands['core:remove-from-view'] = this._onRemoveFromSource; } if (actionSet.length > 0) { commands['core:delete-item'] = this._onDelete; } if (editable) { commands['core:edit-item'] = () => Store.setEditing(actionSet[0].id); } return (
{perspective && perspective.type === 'group' && ( )}
); } } export const ContactDetailToolbar: React.FunctionComponent< ContactDetailToolbarProps > = ListensToFluxStore( ({ listSource, editing, perspective }) => ( ), { stores: [Store], getStateFromStores: () => ({ editing: Store.editing(), listSource: Store.listSource(), perspective: Store.perspective(), }), } ); ContactDetailToolbar.displayName = 'ContactDetailToolbar';