// @flow import * as React from 'react'; import { observable } from 'mobx'; import { inject, observer } from 'mobx-react'; import { Redirect } from 'react-router-dom'; import { PlusIcon, CollectionIcon, PrivateCollectionIcon } from 'outline-icons'; import { newDocumentUrl } from 'utils/routeHelpers'; import CollectionsStore from 'stores/CollectionsStore'; import PoliciesStore from 'stores/PoliciesStore'; import { DropdownMenu, DropdownMenuItem } from 'components/DropdownMenu'; import Button from 'components/Button'; type Props = { label?: React.Node, collections: CollectionsStore, policies: PoliciesStore, }; @observer class NewDocumentMenu extends React.Component { @observable redirectTo: ?string; componentDidUpdate() { this.redirectTo = undefined; } handleNewDocument = (collectionId: string) => { this.redirectTo = newDocumentUrl(collectionId); }; onOpen = () => { const { collections } = this.props; if (collections.orderedData.length === 1) { this.handleNewDocument(collections.orderedData[0].id); } }; render() { if (this.redirectTo) return ; const { collections, policies, label, ...rest } = this.props; return ( } small> New doc ) } onOpen={this.onOpen} {...rest} > Choose a collection… {collections.orderedData.map(collection => { const can = policies.abilities(collection.id); return ( this.handleNewDocument(collection.id)} disabled={!can.update} > {collection.private ? ( ) : ( )}{' '} {collection.name} ); })} ); } } export default inject('collections', 'policies')(NewDocumentMenu);